Skip to content

Commit

Permalink
Migrate to new timezone database - Attempt #2 (#83)
Browse files Browse the repository at this point in the history
* fix windows integration testing

* add timezone abbr to offset

* convert func takes datetime parts instead of millis from epch

* remove old database files

* add toString and correct final/initial for span

* replace default provider with embedded provider

* update tests

* add packages to pubspec

* add timezone generation tool

* replace default provider with embedded

* add java testing

* fix dst bug, add dst and abbr to java provider

* Add CI

* ignore unsued

* remove unused func

* rename DSTRules

* nits

* remove base provider
  • Loading branch information
dickermoshe authored Feb 18, 2025
1 parent 2dac846 commit 5c80700
Show file tree
Hide file tree
Showing 654 changed files with 11,607 additions and 170,808 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sugar_integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- uses: actions/checkout@v4
- uses: subosito/[email protected]
- run: dart pub get
- run: dart run coverage:test_with_coverage test ./integration_test/
- run: dart run coverage:test_with_coverage --test .\integration_test\src\time\zone\platform\platform_provider_test.dart
- uses: codecov/codecov-action@v5
with:
files: ./sugar/coverage/lcov.info
Expand Down Expand Up @@ -87,4 +87,4 @@ jobs:
- run: ./integration_test/src/time/zone/platform/platform_provider_test_browser.sh
- uses: codecov/codecov-action@v5
with:
files: ./sugar/coverage/lcov.info
files: ./sugar/coverage/lcov.info
15 changes: 13 additions & 2 deletions .github/workflows/sugar_unit_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,23 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: subosito/[email protected]
- name: Install Node
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install Java
uses: actions/setup-java@v4
with:
distribution: "adopt"
java-version: "11.0.26+4"
- run: dart pub get
- run: dart analyze --fatal-warnings
- run: dart format . --set-exit-if-changed --output none
- run: dart run tool/generate_timezones.dart
- run: dart run coverage:test_with_coverage
- run: sudo apt-get update -y
- run: sudo apt-get install -y lcov
- run: lcov --remove ./coverage/lcov.info '**.g.dart' -o ./coverage/lcov.info
- run: lcov --remove ./coverage/lcov.info '**.g.dart' -o ./coverage/lcov.info --ignore-errors unused
- uses: codecov/codecov-action@v5
with:
files: ./sugar/coverage/lcov.info
files: ./sugar/coverage/lcov.info
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import 'package:test/test.dart';
/// These tests should be ran using the shell/bat scripts in the same folder.
void main() {
test('defaultPlatformTimezoneProvider() return current timezone', () {
final timezones = DefaultTimezoneProvider();
final timezones = EmbeddedTimezoneProvider();
final timezone = defaultPlatformTimezoneProvider();

expect(timezones.containsKey(timezone), true);
expect(timezone, isNot('Factory'));
}, testOn: 'windows');

test('defaultPlatformTimezoneProvider() return current timezone', () {
final timezones = DefaultTimezoneProvider();
final timezones = EmbeddedTimezoneProvider();
final timezone = defaultPlatformTimezoneProvider();

expect(timezones.containsKey(timezone), true);
Expand All @@ -21,15 +21,15 @@ void main() {

group('posix', () {
test('defaultPlatformTimezoneProvider() known TZ environment variable', () {
final timezones = DefaultTimezoneProvider();
final timezones = EmbeddedTimezoneProvider();
final timezone = defaultPlatformTimezoneProvider();

expect(timezones.containsKey(timezone), true);
expect(timezone, 'Mexico/BajaSur');
});

test('defaultPlatformTimezoneProvider() unknown TZ environment variable', () {
final timezones = DefaultTimezoneProvider();
final timezones = EmbeddedTimezoneProvider();
final timezone = defaultPlatformTimezoneProvider();

expect(timezones.containsKey(timezone), true);
Expand Down
15 changes: 13 additions & 2 deletions sugar/lib/src/time/offset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ sealed class Offset with Orderable<Offset> {
@override
@nonVirtual
@useResult
bool operator ==(Object other) => // ignore: hash_and_equals, invalid_override_of_non_virtual_member
// ignore: hash_and_equals, invalid_override_of_non_virtual_member
bool operator ==(Object other) =>
identical(this, other) ||
other is Offset && (runtimeType == _Offset || runtimeType == LiteralOffset) && compareTo(other) == 0;

Expand All @@ -268,7 +269,17 @@ sealed class Offset with Orderable<Offset> {
/// print(Offset(1, 2, 3)); // '+01:02:03'
/// ```
@override
@mustBeOverridden
@useResult
String toString();

/// Returns the offset represented as a timezone abbreviation, adhering to
/// the ISO 8601 standard.
///
/// See: https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
///
/// Example:
/// ```dart
/// print(Offset(1).toTimezoneAbbreviation()); // '+0100'
/// ```
String toTimezoneAbbreviation() => formatTimezoneAbbreviation(_microseconds);
}
23 changes: 22 additions & 1 deletion sugar/lib/src/time/offsets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ String format(int microseconds) {
return '$sign$hours:$minutes$suffix';
}

/// Format a duration of microseconds into a timezone abbreviation.
///
/// ```dart
/// print(toTimezoneAbbreviation(0)); // '+0000'
/// print(toTimezoneAbbreviation(1)); // '+0100'
/// print(toTimezoneAbbreviation(-1)); // '-0100'
/// ```
String formatTimezoneAbbreviation(int microseconds) {
var value = microseconds ~/ 1000 ~/ 1000 ~/ 60;
final sign = value.isNegative ? '-' : '+';
value = value.abs();
final minute = value % 60;
value ~/= 60;
final hour = value % 60;
final hours = hour.toString().padLeft(2, '0');
final minutes = minute.toString().padLeft(2, '0');
return '$sign$hours$minutes';
}

const _allowed = '''
The following offset formats are accepted:
* Z - for UTC
Expand Down Expand Up @@ -102,7 +121,9 @@ final class _Offset extends Offset {
return parsed;
}

late final int hour, minute, second; // ignore: avoid_multiple_declarations_per_line
late final int hour;
late final int minute;
late final int second; // ignore: avoid_multiple_declarations_per_line
switch (offset.length) {
case 2:
offset = '${offset[0]}0${offset[1]}';
Expand Down
160 changes: 0 additions & 160 deletions sugar/lib/src/time/zone/dynamic_timezone.dart

This file was deleted.

36 changes: 36 additions & 0 deletions sugar/lib/src/time/zone/factory_timezone.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:sugar/src/time/zone/timezone.dart';
import 'package:sugar/src/time/zone/timezone_span.dart';
import 'package:sugar/time.dart';

/// A timezone which is used when the platform's timezone could not be retrieved.
class FactoryTimezone extends Timezone {
/// Creates a new instance of [FactoryTimezone].
///
/// This constructor calls the super constructor with the string 'Factory'.
const FactoryTimezone() : super.from('Factory');

@override
(EpochMicroseconds, TimezoneSpan) convert(
int year, [
int month = 1,
int day = 1,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0,
]) => (0, span(at: 0));

@override
TimezoneSpan span({required EpochMicroseconds at}) => _FactoryTimezoneSpan(offset: Offset());
}

class _FactoryTimezoneSpan extends TimezoneSpan {
@override
EpochMicroseconds get start => TimezoneSpan.range.min.value;
@override
EpochMicroseconds get end => TimezoneSpan.range.max.value;

/// Creates a new instance of [_FactoryTimezoneSpan].
_FactoryTimezoneSpan({required super.offset, super.abbreviation = '+0000', super.dst = false});
}
30 changes: 0 additions & 30 deletions sugar/lib/src/time/zone/fixed_timezone.dart

This file was deleted.

Loading

0 comments on commit 5c80700

Please sign in to comment.