OLD | NEW |
1 part of angular.filter; | 1 part of angular.formatter_internal; |
2 | 2 |
3 /** | 3 /** |
4 * Formats date to a string based on the requested format. | 4 * Formats date to a string based on the requested format. |
5 * See Dart http://api.dartlang.org/docs/releases/latest/intl/DateFormat.html | 5 * See Dart http://api.dartlang.org/docs/releases/latest/intl/DateFormat.html |
6 * for full formating options. | 6 * for full formating options. |
7 * | 7 * |
8 * - `medium`: equivalent to `MMM d, y h:mm:ss a` for en_US locale (e.g. Sep 3,
2010 12:05:08 pm) | 8 * - `medium`: equivalent to `MMM d, y h:mm:ss a` for en_US locale (e.g. Sep 3,
2010 12:05:08 pm) |
9 * - `short`: equivalent to `M/d/yy h:mm a` for en_US locale (e.g. 9/3/10 12:05
pm) | 9 * - `short`: equivalent to `M/d/yy h:mm a` for en_US locale (e.g. 9/3/10 12:05
pm) |
10 * - `fullDate`: equivalent to `EEEE, MMMM d, y` for en_US locale (e.g. Friday,
September 3, 2010) | 10 * - `fullDate`: equivalent to `EEEE, MMMM d, y` for en_US locale (e.g. Friday,
September 3, 2010) |
11 * - `longDate`: equivalent to `MMMM d, y` for en_US locale (e.g. September 3, 2
010) | 11 * - `longDate`: equivalent to `MMMM d, y` for en_US locale (e.g. September 3, 2
010) |
12 * - `mediumDate`: equivalent to `MMM d, y` for en_US locale (e.g. Sep 3, 2010) | 12 * - `mediumDate`: equivalent to `MMM d, y` for en_US locale (e.g. Sep 3, 2010) |
13 * - `shortDate`: equivalent to `M/d/yy` for en_US locale (e.g. 9/3/10) | 13 * - `shortDate`: equivalent to `M/d/yy` for en_US locale (e.g. 9/3/10) |
14 * - `mediumTime`: equivalent to `h:mm:ss a` for en_US locale (e.g. 12:05:08 pm) | 14 * - `mediumTime`: equivalent to `h:mm:ss a` for en_US locale (e.g. 12:05:08 pm) |
15 * - `shortTime`: equivalent to `h:mm a` for en_US locale (e.g. 12:05 pm) | 15 * - `shortTime`: equivalent to `h:mm a` for en_US locale (e.g. 12:05 pm) |
16 * | 16 * |
17 * | 17 * |
18 * Usage: | 18 * Usage: |
19 * | 19 * |
20 * {{ date_expression | date[:format] }} | 20 * {{ date_expression | date[:format] }} |
21 * | 21 * |
22 */ | 22 */ |
23 @NgFilter(name:'date') | 23 @Formatter(name:'date') |
24 class DateFilter implements Function { | 24 class Date implements Function { |
25 static final _MAP = const <String, String> { | 25 static final _MAP = const <String, String> { |
26 'medium': 'MMM d, y h:mm:ss a', | 26 'medium': 'MMM d, y h:mm:ss a', |
27 'short': 'M/d/yy h:mm a', | 27 'short': 'M/d/yy h:mm a', |
28 'fullDate': 'EEEE, MMMM d, y', | 28 'fullDate': 'EEEE, MMMM d, y', |
29 'longDate': 'MMMM d, y', | 29 'longDate': 'MMMM d, y', |
30 'mediumDate': 'MMM d, y', | 30 'mediumDate': 'MMM d, y', |
31 'shortDate': 'M/d/yy', | 31 'shortDate': 'M/d/yy', |
32 'mediumTime': 'h:mm:ss a', | 32 'mediumTime': 'h:mm:ss a', |
33 'shortTime': 'h:mm a', | 33 'shortTime': 'h:mm a', |
34 }; | 34 }; |
35 | 35 |
36 var _dfs = <String, DateFormat>{}; | 36 var _dfs = new Map<String, Map<String, DateFormat>>(); |
37 | 37 |
38 /** | 38 /** |
39 * [date]: Date to format either as Date object, milliseconds | 39 * [date]: Date to format either as Date object, milliseconds |
40 * ([string] or [num]) or various ISO 8601 datetime string formats | 40 * ([string] or [num]) or various ISO 8601 datetime string formats |
41 * (e.g. `yyyy-MM-ddTHH:mm:ss.SSSZ` and its shorter versions like | 41 * (e.g. `yyyy-MM-ddTHH:mm:ss.SSSZ` and its shorter versions like |
42 * `yyyy-MM-ddTHH:mmZ`, `yyyy-MM-dd` or `yyyyMMddTHHmmssZ`). If no | 42 * `yyyy-MM-ddTHH:mmZ`, `yyyy-MM-dd` or `yyyyMMddTHHmmssZ`). If no |
43 * timezone is specified in the string input, the time is considered to | 43 * timezone is specified in the string input, the time is considered to |
44 * be in the local timezone. | 44 * be in the local timezone. |
45 * | 45 * |
46 * [format]: Formatting rules (see Description). If not specified, | 46 * [format]: Formatting rules (see Description). If not specified, |
47 * mediumDate is used | 47 * mediumDate is used |
48 * | 48 * |
49 */ | 49 */ |
50 dynamic call(Object date, [String format = 'mediumDate']) { | 50 dynamic call(Object date, [String format = 'mediumDate']) { |
51 if (date == '' || date == null) return date; | 51 if (date == '' || date == null) return date; |
52 if (date is String) date = DateTime.parse(date); | 52 if (date is String) date = DateTime.parse(date); |
53 if (date is num) date = new DateTime.fromMillisecondsSinceEpoch(date); | 53 if (date is num) date = new DateTime.fromMillisecondsSinceEpoch(date); |
54 if (date is! DateTime) return date; | 54 if (date is! DateTime) return date; |
55 var df = _dfs[format]; | 55 if (_MAP.containsKey(format)) format = _MAP[format]; |
| 56 var verifiedLocale = Intl.verifiedLocale(Intl.getCurrentLocale(), DateFormat
.localeExists); |
| 57 _dfs.putIfAbsent(verifiedLocale, () => new Map<String, DateFormat>()); |
| 58 var df = _dfs[verifiedLocale][format]; |
56 if (df == null) { | 59 if (df == null) { |
57 if (_MAP.containsKey(format)) { | |
58 format = _MAP[format]; | |
59 } | |
60 df = new DateFormat(format); | 60 df = new DateFormat(format); |
61 _dfs[format] = df; | 61 _dfs[verifiedLocale][format] = df; |
62 } | 62 } |
63 return df.format(date); | 63 return df.format(date); |
64 } | 64 } |
65 } | 65 } |
OLD | NEW |