From 9ac8cdac2a404d7cd565948eea9cd42087ee9f0f Mon Sep 17 00:00:00 2001 From: Matthias Ngeo Date: Thu, 4 Apr 2024 07:22:16 +0800 Subject: [PATCH] Fix incorrect DST transition (#166) --- lib/src/date_time.dart | 40 ++++++++---- test/datetime_test.dart | 136 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 11 deletions(-) diff --git a/lib/src/date_time.dart b/lib/src/date_time.dart index b5f5920..a88aafe 100644 --- a/lib/src/date_time.dart +++ b/lib/src/date_time.dart @@ -5,7 +5,7 @@ import 'package:timezone/src/env.dart'; import 'package:timezone/src/location.dart'; -/// TimeZone aware DateTime +/// TimeZone aware DateTime. class TZDateTime implements DateTime { /// Maximum value for time instants. static const int maxMillisecondsSinceEpoch = 8640000000000000; @@ -18,20 +18,38 @@ class TZDateTime implements DateTime { /// Converts a [_localDateTime] into a correct [DateTime]. static DateTime _utcFromLocalDateTime(DateTime local, Location location) { - var unix = local.millisecondsSinceEpoch; - var tzData = location.lookupTimeZone(unix); - if (tzData.timeZone.offset != 0) { - final utc = unix - tzData.timeZone.offset; - if (utc < tzData.start) { - tzData = location.lookupTimeZone(tzData.start - 1); - } else if (utc >= tzData.end) { - tzData = location.lookupTimeZone(tzData.end); + // Adapted from https://github.com/JodaOrg/joda-time/blob/main/src/main/java/org/joda/time/DateTimeZone.java#L951 + // Get the offset at local (first estimate). + final localInstant = local.millisecondsSinceEpoch; + final localTimezone = location.lookupTimeZone(localInstant); + final localOffset = localTimezone.timeZone.offset; + + // Adjust localInstant using the estimate and recalculate the offset. + final adjustedInstant = localInstant - localOffset; + final adjustedTimezone = location.lookupTimeZone(adjustedInstant); + final adjustedOffset = adjustedTimezone.timeZone.offset; + + var milliseconds = localInstant - adjustedOffset; + + // If the offsets differ, we must be near a DST boundary + if (localOffset != adjustedOffset) { + // We need to ensure that time is always after the DST gap + // this happens naturally for positive offsets, but not for negative. + // If we just use adjustedOffset then the time is pushed back before the + // transition, whereas it should be on or after the transition + if (localOffset - adjustedOffset < 0 && + adjustedOffset != + location + .lookupTimeZone(localInstant - adjustedOffset) + .timeZone + .offset) { + milliseconds = adjustedInstant; } - unix -= tzData.timeZone.offset; } + // Ensure original microseconds are preserved regardless of TZ shift. final microsecondsSinceEpoch = - Duration(milliseconds: unix, microseconds: local.microsecond) + Duration(milliseconds: milliseconds, microseconds: local.microsecond) .inMicroseconds; return DateTime.fromMicrosecondsSinceEpoch(microsecondsSinceEpoch, isUtc: true); diff --git a/test/datetime_test.dart b/test/datetime_test.dart index 09f01f0..a352a74 100644 --- a/test/datetime_test.dart +++ b/test/datetime_test.dart @@ -156,6 +156,142 @@ Future main() async { }); }); }); + + group('America/Detroit DST (negative offset)', () { + // https://www.timeanddate.com/time/change/usa/detroit?year=2023 + group('EST/EDT transition', () { + test('2 months before transition', () { + final datetime = TZDateTime(detroit, 2023, 1, 12, 4); + expect(datetime.toString(), '2023-01-12 04:00:00.000-0500'); + }); + + test('1 hour before transition', () { + final datetime = TZDateTime(detroit, 2023, 3, 12, 1); + expect(datetime.toString(), '2023-03-12 01:00:00.000-0500'); + }); + + test('lower transition', () { + final datetime = TZDateTime(detroit, 2023, 3, 12, 2); + expect(datetime.toString(), '2023-03-12 03:00:00.000-0400'); + }); + + test('upper transition', () { + final datetime = TZDateTime(detroit, 2023, 3, 12, 3); + expect(datetime.toString(), '2023-03-12 03:00:00.000-0400'); + }); + + test('1 hour after transition', () { + final datetime = TZDateTime(detroit, 2023, 3, 12, 4); + expect(datetime.toString(), '2023-03-12 04:00:00.000-0400'); + }); + + test('2 months after transition', () { + final datetime = TZDateTime(detroit, 2023, 5, 12, 4); + expect(datetime.toString(), '2023-05-12 04:00:00.000-0400'); + }); + }); + + group('EDT/EST transition', () { + test('2 months before transition', () { + final datetime = TZDateTime(detroit, 2023, 9, 5, 1); + expect(datetime.toString(), '2023-09-05 01:00:00.000-0400'); + }); + + test('1 hour before transition', () { + final datetime = TZDateTime(detroit, 2023, 11, 5); + expect(datetime.toString(), '2023-11-05 00:00:00.000-0400'); + }); + + test('lower transition', () { + final datetime = TZDateTime(detroit, 2023, 11, 5, 1); + expect(datetime.toString(), '2023-11-05 01:00:00.000-0400'); + }); + + test('upper transition', () { + final datetime = TZDateTime(detroit, 2023, 11, 5, 2); + expect(datetime.toString(), '2023-11-05 02:00:00.000-0500'); + }); + + test('1 hour after transition', () { + final datetime = TZDateTime(detroit, 2023, 11, 5, 3); + expect(datetime.toString(), '2023-11-05 03:00:00.000-0500'); + }); + + test('2 months after transition', () { + final datetime = TZDateTime(detroit, 2024, 1, 5, 2); + expect(datetime.toString(), '2024-01-05 02:00:00.000-0500'); + }); + }); + }); + + group('Europe/Berlin DST (positive offset)', () { + // https://www.timeanddate.com/time/change/germany/berlin?year=2023 + final berlin = getLocation('Europe/Berlin'); + + group('EST/EDT transition', () { + test('2 months before transition', () { + final datetime = TZDateTime(berlin, 2023, 1, 26, 2); + expect(datetime.toString(), '2023-01-26 02:00:00.000+0100'); + }); + + test('1 hour before transition', () { + final datetime = TZDateTime(berlin, 2023, 3, 26, 1); + expect(datetime.toString(), '2023-03-26 01:00:00.000+0100'); + }); + + test('lower transition', () { + final datetime = TZDateTime(berlin, 2023, 3, 26, 2); + expect(datetime.toString(), '2023-03-26 03:00:00.000+0200'); + }); + + test('upper transition', () { + final datetime = TZDateTime(berlin, 2023, 3, 26, 3); + expect(datetime.toString(), '2023-03-26 03:00:00.000+0200'); + }); + + test('1 hour after transition', () { + final datetime = TZDateTime(berlin, 2023, 3, 26, 4); + expect(datetime.toString(), '2023-03-26 04:00:00.000+0200'); + }); + + test('2 months after transition', () { + final datetime = TZDateTime(berlin, 2023, 5, 26, 3); + expect(datetime.toString(), '2023-05-26 03:00:00.000+0200'); + }); + }); + + group('EDT/EST transition', () { + test('2 months before transition', () { + final datetime = TZDateTime(berlin, 2023, 8, 29, 2); + expect(datetime.toString(), '2023-08-29 02:00:00.000+0200'); + }); + + test('1 hour before transition', () { + final datetime = TZDateTime(berlin, 2023, 10, 29, 1); + expect(datetime.toString(), '2023-10-29 01:00:00.000+0200'); + }); + + test('lower transition', () { + final datetime = TZDateTime(berlin, 2023, 10, 29, 2); + expect(datetime.toString(), '2023-10-29 02:00:00.000+0100'); + }); + + test('upper transition', () { + final datetime = TZDateTime(berlin, 2023, 10, 29, 3); + expect(datetime.toString(), '2023-10-29 03:00:00.000+0100'); + }); + + test('1 hour after transition', () { + final datetime = TZDateTime(berlin, 2023, 10, 29, 4); + expect(datetime.toString(), '2023-10-29 04:00:00.000+0100'); + }); + + test('2 months after transition', () { + final datetime = TZDateTime(berlin, 2024, 1, 29, 3); + expect(datetime.toString(), '2024-01-29 03:00:00.000+0100'); + }); + }); + }); }); group('Timezones', () {