Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: lib/i18n/date_time_format.dart

Issue 10505005: Polish Date formatting API. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /**
2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3 * for details. All rights reserved. Use of this source code is governed by a
4 * BSD-style license that can be found in the LICENSE file.
5 *
6 * DateTimeFormat is for formatting and parsing dates in a locale-sensitive
7 * manner.
8 * It allows the user to choose from a set of standard date time formats as well
9 * as specify a customized pattern under certain locales. Date elements that
10 * vary across locales include month name, weekname, field, order, etc.
11 * //TODO(efortuna): Customized pattern system -- suggested by i18n needs
12 * // feedback on appropriateness.
13 * We also allow the user to use any customized pattern to parse or format
14 * date-time strings under certain locales. Date elements that vary across
15 * locales include month name, weekname, field, order, etc.
16 *
17 * This library uses the ICU/JDK date/time pattern specification as described
18 * below.
19 *
20 * Time Format Syntax: To specify the time format use a time pattern string.
21 * In this pattern, following letters are reserved as pattern letters, which
22 * are defined in the following manner:
23 *
24 * Symbol Meaning Presentation Example
25 * ------ ------- ------------ -------
26 * G era designator (Text) AD
27 * y# year (Number) 1996
28 * M month in year (Text & Number) July & 07
29 * d day in month (Number) 10
30 * h hour in am/pm (1~12) (Number) 12
31 * H hour in day (0~23) (Number) 0
32 * m minute in hour (Number) 30
33 * s second in minute (Number) 55
34 * S fractional second (Number) 978
35 * E day of week (Text) Tuesday
36 * D day in year (Number) 189
37 * a am/pm marker (Text) PM
38 * k hour in day (1~24) (Number) 24
39 * K hour in am/pm (0~11) (Number) 0
40 * z time zone (Text) Pacific Standard Time
41 * Z time zone (RFC 822) (Number) -0800
42 * v time zone (generic) (Text) Pacific Time
43 * ' escape for text (Delimiter) 'Date='
44 * '' single quote (Literal) 'o''clock'
45 *
46 * Items marked with '#' work differently than in Java.
47 *
48 * The count of pattern letters determine the format.
49 * (Text): 4 or more pattern letters--use full form,
50 * less than 4--use short or abbreviated form if one exists.
51 * In parsing, we will always try long format, then short.
52 * (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")
53 *
54 * (Number): the minimum number of digits. Shorter numbers are zero-padded to
55 * this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled
56 * specially; that is, if the count of 'y' is 2, the Year will be truncated to
57 * 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".) Unlike other
58 * fields, fractional seconds are padded on the right with zero.
59 *
60 * (Text & Number): 3 or over, use text, otherwise use number.
61 *
62 * Any characters that not in the pattern will be treated as quoted text. For
63 * instance, characters like ':', '.', ' ', '#' and '@' will appear in the
64 * resulting time text even they are not embraced within single quotes. In our
65 * current pattern usage, we didn't use up all letters. But those unused
66 * letters are strongly discouraged to be used as quoted text without quote.
67 * That's because we may use other letter for pattern in future.
68 *
69 * Examples Using the US Locale:
70 *
71 * Format Pattern Result
72 * -------------- -------
73 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv"->1996.07.10 AD at 15:08:56 Pacific Time
74 * "EEE, MMM d, ''yy" ->Wed, July 10, '96
75 * "h:mm a" ->12:08 PM
76 * "hh 'o''clock' a, zzzz" ->12 o'clock PM, Pacific Daylight Time
77 * "K:mm a, vvv" ->0:00 PM, PT
78 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->01996.July.10 AD 12:08 PM
79 *
80 * When parsing a date string using the abbreviated year pattern ("yy"),
81 * DateTimeParse must interpret the abbreviated year relative to some
82 * century. It does this by adjusting dates to be within 80 years before and 20
83 * years after the time the parse function is called. For example, using a
84 * pattern of "MM/dd/yy" and a DateTimeParse instance created on Jan 1, 1997,
85 * the string "01/11/12" would be interpreted as Jan 11, 2012 while the string
86 * "05/04/64" would be interpreted as May 4, 1964. During parsing, only
87 * strings consisting of exactly two digits, as defined by {@link
88 * java.lang.Character#isDigit(char)}, will be parsed into the default
89 * century. Any other numeric string, such as a one digit string, a three or
90 * more digit string will be interpreted as its face value.
91 *
92 * If the year pattern does not have exactly two 'y' characters, the year is
93 * interpreted literally, regardless of the number of digits. So using the
94 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
95 *
96 * When numeric fields abut one another directly, with no intervening
97 * delimiter characters, they constitute a run of abutting numeric fields. Such
98 * runs are parsed specially. For example, the format "HHmmss" parses the input
99 * text "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and
100 * fails to parse "1234". In other words, the leftmost field of the run is
101 * flexible, while the others keep a fixed width. If the parse fails anywhere in
102 * the run, then the leftmost field is shortened by one character, and the
103 * entire run is parsed again. This is repeated until either the parse succeeds
104 * or the leftmost field is one character in length. If the parse still fails at
105 * that point, the parse of the run fails.
106 */
107
108 #library('DateTimeFormat');
109
110 class DateTimeFormat {
111
112 /** Definition of this object formats dates. */
113 var formatDefinition;
114
115 /**
116 * String indicating a language code with which the message is to be
117 * formatted (such as en-US).
118 */
119 String _locale;
120
121 /**
122 * Date/Time format "skeleton" patterns. Also specifiable by String, but
123 * written this way so that they can be discoverable via autocomplete.
124 */
125 static final String Hm = 'Hm'; // HH:mm
126 static final String Hms = 'Hms'; // HH:mm:ss
127 static final String M = 'M'; // L
128 static final String MEd = 'MEd'; // E, M/d
129 static final String MMM = 'MMM'; // LLL
130 static final String MMMEd = 'MMMEd'; // E, MMM d
131 static final String MMMMEd = 'MMMMEd'; // E, MMMM d
132 static final String MMMMd = 'MMMMd'; // MMMM d
133 static final String MMMd = 'MMMd'; // MMM d
134 static final String Md = 'Md'; // M/d
135 static final String d = 'd'; // d
136 static final String hm = 'hm'; // h:mm a
137 static final String ms = 'ms'; // mm:ss
138 static final String y = 'y'; // yyyy
139 static final String yM = 'yM'; // M/yyyy
140 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy
141 static final String yMMM = 'yMMM'; // MMM yyyy
142 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy
143 static final String yMMMM = 'yMMMM'; // MMMM yyyy
144 static final String yQ = 'yQ'; // Q yyyy
145 static final String yQQQ = 'yQQQ'; // QQQ yyyy
146
147 /** Date/Time format patterns. */
148 // TODO(alanknight): There's a style question of whether to use fullDate or
149 // FULL_DATE naming conventions.
150 static final int _fullDate = 0;
151 static final int _longDate = 1;
152 static final int _mediumDate = 2;
153 static final int _shortDate = 3;
154 static final int _fullTime = 4;
155 static final int _longTime = 5;
156 static final int _mediumTime = 6;
157 static final int _shortTime = 7;
158 static final int _fullDateTime = 8;
159 static final int _longDateTime = 9;
160 static final int _mediumDateTime = 10;
161 static final int _shortDateTime = 11;
162
163 /**
164 * Named constructors for each of the above values.
165 * These could probably be made shorter if we just set the format to the
166 * constant and the parsing was lazy.
167 */
168 DateTimeFormat.fullDate() : this.formatDefinition = _fullDate;
169 DateTimeFormat.longDate() : this.formatDefinition = _longDate;
170 DateTimeFormat.mediumDate() : this.formatDefinition = _mediumDate;
171 DateTimeFormat.shortDate() : this.formatDefinition = _shortDate;
172 DateTimeFormat.fullTime() : this.formatDefinition = _fullTime;
173 DateTimeFormat.longTime() : this.formatDefinition = _longTime;
174 DateTimeFormat.mediumTime() : this.formatDefinition = _mediumTime;
175 DateTimeFormat.shortTime() : this.formatDefinition = _shortTime;
176 DateTimeFormat.fullDateTime() : this.formatDefinition = _fullDateTime;
177 DateTimeFormat.longDateTime() : this.formatDefinition = _longDateTime;
178 DateTimeFormat.mediumDateTime() : this.formatDefinition = _mediumDateTime;
179 DateTimeFormat.shortDateTime() : this.formatDefinition = _shortDateTime;
180
181 /**
182 * Constructor accepts a [formatDefinition], which can be a String, one of the
183 * predefined static forms, or a custom date format using the syntax described
184 * above. An optional [_locale] can be provided for specifics of the language
185 * locale to be used, otherwise, we will attempt to infer it (acceptable if
186 * Dart is running on the client, we can infer from the browser).
187 */
188 DateTimeFormat(this.formatDefinition, [this._locale]);
189
190 /**
191 * Given user input, attempt to parse the [inputString] into the anticipated
192 * format.
193 */
194 String parse(String inputString) {
195 return inputString;
196 }
197
198 /**
199 * Format the given [date] object according to preset pattern and current
200 * locale and return a formated string for the given date.
201 */
202 String format(Date date, [TimeZone timeZone]) {
203 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated...
204 return date.toString();
205 }
206 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698