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): correct ISO week numbers for ISOWEEKNUM and WEEKNUM type 21 (#8177)
Jay Hemnani42e96b07181b
diff --git a/packages/engine-formula/src/basics/date.ts b/packages/engine-formula/src/basics/date.tsindex 65f0d9817e..124c453e1a 100644--- a/packages/engine-formula/src/basics/date.ts+++ b/packages/engine-formula/src/basics/date.ts@@ -14,7 +14,6 @@* limitations under the License.*/-import type { BaseValueObject } from '../engine/value-object/base-value-object';import {DateSystem,excelDateSerial,@@ -24,6 +23,7 @@ import {isRealNum,numfmt,} from '@univerjs/core';+import type { BaseValueObject } from '../engine/value-object/base-value-object';import { ErrorValueObject } from '../engine/value-object/base-value-object';import { ErrorType } from './error-type';@@ -316,6 +316,55 @@ export function getWeekDayByDateSerialNumber(dateSerialNumber: number, dateSystereturn ((Math.floor(dateSerialNumber) + offset) % 7 + 7) % 7;}+/**+ * Returns the serial number of the Monday that starts ISO 8601 week 1 of the given year.+ *+ * Week 1 is the week that contains the first Thursday of the year, so it starts on or before+ * 1 January when the year starts on Monday to Thursday, and in the last days of December of the+ * previous year when it starts on Friday to Sunday.+ */+function getIsoWeekStartSerialNumber(year: number, dateSystem: DateSystem): number {+ const yearStartSerialNumber = excelDateSerial(new Date(Date.UTC(year, 0, 1)), dateSystem);+ const yearStartWeekDay = getWeekDayByDateSerialNumber(yearStartSerialNumber, dateSystem);++ if (yearStartWeekDay === 0) {+ // 1 January is a Sunday, which still belongs to the last week of the previous year.+ return yearStartSerialNumber + 1;+ }++ if (yearStartWeekDay <= 4) {+ return yearStartSerialNumber - (yearStartWeekDay - 1);+ }++ return yearStartSerialNumber + (8 - yearStartWeekDay);+}++/**+ * Returns the ISO 8601 week number of a date serial, the value of `ISOWEEKNUM` and+ * `WEEKNUM(date, 21)`. The time fraction is ignored so a date-time keeps the week of its day.+ */+export function getIsoWeekNumber(dateSerialNumber: number, dateSystem = DateSystem.Date1900): number {+ const serialNumber = Math.floor(dateSerialNumber);+ const date = excelSerialToDate(serialNumber, dateSystem);+ // Serial 0 is Excel's 1900-01-00, which is kept in the 1900 calendar year.+ const currentYear = dateSystem === DateSystem.Date1900 && serialNumber === 0 ? 1900 : date.getUTCFullYear();++ let weekStartSerialNumber = getIsoWeekStartSerialNumber(currentYear, dateSystem);++ if (serialNumber < weekStartSerialNumber) {+ weekStartSerialNumber = getIsoWeekStartSerialNumber(currentYear - 1, dateSystem);+ } else if (date.getUTCMonth() === 11) {+ // The last days of December belong to week 1 of the next year when it starts Monday to Thursday.+ const nextYearWeekStartSerialNumber = getIsoWeekStartSerialNumber(currentYear + 1, dateSystem);++ if (serialNumber >= nextYearWeekStartSerialNumber) {+ return 1;+ }+ }++ return Math.floor((serialNumber - weekStartSerialNumber) / 7) + 1;+}+interface ITwoDateDaysType {days: number;yearDays: number;diff --git a/packages/engine-formula/src/functions/date/isoweeknum/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/isoweeknum/__tests__/index.spec.tsindex 3d72a6e69c..7167ae641d 100644--- a/packages/engine-formula/src/functions/date/isoweeknum/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/date/isoweeknum/__tests__/index.spec.ts@@ -43,6 +43,34 @@ describe('Test isoweeknum function', () => {expect(result.getValue()).toStrictEqual(52);});+ it('follows ISO 8601 at year boundaries', () => {+ // The last days of December that belong to week 1 of the next year.+ expect(testFunction.calculate(StringValueObject.create('2024-12-30')).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2025-12-29')).getValue()).toBe(1);+ // Week 1 starts in the last days of the previous year when 1 January is Friday or Saturday.+ expect(testFunction.calculate(StringValueObject.create('2021-1-4')).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2015-12-31')).getValue()).toBe(53);+ // 1 January 2016 was a Friday, so week 1 started on 4 January and the rest of the year follows it.+ expect(testFunction.calculate(StringValueObject.create('2016-1-4')).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2016-1-11')).getValue()).toBe(2);+ expect(testFunction.calculate(StringValueObject.create('2021-2-1')).getValue()).toBe(5);+ });++ it('follows ISO 8601 at year boundaries in the 1904 date system', () => {+ testFunction.setDateSystem(DateSystem.Date1904);++ expect(testFunction.calculate(StringValueObject.create('2024-12-30')).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2021-1-4')).getValue()).toBe(1);++ testFunction.setDateSystem(DateSystem.Date1900);+ });++ it('ignores the time fraction of a date serial', () => {+ expect(testFunction.calculate(NumberValueObject.create(43471.5)).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2019-01-06 12:00')).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2024-12-30 23:30')).getValue()).toBe(1);+ });+it('uses the 1904 epoch when serial is zero', () => {testFunction.setDateSystem(DateSystem.Date1900);expect(testFunction.calculate(NumberValueObject.create(0)).getValue()).toBe(52);diff --git a/packages/engine-formula/src/functions/date/isoweeknum/index.ts b/packages/engine-formula/src/functions/date/isoweeknum/index.tsindex 9261e5266c..592888f3fe 100644--- a/packages/engine-formula/src/functions/date/isoweeknum/index.ts+++ b/packages/engine-formula/src/functions/date/isoweeknum/index.ts@@ -15,8 +15,7 @@*/import type { BaseValueObject } from '../../../engine/value-object/base-value-object';-import { DateSystem, excelDateSerial, excelSerialToDate } from '@univerjs/core';-import { getDateSerialNumberByObject, getWeekDayByDateSerialNumber } from '../../../basics/date';+import { getDateSerialNumberByObject, getIsoWeekNumber } from '../../../basics/date';import { NumberValueObject } from '../../../engine/value-object/primitive-object';import { BaseFunction } from '../../base-function';@@ -50,40 +49,6 @@ export class Isoweeknum extends BaseFunction {return dateSerialNumber;}- const currentDate = excelSerialToDate(dateSerialNumber, this.getDateSystem());- const currentYear = this.getDateSystem() === DateSystem.Date1900 && dateSerialNumber === 0- ? 1900- : currentDate.getUTCFullYear();- let yearStart = new Date(Date.UTC(currentYear, 0, 1));- let yearStartSerialNumber = excelDateSerial(yearStart, this.getDateSystem());- let yearStartWeekDay = getWeekDayByDateSerialNumber(yearStartSerialNumber, this.getDateSystem());-- let yearWeekStartSerialNumber: number;-- if (yearStartWeekDay < 1) {- yearWeekStartSerialNumber = yearStartSerialNumber + 1;- } else if (yearStartWeekDay <= 4) {- yearWeekStartSerialNumber = yearStartSerialNumber - (yearStartWeekDay - 1);- } else {- yearWeekStartSerialNumber = yearStartSerialNumber + (11 - yearStartWeekDay);- }-- if (dateSerialNumber < yearWeekStartSerialNumber) {- yearStart = new Date(Date.UTC(currentYear - 1, 0, 1));- yearStartSerialNumber = excelDateSerial(yearStart, this.getDateSystem());- yearStartWeekDay = getWeekDayByDateSerialNumber(yearStartSerialNumber, this.getDateSystem());-- if (yearStartWeekDay < 1) {- yearWeekStartSerialNumber = yearStartSerialNumber + 1;- } else if (yearStartWeekDay <= 4) {- yearWeekStartSerialNumber = yearStartSerialNumber - (yearStartWeekDay - 1);- } else {- yearWeekStartSerialNumber = yearStartSerialNumber + (11 - yearStartWeekDay);- }- }-- const result = Math.ceil((dateSerialNumber - yearWeekStartSerialNumber + 1) / 7);-- return NumberValueObject.create(result);+ return NumberValueObject.create(getIsoWeekNumber(dateSerialNumber, this.getDateSystem()));}}diff --git a/packages/engine-formula/src/functions/date/weeknum/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/weeknum/__tests__/index.spec.tsindex 7f47a840e0..de0a26693b 100644--- a/packages/engine-formula/src/functions/date/weeknum/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/date/weeknum/__tests__/index.spec.ts@@ -51,6 +51,31 @@ describe('Test weeknum function', () => {expect(result2.getValue()).toStrictEqual(52);});+ it('return type 21 follows ISO 8601 at year boundaries', () => {+ const returnType = NumberValueObject.create(21);++ // The last days of December that belong to week 1 of the next year.+ expect(testFunction.calculate(StringValueObject.create('2024-12-30'), returnType).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2025-12-29'), returnType).getValue()).toBe(1);+ // Week 1 starts in the last days of the previous year when 1 January is Friday or Saturday.+ expect(testFunction.calculate(StringValueObject.create('2021-1-4'), returnType).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2021-2-1'), returnType).getValue()).toBe(5);+ expect(testFunction.calculate(StringValueObject.create('2016-1-4'), returnType).getValue()).toBe(1);+ });++ it('ignores the time fraction of a date serial', () => {+ const sundayStart = NumberValueObject.create(1);+ const mondayStart = NumberValueObject.create(2);+ const isoWeek = NumberValueObject.create(21);++ // The last day of a week used to be counted into the next week when a time was present.+ expect(testFunction.calculate(NumberValueObject.create(45654.5), sundayStart).getValue()).toBe(52);+ expect(testFunction.calculate(StringValueObject.create('2024-12-28 12:00'), sundayStart).getValue()).toBe(52);+ expect(testFunction.calculate(StringValueObject.create('2019-01-06 12:00'), mondayStart).getValue()).toBe(1);+ expect(testFunction.calculate(NumberValueObject.create(43471.5), isoWeek).getValue()).toBe(1);+ expect(testFunction.calculate(StringValueObject.create('2024-12-29 12:00'), isoWeek).getValue()).toBe(52);+ });+it('uses the 1904 epoch when serial is zero', () => {testFunction.setDateSystem(DateSystem.Date1900);expect(testFunction.calculate(NumberValueObject.create(0), NumberValueObject.create(1)).getValue()).toBe(0);diff --git a/packages/engine-formula/src/functions/date/weeknum/index.ts b/packages/engine-formula/src/functions/date/weeknum/index.tsindex 5edc4e071b..a40e222a9f 100644--- a/packages/engine-formula/src/functions/date/weeknum/index.ts+++ b/packages/engine-formula/src/functions/date/weeknum/index.ts@@ -14,11 +14,12 @@* limitations under the License.*/+import { DateSystem, excelDateSerial, excelSerialToDate } from '@univerjs/core';import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';import type { BaseValueObject } from '../../../engine/value-object/base-value-object';-import { DateSystem, excelDateSerial, excelSerialToDate } from '@univerjs/core';import {getDateSerialNumberByObject,+ getIsoWeekNumber,getWeekDayByDateSerialNumber,} from '../../../basics/date';import { ErrorType } from '../../../basics/error-type';@@ -97,55 +98,29 @@ export class Weeknum extends BaseFunction {}private _getResult(dateSerialNumber: number, returnTypeValue: number) {- const currentDate = excelSerialToDate(dateSerialNumber, this.getDateSystem());- const currentYear = this.getDateSystem() === DateSystem.Date1900 && dateSerialNumber === 0- ? 1900- : currentDate.getUTCFullYear();- let yearStart = new Date(Date.UTC(currentYear, 0, 1));- let yearStartSerialNumber = excelDateSerial(yearStart, this.getDateSystem());- let yearStartWeekDay = getWeekDayByDateSerialNumber(yearStartSerialNumber, this.getDateSystem());-- let yearWeekStartSerialNumber: number;+ const serialNumber = Math.floor(dateSerialNumber);if (returnTypeValue === 21) {// System 2- // The week containing the first Thursday of the year is the first week of the year, and is numbered as week 1. This system is the methodology specified in ISO 8601, which is commonly known as the European week numbering system.- if (yearStartWeekDay < 1) {- yearWeekStartSerialNumber = yearStartSerialNumber + 1;- } else if (yearStartWeekDay <= 4) {- yearWeekStartSerialNumber = yearStartSerialNumber - (yearStartWeekDay - 1);- } else {- yearWeekStartSerialNumber = yearStartSerialNumber + (11 - yearStartWeekDay);- }-- if (dateSerialNumber < yearWeekStartSerialNumber) {- yearStart = new Date(Date.UTC(currentYear - 1, 0, 1));- yearStartSerialNumber = excelDateSerial(yearStart, this.getDateSystem());- yearStartWeekDay = getWeekDayByDateSerialNumber(yearStartSerialNumber, this.getDateSystem());-- if (yearStartWeekDay < 1) {- yearWeekStartSerialNumber = yearStartSerialNumber + 1;- } else if (yearStartWeekDay <= 4) {- yearWeekStartSerialNumber = yearStartSerialNumber - (yearStartWeekDay - 1);- } else {- yearWeekStartSerialNumber = yearStartSerialNumber + (11 - yearStartWeekDay);- }- }- } else {- // System 1- // The week containing January 1 is the first week of the year, and is numbered week 1.- const weekDay = this._returnTypeMap[returnTypeValue];-- if (yearStartWeekDay < weekDay) {- yearWeekStartSerialNumber = yearStartSerialNumber - (yearStartWeekDay + 7 - weekDay);- } else {- yearWeekStartSerialNumber = yearStartSerialNumber - (yearStartWeekDay - weekDay);- }+ // The week containing the first Thursday of the year is week 1, the methodology specified in ISO 8601.+ return NumberValueObject.create(getIsoWeekNumber(serialNumber, this.getDateSystem()));}- const result = Math.ceil((dateSerialNumber - yearWeekStartSerialNumber + 1) / 7);+ // System 1+ // The week containing January 1 is the first week of the year, and is numbered week 1.+ const currentDate = excelSerialToDate(serialNumber, this.getDateSystem());+ const currentYear = this.getDateSystem() === DateSystem.Date1900 && serialNumber === 0+ ? 1900+ : currentDate.getUTCFullYear();+ const yearStartSerialNumber = excelDateSerial(new Date(Date.UTC(currentYear, 0, 1)), this.getDateSystem());+ const yearStartWeekDay = getWeekDayByDateSerialNumber(yearStartSerialNumber, this.getDateSystem());+ const weekDay = this._returnTypeMap[returnTypeValue];++ const yearWeekStartSerialNumber = yearStartWeekDay < weekDay+ ? yearStartSerialNumber - (yearStartWeekDay + 7 - weekDay)+ : yearStartSerialNumber - (yearStartWeekDay - weekDay);- return NumberValueObject.create(result);+ return NumberValueObject.create(Math.floor((serialNumber - yearWeekStartSerialNumber) / 7) + 1);}private _returnTypeMap: IReturnTypeMap = {