How to get local timezone name?

Viewed 1085

I want get local time zone name like "Asia/Tokyo"

I tried

DateTime.now().timeZoneName

This gives value like +0530. Is there a way to get the time zone name?

2 Answers

There is a timezone package. You can get time zone names from the IANA Time Zone Database with this package. It should look like this:


Future<void> initTimezones() async {
    // get device timezone
    String dtz = await FlutterNativeTimezone.getLocalTimezone();

    // Load timezone data
    var byteData = await rootBundle.load('assets/timezone/2020a.tzf');
    tz.initializeDatabase(byteData.buffer.asUint8List());
    tz.initializeTimeZones();
    // set the local location.
    tz.setLocalLocation(tz.getLocation(dtz));

    // Iterate and use through the list of timezones.
    _locations = tz.timeZoneDatabase.locations;
    _locations.values.forEach((element) {
        print(element.name);
        print(element.currentTimeZone.abbr);
        print(element.currentTimeZone.offset);
        print(element.currentTimeZone.isDst);
        print(element.zones.length);
      });
  }

There are some helpful links about this.

The read.me file shows how to get the timezone name:


import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;

void main() {
  tz.initializeTimeZones();
  var locations = tz.timeZoneDatabase.locations;
  print(locations.length); // => 429
  print(locations.keys.first); // => "Africa/Abidjan"
  print(locations.keys.last); // => "US/Pacific"
}

TimeZone names as the required format - 'Asia/Tokyo' require downloaded library files, package or API services. If reliability is not important you may try this Flutter package or use more reliable TimeZone API from Google. It will require latitude, longitude for providing timeZoneId and other necessary details.

Related