Working with timezones in Flutter

Udit Chugh
Flutter Community
Published in
3 min readSep 12, 2019

--

The DateTime class in Dart lets you do a lot of things with Time from getting the system time to parsing the time received from other sources.

But due to the ever-changing list of timezones and daylight savings, the DateTime class is not time zone aware.

And the ‘timezone’ package by the Google team solves exactly that by making DateTime class time zone aware.

It uses the IANA Time Zone databases which makes it reliable for use in production code.

But I felt that the readme section on the package’s pub.dev page is not updated with detailed instructions for use with Flutter. So I documented the steps while figuring out how to use it so that others don’t have to struggle as much as I did.

Illustration credits: unDraw

Step 1: Add the package to your project’s pubspec.yaml

dependencies:
timezone: ^0.5.4

Step 2: Install the package

flutter pub get

Step 3: Navigate to the package’s installation directory in your terminal.

In my case, I am using Android Studio so I was able to navigate to the directory by going to the external libraries section in the project view.

Under this go to the Dart Packages and open the timezone directory in terminal.

By default that will take you to the ‘lib’ folder of the package, you will need to go one level above it.

cd ..

Step 4: Download the timezone databases.

The package comes with a handy script to download the databases from IANA.

At the time of writing this article, the latest version of the database is 2019b, you might want to check the latest version before you run the script.

Find the latest version number here and run the script using the below command inside the package directory.

flutter pub run tool/get -s 2019b

Step 5: Add databases to your pubspec.yaml

assets:
- packages/timezone/data/2019b.tzf

Step 6: Initialize the library

The database needs to be loaded before using the library and the Github page of the package suggested to do so in the main.dart file but in my case, I created a TimeHelperService in my project so I did the setup in the constructor of the class.

import 'package:timezone/timezone.dart';
import 'package:flutter/services.dart';
class TimeHelperService {TimeHelperService() {
setup();
}
void setup() async {
var byteData = await rootBundle.load('packages/timezone/data/2019b.tzf');
initializeDatabase(byteData.buffer.asUint8List());
}
}

Step 7: Use the library!

void convertLocalToDetroit() async {DateTime indiaTime = DateTime.now(); //Emulator time is India time
final detroitTime =
new TZDateTime.from(indiaTime, getLocation('America/Detroit'));
print('Local India Time: ' + indiaTime.toString());
print('Detroit Time: ' + detroitTime.toString());
}

Output :

flutter: Local India Time: 2019–09–08 15:25:44.858303
flutter: Detroit Time: 2019–09–08 05:55:44.858303–0400

Hope this was helpful.

Do subscribe to my personal newsletter at uditc.tech :)

If this article helped you in any way, spare a second to give it a few claps 👏(Medium lets you clap up to 50 times for an article)

You can reach out to me on Twitter @thecoffeehog or if you like to be old school then at uditravichugh (at) gmail.com

--

--