Skip to content

Commit

Permalink
Fix incorrect DST transition (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Apr 3, 2024
1 parent d2f59a3 commit 9ac8cda
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 11 deletions.
40 changes: 29 additions & 11 deletions lib/src/date_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
136 changes: 136 additions & 0 deletions test/datetime_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,142 @@ Future<void> 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', () {
Expand Down

0 comments on commit 9ac8cda

Please sign in to comment.