Forked from j0nscalet/format_date_with_offset_test.dart
Last active
September 21, 2023 03:48
-
-
Save hectorddmx/5b239f96fc5f35faccfb25e973d6f121 to your computer and use it in GitHub Desktop.
Forked to manually support Z
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:test/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 | |
// This was modified to ommit the `:` so we actually have implemented the Z | |
// Timezone formatter in dart. | |
// | |
// 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("+0800")); | |
}); | |
test('double digit offset hours', () { | |
var brisbane = getLocation('Australia/Brisbane'); | |
var date = TZDateTime.now(brisbane); | |
var formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("+1000")); | |
}); | |
test('single digit offset hours', () { | |
final chicago = getLocation('America/Chicago'); | |
var date = TZDateTime.now(chicago); | |
var formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("-0500")); | |
}); | |
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("-0500")); | |
final detroit = getLocation('America/Detroit'); | |
date = TZDateTime.now(detroit); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("-0400")); | |
// Phoenix | |
final phoenix = getLocation('America/Phoenix'); | |
date = TZDateTime.now(phoenix); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("-0700")); | |
// Denver | |
final denver = getLocation('America/Denver'); | |
date = TZDateTime.now(denver); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("-0600")); | |
final la = getLocation('America/Los_Angeles'); | |
date = TZDateTime.now(la); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("-0700")); | |
final kolkata = getLocation('Asia/Kolkata'); | |
date = TZDateTime.now(kolkata); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("+0530")); | |
final brisbane = getLocation('Australia/Brisbane'); | |
date = TZDateTime.now(brisbane); | |
formattedDate = formatDateWithOffset(date); | |
print(formattedDate); | |
expect(formattedDate, contains("+1000")); | |
final mexicoCity = getLocation('America/Mexico_City'); | |
date = TZDateTime.now(mexicoCity); | |
formattedDate = formatDateWithOffset(date); | |
print("mexicoCity formattedDate $formattedDate"); | |
expect(formattedDate, contains("-0600")); | |
final honolulu = getLocation('Pacific/Honolulu'); | |
date = TZDateTime.now(honolulu); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("-1000")); | |
final zurich = getLocation('Europe/Zurich'); | |
date = TZDateTime.now(zurich); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("+0200")); | |
final london = getLocation('Europe/London'); | |
date = TZDateTime.now(london); | |
formattedDate = formatDateWithOffset(date); | |
expect(formattedDate, contains("+0100")); | |
}); | |
} | |
/// 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