Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion src/generate/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ const parseNoMatchNotice = () => {

const generateConfig: GenerateConfig<Dayjs> = {
// get
getNow: () => dayjs(),
getNow: () => {
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;
},
Comment on lines +110 to +117
Copy link
Contributor Author

Choose a reason for hiding this comment

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

dayjs.tz 是原型上的方法: ref:https://github.com/iamkun/dayjs/blob/fa1612328b4e2dc0263f5b7e83eeeb343ccd7021/src/plugin/timezone/index.js#L95

dayjs().tz 是 Dayjs 对象上的方法:https://github.com/iamkun/dayjs/blob/fa1612328b4e2dc0263f5b7e83eeeb343ccd7021/src/plugin/timezone/index.js#L135
dayjs().tz 是转换到对应时区,不填时,转化到 setDefault 设置的默认时区

以上纯个人看文档理解的,有错误欢迎指出

Copy link
Member

Choose a reason for hiding this comment

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

担心 getNow 不太够,怕有什么地方被穿掉,但是试了一下似乎没问题~

getFixedDate: (string) => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']),
getEndDate: (date) => date.endOf('month'),
getWeekDay: (date) => {
Expand Down
43 changes: 43 additions & 0 deletions tests/generateWithTZ.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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';
import moment from 'moment-timezone';

dayjs.extend(utc);
dayjs.extend(timezone);

const CN = 'Asia/Shanghai';
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();
});

describe('dayjs: getNow', () => {
it('normal', () => {
const D_now = dayjsGenerateConfig.getNow();
const M_now = moment();

expect(D_now.format()).toEqual(M_now.format());
});

it('should work normally in timezone', async () => {
dayjs.tz.setDefault(JP);
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);
});
});
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)