Fix 1-24 hour format
See https://github.com/dart-lang/intl/issues/215
PiperOrigin-RevId: 253094704
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cfc4165..0fb2630 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,5 @@
## 0.16.0
+ * Fix 'k' formatting (1 to 24 hours) which incorrectly showed 0 to 23.
## 0.15.8
* Add return type to some internal methods to improve dart2js output.
diff --git a/lib/src/intl/date_format_field.dart b/lib/src/intl/date_format_field.dart
index 8a5d25e..8c250d2 100644
--- a/lib/src/intl/date_format_field.dart
+++ b/lib/src/intl/date_format_field.dart
@@ -467,7 +467,8 @@
}
String format24Hours(DateTime date) {
- return padTo(width, date.hour);
+ var hour = date.hour == 0 ? 24 : date.hour;
+ return padTo(width, hour);
}
String formatFractionalSeconds(DateTime date) {
diff --git a/test/date_time_format_test_core.dart b/test/date_time_format_test_core.dart
index e6f88f5..951ef42 100644
--- a/test/date_time_format_test_core.dart
+++ b/test/date_time_format_test_core.dart
@@ -470,4 +470,19 @@
// Reset the value.
DateFormat.useNativeDigitsByDefaultFor('ar', true);
});
+
+ // This just tests the basic logic, which was in error, not
+ // formatting in different locales. See #215.
+ test('hours', () {
+ var oneToTwentyFour = DateFormat('kk');
+ var oneToTwelve = DateFormat('hh');
+ var zeroToTwentyThree = DateFormat('KK');
+ var zeroToEleven = DateFormat('HH');
+ var late = DateTime(2019, 1, 2, 0, 4, 6);
+ expect(oneToTwentyFour.format(late), '24');
+ expect(zeroToTwentyThree.format(late), '00');
+ expect(oneToTwelve.format(late), '12');
+ expect(zeroToEleven.format(late), '00');
+
+ });
}