Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(getNow): improve the current time acquisition method #878

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
40 changes: 40 additions & 0 deletions tests/generateWithTZ.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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());
});

afterEach(() => {
dayjs.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)

Loading