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

Side by Side Diff: pkg/intl/lib/src/date_format_field.dart

Issue 24156005: Support "D" for ordinal date in Intl.DateFormat (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of intl; 5 part of intl;
6 6
7 /** 7 /**
8 * This is a private class internal to DateFormat which is used for formatting 8 * This is a private class internal to DateFormat which is used for formatting
9 * particular fields in a template. e.g. if the format is hh:mm:ss then the 9 * particular fields in a template. e.g. if the format is hh:mm:ss then the
10 * fields would be "hh", ":", "mm", ":", and "ss". Each type of field knows 10 * fields would be "hh", ":", "mm", ":", and "ss". Each type of field knows
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 /** 123 /**
124 * Parse a field representing part of a date pattern. Note that we do not 124 * Parse a field representing part of a date pattern. Note that we do not
125 * return a value, but rather build up the result in [builder]. 125 * return a value, but rather build up the result in [builder].
126 */ 126 */
127 void parseField(_Stream input, _DateBuilder builder) { 127 void parseField(_Stream input, _DateBuilder builder) {
128 try { 128 try {
129 switch(pattern[0]) { 129 switch(pattern[0]) {
130 case 'a': parseAmPm(input, builder); break; 130 case 'a': parseAmPm(input, builder); break;
131 case 'c': parseStandaloneDay(input); break; 131 case 'c': parseStandaloneDay(input); break;
132 case 'd': handleNumericField(input, builder.setDay); break; // day 132 case 'd': handleNumericField(input, builder.setDay); break; // day
133 // Day of year. Setting month=January with any day of the year works
134 case 'D': handleNumericField(input, builder.setDay); break; // dayofyear
133 case 'E': parseDayOfWeek(input); break; 135 case 'E': parseDayOfWeek(input); break;
134 case 'G': break; // era 136 case 'G': break; // era
135 case 'h': parse1To12Hours(input, builder); break; 137 case 'h': parse1To12Hours(input, builder); break;
136 case 'H': handleNumericField(input, builder.setHour); break; // hour 0-23 138 case 'H': handleNumericField(input, builder.setHour); break; // hour 0-23
137 case 'K': handleNumericField(input, builder.setHour); break; //hour 0-11 139 case 'K': handleNumericField(input, builder.setHour); break; //hour 0-11
138 case 'k': handleNumericField(input, builder.setHour,-1); break; //hr 1-24 140 case 'k': handleNumericField(input, builder.setHour,-1); break; //hr 1-24
139 case 'L': parseStandaloneMonth(input, builder); break; 141 case 'L': parseStandaloneMonth(input, builder); break;
140 case 'M': parseMonth(input, builder); break; 142 case 'M': parseMonth(input, builder); break;
141 case 'm': handleNumericField(input, builder.setMinute); break; // minutes 143 case 'm': handleNumericField(input, builder.setMinute); break; // minutes
142 case 'Q': break; // quarter 144 case 'Q': break; // quarter
143 case 'S': handleNumericField(input, builder.setFractionalSecond); break; 145 case 'S': handleNumericField(input, builder.setFractionalSecond); break;
144 case 's': handleNumericField(input, builder.setSecond); break; 146 case 's': handleNumericField(input, builder.setSecond); break;
145 case 'v': break; // time zone id 147 case 'v': break; // time zone id
146 case 'y': handleNumericField(input, builder.setYear); break; 148 case 'y': handleNumericField(input, builder.setYear); break;
147 case 'z': break; // time zone 149 case 'z': break; // time zone
148 case 'Z': break; // time zone RFC 150 case 'Z': break; // time zone RFC
149 default: return; 151 default: return;
150 } 152 }
151 } catch (e) { throwFormatException(input); } 153 } catch (e) { throwFormatException(input); }
152 } 154 }
153 155
154 /** Formatting logic if we are of type FIELD */ 156 /** Formatting logic if we are of type FIELD */
155 String formatField(DateTime date) { 157 String formatField(DateTime date) {
156 switch (pattern[0]) { 158 switch (pattern[0]) {
157 case 'a': return formatAmPm(date); 159 case 'a': return formatAmPm(date);
158 case 'c': return formatStandaloneDay(date); 160 case 'c': return formatStandaloneDay(date);
159 case 'd': return formatDayOfMonth(date); 161 case 'd': return formatDayOfMonth(date);
162 case 'D': return formatDayOfYear(date);
160 case 'E': return formatDayOfWeek(date); 163 case 'E': return formatDayOfWeek(date);
161 case 'G': return formatEra(date); 164 case 'G': return formatEra(date);
162 case 'h': return format1To12Hours(date); 165 case 'h': return format1To12Hours(date);
163 case 'H': return format0To23Hours(date); 166 case 'H': return format0To23Hours(date);
164 case 'K': return format0To11Hours(date); 167 case 'K': return format0To11Hours(date);
165 case 'k': return format24Hours(date); 168 case 'k': return format24Hours(date);
166 case 'L': return formatStandaloneMonth(date); 169 case 'L': return formatStandaloneMonth(date);
167 case 'M': return formatMonth(date); 170 case 'M': return formatMonth(date);
168 case 'm': return formatMinutes(date); 171 case 'm': return formatMinutes(date);
169 case 'Q': return formatQuarter(date); 172 case 'Q': return formatQuarter(date);
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 if (width < 4) { 352 if (width < 4) {
350 return symbols.SHORTQUARTERS[quarter]; 353 return symbols.SHORTQUARTERS[quarter];
351 } else { 354 } else {
352 return symbols.QUARTERS[quarter]; 355 return symbols.QUARTERS[quarter];
353 } 356 }
354 } 357 }
355 String formatDayOfMonth(DateTime date) { 358 String formatDayOfMonth(DateTime date) {
356 return padTo(width, date.day); 359 return padTo(width, date.day);
357 } 360 }
358 361
362 String formatDayOfYear(DateTime date) => padTo(width, dayNumberInYear(date));
363
364 /** Return the ordinal day, i.e. the day number in the year. */
365 int dayNumberInYear(DateTime date) {
366 if (date.month == 1) return date.day;
367 if (date.month == 2) return date.day + 31;
368 return ordinalDayFromMarchFirst(date) + 59 + (isLeapYear(date) ? 1 : 0);
369 }
370
371 /**
372 * Return the day of the year counting March 1st as 1, after which the
373 * number of days per month is constant, so it's easier to calculate.
374 * Formula from http://en.wikipedia.org/wiki/Ordinal_date
375 */
376 int ordinalDayFromMarchFirst(DateTime date) =>
377 ((30.6 * date.month) - 91.4).floor() + date.day;
378
379 /**
380 * Return true if this is a leap year. Rely on [DateTime] to do the
381 * underlying calculation, even though it doesn't expose the test to us. */
Emily Fortuna 2013/09/23 19:48:27 nit end the */ on its own line
Alan Knight 2013/09/25 17:17:58 Done.
382 bool isLeapYear(DateTime date) {
383 var feb29 = new DateTime(date.year, 2, 29);
384 return feb29.month == 2;
385 }
386
359 String formatDayOfWeek(DateTime date) { 387 String formatDayOfWeek(DateTime date) {
360 // Note that Dart's weekday returns 1 for Monday and 7 for Sunday. 388 // Note that Dart's weekday returns 1 for Monday and 7 for Sunday.
361 return (width >= 4 ? symbols.WEEKDAYS : 389 return (width >= 4 ? symbols.WEEKDAYS :
362 symbols.SHORTWEEKDAYS)[(date.weekday) % 7]; 390 symbols.SHORTWEEKDAYS)[(date.weekday) % 7];
363 } 391 }
364 392
365 void parseDayOfWeek(_Stream input) { 393 void parseDayOfWeek(_Stream input) {
366 // This is IGNORED, but we still have to skip over it the correct amount. 394 // This is IGNORED, but we still have to skip over it the correct amount.
367 var possibilities = width >= 4 ? symbols.WEEKDAYS : symbols.SHORTWEEKDAYS; 395 var possibilities = width >= 4 ? symbols.WEEKDAYS : symbols.SHORTWEEKDAYS;
368 parseEnumeratedString(input, possibilities); 396 parseEnumeratedString(input, possibilities);
(...skipping 28 matching lines...) Expand all
397 var basicString = toBePrinted.toString(); 425 var basicString = toBePrinted.toString();
398 if (basicString.length >= width) return basicString; 426 if (basicString.length >= width) return basicString;
399 var buffer = new StringBuffer(); 427 var buffer = new StringBuffer();
400 for (var i = 0; i < width - basicString.length; i++) { 428 for (var i = 0; i < width - basicString.length; i++) {
401 buffer.write('0'); 429 buffer.write('0');
402 } 430 }
403 buffer.write(basicString); 431 buffer.write(basicString);
404 return buffer.toString(); 432 return buffer.toString();
405 } 433 }
406 } 434 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698