Working with timezones in Flutter

Udit Chugh
Sep 12 · 3 min read

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.

Don’t forget to give this article a few claps, it will motivate me to write more such articles :)

Flutter Community

Articles and Stories from the Flutter Community

Udit Chugh

Written by

Tech, Products and Startups Enthusiast.

Flutter Community

Articles and Stories from the Flutter Community