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

Side by Side Diff: sdk/lib/core/date.dart

Issue 11770004: Rename Date to DateTime. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 | « sdk/lib/_internal/dartdoc/lib/dartdoc.dart ('k') | sdk/lib/html/html_common/conversions.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 dart.core; 5 part of dart.core;
Lasse Reichstein Nielsen 2013/01/09 16:09:29 Rename file to datetime.dart
floitsch 2013/01/22 20:23:39 renamed to date_time.dart
6 6
7 /** 7 /**
8 * Date is the public interface to a point in time. 8 * Date is the public interface to a point in time.
9 * 9 *
10 * It can represent time values that are at a distance of at most 10 * It can represent time values that are at a distance of at most
11 * 8,640,000,000,000,000ms (100,000,000 days) from epoch (1970-01-01 UTC). In 11 * 8,640,000,000,000,000ms (100,000,000 days) from epoch (1970-01-01 UTC). In
12 * other words: [:millisecondsSinceEpoch.abs() <= 8640000000000000:]. 12 * other words: [:millisecondsSinceEpoch.abs() <= 8640000000000000:].
13 * 13 *
14 * Also see [Stopwatch] for means to measure time-spans. 14 * Also see [Stopwatch] for means to measure time-spans.
15 */ 15 */
16 abstract class Date implements Comparable { 16 abstract class DateTime implements Comparable {
Lasse Reichstein Nielsen 2013/01/09 16:09:29 Begin rant: I still think it's a bad name. It's no
17 // Weekday constants that are returned by [weekday] method: 17 // Weekday constants that are returned by [weekday] method:
18 static const int MON = 1; 18 static const int MON = 1;
19 static const int TUE = 2; 19 static const int TUE = 2;
20 static const int WED = 3; 20 static const int WED = 3;
21 static const int THU = 4; 21 static const int THU = 4;
22 static const int FRI = 5; 22 static const int FRI = 5;
23 static const int SAT = 6; 23 static const int SAT = 6;
24 static const int SUN = 7; 24 static const int SUN = 7;
25 static const int DAYS_IN_WEEK = 7; 25 static const int DAYS_IN_WEEK = 7;
26 26
27 // Month constants that are returned by the [month] getter. 27 // Month constants that are returned by the [month] getter.
28 static const int JAN = 1; 28 static const int JAN = 1;
29 static const int FEB = 2; 29 static const int FEB = 2;
30 static const int MAR = 3; 30 static const int MAR = 3;
31 static const int APR = 4; 31 static const int APR = 4;
32 static const int MAY = 5; 32 static const int MAY = 5;
33 static const int JUN = 6; 33 static const int JUN = 6;
34 static const int JUL = 7; 34 static const int JUL = 7;
35 static const int AUG = 8; 35 static const int AUG = 8;
36 static const int SEP = 9; 36 static const int SEP = 9;
37 static const int OCT = 10; 37 static const int OCT = 10;
38 static const int NOV = 11; 38 static const int NOV = 11;
39 static const int DEC = 12; 39 static const int DEC = 12;
40 40
41 /** 41 /**
42 * Constructs a [Date] instance based on the individual parts. The date is 42 * Constructs a [DateTime] instance based on the individual parts. The date is
43 * in the local time zone. 43 * in the local time zone.
44 * 44 *
45 * [month] and [day] are one-based. For example 45 * [month] and [day] are one-based. For example
46 * [:new Date(1938, 1, 10):] represents the 10th of January 1938. 46 * [:new DateTime(1938, 1, 10):] represents the 10th of January 1938.
47 */ 47 */
48 factory Date(int year, 48 factory DateTime(int year,
49 [int month = 1, 49 [int month = 1,
50 int day = 1, 50 int day = 1,
51 int hour = 0, 51 int hour = 0,
52 int minute = 0, 52 int minute = 0,
53 int second = 0, 53 int second = 0,
54 int millisecond = 0]) { 54 int millisecond = 0]) {
55 return new _DateImpl( 55 return new _DateImpl(
56 year, month, day, hour, minute, second, millisecond, false); 56 year, month, day, hour, minute, second, millisecond, false);
57 } 57 }
58 58
59 /** 59 /**
60 * Constructs a [Date] instance based on the individual parts. The date is 60 * Constructs a [DateTime] instance based on the individual parts. The date is
61 * in the UTC time zone. 61 * in the UTC time zone.
62 * 62 *
63 * [month] and [day] are one-based. For example 63 * [month] and [day] are one-based. For example
64 * [:new Date.utc(1938, 1, 10):] represents the 10th of January 1938 in 64 * [:new DateTime.utc(1938, 1, 10):] represents the 10th of January 1938 in
65 * Coordinated Universal Time. 65 * Coordinated Universal Time.
66 */ 66 */
67 factory Date.utc(int year, 67 factory DateTime.utc(int year,
68 [int month = 1, 68 [int month = 1,
69 int day = 1, 69 int day = 1,
70 int hour = 0, 70 int hour = 0,
71 int minute = 0, 71 int minute = 0,
72 int second = 0, 72 int second = 0,
73 int millisecond = 0]) { 73 int millisecond = 0]) {
74 return new _DateImpl( 74 return new _DateImpl(
75 year, month, day, hour, minute, second, millisecond, true); 75 year, month, day, hour, minute, second, millisecond, true);
76 } 76 }
77 77
78 /** 78 /**
79 * Constructs a new [Date] instance with current date time value in the 79 * Constructs a new [DateTime] instance with current date time value in the
80 * local time zone. 80 * local time zone.
81 */ 81 */
82 factory Date.now() => new _DateImpl.now(); 82 factory DateTime.now() => new _DateImpl.now();
83 83
84 /** 84 /**
85 * Constructs a new [Date] instance based on [formattedString]. 85 * Constructs a new [DateTime] instance based on [formattedString].
86 */ 86 */
87 factory Date.fromString(String formattedString) 87 factory DateTime.fromString(String formattedString)
88 => new _DateImpl.fromString(formattedString); 88 => new _DateImpl.fromString(formattedString);
89 89
90 /** 90 /**
91 * Constructs a new [Date] instance with the given [millisecondsSinceEpoch]. 91 * Constructs a new [DateTime] instance with the given [millisecondsSinceEpoch ].
92 * If [isUtc] is false then the date is in the local time zone. 92 * If [isUtc] is false then the date is in the local time zone.
93 * 93 *
94 * The constructed [Date] represents 94 * The constructed [DateTime] represents
95 * 1970-01-01T00:00:00Z + [millisecondsSinceEpoch]ms in the given 95 * 1970-01-01T00:00:00Z + [millisecondsSinceEpoch]ms in the given
96 * time zone (local or UTC). 96 * time zone (local or UTC).
97 */ 97 */
98 // TODO(floitsch): the spec allows default values in interfaces, but our 98 // TODO(floitsch): the spec allows default values in interfaces, but our
99 // tools don't yet. Eventually we want to have default values here. 99 // tools don't yet. Eventually we want to have default values here.
Lasse Reichstein Nielsen 2013/01/09 16:09:29 Comment out-dated? We have a default value in the
floitsch 2013/01/22 20:23:39 Done.
100 // TODO(lrn): Have two constructors instead of taking an optional bool. 100 // TODO(lrn): Have two constructors instead of taking an optional bool.
101 factory Date.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, 101 factory DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch,
102 {bool isUtc: false}) { 102 {bool isUtc: false}) {
103 return new _DateImpl.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 103 return new _DateImpl.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
104 isUtc); 104 isUtc);
105 } 105 }
106 106
107 /** 107 /**
108 * Returns true if [this] occurs at the same time as [other]. The 108 * Returns true if [this] occurs at the same time as [other]. The
109 * comparison is independent of whether the time is utc or in the local 109 * comparison is independent of whether the time is utc or in the local
110 * time zone. 110 * time zone.
111 */ 111 */
112 bool operator ==(Date other); 112 bool operator ==(DateTime other);
113 /** 113 /**
114 * Returns true if [this] occurs before [other]. The comparison is independent 114 * Returns true if [this] occurs before [other]. The comparison is independent
115 * of whether the time is utc or in the local time zone. 115 * of whether the time is utc or in the local time zone.
116 */ 116 */
117 bool operator <(Date other); 117 bool operator <(DateTime other);
118 /** 118 /**
119 * Returns true if [this] occurs at the same time or before [other]. The 119 * Returns true if [this] occurs at the same time or before [other]. The
120 * comparison is independent of whether the time is utc or in the local 120 * comparison is independent of whether the time is utc or in the local
121 * time zone. 121 * time zone.
122 */ 122 */
123 bool operator <=(Date other); 123 bool operator <=(DateTime other);
124 /** 124 /**
125 * Returns true if [this] occurs after [other]. The comparison is independent 125 * Returns true if [this] occurs after [other]. The comparison is independent
126 * of whether the time is utc or in the local time zone. 126 * of whether the time is utc or in the local time zone.
127 */ 127 */
128 bool operator >(Date other); 128 bool operator >(DateTime other);
129 /** 129 /**
130 * Returns true if [this] occurs at the same time or after [other]. The 130 * Returns true if [this] occurs at the same time or after [other]. The
131 * comparison is independent of whether the time is utc or in the local 131 * comparison is independent of whether the time is utc or in the local
132 * time zone. 132 * time zone.
133 */ 133 */
134 bool operator >=(Date other); 134 bool operator >=(DateTime other);
135 135
136 136
137 /** 137 /**
138 * Returns [this] in the local time zone. Returns itself if it is already in 138 * Returns [this] in the local time zone. Returns itself if it is already in
139 * the local time zone. Otherwise, this method is equivalent to 139 * the local time zone. Otherwise, this method is equivalent to
140 * [:new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 140 * [:new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
141 * isUtc: false):]. 141 * isUtc: false):].
142 */ 142 */
143 Date toLocal(); 143 DateTime toLocal();
144 144
145 /** 145 /**
146 * Returns [this] in UTC. Returns itself if it is already in UTC. Otherwise, 146 * Returns [this] in UTC. Returns itself if it is already in UTC. Otherwise,
147 * this method is equivalent to 147 * this method is equivalent to
148 * [:new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 148 * [:new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
149 * isUtc: true):]. 149 * isUtc: true):].
150 */ 150 */
151 Date toUtc(); 151 DateTime toUtc();
152 152
153 /** 153 /**
154 * Returns the abbreviated time-zone name. 154 * Returns the abbreviated time-zone name.
155 * 155 *
156 * Examples: [:"CET":] or [:"CEST":]. 156 * Examples: [:"CET":] or [:"CEST":].
157 */ 157 */
158 String get timeZoneName; 158 String get timeZoneName;
159 159
160 /** 160 /**
161 * The time-zone offset is the difference between local time and UTC. That is, 161 * The time-zone offset is the difference between local time and UTC. That is,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 210
211 /** 211 /**
212 * The milliseconds since 1970-01-01T00:00:00Z (UTC). This value is 212 * The milliseconds since 1970-01-01T00:00:00Z (UTC). This value is
213 * independent of the time zone. 213 * independent of the time zone.
214 * 214 *
215 * See [Stopwatch] for means to measure time-spans. 215 * See [Stopwatch] for means to measure time-spans.
216 */ 216 */
217 int get millisecondsSinceEpoch; 217 int get millisecondsSinceEpoch;
218 218
219 /** 219 /**
220 * True if this [Date] is set to UTC time. 220 * True if this [DateTime] is set to UTC time.
221 */ 221 */
222 bool get isUtc; 222 bool get isUtc;
223 223
224 /** 224 /**
225 * Returns a human readable string for this instance. 225 * Returns a human readable string for this instance.
226 * The returned string is constructed for the time zone of this instance. 226 * The returned string is constructed for the time zone of this instance.
227 */ 227 */
228 String toString(); 228 String toString();
229 229
230 /** 230 /**
231 * Returns a new [Date] with the [duration] added to this instance. 231 * Returns a new [DateTime] with the [duration] added to this instance.
232 */ 232 */
233 Date add(Duration duration); 233 DateTime add(Duration duration);
234 234
235 /** 235 /**
236 * Returns a new [Date] with the [duration] subtracted from this instance. 236 * Returns a new [DateTime] with the [duration] subtracted from this instance.
237 */ 237 */
238 Date subtract(Duration duration); 238 DateTime subtract(Duration duration);
239 239
240 /** 240 /**
241 * Returns a [Duration] with the difference of [:this:] and [other]. 241 * Returns a [Duration] with the difference of [:this:] and [other].
242 */ 242 */
243 Duration difference(Date other); 243 Duration difference(DateTime other);
244 } 244 }
245 245
246 class _DateImpl implements Date { 246 class _DateImpl implements DateTime {
247 final int millisecondsSinceEpoch; 247 final int millisecondsSinceEpoch;
248 final bool isUtc; 248 final bool isUtc;
249 249
250 factory _DateImpl.fromString(String formattedString) { 250 factory _DateImpl.fromString(String formattedString) {
251 // Read in (a subset of) ISO 8601. 251 // Read in (a subset of) ISO 8601.
252 // Examples: 252 // Examples:
253 // - "2012-02-27 13:27:00" 253 // - "2012-02-27 13:27:00"
254 // - "2012-02-27 13:27:00.423z" 254 // - "2012-02-27 13:27:00.423z"
255 // - "20120227 13:27:00" 255 // - "20120227 13:27:00"
256 // - "20120227T132700" 256 // - "20120227T132700"
(...skipping 30 matching lines...) Expand all
287 millisecond = 999; 287 millisecond = 999;
288 } 288 }
289 // TODO(floitsch): we should not need to test against the empty string. 289 // TODO(floitsch): we should not need to test against the empty string.
290 bool isUtc = (match[8] != null) && (match[8] != ""); 290 bool isUtc = (match[8] != null) && (match[8] != "");
291 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( 291 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
292 years, month, day, hour, minute, second, millisecond, isUtc); 292 years, month, day, hour, minute, second, millisecond, isUtc);
293 if (millisecondsSinceEpoch == null) { 293 if (millisecondsSinceEpoch == null) {
294 throw new ArgumentError(formattedString); 294 throw new ArgumentError(formattedString);
295 } 295 }
296 if (addOneMillisecond) millisecondsSinceEpoch++; 296 if (addOneMillisecond) millisecondsSinceEpoch++;
297 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 297 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
298 isUtc: isUtc); 298 isUtc: isUtc);
299 } else { 299 } else {
300 throw new ArgumentError(formattedString); 300 throw new ArgumentError(formattedString);
301 } 301 }
302 } 302 }
303 303
304 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; 304 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
305 305
306 _DateImpl.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, 306 _DateImpl.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch,
307 this.isUtc) { 307 this.isUtc) {
308 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { 308 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
309 throw new ArgumentError(millisecondsSinceEpoch); 309 throw new ArgumentError(millisecondsSinceEpoch);
310 } 310 }
311 if (isUtc == null) throw new ArgumentError(isUtc); 311 if (isUtc == null) throw new ArgumentError(isUtc);
312 } 312 }
313 313
314 bool operator ==(other) { 314 bool operator ==(other) {
315 if (!(other is Date)) return false; 315 if (!(other is DateTime)) return false;
316 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch); 316 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch);
317 } 317 }
318 318
319 bool operator <(Date other) 319 bool operator <(DateTime other)
320 => millisecondsSinceEpoch < other.millisecondsSinceEpoch; 320 => millisecondsSinceEpoch < other.millisecondsSinceEpoch;
321 321
322 bool operator <=(Date other) 322 bool operator <=(DateTime other)
323 => millisecondsSinceEpoch <= other.millisecondsSinceEpoch; 323 => millisecondsSinceEpoch <= other.millisecondsSinceEpoch;
324 324
325 bool operator >(Date other) 325 bool operator >(DateTime other)
326 => millisecondsSinceEpoch > other.millisecondsSinceEpoch; 326 => millisecondsSinceEpoch > other.millisecondsSinceEpoch;
327 327
328 bool operator >=(Date other) 328 bool operator >=(DateTime other)
329 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch; 329 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch;
330 330
331 int compareTo(Date other) 331 int compareTo(DateTime other)
332 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch); 332 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch);
333 333
334 int get hashCode => millisecondsSinceEpoch; 334 int get hashCode => millisecondsSinceEpoch;
335 335
336 Date toLocal() { 336 DateTime toLocal() {
337 if (isUtc) { 337 if (isUtc) {
338 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 338 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
339 isUtc: false); 339 isUtc: false);
340 } 340 }
341 return this; 341 return this;
342 } 342 }
343 343
344 Date toUtc() { 344 DateTime toUtc() {
345 if (isUtc) return this; 345 if (isUtc) return this;
346 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 346 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
347 isUtc: true); 347 isUtc: true);
348 } 348 }
349 349
350 String toString() { 350 String toString() {
351 String fourDigits(int n) { 351 String fourDigits(int n) {
352 int absN = n.abs(); 352 int absN = n.abs();
353 String sign = n < 0 ? "-" : ""; 353 String sign = n < 0 ? "-" : "";
354 if (absN >= 1000) return "$n"; 354 if (absN >= 1000) return "$n";
355 if (absN >= 100) return "${sign}0$absN"; 355 if (absN >= 100) return "${sign}0$absN";
356 if (absN >= 10) return "${sign}00$absN"; 356 if (absN >= 10) return "${sign}00$absN";
(...skipping 18 matching lines...) Expand all
375 String min = twoDigits(minute); 375 String min = twoDigits(minute);
376 String sec = twoDigits(second); 376 String sec = twoDigits(second);
377 String ms = threeDigits(millisecond); 377 String ms = threeDigits(millisecond);
378 if (isUtc) { 378 if (isUtc) {
379 return "$y-$m-$d $h:$min:$sec.${ms}Z"; 379 return "$y-$m-$d $h:$min:$sec.${ms}Z";
380 } else { 380 } else {
381 return "$y-$m-$d $h:$min:$sec.$ms"; 381 return "$y-$m-$d $h:$min:$sec.$ms";
382 } 382 }
383 } 383 }
384 384
385 /** Returns a new [Date] with the [duration] added to [this]. */ 385 /** Returns a new [DateTime] with the [duration] added to [this]. */
386 Date add(Duration duration) { 386 DateTime add(Duration duration) {
387 int ms = millisecondsSinceEpoch; 387 int ms = millisecondsSinceEpoch;
388 return new Date.fromMillisecondsSinceEpoch( 388 return new DateTime.fromMillisecondsSinceEpoch(
389 ms + duration.inMilliseconds, isUtc: isUtc); 389 ms + duration.inMilliseconds, isUtc: isUtc);
390 } 390 }
391 391
392 /** Returns a new [Date] with the [duration] subtracted from [this]. */ 392 /** Returns a new [DateTime] with the [duration] subtracted from [this]. */
393 Date subtract(Duration duration) { 393 DateTime subtract(Duration duration) {
394 int ms = millisecondsSinceEpoch; 394 int ms = millisecondsSinceEpoch;
395 return new Date.fromMillisecondsSinceEpoch( 395 return new DateTime.fromMillisecondsSinceEpoch(
396 ms - duration.inMilliseconds, isUtc: isUtc); 396 ms - duration.inMilliseconds, isUtc: isUtc);
397 } 397 }
398 398
399 /** Returns a [Duration] with the difference of [this] and [other]. */ 399 /** Returns a [Duration] with the difference of [this] and [other]. */
400 Duration difference(Date other) { 400 Duration difference(DateTime other) {
401 int ms = millisecondsSinceEpoch; 401 int ms = millisecondsSinceEpoch;
402 int otherMs = other.millisecondsSinceEpoch; 402 int otherMs = other.millisecondsSinceEpoch;
403 return new Duration(milliseconds: ms - otherMs); 403 return new Duration(milliseconds: ms - otherMs);
404 } 404 }
405 405
406 external _DateImpl(int year, 406 external _DateImpl(int year,
407 int month, 407 int month,
408 int day, 408 int day,
409 int hour, 409 int hour,
410 int minute, 410 int minute,
411 int second, 411 int second,
412 int millisecond, 412 int millisecond,
413 bool isUtc); 413 bool isUtc);
414 external _DateImpl.now(); 414 external _DateImpl.now();
415 external static int _brokenDownDateToMillisecondsSinceEpoch( 415 external static int _brokenDownDateToMillisecondsSinceEpoch(
416 int year, int month, int day, int hour, int minute, int second, 416 int year, int month, int day, int hour, int minute, int second,
417 int millisecond, bool isUtc); 417 int millisecond, bool isUtc);
418 external String get timeZoneName; 418 external String get timeZoneName;
419 external Duration get timeZoneOffset; 419 external Duration get timeZoneOffset;
420 external int get year; 420 external int get year;
421 external int get month; 421 external int get month;
422 external int get day; 422 external int get day;
423 external int get hour; 423 external int get hour;
424 external int get minute; 424 external int get minute;
425 external int get second; 425 external int get second;
426 external int get millisecond; 426 external int get millisecond;
427 external int get weekday; 427 external int get weekday;
428 } 428 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/dartdoc/lib/dartdoc.dart ('k') | sdk/lib/html/html_common/conversions.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698