OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. All rights reserved. | 2 * Copyright 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style | 4 * Use of this source code is governed by a BSD-style |
5 * license that can be found in the LICENSE file or at | 5 * license that can be found in the LICENSE file or at |
6 * https://developers.google.com/open-source/licenses/bsd | 6 * https://developers.google.com/open-source/licenses/bsd |
7 */ | 7 */ |
8 part of charted.locale.format; | 8 part of charted.locale.format; |
9 | 9 |
10 typedef String TimeFormatFunction(DateTime date); | 10 typedef String TimeFormatFunction(DateTime date); |
11 | 11 |
12 //TODO(songrenchu): Document time format; Add test for time format. | 12 //TODO(songrenchu): Document time format; Add test for time format. |
13 | 13 |
14 class TimeFormat { | 14 class TimeFormat { |
15 String _template; | 15 String _template; |
16 String _locale; | 16 String _locale; |
17 DateFormat _dateFormat; | 17 DateFormat _dateFormat; |
18 | 18 |
19 TimeFormat([String template = null, String identifier = 'en_US']) { | 19 TimeFormat([String template = null, String identifier = 'en_US']) { |
20 _template = template; | 20 _template = template; |
21 _locale = identifier; | 21 _locale = identifier; |
22 if (_template != null) | 22 if (_template != null) _dateFormat = |
23 _dateFormat = new DateFormat(_wrapStrptime2ICU(_template), _locale); | 23 new DateFormat(_wrapStrptime2ICU(_template), _locale); |
24 } | 24 } |
25 | 25 |
26 TimeFormat _getInstance(String template) { | 26 TimeFormat _getInstance(String template) { |
27 return new TimeFormat(template, _locale); | 27 return new TimeFormat(template, _locale); |
28 } | 28 } |
29 | 29 |
30 String apply(DateTime date) { | 30 String apply(DateTime date) { |
31 assert(_dateFormat != null); | 31 assert(_dateFormat != null); |
32 return _dateFormat.format(date); | 32 return _dateFormat.format(date); |
33 } | 33 } |
34 | 34 |
35 String toString() => _template; | 35 String toString() => _template; |
36 | 36 |
37 DateTime parse(String string) { | 37 DateTime parse(String string) { |
38 assert(_dateFormat != null); | 38 assert(_dateFormat != null); |
39 return _dateFormat.parse(string); | 39 return _dateFormat.parse(string); |
40 } | 40 } |
41 | 41 |
42 TimeFormatFunction multi(List<List> formats) { | 42 TimeFormatFunction multi(List<List> formats) { |
43 var n = formats.length, | 43 var n = formats.length, i = -1; |
44 i = -1; | 44 while (++i < n) formats[i][0] = _getInstance(formats[i][0] as String); |
45 while (++i < n) | |
46 formats[i][0] = _getInstance(formats[i][0] as String); | |
47 return (var date) { | 45 return (var date) { |
48 if (date is num) { | 46 if (date is num) { |
49 date = new DateTime.fromMillisecondsSinceEpoch(date.toInt()); | 47 date = new DateTime.fromMillisecondsSinceEpoch(date.toInt()); |
50 } | 48 } |
51 var i = 0, | 49 var i = 0, f = formats[i]; |
52 f = formats[i]; | |
53 while (f.length < 2 || f[1](date) == false) { | 50 while (f.length < 2 || f[1](date) == false) { |
54 i++; | 51 i++; |
55 if (i < n) f = formats[i]; | 52 if (i < n) f = formats[i]; |
56 } | 53 } |
57 if (i == n) return null; | 54 if (i == n) return null; |
58 return f[0].apply(date); | 55 return f[0].apply(date); |
59 }; | 56 }; |
60 } | 57 } |
61 | 58 |
62 UTCTimeFormat utc([String specifier = null]) { | 59 UTCTimeFormat utc([String specifier = null]) { |
63 return new UTCTimeFormat(specifier == null ? | 60 return new UTCTimeFormat( |
64 _template : specifier, _locale); | 61 specifier == null ? _template : specifier, _locale); |
65 } | 62 } |
66 | 63 |
67 static UTCTimeFormat iso() { | 64 static UTCTimeFormat iso() { |
68 return new UTCTimeFormat("%Y-%m-%dT%H:%M:%S.%LZ"); | 65 return new UTCTimeFormat("%Y-%m-%dT%H:%M:%S.%LZ"); |
69 } | 66 } |
70 | 67 |
71 static Map timeFormatPads = {"-": "", "_": " ", "0": "0"}; | 68 static Map timeFormatPads = {"-": "", "_": " ", "0": "0"}; |
72 // TODO(songrenchu): Cannot fully be equivalent now. | 69 // TODO(songrenchu): Cannot fully be equivalent now. |
73 static Map timeFormatsTransform = { | 70 static Map timeFormatsTransform = { |
74 'a': 'EEE', | 71 'a': 'EEE', |
75 'A': 'EEEE', | 72 'A': 'EEEE', |
76 'b': 'MMM', | 73 'b': 'MMM', |
77 'B': 'MMMM', | 74 'B': 'MMMM', |
78 'c': 'EEE MMM d HH:mm:ss yyyy', | 75 'c': 'EEE MMM d HH:mm:ss yyyy', |
79 'd': 'dd', | 76 'd': 'dd', |
80 'e': 'd', // TODO(songrenchu): zero padding not supported | 77 'e': 'd', // TODO(songrenchu): zero padding not supported |
81 'H': 'HH', | 78 'H': 'HH', |
82 'I': 'hh', | 79 'I': 'hh', |
83 'j': 'DDD', | 80 'j': 'DDD', |
84 'm': 'MM', | 81 'm': 'MM', |
85 'M': 'mm', | 82 'M': 'mm', |
86 'L': 'SSS', | 83 'L': 'SSS', |
87 'p': 'a', | 84 'p': 'a', |
88 'S': 'ss', | 85 'S': 'ss', |
89 'U': 'ww', // TODO(songrenchu): ICU doesn't distinguish 'U' and 'W', | 86 'U': 'ww', // TODO(songrenchu): ICU doesn't distinguish 'U' and 'W', |
90 // and not supported by Dart: DateFormat | 87 // and not supported by Dart: DateFormat |
91 'w': 'ee', // TODO(songrenchu): e not supported by Dart: DateFormat | 88 'w': 'ee', // TODO(songrenchu): e not supported by Dart: DateFormat |
92 'W': 'ww', // TODO(songrenchu): ICU doesn't distinguish 'U' and 'W', | 89 'W': 'ww', // TODO(songrenchu): ICU doesn't distinguish 'U' and 'W', |
93 // and not supported by Dart: DateFormat | 90 // and not supported by Dart: DateFormat |
94 'x': 'MM/dd/yyyy', | 91 'x': 'MM/dd/yyyy', |
95 'X': 'HH:mm:ss', | 92 'X': 'HH:mm:ss', |
96 'y': 'yy', | 93 'y': 'yy', |
97 'Y': 'yyyy', | 94 'Y': 'yyyy', |
98 'Z': 'Z', | 95 'Z': 'Z', |
99 '%': '%' | 96 '%': '%' |
100 }; | 97 }; |
101 | 98 |
102 String _wrapStrptime2ICU(String template) { | 99 String _wrapStrptime2ICU(String template) { |
103 var string = [], | 100 var string = [], i = -1, j = 0, n = template.length, tempChar; |
104 i = -1, | |
105 j = 0, | |
106 n = template.length, | |
107 tempChar; | |
108 while (++i < n) { | 101 while (++i < n) { |
109 if (template[i] == '%') { | 102 if (template[i] == '%') { |
110 string.add(template.substring(j, i)); | 103 string.add(template.substring(j, i)); |
111 if ((timeFormatPads[tempChar = template[++i]]) != null) | 104 if ((timeFormatPads[tempChar = template[++i]]) != null) tempChar = |
112 tempChar = template[++i]; | 105 template[++i]; |
113 if (timeFormatsTransform[tempChar] != null) | 106 if (timeFormatsTransform[tempChar] != null) string |
114 string.add(timeFormatsTransform[tempChar]); | 107 .add(timeFormatsTransform[tempChar]); |
115 j = i + 1; | 108 j = i + 1; |
116 } | 109 } |
117 } | 110 } |
118 if (j < i) | 111 if (j < i) string.add("'" + template.substring(j, i) + "'"); |
119 string.add("'" + template.substring(j, i) + "'"); | |
120 return string.join(""); | 112 return string.join(""); |
121 } | 113 } |
122 } | 114 } |
123 | 115 |
124 class UTCTimeFormat extends TimeFormat { | 116 class UTCTimeFormat extends TimeFormat { |
125 UTCTimeFormat(String template, [String identifier = 'en_US']): | 117 UTCTimeFormat(String template, [String identifier = 'en_US']) |
126 super(template, identifier); | 118 : super(template, identifier); |
127 | 119 |
128 UTCTimeFormat _getInstance(String template) { | 120 UTCTimeFormat _getInstance(String template) { |
129 return new UTCTimeFormat(template, _locale); | 121 return new UTCTimeFormat(template, _locale); |
130 } | 122 } |
131 | 123 |
132 DateTime parse(String string) { | 124 DateTime parse(String string) { |
133 assert(_dateFormat != null); | 125 assert(_dateFormat != null); |
134 return _dateFormat.parseUTC(string); | 126 return _dateFormat.parseUTC(string); |
135 } | 127 } |
136 } | 128 } |
OLD | NEW |