dart-lang / i18n

A general mono-repo for Dart i18n and l10n packages.
BSD 3-Clause "New" or "Revised" License
58 stars 34 forks source link

Format hour using single "H" return two digits #811

Closed Yiling-J closed 3 months ago

Yiling-J commented 3 months ago

Describe the bug Format hour using single "H" return two digits. To Reproduce Add a minimal working example or, if not possible or available, any code which might help to reproduce the problem

import 'package:intl/intl.dart';

main() {
  final DateTime dt = DateTime(2011, 12, 12, 2);
  var formatter = DateFormat('H');
  var formatted = formatter.format(dt);
  print(formatted);

  formatter = DateFormat('yy-H');
  formatted = formatter.format(dt);
  print(formatted);

  formatter = DateFormat('h');
  formatted = formatter.format(dt);
  print(formatted);
  formatter = DateFormat('yy-h');
  formatted = formatter.format(dt);
  print(formatted);
}

output:

02
11-2
2
11-2

System info from https://dartpad.dev/

mosuem commented 3 months ago

The skeletons such as H are not meant to be of the same length as the output number, see the docs. You might want to look at https://pub.dev/documentation/convert/latest/convert/FixedDateTimeFormatter-class.html for fixed, locale-independent formatting.

Yiling-J commented 3 months ago

@mosuem Thanks for the reply. I'm developing a locale-aware Excel number formatter, for example [$-484]mmmm dd yyyy h:mm AM/PM -> Januar 01 2022 4:32 vorm.. and the single 'H' test case failed. I will comment out that one because it's likely an uncommon use case.

However the documentation is a little confusing because it shows H is number, and for number type, the count of pattern means the minimum number of digits. Even the example for H shows "0", not "00".