From the documentation
The constructed DateTime represents 1970-01-01T00:00:00Z millisecondsSinceEpoch ms in the given time zone (local or UTC).
Therefore, if my local timezone is 1, this test should pass -
test('DateTime', () {
var dt = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
expect(dt.toIso8601String(), '1970-01-01T00:00:00.000Z');
var dtLocal = DateTime.fromMillisecondsSinceEpoch(0, isUtc: false);
expect(dtLocal.toIso8601String(), '1970-01-01T00:00:00.000');
});
However, it fails as dtLocal.toIso8601String()
gives 1970-01-01T01:00:00.000
. Is it just me or is the documentation unclear? I would expect it to just change the timezone, not the milliseconds since epoch based on the local timezone.
CodePudding user response:
The start of the epoch is 1970-01-01T00:00:00 UTC. The start of the the epoch is not different for different time zones. So the start of the epoch in my local time zone (EST/UTC-5) would be 5 hours before that date: 1969-12-31T19:00:00.
This is exactly what your code is trying to do. dt
is getting the time of the epoch start in UTC 0. dtLocal
is getting that exact same time, but putting it in your time zone, which appears to be UTC 1.
The behavior you're getting is expected.