Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

Flutter/Dart - Why does DateTime.toLocal() behave differently for dates before 01.01.1970 UTC aka UNIX timestamp 0?

Using .toLocal() on DateTimes before 01.01.1970 (thus with a negative UNIX timestamp) seemingly results in the timezone information getting lost.

final DateTime date1 = DateTime.parse('1970-01-01T00:59:59.999+01:00');
final DateTime date2 = DateTime.parse('1970-01-01T01:00:00.000+01:00');
final DateTime date3 = DateTime.parse('1970-01-01T01:00:00.001+01:00');

// executing the following lines on a machine with timezone UTC+01:00

print(date1.toLocal().toIso8601String());
// expected: 1970-01-01T00:59:59.999
// actual:   1969-12-31T23:59:59.999

print(date2.toLocal().toIso8601String());
// expected: 1970-01-01T01:00:00.000
// actual:   1970-01-01T01:00:00.000

print(date3.toLocal().toIso8601String());
// expected: 1970-01-01T01:00:00.001
// actual:   1970-01-01T01:00:00.001

Is this a bug or intended behavior? And if the latter, how would I preserve the timezone for DateTimes older than 01.01.1970?

Info:
OS: Windows 10 Pro x64

$ flutter --version
Flutter 1.22.5 ? channel stable ? https://github.com/flutter/flutter.git
Framework ? revision 7891006299 (5 weeks ago) ? 2020-12-10 11:54:40 -0800
Engine ? revision ae90085a84
Tools ? Dart 2.10.4

Note:
This issue does not seem to occur on dartpad.dev (Based on Flutter 1.25.0-8.2.pre Dart SDK 2.10.4)

UPDATE:

I filed an issue in the Dart SDK repository: https://github.com/dart-lang/sdk/issues/44656


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...