Skip to content

Instantly share code, notes, and snippets.

@j0nscalet
Created May 27, 2020 06:37
Show Gist options
  • Save j0nscalet/78a6e59d1e07b885e144e19675a04d3e to your computer and use it in GitHub Desktop.
Save j0nscalet/78a6e59d1e07b885e144e19675a04d3e to your computer and use it in GitHub Desktop.
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart';
import 'package:timezone/timezone.dart';
import 'package:timezone/data/latest.dart';
// A DateTime in dart contains the TimezoneOffset from UTC/GMT
// However formatting a Date using timezone patterns/symbols like "Z" or "Z"
// is not supported yet.
// See: https://github.com/dart-lang/intl/issues/19 for more details.
Future<void> main() async {
initializeTimeZones();
test('format date that includes offset', () {
var date = TZDateTime.now(getLocation('Asia/Taipei'));
var formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("+08:00"));
});
test('double digit offset hours', () {
var brisbane = getLocation('Australia/Brisbane');
var date = TZDateTime.now(brisbane);
var formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("+10:00"));
});
test('single digit offset hours', () {
final chicago = getLocation('America/Chicago');
var date = TZDateTime.now(chicago);
var formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("-05:00"));
});
test('30 minute offset', () {
final brokenHill = getLocation('Australia/Broken_Hill');
var date = TZDateTime.now(brokenHill);
var formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains(":30"));
});
test('across several different timezones', () {
final chicago = getLocation('America/Chicago');
var date = TZDateTime.now(chicago);
var formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("-05:00"));
final detroit = getLocation('America/Detroit');
date = TZDateTime.now(detroit);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("-04:00"));
// Phoenix
final phoenix = getLocation('America/Phoenix');
date = TZDateTime.now(phoenix);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("-07:00"));
// Denver
final denver = getLocation('America/Denver');
date = TZDateTime.now(denver);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("-06:00"));
final la = getLocation('America/Los_Angeles');
date = TZDateTime.now(la);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("-07:00"));
final kolkata = getLocation('Asia/Kolkata');
date = TZDateTime.now(kolkata);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("+05:30"));
final brisbane = getLocation('Australia/Brisbane');
date = TZDateTime.now(brisbane);
formattedDate = formatDateWithOffset(date);
print(formattedDate);
expect(formattedDate, contains("+10:00"));
final honolulu = getLocation('Pacific/Honolulu');
date = TZDateTime.now(honolulu);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("-10:00"));
final zurich = getLocation('Europe/Zurich');
date = TZDateTime.now(zurich);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("+02:00"));
final london = getLocation('Europe/London');
date = TZDateTime.now(london);
formattedDate = formatDateWithOffset(date);
expect(formattedDate, contains("+01:00"));
});
}
/// Formats Date and includes the time zone offset in iso8601 format
String formatDateWithOffset(DateTime date) {
String twoDigits(int n) => n >= 10 ? "$n" : "0$n";
var hours = twoDigits(date.timeZoneOffset.inHours.abs());
var minutes = twoDigits(date.timeZoneOffset.inMinutes.remainder(60));
var sign = date.timeZoneOffset.inHours > 0 ? "+" : "-";
var formattedDate = DateFormat("yyyy-MM-ddTHH:mm:ss").format(date);
return "$formattedDate$sign$hours:$minutes";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment