Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/generate/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ const parseNoMatchNotice = () => {

const generateConfig: GenerateConfig<Dayjs> = {
// 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) => {
Expand Down
37 changes: 37 additions & 0 deletions tests/generateWithTZ.spec.tsx
Original file line number Diff line number Diff line change
@@ -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');
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"src/**/*.tsx",
"docs/examples/*.tsx",
"tests/**/*.ts",
"tests/**/*.tsx"
"tests/**/*.tsx",
"typings.d.ts"
]
}
3 changes: 3 additions & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'dayjs/plugin/timezone';

export {};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议移除空的导出语句。

空的导出语句在这里是不必要的,因为文件已经有了一个导入语句。移除它可以使代码更简洁。

建议应用以下更改:

import 'dayjs/plugin/timezone';

- export {};
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export {};
import 'dayjs/plugin/timezone';
Tools
Biome

[error] 2-3: This empty export is useless because there's another export or import.

This import makes useless the empty export.

Safe fix: Remove this useless empty export.

(lint/complexity/noUselessEmptyExport)

Loading