Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.11.0

- Make `Location.offset` a `Duration` instead of an `int`.

# 0.10.2

- Upgrade minimum SDK to `^3.10.0` and the databases to [2025c].
Expand Down
26 changes: 11 additions & 15 deletions lib/src/date_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class TZDateTime implements DateTime {
// Get the offset at local (first estimate).
final localInstant = local.millisecondsSinceEpoch;
final localTimezone = location.lookupTimeZone(localInstant);
final localOffset = localTimezone.timeZone.offset;
final localOffset = localTimezone.timeZone.offset.inMilliseconds;

// Adjust localInstant using the estimate and recalculate the offset.
final adjustedInstant = localInstant - localOffset;
final adjustedTimezone = location.lookupTimeZone(adjustedInstant);
final adjustedOffset = adjustedTimezone.timeZone.offset;
final adjustedOffset = adjustedTimezone.timeZone.offset.inMilliseconds;

var milliseconds = localInstant - adjustedOffset;

Expand All @@ -42,7 +42,8 @@ class TZDateTime implements DateTime {
location
.lookupTimeZone(localInstant - adjustedOffset)
.timeZone
.offset) {
.offset
.inMilliseconds) {
milliseconds = adjustedInstant;
}
}
Expand Down Expand Up @@ -269,9 +270,7 @@ class TZDateTime implements DateTime {
);

TZDateTime._(this.native, this.location, this.timeZone)
: _localDateTime = _isUtc(location)
? native
: native.add(_timeZoneOffset(timeZone));
: _localDateTime = _isUtc(location) ? native : native.add(timeZone.offset);

/// Constructs a new [TZDateTime] instance based on [formattedString].
///
Expand Down Expand Up @@ -362,7 +361,7 @@ class TZDateTime implements DateTime {
String toIso8601String() => _toString(iso8601: true);

String _toString({bool iso8601 = true}) {
var offset = timeZone.offset;
final offset = timeZone.offset;

final y = _fourDigits(year);
final m = _twoDigits(month);
Expand All @@ -377,10 +376,10 @@ class TZDateTime implements DateTime {
if (isUtc) {
return '$y-$m-$d$sep$h:$min:$sec.$ms${us}Z';
} else {
final offSign = offset.sign >= 0 ? '+' : '-';
offset = offset.abs() ~/ 1000;
final offH = _twoDigits(offset ~/ 3600);
final offM = _twoDigits((offset % 3600) ~/ 60);
final offSign = offset.isNegative ? '-' : '+';
final offsetSeconds = offset.abs().inSeconds;
final offH = _twoDigits(offsetSeconds ~/ 3600);
final offM = _twoDigits((offsetSeconds % 3600) ~/ 60);

return '$y-$m-$d$sep$h:$min:$sec.$ms$us$offSign$offH$offM';
}
Expand Down Expand Up @@ -490,10 +489,7 @@ class TZDateTime implements DateTime {
/// local time. Java, C# and Ruby return the difference between local time and
/// UTC.
@override
Duration get timeZoneOffset => _timeZoneOffset(timeZone);

static Duration _timeZoneOffset(TimeZone timeZone) =>
Duration(milliseconds: timeZone.offset);
Duration get timeZoneOffset => timeZone.offset;

/// The year.
@override
Expand Down
28 changes: 19 additions & 9 deletions lib/src/location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class Location {
/// translate instant in time expressed as milliseconds since
/// January 1, 1970 00:00:00 UTC to this [Location].
int translate(int millisecondsSinceEpoch) {
return millisecondsSinceEpoch + timeZone(millisecondsSinceEpoch).offset;
return millisecondsSinceEpoch +
timeZone(millisecondsSinceEpoch).offset.inMilliseconds;
}

/// translate instant in time expressed as milliseconds since
Expand All @@ -84,14 +85,18 @@ class Location {

var utc = millisecondsSinceEpoch;

if (tz.offset != 0) {
utc -= tz.offset;
final offset = tz.offset.inMilliseconds;
if (offset != 0) {
utc -= offset;

if (utc < start) {
utc =
millisecondsSinceEpoch - lookupTimeZone(start - 1).timeZone.offset;
millisecondsSinceEpoch -
lookupTimeZone(start - 1).timeZone.offset.inMilliseconds;
} else if (utc >= end) {
utc = millisecondsSinceEpoch - lookupTimeZone(end).timeZone.offset;
utc =
millisecondsSinceEpoch -
lookupTimeZone(end).timeZone.offset.inMilliseconds;
}
}

Expand Down Expand Up @@ -151,8 +156,9 @@ class Location {
final start = t.start;
final end = t.end;

if (tz.offset != 0) {
final utc = millisecondsSinceEpoch - tz.offset;
final offset = tz.offset.inMilliseconds;
if (offset != 0) {
final utc = millisecondsSinceEpoch - offset;

if (utc < start) {
tz = lookupTimeZone(start - 1).timeZone;
Expand Down Expand Up @@ -242,10 +248,14 @@ class Location {
/// A [TimeZone] represents a single time zone such as CEST or CET.
class TimeZone {
// ignore: constant_identifier_names
static const TimeZone UTC = TimeZone(0, isDst: false, abbreviation: 'UTC');
static const TimeZone UTC = TimeZone(
Duration.zero,
isDst: false,
abbreviation: 'UTC',
);

/// Milliseconds east of UTC.
final int offset;
final Duration offset;

/// Is this [TimeZone] Daylight Savings Time?
final bool isDst;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ Location tzfileLocationToNativeLocation(tzfile.Location loc) {
for (final z in loc.zones) {
zones.add(
TimeZone(
z.offset * 1000,
Duration(seconds: z.offset),
isDst: z.isDst,
abbreviation: loc.abbreviations[z.abbreviationIndex],
),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/tzdb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Uint8List _serializeLocation(Location location) {
offset = zonesOffset;
for (var i = 0; i < location.zones.length; i++) {
final zone = location.zones[i];
buffer.setInt32(offset, zone.offset ~/ 1000); // convert to sec
buffer.setInt32(offset, zone.offset.inSeconds); // convert to sec
buffer.setUint8(offset + 4, zone.isDst ? 1 : 0);
buffer.setUint8(offset + 5, zoneAbbreviationOffsets[i]);
offset += 8;
Expand Down Expand Up @@ -203,7 +203,7 @@ Location _deserializeLocation(Uint8List data) {
offset = zonesOffset;
assert((offset % 4) == 0);
for (var i = 0; i < zonesLength; i++) {
final zoneOffset = bdata.getInt32(offset) * 1000; // convert to ms
final zoneOffset = Duration(seconds: bdata.getInt32(offset));
final zoneIsDst = bdata.getUint8(offset + 4);
final zoneAbbreviationIndex = bdata.getUint8(offset + 5);
offset += 8;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: timezone
description: Time zone databases and time zone aware `DateTime`.
repository: https://github.com/srawlins/timezone
version: 0.10.2
version: 0.11.0

environment:
sdk: ^3.10.0
Expand Down