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

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

Issue 23007017: Merge branch 'master' into datetimedocs (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Integrated KWs most excellent comments. Created 7 years, 4 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 | « no previous file | no next file » | 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;
6 6
7 /** 7 /**
8 * A DateTime object represents a point in time. 8 * An instant in time, such as July 20, 1969, 8:18pm.
sethladd 2013/08/20 19:32:49 I'm left wondering about timezone here.
mem 2013/08/21 20:08:55 Done.
9 * 9 *
10 * It can represent time values that are at a distance of at most 10 * Create DateTime objects by using one of the constructors
11 * 8,640,000,000,000,000ms (100,000,000 days) from epoch (1970-01-01 UTC). In 11 * or by parsing a correctly formatted string.
sethladd 2013/08/20 19:32:49 where is the definition of a correctly formatted s
mem 2013/08/21 20:08:55 Done.
12 * other words: [:millisecondsSinceEpoch.abs() <= 8640000000000000:]. 12 * Note that hours are specified between 0 and 23,
13 * as in a 24-hour clock.
14 * For example:
13 * 15 *
14 * Also see [Stopwatch] for means to measure time-spans. 16 * DateTime now = new DateTime.now();
17 * DateTime berlinWallFell = new DateTime(1989, 11, 9);
18 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); // 8:18pm
19 *
20 * Once created, a DateTime object cannot be changed; it has no setters.
sethladd 2013/08/20 19:32:49 I don't think we need "it has no setters"
mem 2013/08/21 20:08:55 Done.
21 *
22 * You can use properties to get
23 * the individual units of a DateTime object.
24 *
25 * assert(berlinWallFell.month == 11);
26 * assert(moonLanding.hour == 20);
27 *
28 * For convenience and readability,
29 * the DateTime class provides a constant for each day and month
30 * name&mdash;for example, [AUGUST] and [FRIDAY].
31 * You can use these constants to improve code readibility:
32 *
33 * DateTime berlinWallFell = new DateTime(1989, DateTime.NOVEMBER, 9);
34 * assert(berlinWallFell.month == DateTime.SATURDAY);
35 *
36 * Day and month values begin at 1, and the week starts on Monday.
37 * That is, the constants [JANUARY] and [MONDAY] are both 1.
38 *
39 * ## Working with time zones
40 *
41 * The DateTime class provides support for handling time zones.
sethladd 2013/08/20 19:32:49 I think this is too generous. This only creates da
mem 2013/08/21 20:08:55 Done.
42 * One constructor allows you to create a DateTime object specified
43 * in the UTC (Universal Time Coordinated) time zone.
44 * UTC is based on the 0° longitude meridian,
45 * also known as the Greenwich meridian.
46 *
47 * DateTime dDay = new DateTime.utc(1944, 6, 6);
48 *
49 * Use the methods [toLocal] and [toUtc]
sethladd 2013/08/20 19:32:49 I'm left wondering how DateTime knows my local tim
mem 2013/08/21 20:08:55 Done.
50 * to convert between UTC and local time.
51 * [isUtc] indicates whether a DateTime object is based in UTC.
52 * To discover the name of the time zone use [timeZoneName].
sethladd 2013/08/20 19:32:49 can we be more specific? "the time zone used when
mem 2013/08/21 20:08:55 Done.
53 * To find the difference
54 * between UTC and the time zone of a DateTime object
55 * call [timeZoneOffset].
56 *
57 * ## Comparing DateTime objects
58 *
59 * The DateTime class contains several handy methods,
60 * such as [isAfter], [isBefore], and [isAtSameMomentAs],
61 * for comparing DateTime objects.
62 *
63 * assert(berlinWallFell.isAfter(moonLanding) == true);
64 * assert(berlinWallFell.isBefore(moonLanding) == false);
65 *
66 * ## Using DateTime with Duration
67 *
68 * Use the [add] and [subtract] methods with a [Duration] object
69 * to create a new DateTime object based on another.
70 * For example, to find the date that is sixty days after today, write:
71 *
72 * DateTime today = new DateTime.now();
73 * DateTime sixtyDaysFromNow = today.add(new Duration(days: 60));
74 *
75 * To find out how much time is between two DateTime objects use
76 * [difference], which returns a [Duration] object:
77 *
78 * Duration difference = berlinWallFell.difference(dDay);
79 * assert(difference.inDays == 16592);
80 *
81 * ## Other resources
82 *
83 * See [Duration] to represent a span of time.
84 * See [Stopwatch] to measure timespans.
85 *
86 * The DateTime class does not provide internationalization.
87 * To internationalize your code, use
88 * the [intl](http://pub.dartlang.org/packages/intl) package.
sethladd 2013/08/20 19:32:49 nice link!
mem 2013/08/21 20:08:55 Done.
89 *
15 */ 90 */
16 class DateTime implements Comparable { 91 class DateTime implements Comparable {
17 // Weekday constants that are returned by [weekday] method: 92 // Weekday constants that are returned by [weekday] method:
18 static const int MONDAY = 1; 93 static const int MONDAY = 1;
19 static const int TUESDAY = 2; 94 static const int TUESDAY = 2;
20 static const int WEDNESDAY = 3; 95 static const int WEDNESDAY = 3;
21 static const int THURSDAY = 4; 96 static const int THURSDAY = 4;
22 static const int FRIDAY = 5; 97 static const int FRIDAY = 5;
23 static const int SATURDAY = 6; 98 static const int SATURDAY = 6;
24 static const int SUNDAY = 7; 99 static const int SUNDAY = 7;
25 static const int DAYS_PER_WEEK = 7; 100 static const int DAYS_PER_WEEK = 7;
26 101
27 // Month constants that are returned by the [month] getter. 102 // Month constants that are returned by the [month] getter.
28 static const int JANUARY = 1; 103 static const int JANUARY = 1;
29 static const int FEBRUARY = 2; 104 static const int FEBRUARY = 2;
30 static const int MARCH = 3; 105 static const int MARCH = 3;
31 static const int APRIL = 4; 106 static const int APRIL = 4;
32 static const int MAY = 5; 107 static const int MAY = 5;
33 static const int JUNE = 6; 108 static const int JUNE = 6;
34 static const int JULY = 7; 109 static const int JULY = 7;
35 static const int AUGUST = 8; 110 static const int AUGUST = 8;
36 static const int SEPTEMBER = 9; 111 static const int SEPTEMBER = 9;
37 static const int OCTOBER = 10; 112 static const int OCTOBER = 10;
38 static const int NOVEMBER = 11; 113 static const int NOVEMBER = 11;
39 static const int DECEMBER = 12; 114 static const int DECEMBER = 12;
40 static const int MONTHS_PER_YEAR = 12; 115 static const int MONTHS_PER_YEAR = 12;
41 116
42 /** 117 /**
43 * The milliseconds since 1970-01-01T00:00:00Z (UTC). This value is 118 * Returns the number of milliseconds since
Kathy Walrath 2013/08/20 19:58:23 Hmmm... This brings up the whole property vs. meth
mem 2013/08/21 20:08:55 The guidelines DO say that vars, getters should be
44 * independent of the time zone. 119 * the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
45 * 120 *
46 * See [Stopwatch] for means to measure time-spans. 121 * This value is independent of the time zone.
122 *
123 * This value is at most
124 * 8,640,000,000,000,000ms (100,000,000 days) from the Unix epoch.
125 * In other words: [:millisecondsSinceEpoch.abs() <= 8640000000000000:].
126 *
47 */ 127 */
48 final int millisecondsSinceEpoch; 128 final int millisecondsSinceEpoch;
49 129
50 /** 130 /**
51 * True if this [DateTime] is set to UTC time. 131 * Returns true if this [DateTime] is set to UTC time.
Kathy Walrath 2013/08/20 19:58:23 -> True...?
mem 2013/08/21 20:08:55 Done.
132 *
133 * DateTime dDay = new DateTime.utc(1944, 6, 6);
134 * assert(dDay.isUtc);
135 *
52 */ 136 */
53 final bool isUtc; 137 final bool isUtc;
54 138
55 /** 139 /**
56 * Constructs a [DateTime] instance based on the individual parts. The date is 140 * Constructs a [DateTime] instance based on the individual units.
Kathy Walrath 2013/08/20 19:58:23 Maybe combine the first 2 sentences into 1: Const
mem 2013/08/21 20:08:55 Done.
57 * in the local time zone.
58 * 141 *
59 * [month] and [day] are one-based. For example 142 * The date is in the local time zone.
60 * [:new DateTime(1938, 1, 10):] represents the 10th of January 1938. 143 * For example,
144 * to create a new DateTime object representing April 29, 2014, 6:04am:
145 *
146 * DateTime annularEclipse = new DateTime(2014, DateTime.APRIL, 29, 6, 4);
61 */ 147 */
62 // TODO(8042): This should be a redirecting constructor and not a factory. 148 // TODO(8042): This should be a redirecting constructor and not a factory.
63 factory DateTime(int year, 149 factory DateTime(int year,
64 [int month = 1, 150 [int month = 1,
65 int day = 1, 151 int day = 1,
66 int hour = 0, 152 int hour = 0,
67 int minute = 0, 153 int minute = 0,
68 int second = 0, 154 int second = 0,
69 int millisecond = 0]) { 155 int millisecond = 0]) {
70 return new DateTime._internal( 156 return new DateTime._internal(
71 year, month, day, hour, minute, second, millisecond, false); 157 year, month, day, hour, minute, second, millisecond, false);
72 } 158 }
73 159
74 /** 160 /**
75 * Constructs a [DateTime] instance based on the individual parts. The date is 161 * Constructs a [DateTime] instance based on the individual units.
76 * in the UTC time zone.
77 * 162 *
78 * [month] and [day] are one-based. For example 163 * The date is in the UTC time zone.
Kathy Walrath 2013/08/20 19:58:23 Maybe just combine the first 2 sentences into one:
mem 2013/08/21 20:08:55 Done.
79 * [:new DateTime.utc(1938, 1, 10):] represents the 10th of January 1938 in 164 *
80 * Coordinated Universal Time. 165 * DateTime dDay = new DateTime.utc(1944, DateTime.JUNE, 6);
81 */ 166 */
82 // TODO(8042): This should be a redirecting constructor and not a factory. 167 // TODO(8042): This should be a redirecting constructor and not a factory.
83 factory DateTime.utc(int year, 168 factory DateTime.utc(int year,
84 [int month = 1, 169 [int month = 1,
85 int day = 1, 170 int day = 1,
86 int hour = 0, 171 int hour = 0,
87 int minute = 0, 172 int minute = 0,
88 int second = 0, 173 int second = 0,
89 int millisecond = 0]) { 174 int millisecond = 0]) {
90 return new DateTime._internal( 175 return new DateTime._internal(
91 year, month, day, hour, minute, second, millisecond, true); 176 year, month, day, hour, minute, second, millisecond, true);
92 } 177 }
93 178
94 /** 179 /**
95 * Constructs a new [DateTime] instance with current date time value in the 180 * Constructs a [DateTime] instance with current date and time in the
96 * local time zone. 181 * local time zone.
182 *
183 * DateTime thisInstant = new DateTime.now();
184 *
97 */ 185 */
98 // TODO(8042): This should be a redirecting constructor and not a factory. 186 // TODO(8042): This should be a redirecting constructor and not a factory.
99 factory DateTime.now() { return new DateTime._now(); } 187 factory DateTime.now() { return new DateTime._now(); }
100 188
101 /** 189 /**
102 * Constructs a new [DateTime] instance based on [formattedString]. 190 * Constructs a new [DateTime] instance based on [formattedString].
103 * 191 *
104 * The function parses a subset of ISO 8601. Examples of accepted strings: 192 * The function parses a subset of ISO 8601. Examples of accepted strings:
105 * 193 *
106 * * `"2012-02-27 13:27:00"` 194 * * `"2012-02-27 13:27:00"`
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 240 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
153 isUtc: isUtc); 241 isUtc: isUtc);
154 } else { 242 } else {
155 throw new ArgumentError(formattedString); 243 throw new ArgumentError(formattedString);
156 } 244 }
157 } 245 }
158 246
159 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; 247 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
160 248
161 /** 249 /**
162 * Constructs a new [DateTime] instance with the given [millisecondsSinceEpoch ]. 250 * Constructs a new [DateTime] instance
251 * with the given [millisecondsSinceEpoch].
252 *
163 * If [isUtc] is false then the date is in the local time zone. 253 * If [isUtc] is false then the date is in the local time zone.
164 * 254 *
165 * The constructed [DateTime] represents 255 * The constructed [DateTime] represents
166 * 1970-01-01T00:00:00Z + [millisecondsSinceEpoch]ms in the given 256 * 1970-01-01T00:00:00Z + [millisecondsSinceEpoch] ms in the given
167 * time zone (local or UTC). 257 * time zone (local or UTC).
168 */ 258 */
169 // TODO(lrn): Have two constructors instead of taking an optional bool. 259 // TODO(lrn): Have two constructors instead of taking an optional bool.
170 DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, 260 DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch,
171 {bool isUtc: false}) 261 {bool isUtc: false})
172 : this.millisecondsSinceEpoch = millisecondsSinceEpoch, 262 : this.millisecondsSinceEpoch = millisecondsSinceEpoch,
173 this.isUtc = isUtc { 263 this.isUtc = isUtc {
174 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { 264 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
175 throw new ArgumentError(millisecondsSinceEpoch); 265 throw new ArgumentError(millisecondsSinceEpoch);
176 } 266 }
177 if (isUtc == null) throw new ArgumentError(isUtc); 267 if (isUtc == null) throw new ArgumentError(isUtc);
178 } 268 }
179 269
180 /** 270 /**
181 * Returns true if [other] is a [DateTime] at the same moment and in the 271 * Returns true if [other] is a [DateTime] at the same moment and in the
182 * same timezone (UTC or local). 272 * same time zone (UTC or local).
183 * 273 *
184 * See [isAtSameMomentAs] for a comparison that ignores the timezone. 274 * DateTime dDayUtc = new DateTime.utc(1944, DateTime.JUNE, 6);
275 * DateTime dDayLocal = new DateTime(1944, DateTime.JUNE, 6);
276 *
277 * assert(dDayUtc.isAtSameMomentAs(dDayLocal) == false);
278 *
279 * See [isAtSameMomentAs] for a comparison that adjusts for time zone.
185 */ 280 */
186 bool operator ==(other) { 281 bool operator ==(other) {
187 if (!(other is DateTime)) return false; 282 if (!(other is DateTime)) return false;
188 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch && 283 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch &&
189 isUtc == other.isUtc); 284 isUtc == other.isUtc);
190 } 285 }
191 286
192 /** 287 /**
193 * Returns true if [this] occurs before [other]. The comparison is independent 288 * Returns true if [this] occurs before [other].
289 *
290 * The comparison is independent
194 * of whether the time is in UTC or in the local time zone. 291 * of whether the time is in UTC or in the local time zone.
292 *
293 * DateTime berlinWallFell = new DateTime(1989, 11, 9);
294 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
295 *
296 * assert(berlinWallFell.isBefore(moonLanding) == false);
297 *
195 */ 298 */
196 bool isBefore(DateTime other) { 299 bool isBefore(DateTime other) {
197 return millisecondsSinceEpoch < other.millisecondsSinceEpoch; 300 return millisecondsSinceEpoch < other.millisecondsSinceEpoch;
198 } 301 }
199 302
200 /** 303 /**
201 * Returns true if [this] occurs after [other]. The comparison is independent 304 * Returns true if [this] occurs after [other].
305 *
306 * The comparison is independent
202 * of whether the time is in UTC or in the local time zone. 307 * of whether the time is in UTC or in the local time zone.
308 *
309 * DateTime berlinWallFell = new DateTime(1989, 11, 9);
310 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
311 *
312 * assert(berlinWallFell.isAfter(moonLanding) == true);
313 *
203 */ 314 */
204 bool isAfter(DateTime other) { 315 bool isAfter(DateTime other) {
205 return millisecondsSinceEpoch > other.millisecondsSinceEpoch; 316 return millisecondsSinceEpoch > other.millisecondsSinceEpoch;
206 } 317 }
207 318
208 /** 319 /**
209 * Returns true if [this] occurs at the same moment as [other]. The 320 * Returns true if [this] occurs at the same moment as [other].
210 * comparison is independent of whether the time is in UTC or in the local 321 *
322 * The comparison is independent of whether the time is in UTC or in the local
211 * time zone. 323 * time zone.
324 *
325 * DateTime berlinWallFell = new DateTime(1989, 11, 9);
326 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
327 *
328 * assert(berlinWallFell.isAtSameMomentAs(moonLanding) == false);
212 */ 329 */
213 bool isAtSameMomentAs(DateTime other) { 330 bool isAtSameMomentAs(DateTime other) {
214 return millisecondsSinceEpoch == other.millisecondsSinceEpoch; 331 return millisecondsSinceEpoch == other.millisecondsSinceEpoch;
215 } 332 }
216 333
334 /**
335 * Compares this DateTime object to [other],
336 * returning zero if the values are equal.
337 *
338 * This function returns a negative integer
339 * if this DateTime is smaller (earlier) than [other],
340 * or a positive integer if it is greater (later).
Kathy Walrath 2013/08/20 19:58:23 I like the rewrite!
mem 2013/08/21 20:08:55 Done.
341 */
217 int compareTo(DateTime other) 342 int compareTo(DateTime other)
218 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch); 343 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch);
219 344
220 int get hashCode => millisecondsSinceEpoch; 345 int get hashCode => millisecondsSinceEpoch;
221 346
222 /** 347 /**
223 * Returns [this] in the local time zone. Returns itself if it is already in 348 * Returns this DateTime value in the local time zone.
224 * the local time zone. Otherwise, this method is equivalent to 349 *
350 * Returns [this] if it is already in the local time zone,
351 * otherwise this method is equivalent to:
Kathy Walrath 2013/08/20 19:58:23 , o -> . O
mem 2013/08/21 20:08:55 Done.
225 * 352 *
226 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 353 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
227 * isUtc: false) 354 * isUtc: false)
228 */ 355 */
229 DateTime toLocal() { 356 DateTime toLocal() {
230 if (isUtc) { 357 if (isUtc) {
231 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 358 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
232 isUtc: false); 359 isUtc: false);
233 } 360 }
234 return this; 361 return this;
235 } 362 }
236 363
237 /** 364 /**
238 * Returns [this] in UTC. Returns itself if it is already in UTC. Otherwise, 365 * Returns this DateTime value in the UTC time zone.
239 * this method is equivalent to 366 *
367 * Returns [this] if it is already in UTC,
368 * otherwise this method is equivalent to:
240 * 369 *
241 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 370 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
242 * isUtc: true) 371 * isUtc: true)
243 */ 372 */
244 DateTime toUtc() { 373 DateTime toUtc() {
245 if (isUtc) return this; 374 if (isUtc) return this;
246 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 375 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
247 isUtc: true); 376 isUtc: true);
248 } 377 }
249 378
250 /** 379 /**
251 * Returns a human readable string for this instance. 380 * Returns a human-readable string for this instance.
381 *
252 * The returned string is constructed for the time zone of this instance. 382 * The returned string is constructed for the time zone of this instance.
383 * The `toString()` method provides a simply formatted string.
384 * It does not support internationalized strings.
385 * Use the [intl](http://pub.dartlang.org/packages/intl) package
386 * at the pub shared packages repo.
253 */ 387 */
254 String toString() { 388 String toString() {
255 String fourDigits(int n) { 389 String fourDigits(int n) {
256 int absN = n.abs(); 390 int absN = n.abs();
257 String sign = n < 0 ? "-" : ""; 391 String sign = n < 0 ? "-" : "";
258 if (absN >= 1000) return "$n"; 392 if (absN >= 1000) return "$n";
259 if (absN >= 100) return "${sign}0$absN"; 393 if (absN >= 100) return "${sign}0$absN";
260 if (absN >= 10) return "${sign}00$absN"; 394 if (absN >= 10) return "${sign}00$absN";
261 return "${sign}000$absN"; 395 return "${sign}000$absN";
262 } 396 }
(...skipping 16 matching lines...) Expand all
279 String min = twoDigits(minute); 413 String min = twoDigits(minute);
280 String sec = twoDigits(second); 414 String sec = twoDigits(second);
281 String ms = threeDigits(millisecond); 415 String ms = threeDigits(millisecond);
282 if (isUtc) { 416 if (isUtc) {
283 return "$y-$m-$d $h:$min:$sec.${ms}Z"; 417 return "$y-$m-$d $h:$min:$sec.${ms}Z";
284 } else { 418 } else {
285 return "$y-$m-$d $h:$min:$sec.$ms"; 419 return "$y-$m-$d $h:$min:$sec.$ms";
286 } 420 }
287 } 421 }
288 422
289 /** Returns a new [DateTime] with the [duration] added to [this]. */ 423 /**
424 * Returns a new [DateTime] instance with [duration] added to [this].
425 *
426 * DateTime today = new DateTime.now();
427 * DateTime sixtyDaysFromNow = today.add(new Duration(days: 60));
428 */
429
290 DateTime add(Duration duration) { 430 DateTime add(Duration duration) {
291 int ms = millisecondsSinceEpoch; 431 int ms = millisecondsSinceEpoch;
292 return new DateTime.fromMillisecondsSinceEpoch( 432 return new DateTime.fromMillisecondsSinceEpoch(
293 ms + duration.inMilliseconds, isUtc: isUtc); 433 ms + duration.inMilliseconds, isUtc: isUtc);
294 } 434 }
295 435
296 /** Returns a new [DateTime] with the [duration] subtracted from [this]. */ 436 /**
437 * Returns a new [DateTime] instance with [duration] subtracted from [this].
438 *
439 * DateTime today = new DateTime.now();
440 * DateTime sixtyDaysAgo = today.subtract(new Duration(days: 60));
441 */
297 DateTime subtract(Duration duration) { 442 DateTime subtract(Duration duration) {
298 int ms = millisecondsSinceEpoch; 443 int ms = millisecondsSinceEpoch;
299 return new DateTime.fromMillisecondsSinceEpoch( 444 return new DateTime.fromMillisecondsSinceEpoch(
300 ms - duration.inMilliseconds, isUtc: isUtc); 445 ms - duration.inMilliseconds, isUtc: isUtc);
301 } 446 }
302 447
303 /** Returns a [Duration] with the difference of [this] and [other]. */ 448 /**
449 * Returns a [Duration] with the difference between [this] and [other].
450 *
451 * DateTime berlinWallFell = new DateTime(1989, DateTime.NOVEMBER, 9);
452 * DateTime dDay = new DateTime(1944, DateTime.JUNE, 6);
453 *
454 * Duration difference = berlinWallFell.difference(dDay);
455 * assert(difference.inDays == 16592);
456 */
457
304 Duration difference(DateTime other) { 458 Duration difference(DateTime other) {
305 int ms = millisecondsSinceEpoch; 459 int ms = millisecondsSinceEpoch;
306 int otherMs = other.millisecondsSinceEpoch; 460 int otherMs = other.millisecondsSinceEpoch;
307 return new Duration(milliseconds: ms - otherMs); 461 return new Duration(milliseconds: ms - otherMs);
308 } 462 }
309 463
310 external DateTime._internal(int year, 464 external DateTime._internal(int year,
311 int month, 465 int month,
312 int day, 466 int day,
313 int hour, 467 int hour,
314 int minute, 468 int minute,
315 int second, 469 int second,
316 int millisecond, 470 int millisecond,
317 bool isUtc); 471 bool isUtc);
318 external DateTime._now(); 472 external DateTime._now();
319 external static int _brokenDownDateToMillisecondsSinceEpoch( 473 external static int _brokenDownDateToMillisecondsSinceEpoch(
320 int year, int month, int day, int hour, int minute, int second, 474 int year, int month, int day, int hour, int minute, int second,
321 int millisecond, bool isUtc); 475 int millisecond, bool isUtc);
322 476
323 /** 477 /**
324 * Returns the abbreviated time-zone name. 478 * Returns the abbreviated time zone name&mdash;for example,
325 * 479 * [:"CET":] or [:"CEST":].
326 * Examples: [:"CET":] or [:"CEST":].
327 */ 480 */
328 external String get timeZoneName; 481 external String get timeZoneName;
329 482
330 /** 483 /**
331 * The time-zone offset is the difference between local time and UTC. That is, 484 * Returns the time zone offset, which
332 * the offset is positive for time zones west of UTC. 485 * is the difference between local time and UTC.
486 *
487 * The offset is positive for time zones west of UTC.
333 * 488 *
334 * Note, that JavaScript, Python and C return the difference between UTC and 489 * Note, that JavaScript, Python and C return the difference between UTC and
335 * local time. Java, C# and Ruby return the difference between local time and 490 * local time. Java, C# and Ruby return the difference between local time and
336 * UTC. 491 * UTC.
337 */ 492 */
338 external Duration get timeZoneOffset; 493 external Duration get timeZoneOffset;
339 494
340 /** 495 /**
341 * Returns the year. 496 * Returns the year.
497 *
498 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
499 * assert(moonLanding.year == 1969);
342 */ 500 */
343 external int get year; 501 external int get year;
344 502
345 /** 503 /**
346 * Returns the month into the year [1..12]. 504 * Returns the month [1..12].
505 *
506 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
507 * assert(moonLanding.month == 7);
508 * assert(moonLanding.month == JULY);
347 */ 509 */
348 external int get month; 510 external int get month;
349 511
350 /** 512 /**
351 * Returns the day into the month [1..31]. 513 * Returns the day of the month [1..31].
514 *
515 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
516 * assert(moonLanding.day == 20);
352 */ 517 */
353 external int get day; 518 external int get day;
354 519
355 /** 520 /**
356 * Returns the hour into the day [0..23]. 521 * Returns the hour of the day [0..23].
522 *
523 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
524 * assert(moonLanding.hour == 20);
357 */ 525 */
358 external int get hour; 526 external int get hour;
359 527
360 /** 528 /**
361 * Returns the minute into the hour [0...59]. 529 * Returns the minute [0...59].
530 *
531 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
532 * assert(moonLanding.minute == 18);
362 */ 533 */
363 external int get minute; 534 external int get minute;
364 535
365 /** 536 /**
366 * Returns the second into the minute [0...59]. 537 * Returns the second [0...59].
538 *
539 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
540 * assert(moonLanding.second == 0);
367 */ 541 */
368 external int get second; 542 external int get second;
369 543
370 /** 544 /**
371 * Returns the millisecond into the second [0...999]. 545 * Returns the millisecond [0...999].
546 *
547 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
548 * assert(moonLanding.millisecond == 0);
372 */ 549 */
373 external int get millisecond; 550 external int get millisecond;
374 551
375 /** 552 /**
376 * Returns the week day [MON..SUN]. In accordance with ISO 8601 553 * Returns the day of the week [MONDAY]..[SUNDAY].
377 * a week starts with Monday which has the value 1. 554 *
555 * In accordance with ISO 8601
556 * a week starts with Monday, which has the value 1.
557 *
558 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
559 * assert(moonLanding.weekday == 7);
560 * assert(moonLanding.weekday == DateTime.SUNDAY);
561 *
378 */ 562 */
379 external int get weekday; 563 external int get weekday;
380 } 564 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698