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

Categories

0 votes
4.6k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...