Skip to content

Commit c294b2b

Browse files
committed
utils: added getNowInTimezone helper
1 parent cb214b5 commit c294b2b

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/foundation.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:timezone/data/latest.dart' as tz;
34

45
import 'licenses.dart';
56
import 'log.dart';
@@ -13,5 +14,6 @@ void main() {
1314
}());
1415
LicenseRegistry.addLicense(additionalLicenses);
1516
LiveZulipBinding.ensureInitialized();
17+
tz.initializeTimeZones();
1618
runApp(const ZulipApp());
1719
}

lib/utils/date.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:timezone/timezone.dart' as tz;
2+
3+
DateTime? getNowInTimezone(String timezone) {
4+
if (timezone.isEmpty) {
5+
return null;
6+
}
7+
final tz.Location location;
8+
try {
9+
location = tz.getLocation(timezone);
10+
} on tz.LocationNotFoundException {
11+
return null;
12+
}
13+
return tz.TZDateTime.now(location);
14+
}

0 commit comments

Comments
 (0)