From 292eb698a16d54f17f2171c217f5a38914a95dd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=91=BE=F0=9D=92=96=F0=9D=92=99=F0=9D=92=89?= Date: Mon, 23 Sep 2024 21:25:00 +0800 Subject: [PATCH 1/6] feat(getNow): Improve the current time acquisition method background: https://github.com/ant-design/ant-design/discussions/50934 --- src/generate/dayjs.ts | 8 +++++++- tsconfig.json | 3 ++- typings.d.ts | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 typings.d.ts diff --git a/src/generate/dayjs.ts b/src/generate/dayjs.ts index aae56610b..ddb48c27b 100644 --- a/src/generate/dayjs.ts +++ b/src/generate/dayjs.ts @@ -107,7 +107,13 @@ const parseNoMatchNotice = () => { const generateConfig: GenerateConfig = { // get - getNow: () => dayjs(), + getNow: () => { + if (typeof dayjs.tz === 'function') { + // https://github.com/ant-design/ant-design/discussions/50934 + return dayjs.tz(); + } + return dayjs(); + }, getFixedDate: (string) => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']), getEndDate: (date) => date.endOf('month'), getWeekDay: (date) => { diff --git a/tsconfig.json b/tsconfig.json index 5e7976cfb..06c770c29 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,6 +19,7 @@ "src/**/*.tsx", "docs/examples/*.tsx", "tests/**/*.ts", - "tests/**/*.tsx" + "tests/**/*.tsx", + "typings.d.ts" ] } diff --git a/typings.d.ts b/typings.d.ts new file mode 100644 index 000000000..91381c62b --- /dev/null +++ b/typings.d.ts @@ -0,0 +1,3 @@ +import 'dayjs/plugin/timezone'; + +export {}; From cfa83eaad105f7fa994f4990d7429ce05958e9ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=91=BE=F0=9D=92=96=F0=9D=92=99=F0=9D=92=89?= Date: Mon, 23 Sep 2024 22:38:42 +0800 Subject: [PATCH 2/6] test: add unit test --- tests/generateWithTZ.spec.tsx | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/generateWithTZ.spec.tsx diff --git a/tests/generateWithTZ.spec.tsx b/tests/generateWithTZ.spec.tsx new file mode 100644 index 000000000..0adb673ef --- /dev/null +++ b/tests/generateWithTZ.spec.tsx @@ -0,0 +1,37 @@ +import MockDate from 'mockdate'; +import dayjsGenerateConfig from '../src/generate/dayjs'; + +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc'; +import timezone from 'dayjs/plugin/timezone'; + +dayjs.extend(utc); +dayjs.extend(timezone); + +const CN = 'Asia/Shanghai'; +const JP = 'Asia/Tokyo'; + +beforeEach(() => { + MockDate.set(dayjs.tz('2024-09-23 05:02:03.172', CN).toDate()); +}); + +afterEach(() => { + dayjs.tz.setDefault(); + MockDate.reset(); +}); + +describe('dayjs: getNow', () => { + it('normal', () => { + const now = new Date(); + expect(now.toDateString()).toEqual('Mon Sep 23 2024'); + expect(now.toTimeString()).toContain('05:02:03 GMT+0800'); + }); + + it('should be work', async () => { + dayjs.tz.setDefault(JP); + const now = dayjsGenerateConfig.getNow(); + const L_now = dayjs(); + expect(L_now.format()).toEqual('2024-09-23T05:02:03+08:00'); + expect(now.format()).toEqual('2024-09-23T06:02:03+09:00'); + }); +}); From 1d6b65abea232e2debdc96087e0f22c4c7f18a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=91=BE=F0=9D=92=96=F0=9D=92=99=F0=9D=92=89?= Date: Tue, 24 Sep 2024 09:55:07 +0800 Subject: [PATCH 3/6] fix: unit test --- package.json | 1 + tests/generateWithTZ.spec.tsx | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index be0d68cab..adf64da19 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "luxon": "3.x", "mockdate": "^3.0.2", "moment": "^2.24.0", + "moment-timezone": "^0.5.45", "np": "^10.0.2", "prettier": "^3.1.0", "rc-test": "^7.0.9", diff --git a/tests/generateWithTZ.spec.tsx b/tests/generateWithTZ.spec.tsx index 0adb673ef..f64585f4b 100644 --- a/tests/generateWithTZ.spec.tsx +++ b/tests/generateWithTZ.spec.tsx @@ -4,6 +4,7 @@ import dayjsGenerateConfig from '../src/generate/dayjs'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; +import moment from 'moment-timezone'; dayjs.extend(utc); dayjs.extend(timezone); @@ -12,7 +13,7 @@ const CN = 'Asia/Shanghai'; const JP = 'Asia/Tokyo'; beforeEach(() => { - MockDate.set(dayjs.tz('2024-09-23 05:02:03.172', CN).toDate()); + MockDate.set(new Date()); }); afterEach(() => { @@ -22,16 +23,18 @@ afterEach(() => { describe('dayjs: getNow', () => { it('normal', () => { - const now = new Date(); - expect(now.toDateString()).toEqual('Mon Sep 23 2024'); - expect(now.toTimeString()).toContain('05:02:03 GMT+0800'); + const D_now = dayjsGenerateConfig.getNow(); + const M_now = moment(); + + expect(D_now.format()).toEqual(M_now.format()); }); - it('should be work', async () => { + it('should work normally in timezone', async () => { dayjs.tz.setDefault(JP); - const now = dayjsGenerateConfig.getNow(); - const L_now = dayjs(); - expect(L_now.format()).toEqual('2024-09-23T05:02:03+08:00'); - expect(now.format()).toEqual('2024-09-23T06:02:03+09:00'); + const D_now = dayjsGenerateConfig.getNow(); + const M_now = moment().tz(JP); + + expect(D_now.format()).toEqual(M_now.format()); + expect(D_now.get('hour') - D_now.utc().get('hour')).toEqual(9); }); }); From 8c8d08346fe72a3e8dcc00cfdeb62b5b147a079c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=91=BE=F0=9D=92=96=F0=9D=92=99=F0=9D=92=89?= Date: Tue, 24 Sep 2024 10:56:47 +0800 Subject: [PATCH 4/6] chore: update? --- tests/generateWithTZ.spec.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/generateWithTZ.spec.tsx b/tests/generateWithTZ.spec.tsx index f64585f4b..d53b344d8 100644 --- a/tests/generateWithTZ.spec.tsx +++ b/tests/generateWithTZ.spec.tsx @@ -14,10 +14,13 @@ const JP = 'Asia/Tokyo'; beforeEach(() => { MockDate.set(new Date()); + dayjs.tz.setDefault(CN); + moment.tz.setDefault(CN); }); afterEach(() => { dayjs.tz.setDefault(); + moment.tz.setDefault(); MockDate.reset(); }); From 793008730ef9b613efebffbaef004ae7c854cc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=91=BE=F0=9D=92=96=F0=9D=92=99=F0=9D=92=89?= Date: Tue, 24 Sep 2024 11:09:04 +0800 Subject: [PATCH 5/6] test: CI --- src/generate/dayjs.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/generate/dayjs.ts b/src/generate/dayjs.ts index ddb48c27b..02cd18ea4 100644 --- a/src/generate/dayjs.ts +++ b/src/generate/dayjs.ts @@ -108,10 +108,10 @@ const parseNoMatchNotice = () => { const generateConfig: GenerateConfig = { // get getNow: () => { - if (typeof dayjs.tz === 'function') { - // https://github.com/ant-design/ant-design/discussions/50934 - return dayjs.tz(); - } + // if (typeof dayjs.tz === 'function') { + // // https://github.com/ant-design/ant-design/discussions/50934 + // return dayjs.tz(); + // } return dayjs(); }, getFixedDate: (string) => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']), From 2f31f29a24448adea136af3a1cba86d007fa8700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=91=BE=F0=9D=92=96=F0=9D=92=99=F0=9D=92=89?= Date: Tue, 24 Sep 2024 11:15:40 +0800 Subject: [PATCH 6/6] chore: update logic --- src/generate/dayjs.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/generate/dayjs.ts b/src/generate/dayjs.ts index 02cd18ea4..91291ce30 100644 --- a/src/generate/dayjs.ts +++ b/src/generate/dayjs.ts @@ -108,11 +108,12 @@ const parseNoMatchNotice = () => { const generateConfig: GenerateConfig = { // get getNow: () => { - // if (typeof dayjs.tz === 'function') { - // // https://github.com/ant-design/ant-design/discussions/50934 - // return dayjs.tz(); - // } - return dayjs(); + const now = dayjs(); + // https://github.com/ant-design/ant-design/discussions/50934 + if (typeof now.tz === 'function') { + return now.tz(); // use default timezone + } + return now; }, getFixedDate: (string) => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']), getEndDate: (date) => date.endOf('month'),