Chromium Code Reviews| Index: pkg/intl/lib/src/date_format_field.dart |
| diff --git a/pkg/intl/lib/src/date_format_field.dart b/pkg/intl/lib/src/date_format_field.dart |
| index fb8274ce1095f8b1c92db7fd241892028b6a60dd..896dfcbd177e64d38f82af09beba7bd7af57985b 100644 |
| --- a/pkg/intl/lib/src/date_format_field.dart |
| +++ b/pkg/intl/lib/src/date_format_field.dart |
| @@ -130,6 +130,8 @@ class _DateFormatPatternField extends _DateFormatField { |
| case 'a': parseAmPm(input, builder); break; |
| case 'c': parseStandaloneDay(input); break; |
| case 'd': handleNumericField(input, builder.setDay); break; // day |
| + // Day of year. Setting month=January with any day of the year works |
| + case 'D': handleNumericField(input, builder.setDay); break; // dayofyear |
| case 'E': parseDayOfWeek(input); break; |
| case 'G': break; // era |
| case 'h': parse1To12Hours(input, builder); break; |
| @@ -157,6 +159,7 @@ class _DateFormatPatternField extends _DateFormatField { |
| case 'a': return formatAmPm(date); |
| case 'c': return formatStandaloneDay(date); |
| case 'd': return formatDayOfMonth(date); |
| + case 'D': return formatDayOfYear(date); |
| case 'E': return formatDayOfWeek(date); |
| case 'G': return formatEra(date); |
| case 'h': return format1To12Hours(date); |
| @@ -356,6 +359,31 @@ class _DateFormatPatternField extends _DateFormatField { |
| return padTo(width, date.day); |
| } |
| + String formatDayOfYear(DateTime date) => padTo(width, dayNumberInYear(date)); |
| + |
| + /** Return the ordinal day, i.e. the day number in the year. */ |
| + int dayNumberInYear(DateTime date) { |
| + if (date.month == 1) return date.day; |
| + if (date.month == 2) return date.day + 31; |
| + return ordinalDayFromMarchFirst(date) + 59 + (isLeapYear(date) ? 1 : 0); |
| + } |
| + |
| + /** |
| + * Return the day of the year counting March 1st as 1, after which the |
| + * number of days per month is constant, so it's easier to calculate. |
| + * Formula from http://en.wikipedia.org/wiki/Ordinal_date |
| + */ |
| + int ordinalDayFromMarchFirst(DateTime date) => |
| + ((30.6 * date.month) - 91.4).floor() + date.day; |
| + |
| + /** |
| + * Return true if this is a leap year. Rely on [DateTime] to do the |
| + * 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.
|
| + bool isLeapYear(DateTime date) { |
| + var feb29 = new DateTime(date.year, 2, 29); |
| + return feb29.month == 2; |
| + } |
| + |
| String formatDayOfWeek(DateTime date) { |
| // Note that Dart's weekday returns 1 for Monday and 7 for Sunday. |
| return (width >= 4 ? symbols.WEEKDAYS : |