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