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