dart-lang / i18n

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

Add lowercase am/pm marker #188

Open abhishekamit opened 6 years ago

abhishekamit commented 6 years ago

It'd be helpful to have a character that will print "am" or "pm" instead of "AM" or "PM".

libc seems to support this with %p vs %P: http://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html.

Currently I'm doing DateFormat('h:mm a').format(eventTime).toLowerCase() to get this result, but that's not ideal.

SteveAlexander commented 6 years ago

I'm seeing the same thing.

I have a proposal for a syntax for this, however I realize that this would be a breaking change.

The proposal is to make the case of 'am/pm' follow the case of the DateFormat syntax used. So, 'a' would indicate 'am' or 'pm' and 'A' would indicate 'AM' or 'PM'. Right now, 'a' indicates 'AM' or 'PM', which is not so intuitive.

DateFormat('h:mm a').format(eventTime) format as "12:34 am" DateFormat('h:mm A').format(eventTime) format as "12:34 AM"

As of now, only the lower-case 'a' DateFormat('h:mm a').format(eventTime) works, and formats (in my experience, counterintuitively) as upper case 'AM' or 'PM'.

alan-knight commented 6 years ago

We're following the ICU patterns - http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax so I'd be reluctant to deviate from that. And they use 'A' already for something else. But also, it's hard to know how to interpret that in other locales. If you ask for upper, do you want e.g. DOP. and ODP. in Czech?

vallamost commented 4 years ago

What if you added an alias of 'aa' for lowercase, and AA for uppercase?

sajadj313 commented 3 years ago

Any update on this issue? Its still existing to date.

As a workaround, I replace all the "am/pm" with "AM/PM" and do the parsing but a dedicated time format for this would come in handy.

Thanks.

apparaopapi88 commented 3 years ago

We're following the ICU patterns - http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax so I'd be reluctant to deviate from that. And they use 'A' already for something else. But also, it's hard to know how to interpret that in other locales. If you ask for upper, do you want e.g. DOP. and ODP. in Czech?

How about format symbols for entire application? Like in Java? DateFormatSymbols.amPmStrings SimpleDateFormat.dateFormatSymbols

syntheticgoo commented 2 years ago

Note the new link for ICU docs: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax

mosuem commented 1 year ago

I agree that adding a character for lowercase/uppercase does not sound great - being able to override the am/pm strings for a given locale sounds like a better solution at first thought at least.

mosuem commented 1 year ago

Currently, this is possible but a bit contrived:

import 'package:intl/date_symbol_data_custom.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/date_symbols.dart';
import 'package:intl/date_time_patterns.dart';
import 'package:intl/intl.dart';

void main(List<String> arguments) {
  var symbols = dateTimeSymbolMap()["en_US"] as DateSymbols;
  var symbolsAsMap = symbols.serializeToMap();
  symbolsAsMap['AMPMS'] = ['am', 'pm'];
  var customSymbols = DateSymbols.deserializeFromMap(symbolsAsMap);
  var patterns = dateTimePatternMap()["en_US"];

  initializeDateFormattingCustom(
    locale: 'en_US',
    symbols: customSymbols,
    patterns: patterns,
  );

  print(DateFormat('h:mm a').format(DateTime.now()));
}

It may be worth investigating how to simplify this.

stefan-sherwood commented 1 year ago

DateFormat('h:mm a').format(eventTime)

DateFormat('h:mm a').format(eventTime).toLowerCase()

does what's being requested. It's not ideal but it works in cases where the format string doesn't include Month or Day of Week, such as the given example.