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

Unified 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: Fixes from review 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/intl/lib/date_format.dart ('k') | pkg/intl/test/date_time_format_test_core.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..14990e69ee7343192f30264a4e3ef1a198cde1b7 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,32 @@ 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.
+ */
+ 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 :
« no previous file with comments | « pkg/intl/lib/date_format.dart ('k') | pkg/intl/test/date_time_format_test_core.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698