Skip to content

Commit 4ca9ccb

Browse files
committed
feat(date): Use localized patterns for shorthand format
closes dart-archive#1019
1 parent 4dfd99b commit 4ca9ccb

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

lib/formatter/date.dart

+41-29
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@ part of angular.formatter_internal;
77
*
88
* {{ date_expression | date[:format] }}
99
*
10-
* Here `format` may be specified explicitly, or by using one of the following predefined
11-
* localizable names:
10+
* `format` may be specified explicitly, or by using one of the following predefined shorthand:
1211
*
13-
* FORMAT NAME AS DEFINED FOR en_US OUTPUT
14-
* ------------- ---------------------- ---------------------------
15-
* medium MMM d, y h:mm:ss a Sep 3, 2010 12:05:08 pm
16-
* short M/d/yy h:mm a 9/3/10 12:05 pm
17-
* fullDate EEEE, MMMM d, y Friday, September 3, 2010
18-
* longDate MMMM d, y September 3, 2010
19-
* mediumDate MMM d, y Sep 3, 2010
20-
* shortDate M/d/yy 9/3/10
21-
* mediumTime h:mm:ss a 12:05:08 pm
22-
* shortTime h:mm a 12:05 pm
12+
* FORMAT NAME OUTPUT for en_US
13+
* ------------- ---------------------------
14+
* medium Sep 3, 2010 12:05:08 pm
15+
* short 9/3/10 12:05 pm
16+
* fullDate Friday, September 3, 2010
17+
* longDate September 3, 2010
18+
* mediumDate Sep 3, 2010
19+
* shortDate 9/3/10
20+
* mediumTime 12:05:08 pm
21+
* shortTime 12:05 pm
2322
*
2423
*
2524
* For more on explicit formatting of dates and date syntax, see the documentation for the
26-
* [DartFormat class](http://api.dartlang.org/docs/releases/latest/intl/DateFormat.html).
25+
* [DartFormat class](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/intl/intl.DateFormat).
2726
*
2827
*/
2928
@Formatter(name:'date')
3029
class Date implements Function {
31-
static final _MAP = const <String, String> {
32-
'medium': 'MMM d, y h:mm:ss a',
33-
'short': 'M/d/yy h:mm a',
34-
'fullDate': 'EEEE, MMMM d, y',
35-
'longDate': 'MMMM d, y',
36-
'mediumDate': 'MMM d, y',
37-
'shortDate': 'M/d/yy',
38-
'mediumTime': 'h:mm:ss a',
39-
'shortTime': 'h:mm a',
30+
static final _PATTERNS = const <String, dynamic> {
31+
'medium': const [DateFormat.YEAR_ABBR_MONTH_DAY, DateFormat.HOUR_MINUTE_SECOND],
32+
'short': const [DateFormat.YEAR_NUM_MONTH_DAY, DateFormat.HOUR_MINUTE],
33+
'fullDate': DateFormat.YEAR_MONTH_WEEKDAY_DAY,
34+
'longDate': DateFormat.YEAR_MONTH_DAY,
35+
'mediumDate': DateFormat.YEAR_ABBR_MONTH_DAY,
36+
'shortDate': DateFormat.YEAR_NUM_MONTH_DAY,
37+
'mediumTime': DateFormat.HOUR_MINUTE_SECOND,
38+
'shortTime': DateFormat.HOUR_MINUTE,
4039
};
4140

41+
/// locale -> (format -> DateFormat)
4242
var _dfs = new Map<String, Map<String, DateFormat>>();
4343

4444
/**
@@ -55,14 +55,26 @@ class Date implements Function {
5555
if (date is String) date = DateTime.parse(date);
5656
if (date is num) date = new DateTime.fromMillisecondsSinceEpoch(date);
5757
if (date is! DateTime) return date;
58-
if (_MAP.containsKey(format)) format = _MAP[format];
5958
var verifiedLocale = Intl.verifiedLocale(Intl.getCurrentLocale(), DateFormat.localeExists);
60-
_dfs.putIfAbsent(verifiedLocale, () => new Map<String, DateFormat>());
61-
var df = _dfs[verifiedLocale][format];
62-
if (df == null) {
63-
df = new DateFormat(format);
64-
_dfs[verifiedLocale][format] = df;
59+
return _getDateFormat(verifiedLocale, format).format(date);
60+
}
61+
62+
DateFormat _getDateFormat(String locale, String format) {
63+
_dfs.putIfAbsent(locale, () => <String, DateFormat>{});
64+
65+
if (_dfs[locale][format] == null) {
66+
var pattern = _PATTERNS.containsKey(format) ? _PATTERNS[format] : format;
67+
if (pattern is !Iterable) pattern = [pattern];
68+
var df = new DateFormat();
69+
pattern.forEach((p) {
70+
df.addPattern(p);
71+
});
72+
if (format == "short" || format == "shortDate") {
73+
// "short" and "shortDate" formats use a 2-digit year
74+
df = new DateFormat(df.pattern.replaceAll(new RegExp('y+'), 'yy'));
75+
}
76+
_dfs[locale][format] = df;
6577
}
66-
return df.format(date);
78+
return _dfs[locale][format];
6779
}
6880
}

0 commit comments

Comments
 (0)