-
-
Notifications
You must be signed in to change notification settings - Fork 334
feat(getNow): improve the current time acquisition method #878
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
Changes from all commits
292eb69
cfa83ea
1d6b65a
8c8d083
7930087
2f31f29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"src/**/*.tsx", | ||
"docs/examples/*.tsx", | ||
"tests/**/*.ts", | ||
"tests/**/*.tsx" | ||
"tests/**/*.tsx", | ||
"typings.d.ts" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,3 @@ | ||||||
import 'dayjs/plugin/timezone'; | ||||||
|
||||||
export {}; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议移除空的导出语句。 空的导出语句在这里是不必要的,因为文件已经有了一个导入语句。移除它可以使代码更简洁。 建议应用以下更改: import 'dayjs/plugin/timezone';
- export {}; Committable suggestion
Suggested change
ToolsBiome
|
There was a problem hiding this comment.
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 设置的默认时区
以上纯个人看文档理解的,有错误欢迎指出
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
担心 getNow 不太够,怕有什么地方被穿掉,但是试了一下似乎没问题~