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