Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(formula): fix time zone issues in date-related formulas (#6578)
wpxp1234560cc433e357bb
diff --git a/packages/engine-formula/src/basics/__tests__/date.spec.ts b/packages/engine-formula/src/basics/__tests__/date.spec.tsindex 9592c53513..652308ac60 100644--- a/packages/engine-formula/src/basics/__tests__/date.spec.ts+++ b/packages/engine-formula/src/basics/__tests__/date.spec.ts@@ -20,11 +20,11 @@ import { excelDateSerial, excelDateTimeSerial, excelSerialToDate, formatDateDefadescribe('Test date', () => {it('Function excelDateSerial', () => {- expect(excelDateSerial(new Date(1900, 1, 28))).toBe(59);- expect(excelDateSerial(new Date(1900, 1, 29))).toBe(61);- expect(excelDateSerial(new Date(1900, 2, 1))).toBe(61);- expect(excelDateSerial(new Date(1901, 0, 1))).toBe(367);- expect(excelDateSerial(new Date(2024, 1, 2))).toBe(45324);+ expect(excelDateSerial(new Date(Date.UTC(1900, 1, 28)))).toBe(59);+ expect(excelDateSerial(new Date(Date.UTC(1900, 1, 29)))).toBe(61);+ expect(excelDateSerial(new Date(Date.UTC(1900, 2, 1)))).toBe(61);+ expect(excelDateSerial(new Date(Date.UTC(1901, 0, 1)))).toBe(367);+ expect(excelDateSerial(new Date(Date.UTC(2024, 1, 2)))).toBe(45324);});it('Function excelDateSerial should account for hours, minutes, and seconds', () => {diff --git a/packages/engine-formula/src/basics/date.ts b/packages/engine-formula/src/basics/date.tsindex 7624e89b41..1542de58ab 100644--- a/packages/engine-formula/src/basics/date.ts+++ b/packages/engine-formula/src/basics/date.ts@@ -23,6 +23,8 @@ export const DEFAULT_DATE_FORMAT = 'yyyy/mm/dd;@';export const DEFAULT_NOW_FORMAT = 'yyyy/mm/dd hh:mm';export const DEFAULT_TIME_FORMAT = 'h:mm A/P';+const perDayMilliseconds = 24 * 60 * 60 * 1000;+/*** Excel stores dates as sequential serial numbers so they can be used in calculations. By default, January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,447 days after January 1, 1900.*@@ -31,19 +33,17 @@ export const DEFAULT_TIME_FORMAT = 'h:mm A/P';* 1900.2.29 Date Serial 61* 1900.3.1 Date Serial 61* 1901.1.1 Date Serial 367- * @param date- * @returns*/export function excelDateSerial(date: Date): number {- const baseDate = new Date(Date.UTC(1900, 0, 1)); // January 1, 1900, UTC- const leapDayDate = new Date(Date.UTC(1900, 1, 28)); // February 28, 1900, UTC- const dateInUTC = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());+ const baseDateTime = Date.UTC(1900, 0, 1); // January 1, 1900, UTC+ const leapDayDateTime = Date.UTC(1900, 1, 28); // February 28, 1900, UTC+ const dateTime = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()); // Input date at UTC// Calculate the difference in days between the base date and the input date- let dayDifference = (dateInUTC - baseDate.getTime()) / (1000 * 3600 * 24);+ let dayDifference = (dateTime - baseDateTime) / perDayMilliseconds;// If the date is later than February 28, 1900, the day difference needs to be adjusted to account for Excel errors- if (dateInUTC > leapDayDate.getTime()) {+ if (dateTime > leapDayDateTime) {dayDifference += 1;}@@ -52,19 +52,17 @@ export function excelDateSerial(date: Date): number {/*** Time serial number with date- * @param date- * @returns*/export function excelDateTimeSerial(date: Date): number {- const baseDate = new Date(Date.UTC(1900, 0, 1, 0, 0, 0)); // January 1, 1900, UTC at midnight- const leapDayDate = new Date(Date.UTC(1900, 1, 28, 0, 0, 0)); // February 28, 1900, UTC at midnight+ const baseDateTime = Date.UTC(1900, 0, 1, 0, 0, 0); // January 1, 1900, UTC at midnight+ const leapDayDateTime = Date.UTC(1900, 1, 28, 0, 0, 0); // February 28, 1900, UTC at midnight+ const dateTime = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds()); // Input date at UTC midnight// Calculate the difference in milliseconds between the input date and the base date- const diffMilliseconds = date.getTime() - baseDate.getTime();- let dayDifference = diffMilliseconds / (1000 * 3600 * 24);+ let dayDifference = (dateTime - baseDateTime) / perDayMilliseconds;// Adjusting for the Excel leap year bug- if (date > leapDayDate) {+ if (dateTime > leapDayDateTime) {dayDifference += 1;}@@ -72,28 +70,27 @@ export function excelDateTimeSerial(date: Date): number {}export function excelSerialToDate(serial: number): Date {- const baseDate = new Date(Date.UTC(1900, 0, 1)); // January 1, 1900, UTC- const leapDayDate = new Date(Date.UTC(1900, 1, 28)); // February 28, 1900, UTC+ const baseDateTime = Date.UTC(1900, 0, 1); // January 1, 1900, UTC+ const leapDayDateTime = Date.UTC(1900, 1, 28); // February 28, 1900, UTClet dayDifference = Math.floor(serial) - 1; // Adjust for Excel serial number starting from 1// If the serial number corresponds to a date later than February 28, 1900, adjust the day difference- if (dayDifference > (leapDayDate.getTime() - baseDate.getTime()) / (1000 * 3600 * 24)) {+ if (dayDifference > (leapDayDateTime - baseDateTime) / perDayMilliseconds) {dayDifference -= 1;}- const resultDate = new Date(baseDate.getTime() + dayDifference * (1000 * 3600 * 24));- return resultDate;+ return new Date(baseDateTime + dayDifference * perDayMilliseconds);}export function excelSerialToDateTime(serial: number): Date {- const baseDate = new Date(Date.UTC(1900, 0, 1, 0, 0, 0)); // January 1, 1900, UTC- const leapDayDate = new Date(Date.UTC(1900, 1, 28, 0, 0, 0)); // February 28, 1900, UTC+ const baseDateTime = Date.UTC(1900, 0, 1, 0, 0, 0); // January 1, 1900, UTC at midnight+ const leapDayDateTime = Date.UTC(1900, 1, 28, 0, 0, 0); // February 28, 1900, UTC at midnightlet dayDifference = serial - 1; // Adjust for Excel serial number starting from 1// If the serial number corresponds to a date later than February 28, 1900, adjust the day difference- if (dayDifference > (leapDayDate.getTime() - baseDate.getTime()) / (1000 * 3600 * 24)) {+ if (dayDifference > (leapDayDateTime - baseDateTime) / perDayMilliseconds) {dayDifference -= 1;}@@ -101,8 +98,7 @@ export function excelSerialToDateTime(serial: number): Date {dayDifference = serial;}- const resultDate = new Date(baseDate.getTime() + dayDifference * (1000 * 3600 * 24));- return resultDate;+ return new Date(baseDateTime + dayDifference * perDayMilliseconds);}export function formatDateDefault(date: Date): string {@@ -125,8 +121,6 @@ export function formatDateDefault(date: Date): string {* Validate date string** TODO @Dushusir: Internationalization and more format support, can be reused when editing and saving cells, like "2020年1月1日"- * @param dateStr- * @returns*/export function isValidDateStr(dateStr: string): boolean {// Regular expression to validate date format@@ -335,14 +329,14 @@ export function getWeekDayByDateSerialNumber(dateSerialNumber: number): number {let date = excelSerialToDate(dateSerialNumber);- const dateTime = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())).getTime();- const leapDayDateTime = new Date(Date.UTC(1900, 1, 28)).getTime(); // February 28, 1900, UTC+ const leapDayDateTime = Date.UTC(1900, 1, 28); // February 28, 1900, UTC+ const dateTime = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());if (!isDate19000229 && dateTime <= leapDayDateTime) {- date = new Date(dateTime - 24 * 3600 * 1000);+ date = new Date(dateTime - perDayMilliseconds);}- return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())).getUTCDay();+ return date.getUTCDay();}interface ITwoDateDaysType {diff --git a/packages/engine-formula/src/functions/date/date/index.ts b/packages/engine-formula/src/functions/date/date/index.tsindex 1c69914e25..5273735a9b 100644--- a/packages/engine-formula/src/functions/date/date/index.ts+++ b/packages/engine-formula/src/functions/date/date/index.ts@@ -81,7 +81,7 @@ export class DateFunction extends BaseFunction {yearValue += 1900;}- const date = new Date(yearValue, monthValue - 1, dayValue);+ const date = new Date(Date.UTC(yearValue, monthValue - 1, dayValue));const currentSerial = excelDateSerial(date);diff --git a/packages/engine-formula/src/functions/date/datevalue/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/datevalue/__tests__/index.spec.tsindex cd74df590d..21bddade08 100644--- a/packages/engine-formula/src/functions/date/datevalue/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/date/datevalue/__tests__/index.spec.ts@@ -35,7 +35,7 @@ describe('Test datevalue function', () => {const dateText2 = StringValueObject.create('5-Jul'); // 7.5 of current yearconst result2 = testFunction.calculate(dateText2);- expect(getObjectValue(result2)).toStrictEqual(excelDateSerial(new Date(new Date().getFullYear(), 6, 5)));+ expect(getObjectValue(result2)).toStrictEqual(excelDateSerial(new Date(Date.UTC(new Date().getUTCFullYear(), 6, 5))));const dateText3 = StringValueObject.create('2020-01-02 13:14:15');const result3 = testFunction.calculate(dateText3);diff --git a/packages/engine-formula/src/functions/date/day/index.ts b/packages/engine-formula/src/functions/date/day/index.tsindex 673b50f0b1..20e8067014 100644--- a/packages/engine-formula/src/functions/date/day/index.ts+++ b/packages/engine-formula/src/functions/date/day/index.ts@@ -14,9 +14,9 @@* limitations under the License.*/+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';import { excelSerialToDate, isValidDateStr } from '../../../basics/date';import { ErrorType } from '../../../basics/error-type';-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';import { ErrorValueObject } from '../../../engine/value-object/base-value-object';import { NumberValueObject } from '../../../engine/value-object/primitive-object';import { BaseFunction } from '../../base-function';@@ -64,9 +64,8 @@ export class Day extends BaseFunction {date = excelSerialToDate(dateSerial);}- const month = date.getDate();- const valueObject = NumberValueObject.create(month);+ const day = date.getUTCDate();- return valueObject;+ return NumberValueObject.create(day);}}diff --git a/packages/engine-formula/src/functions/date/now/index.ts b/packages/engine-formula/src/functions/date/now/index.tsindex 7c8eb8dfdd..261ffb7a7d 100644--- a/packages/engine-formula/src/functions/date/now/index.ts+++ b/packages/engine-formula/src/functions/date/now/index.ts@@ -25,6 +25,7 @@ export class Now extends BaseFunction {override calculate() {const now = new Date();+ // Here we use local time to calculate UTC date time serial numberconst utcNow = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()));const currentSerial = excelDateTimeSerial(utcNow);const valueObject = NumberValueObject.create(currentSerial, DEFAULT_NOW_FORMAT);diff --git a/packages/engine-formula/src/functions/date/today/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/today/__tests__/index.spec.tsindex 41d8033ad6..9b962749a6 100644--- a/packages/engine-formula/src/functions/date/today/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/date/today/__tests__/index.spec.ts@@ -21,7 +21,7 @@ import { Today } from '../index';describe('Test today function', () => {beforeEach(() => {vi.useFakeTimers();- vi.setSystemTime(new Date(2020, 0, 1));+ vi.setSystemTime(Date.UTC(2020, 0, 1));});afterEach(() => {diff --git a/packages/engine-formula/src/functions/date/today/index.ts b/packages/engine-formula/src/functions/date/today/index.tsindex 6a392b46b3..4272feb4fe 100644--- a/packages/engine-formula/src/functions/date/today/index.ts+++ b/packages/engine-formula/src/functions/date/today/index.ts@@ -24,7 +24,9 @@ export class Today extends BaseFunction {override maxParams = 0;override calculate() {- const currentSerial = excelDateSerial(new Date());+ const now = new Date();+ const utcNow = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));+ const currentSerial = excelDateSerial(utcNow);const valueObject = NumberValueObject.create(currentSerial, DEFAULT_DATE_FORMAT);return valueObject;}