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

Side by Side Diff: samples/swarm/swarm_ui_lib/util/DateUtils.dart

Issue 14065003: Rename DAYS_IN_WEEK to DAYS_PER_WEEK. Add MONTHS_PER_YEAR. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comment. Created 7 years, 8 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
« no previous file with comments | « runtime/lib/date_patch.dart ('k') | sdk/lib/core/date_time.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 utilslib; 5 part of utilslib;
6 6
7 /** 7 /**
8 * General purpose date/time utilities. 8 * General purpose date/time utilities.
9 */ 9 */
10 class DateUtils { 10 class DateUtils {
11 // TODO(jmesserly): localized strings 11 // TODO(jmesserly): localized strings
12 static const WEEKDAYS = const ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 12 static const WEEKDAYS = const ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
13 'Friday', 'Saturday', 'Sunday']; 13 'Friday', 'Saturday', 'Sunday'];
14 14
15 static const YESTERDAY = 'Yesterday'; 15 static const YESTERDAY = 'Yesterday';
16 16
17 static const MS_IN_WEEK = DateTime.DAYS_IN_WEEK * Duration.MILLISECONDS_PER_DA Y; 17 static const MS_IN_WEEK =
18 DateTime.DAYS_PER_WEEK * Duration.MILLISECONDS_PER_DAY;
18 19
19 // TODO(jmesserly): workaround for missing DateTime.fromDate in Dartium 20 // TODO(jmesserly): workaround for missing DateTime.fromDate in Dartium
20 // Remove this once that is implemented. See b/5055106 21 // Remove this once that is implemented. See b/5055106
21 // Parse a string like: "Mon, 27 Jun 2011 15:22:00 -0700" 22 // Parse a string like: "Mon, 27 Jun 2011 15:22:00 -0700"
22 static DateTime fromString(String text) { 23 static DateTime fromString(String text) {
23 final parts = text.split(' '); 24 final parts = text.split(' ');
24 if (parts.length == 1) { 25 if (parts.length == 1) {
25 return _parseIsoDate(text); 26 return _parseIsoDate(text);
26 } 27 }
27 28
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 } 149 }
149 } 150 }
150 151
151 // TODO(jmesserly): this is a workaround for unimplemented DateTime.weekday 152 // TODO(jmesserly): this is a workaround for unimplemented DateTime.weekday
152 // Code inspired by v8/src/date.js 153 // Code inspired by v8/src/date.js
153 static int getWeekday(DateTime dateTime) { 154 static int getWeekday(DateTime dateTime) {
154 final unixTimeStart = new DateTime(1970, 1, 1, 0, 0, 0, 0); 155 final unixTimeStart = new DateTime(1970, 1, 1, 0, 0, 0, 0);
155 int msSince1970 = dateTime.difference(unixTimeStart).inMilliseconds; 156 int msSince1970 = dateTime.difference(unixTimeStart).inMilliseconds;
156 int daysSince1970 = msSince1970 ~/ Duration.MILLISECONDS_PER_DAY; 157 int daysSince1970 = msSince1970 ~/ Duration.MILLISECONDS_PER_DAY;
157 // 1970-1-1 was Thursday 158 // 1970-1-1 was Thursday
158 return ((daysSince1970 + DateTime.THURSDAY) % DateTime.DAYS_IN_WEEK); 159 return ((daysSince1970 + DateTime.THURSDAY) % DateTime.DAYS_PER_WEEK);
159 } 160 }
160 161
161 /** Formats a time in H:MM A format */ 162 /** Formats a time in H:MM A format */
162 // TODO(jmesserly): should get 12 vs 24 hour clock setting from the locale 163 // TODO(jmesserly): should get 12 vs 24 hour clock setting from the locale
163 static String toHourMinutesString(Duration duration) { 164 static String toHourMinutesString(Duration duration) {
164 assert(duration.inDays == 0); 165 assert(duration.inDays == 0);
165 int hours = duration.inHours; 166 int hours = duration.inHours;
166 String a; 167 String a;
167 if (hours >= 12) { 168 if (hours >= 12) {
168 a = 'pm'; 169 a = 'pm';
169 if (hours != 12) { 170 if (hours != 12) {
170 hours -= 12; 171 hours -= 12;
171 } 172 }
172 } else { 173 } else {
173 a = 'am'; 174 a = 'am';
174 if (hours == 0) { 175 if (hours == 0) {
175 hours += 12; 176 hours += 12;
176 } 177 }
177 } 178 }
178 String twoDigits(int n) { 179 String twoDigits(int n) {
179 if (n >= 10) return "${n}"; 180 if (n >= 10) return "${n}";
180 return "0${n}"; 181 return "0${n}";
181 } 182 }
182 String mm = 183 String mm =
183 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR)); 184 twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR));
184 return "${hours}:${mm} ${a}"; 185 return "${hours}:${mm} ${a}";
185 } 186 }
186 } 187 }
OLDNEW
« no previous file with comments | « runtime/lib/date_patch.dart ('k') | sdk/lib/core/date_time.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698