Open avendiart opened 4 years ago
Hi, I'm not sure what caused this wired error.
I did not reproduce this error even if I changed my local timezone to "Europe/Berlin".
There's one thing I could confirm is the test runs fine in my local timezone UTC+8 and the Travis CI unit test machine UTC 0.
@iamkun have you tried running it with the specified configuration within the container?
I don't know docker much.
I've only updated my system timezone in "System preferences".
Can you set the Travis CI to a different Timezone? Something like UTC+2 and check if tests still pass?
We've already did this https://github.com/iamkun/dayjs/blob/dev/package.json#L9
But I will try UTC+2 later.
I'm not 100% sure, but I don't think that actually changes the timezone of the system. I'd try setting it like:
before_install:
- export TZ=Europe/Berlin
or trying to run tests using the specified container. Moment and Luxon seem to respect the system timezone correctly, while I had no luck with dayjs. Incidentally it works fine if system timezone is UTC, but I think it's indeed rather a coincidence than the desired behaviour.
This should work will.
I'll close this issue since it's been a while since it's been opened. Feel free to reopen if you have updates on this
@iamkun did you tried the repro above?
Yes, but everything seems all right
Well, I get following result:
Summary of all failing tests
FAIL test/plugin/timezone.test.js (6.961s)
● Parse › parse and convert between timezones
expect(received).toBe(expected) // Object.is equality
Expected value to be:
"2014-06-01T09:00:00-07:00"
Received:
"2014-06-01T09:00:00-08:00"
57 | it('parse and convert between timezones', () => {
58 | const newYork = dayjs.tz('2014-06-01 12:00', NY)
> 59 | expect(newYork.tz('America/Los_Angeles').format()).toBe('2014-06-01T09:00:00-07:00')
60 | expect(newYork.tz('Europe/London').format()).toBe('2014-06-01T17:00:00+01:00')
61 | })
62 |
at Object.<anonymous> (test/plugin/timezone.test.js:59:56)
● Convert › convert to target time
expect(received).toBe(expected) // Object.is equality
Expected value to be:
"2014-06-01T05:00:00-07:00"
Received:
"2014-06-01T05:00:00-08:00"
74 | const losAngeles = dayjs('2014-06-01T12:00:00Z').tz('America/Los_Angeles')
75 | const MlosAngeles = moment('2014-06-01T12:00:00Z').tz('America/Los_Angeles')
> 76 | expect(losAngeles.format()).toBe('2014-06-01T05:00:00-07:00')
77 | expect(losAngeles.format()).toBe(MlosAngeles.format())
78 | expect(losAngeles.valueOf()).toBe(1401624000000)
79 | expect(losAngeles.valueOf()).toBe(MlosAngeles.valueOf())
at Object.<anonymous> (test/plugin/timezone.test.js:76:33)
● Convert › convert to target time
expect(received).toBe(expected) // Object.is equality
Expected value to be:
"2014-06-01T05:00:00-07:00"
Received:
"2014-06-01T05:00:00-08:00"
85 | [dayjs, moment].forEach((_) => {
86 | const losAngeles = _('2014-06-01T12:00:00Z').tz('America/Los_Angeles')
> 87 | expect(losAngeles.format()).toBe('2014-06-01T05:00:00-07:00')
88 | expect(losAngeles.valueOf()).toBe(1401624000000)
89 | })
90 | })
at forEach (test/plugin/timezone.test.js:87:35)
at Array.forEach (<anonymous>)
at Object.<anonymous> (test/plugin/timezone.test.js:85:21)
● Convert › convert from time with timezone to target time
expect(received).toBe(expected) // Object.is equality
Expected value to be:
"2014-06-01T12:00:00Z"
Received:
"2014-06-01T12:00:00-01:00"
93 | const losAngelesInUTC = dayjs('2014-06-01T05:00:00-07:00').tz('UTC')
94 | const MlosAngelesInUTC = moment('2014-06-01T05:00:00-07:00').tz('UTC')
> 95 | expect(losAngelesInUTC.format()).toBe('2014-06-01T12:00:00Z')
96 | expect(losAngelesInUTC.format()).toBe(MlosAngelesInUTC.format())
97 | })
98 |
at Object.<anonymous> (test/plugin/timezone.test.js:95:38)
● Convert › format Z
expect(received).toBe(expected) // Object.is equality
Expected value to be:
"+09:00"
Received:
"+08:00"
115 | [dayjs, moment].forEach((_) => {
116 | const t = _('2020-08-06T03:48:10.258Z').tz(TOKYO)
> 117 | expect(t.format('Z')).toBe('+09:00')
118 | })
119 | })
120 | })
at forEach (test/plugin/timezone.test.js:117:29)
at Array.forEach (<anonymous>)
at Object.<anonymous> (test/plugin/timezone.test.js:115:21)
Test Suites: 1 failed, 64 passed, 65 total
Tests: 5 failed, 597 passed, 602 total
with current master.
@Bessonov Yes, finally I can reproduce it after changing the macOS system's timezone. This might be a bug.
I don't know if it's linked by this bug, but i encountered a problem when i try to setup timezone with jest tests :
On jest.setup.js :
dayjs.tz.setDefault('Europe/Paris');
And when i log dayjs.tz.guess()
on tests, i have following error :
TypeError: Intl.DateTimeFormat(...).resolvedOptions is not a function
jest test are launched with NODE_ICU_DATA=node_modules/full-icu jest
command in package.json
I'm also running into something similar. My tests pass locally, but not in CI. There is a -7:00 timezone difference between the expected and the actual.
@antoninmorvan Did you import the plugins for utc
and timezone
?
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.tz.setDefault('Europe/Paris')
Hi, @amberfrontend I'm getting the same trouble when testing in docker, I tried your idea and I got in these topic of the documentation https://day.js.org/docs/en/parse/utc
// default local time
dayjs().format() //2019-03-06T08:00:00+08:00
It looks like that using dayjs()
will always use the local time instead of the timezone that we set up when testing. So probably this is a bug in the library and I still haven't figured it out how we can use it.
Maybe we can always use the dayjs.utc()
to always get the UTC time, but I need to check it out if it will work.
UPDATE: I got it working with mocha+sinon to fake the time on the tests, you can still use the timezone the you set it up and make sinon to use a fake time with the timezone configured like this:
const fakeDate = dayjs.tz('2021-11-23').toDate();
const datetime = sinon.useFakeTimers(fakeDate);
This should probably force the timezone in the test that you're running, so if you use dayjs()
in your code/component it should also use the timezone.
Describe the bug While working with dayjs and the timezone plugin I've noticed incorrect handling of timezones. Checking out the repository and running tests locally in a container results in some tests failing, which I suppose is the reason for the bug I've encountered in development under Node 12.18.0
Expected behavior Tests passing.
Information
docker-compose.yml used to check out the repository:
Steps with complete logs: