my_timezone returns the device's local IANA time zone identifier and the
available identifiers on Android, iOS, macOS, and web.
Features
- Get the local timezone from the native layer.
- Get the list of available timezones.
- Uses one stable MethodChannel contract across Android, iOS, macOS, and web.
- Requires Flutter 3.47.1 or newer and Dart 3.13.1 or newer.
Platform requirements
- Android API 24 or newer.
- iOS 15.0 or newer.
- macOS 12.0 or newer.
- A modern browser with
Intl.DateTimeFormatsupport.
Installation
Add the following to your pubspec.yaml:
my_timezone: ^0.0.4
Then run:
flutter pub get
Basic usage
import 'package:my_timezone/my_timezone.dart';
final String localTimezone = await MyTimezone.getLocalTimezone();
final List<String> availableTimezones =
await MyTimezone.getAvailableTimezones();
On web, browsers expose the local identifier but not the complete IANA time
zone database. getAvailableTimezones() therefore returns a one-item list
containing the local identifier.
Example in a widget
class _MyAppState extends State<MyApp> {
String? _timezone;
@override
void initState() {
super.initState();
_loadTimezone();
}
Future<void> _loadTimezone() async {
final timezone = await MyTimezone.getLocalTimezone();
if (!mounted) return;
setState(() => _timezone = timezone);
}
}
Contribution
If you have any suggestions or find any issues, feel free to open an issue or submit a pull request on GitHub.