Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit fb1bcf4

Browse files
vicbtravis@travis-ci.org
authored andcommitted
feat(date): Use localized patterns for shorthand format
closes #1019
1 parent fef0da0 commit fb1bcf4

File tree

3 files changed

+58
-66
lines changed

3 files changed

+58
-66
lines changed

Diff for: lib/formatter/currency.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ part of angular.formatter_internal;
1515
*
1616
* {{ 1234 | currency }} // output is $1,234.00
1717
* {{ 1234 | currency:'CAD' }} // output is CAD1,234.00
18-
* {{ 1234 | currency:'CAD':false }} // output is 1,234.00CAD
18+
* {{ 1234 | currency:'CAD':false }} // output is 1,234.00CAD
1919
*
2020
*
2121
*/

Diff for: lib/formatter/date.dart

+45-30
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,45 @@ part of angular.formatter_internal;
33
/**
44
* Formats a date value to a string based on the requested format.
55
*
6-
* Usage:
6+
* # Usage
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:
12-
*
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
10+
* `format` may be specified explicitly, or by using one of the following predefined shorthand:
2311
*
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
2422
*
2523
* For more on explicit formatting of dates and date syntax, see the documentation for the
2624
* [DartFormat class](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/intl/intl.DateFormat).
2725
*
26+
* # Example
27+
*
28+
* '2014-05-22' | date:'fullDate' // "Thursday, May 22, 2014" for the en_US locale
29+
*
2830
*/
2931
@Formatter(name:'date')
3032
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',
33+
static final _PATTERNS = const <String, dynamic> {
34+
'medium': const [DateFormat.YEAR_ABBR_MONTH_DAY, DateFormat.HOUR_MINUTE_SECOND],
35+
'short': const [DateFormat.YEAR_NUM_MONTH_DAY, DateFormat.HOUR_MINUTE],
36+
'fullDate': DateFormat.YEAR_MONTH_WEEKDAY_DAY,
37+
'longDate': DateFormat.YEAR_MONTH_DAY,
38+
'mediumDate': DateFormat.YEAR_ABBR_MONTH_DAY,
39+
'shortDate': DateFormat.YEAR_NUM_MONTH_DAY,
40+
'mediumTime': DateFormat.HOUR_MINUTE_SECOND,
41+
'shortTime': DateFormat.HOUR_MINUTE,
4042
};
4143

44+
/// locale -> (format -> DateFormat)
4245
var _dfs = new Map<String, Map<String, DateFormat>>();
4346

4447
/**
@@ -55,14 +58,26 @@ class Date implements Function {
5558
if (date is String) date = DateTime.parse(date);
5659
if (date is num) date = new DateTime.fromMillisecondsSinceEpoch(date);
5760
if (date is! DateTime) return date;
58-
if (_MAP.containsKey(format)) format = _MAP[format];
5961
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;
62+
return _getDateFormat(verifiedLocale, format).format(date);
63+
}
64+
65+
DateFormat _getDateFormat(String locale, String format) {
66+
_dfs.putIfAbsent(locale, () => <String, DateFormat>{});
67+
68+
if (_dfs[locale][format] == null) {
69+
var pattern = _PATTERNS.containsKey(format) ? _PATTERNS[format] : format;
70+
if (pattern is !Iterable) pattern = [pattern];
71+
var df = new DateFormat();
72+
pattern.forEach((p) {
73+
df.addPattern(p);
74+
});
75+
if (format == "short" || format == "shortDate") {
76+
// "short" and "shortDate" formats use a 2-digit year
77+
df = new DateFormat(df.pattern.replaceAll(new RegExp('y+'), 'yy'));
78+
}
79+
_dfs[locale][format] = df;
6580
}
66-
return df.format(date);
81+
return _dfs[locale][format];
6782
}
6883
}

Diff for: test/formatter/date_spec.dart

+12-35
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,29 @@ void main() {
2626
});
2727

2828
it('should accept various format strings', () {
29-
expect(date(morning, "yy-MM-dd HH:mm:ss")).
30-
toEqual('10-09-03 07:05:08');
31-
32-
expect(date(morning, "yy-MM-dd HH:mm:ss.sss")).
33-
toEqual('10-09-03 07:05:08.008');
29+
expect(date(morning, "yy-MM-dd HH:mm:ss")).toEqual('10-09-03 07:05:08');
30+
expect(date(morning, "yy-MM-dd HH:mm:ss.sss")).toEqual('10-09-03 07:05:08.008');
3431
});
3532

3633
it('should accept default formats', () {
37-
38-
expect(date(noon, "medium")).
39-
toEqual('Sep 3, 2010 12:05:08 PM');
40-
41-
expect(date(noon, "short")).
42-
toEqual('9/3/10 12:05 PM');
43-
44-
expect(date(noon, "fullDate")).
45-
toEqual('Friday, September 3, 2010');
46-
47-
expect(date(noon, "longDate")).
48-
toEqual('September 3, 2010');
49-
50-
expect(date(noon, "mediumDate")).
51-
toEqual('Sep 3, 2010');
52-
53-
expect(date(noon, "shortDate")).
54-
toEqual('9/3/10');
55-
56-
expect(date(noon, "mediumTime")).
57-
toEqual('12:05:08 PM');
58-
59-
expect(date(noon, "shortTime")).
60-
toEqual('12:05 PM');
34+
expect(date(noon, "medium")).toEqual('Sep 3, 2010 12:05:08 PM');
35+
expect(date(noon, "short")).toEqual('9/3/10 12:05 PM');
36+
expect(date(noon, "fullDate")).toEqual('Friday, September 3, 2010');
37+
expect(date(noon, "longDate")).toEqual('September 3, 2010');
38+
expect(date(noon, "mediumDate")).toEqual('Sep 3, 2010');
39+
expect(date(noon, "shortDate")).toEqual('9/3/10');
40+
expect(date(noon, "mediumTime")).toEqual('12:05:08 PM');
41+
expect(date(noon, "shortTime")).toEqual('12:05 PM');
6142
});
6243

6344
it('should use cache without any error', () {
64-
6545
date(noon, "shortTime");
6646
date(noon, "shortTime");
6747
});
6848

69-
7049
it('should accept various locales', async(() {
71-
expect(Intl.withLocale('de', () => date(noon, "medium"))).
72-
toEqual('Sep 3, 2010 12:05:08 nachm.');
73-
expect(Intl.withLocale('fr', () => date(noon, "medium"))).
74-
toEqual('sept. 3, 2010 12:05:08 PM');
50+
expect(Intl.withLocale('de', () => date(noon, "medium"))).toEqual('3. Sep 2010 12:05:08');
51+
expect(Intl.withLocale('fr', () => date(noon, "medium"))).toEqual('3 sept. 2010 12:05:08');
7552
}));
7653
});
7754
}

0 commit comments

Comments
 (0)