@@ -2,25 +2,29 @@ import 'dart:convert' as convert;
2
2
import 'dart:io' ;
3
3
4
4
import 'package:http/http.dart' as http;
5
- import 'package:timezone/data/latest.dart' as tz;
6
- import 'package:timezone/timezone.dart' as tz;
7
5
8
- const String _version = '2024a' ;
6
+ import 'timezone_compatibility.dart' ;
7
+
8
+ const String TIMEZONE_DATABASE_VERSION = '2024a' ;
9
9
10
10
void main () async {
11
- final timezones = await loadTimezones (_version);
11
+ final timezones = await loadTimezonesWithCountryCodes (
12
+ TIMEZONE_DATABASE_VERSION ,
13
+ );
12
14
if (timezones.isEmpty) return ;
13
15
14
16
generateSourceFile ('timezone_to_country.g.dart' , '_map' , timezones);
15
17
16
- final unsupported = unsupportedTimezones (timezones);
17
- if (timezones .isEmpty) return ;
18
+ final unsupported = findUnmappedTimezonesWithCountryCodes (timezones);
19
+ if (unsupported .isEmpty) return ;
18
20
19
21
generateSourceFile ('timezone_to_country.manual.g.dart' , '_map2' , unsupported);
20
22
}
21
23
22
- /// load timezones and parse
23
- Future <Map <String , String >> loadTimezones (String version) async {
24
+ /// load timezones with their country code from tz database
25
+ Future <Map <String , String >> loadTimezonesWithCountryCodes (
26
+ String version,
27
+ ) async {
24
28
/// download the timezone file
25
29
final url = Uri .parse (
26
30
'https://raw.githubusercontent.com/eggert/tz/$version /zone.tab' );
@@ -40,48 +44,11 @@ Future<Map<String, String>> loadTimezones(String version) async {
40
44
return map;
41
45
}
42
46
43
- /// finds unsupported timezones from timezone package
44
- /// and guess their country code
45
- Map <String , String > unsupportedTimezones (Map <String , String > timezones) {
46
- String ? guess (String timezoneId) {
47
- if (timezoneId == 'GMT' || timezoneId == 'UTC' ) return null ;
48
- if (timezoneId.startsWith ('US/' )) return 'US' ;
49
- if (timezoneId.startsWith ('Canada/' )) return 'CA' ;
50
- if (timezoneId.startsWith ('Australia/' )) return 'AU' ;
51
-
52
- final code = {
53
- 'America/Godthab' : 'GL' ,
54
- 'America/Montreal' : 'CA' ,
55
- 'America/Nipigon' : 'CA' ,
56
- 'America/Pangnirtung' : 'CA' ,
57
- 'America/Rainy_River' : 'CA' ,
58
- 'America/Santa_Isabel' : 'MX' ,
59
- 'America/Thunder_Bay' : 'CA' ,
60
- 'America/Yellowknife' : 'CA' ,
61
- 'Asia/Rangoon' : 'MM' ,
62
- 'Europe/Uzhgorod' : 'UA' ,
63
- 'Europe/Zaporozhye' : 'UA' ,
64
- 'Pacific/Enderbury' : 'KI' ,
65
- 'Pacific/Johnston' : 'US' ,
66
- }[timezoneId];
67
-
68
- assert (code != null , 'You should implement: $timezoneId .' );
69
- return code;
70
- }
71
-
72
- tz.initializeTimeZones ();
73
- final unsupported = Map .fromEntries (() sync * {
74
- for (final timezoneId in tz.timeZoneDatabase.locations.keys
75
- .where ((timezoneId) => timezones[timezoneId] == null )) {
76
- final code = guess (timezoneId);
77
- if (code != null ) yield MapEntry (timezoneId, code);
78
- }
79
- }());
80
- return unsupported;
81
- }
82
-
83
47
Future <void > generateSourceFile (
84
- String fileName, String mapName, Map <String , String > timezones) async {
48
+ String fileName,
49
+ String mapName,
50
+ Map <String , String > timezones,
51
+ ) async {
85
52
/// write to source file
86
53
final sb = StringBuffer ();
87
54
sb.writeln ("part of 'timezone_to_country.dart';" );
0 commit comments