How to get local timezone name?

586

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"
}

Share:
586
Janaka
Author by

Janaka

Updated on December 25, 2022

Comments

  • Janaka
    Janaka over 1 year

    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?