| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.core; | |
| 6 | |
| 7 /** | |
| 8 * An instant in time, such as July 20, 1969, 8:18pm GMT. | |
| 9 * | |
| 10 * Create a DateTime object by using one of the constructors | |
| 11 * or by parsing a correctly formatted string, | |
| 12 * which complies with a subset of ISO 8601. | |
| 13 * Note that hours are specified between 0 and 23, | |
| 14 * as in a 24-hour clock. | |
| 15 * For example: | |
| 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 * 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.weekday == DateTime.THURSDAY); | |
| 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(moonLanding) | |
| 83 * assert(difference.inDays == 7416); | |
| 84 * | |
| 85 * The difference between two dates in different time zones | |
| 86 * is just the number of nanoseconds between the two points in time. | |
| 87 * It doesn't take calendar days into account. | |
| 88 * That means that the difference between two midnights in local time may be | |
| 89 * less than 24 hours times the number of days between them, | |
| 90 * if there is a daylight saving change in between. | |
| 91 * If the difference above is calculated using Australian local time, the | |
| 92 * difference is 7415 days and 23 hours, which is only 7415 whole days as | |
| 93 * reported by `inDays`. | |
| 94 * | |
| 95 * ## Other resources | |
| 96 * | |
| 97 * See [Duration] to represent a span of time. | |
| 98 * See [Stopwatch] to measure timespans. | |
| 99 * | |
| 100 * The DateTime class does not provide internationalization. | |
| 101 * To internationalize your code, use | |
| 102 * the [intl](http://pub.dartlang.org/packages/intl) package. | |
| 103 * | |
| 104 */ | |
| 105 class DateTime implements Comparable { | |
| 106 // Weekday constants that are returned by [weekday] method: | |
| 107 static const int MONDAY = 1; | |
| 108 static const int TUESDAY = 2; | |
| 109 static const int WEDNESDAY = 3; | |
| 110 static const int THURSDAY = 4; | |
| 111 static const int FRIDAY = 5; | |
| 112 static const int SATURDAY = 6; | |
| 113 static const int SUNDAY = 7; | |
| 114 static const int DAYS_PER_WEEK = 7; | |
| 115 | |
| 116 // Month constants that are returned by the [month] getter. | |
| 117 static const int JANUARY = 1; | |
| 118 static const int FEBRUARY = 2; | |
| 119 static const int MARCH = 3; | |
| 120 static const int APRIL = 4; | |
| 121 static const int MAY = 5; | |
| 122 static const int JUNE = 6; | |
| 123 static const int JULY = 7; | |
| 124 static const int AUGUST = 8; | |
| 125 static const int SEPTEMBER = 9; | |
| 126 static const int OCTOBER = 10; | |
| 127 static const int NOVEMBER = 11; | |
| 128 static const int DECEMBER = 12; | |
| 129 static const int MONTHS_PER_YEAR = 12; | |
| 130 | |
| 131 /** | |
| 132 * The number of milliseconds since | |
| 133 * the "Unix epoch" 1970-01-01T00:00:00Z (UTC). | |
| 134 * | |
| 135 * This value is independent of the time zone. | |
| 136 * | |
| 137 * This value is at most | |
| 138 * 8,640,000,000,000,000ms (100,000,000 days) from the Unix epoch. | |
| 139 * In other words: [:millisecondsSinceEpoch.abs() <= 8640000000000000:]. | |
| 140 * | |
| 141 */ | |
| 142 final int millisecondsSinceEpoch; | |
| 143 | |
| 144 /** | |
| 145 * True if this [DateTime] is set to UTC time. | |
| 146 * | |
| 147 * DateTime dDay = new DateTime.utc(1944, 6, 6); | |
| 148 * assert(dDay.isUtc); | |
| 149 * | |
| 150 */ | |
| 151 final bool isUtc; | |
| 152 | |
| 153 /** | |
| 154 * Constructs a [DateTime] instance specified in the local time zone. | |
| 155 * | |
| 156 * For example, | |
| 157 * to create a new DateTime object representing April 29, 2014, 6:04am: | |
| 158 * | |
| 159 * DateTime annularEclipse = new DateTime(2014, DateTime.APRIL, 29, 6, 4); | |
| 160 */ | |
| 161 DateTime(int year, | |
| 162 [int month = 1, | |
| 163 int day = 1, | |
| 164 int hour = 0, | |
| 165 int minute = 0, | |
| 166 int second = 0, | |
| 167 int millisecond = 0]) | |
| 168 : this._internal( | |
| 169 year, month, day, hour, minute, second, millisecond, false); | |
| 170 | |
| 171 /** | |
| 172 * Constructs a [DateTime] instance specified in the UTC time zone. | |
| 173 * | |
| 174 * DateTime dDay = new DateTime.utc(1944, DateTime.JUNE, 6); | |
| 175 */ | |
| 176 DateTime.utc(int year, | |
| 177 [int month = 1, | |
| 178 int day = 1, | |
| 179 int hour = 0, | |
| 180 int minute = 0, | |
| 181 int second = 0, | |
| 182 int millisecond = 0]) | |
| 183 : this._internal( | |
| 184 year, month, day, hour, minute, second, millisecond, true); | |
| 185 | |
| 186 /** | |
| 187 * Constructs a [DateTime] instance with current date and time in the | |
| 188 * local time zone. | |
| 189 * | |
| 190 * DateTime thisInstant = new DateTime.now(); | |
| 191 * | |
| 192 */ | |
| 193 DateTime.now() : this._now(); | |
| 194 | |
| 195 /** | |
| 196 * Constructs a new [DateTime] instance based on [formattedString]. | |
| 197 * | |
| 198 * Throws a [FormatException] if the input cannot be parsed. | |
| 199 * | |
| 200 * The function parses a subset of ISO 8601 | |
| 201 * which includes the subset accepted by RFC 3339. | |
| 202 * | |
| 203 * The accepted inputs are currently: | |
| 204 * | |
| 205 * * A date: A signed four-to-six digit year, two digit month and | |
| 206 * two digit day, optionally separated by `-` characters. | |
| 207 * Examples: "19700101", "-0004-12-24", "81030-04-01". | |
| 208 * * An optional time part, separated from the date by either `T` or a space. | |
| 209 * The time part is a two digit hour, | |
| 210 * then optionally a two digit minutes value, | |
| 211 * then optionally a two digit seconds value, and | |
| 212 * then optionally a '.' followed by a one-to-six digit second fraction. | |
| 213 * The minuts and seconds may be separated from the previous parts by a ':'. | |
| 214 * Examples: "12", "12:30:24.124", "123010.50". | |
| 215 * * An optional time-zone offset part, | |
| 216 * possibly separated from the previous by a space. | |
| 217 * The time zone is either 'z' or 'Z', or it is a signed two digit hour | |
| 218 * part and an optional two digit minute part. | |
| 219 * The minutes may be separted from the hours by a ':'. | |
| 220 * Examples: "Z", "-10", "01:30", "1130". | |
| 221 * | |
| 222 * This includes the output of both [toString] and [toIso8601String], which | |
| 223 * will be parsed back into a `DateTime` object with the same time as the | |
| 224 * original. | |
| 225 * | |
| 226 * The result is always in either local time or UTC. | |
| 227 * If a time zone offset other than UTC is specified, | |
| 228 * the time is converted to the equivalent UTC time. | |
| 229 * | |
| 230 * Examples of accepted strings: | |
| 231 * | |
| 232 * * `"2012-02-27 13:27:00"` | |
| 233 * * `"2012-02-27 13:27:00.123456z"` | |
| 234 * * `"20120227 13:27:00"` | |
| 235 * * `"20120227T132700"` | |
| 236 * * `"20120227"` | |
| 237 * * `"+20120227"` | |
| 238 * * `"2012-02-27T14Z"` | |
| 239 * * `"2012-02-27T14+00:00"` | |
| 240 * * `"-123450101 00:00:00 Z"`: in the year -12345. | |
| 241 * * `"2002-02-27T14:00:00-0500"`: Same as `"2002-02-27T19:00:00Z"` | |
| 242 */ | |
| 243 // TODO(lrn): restrict incorrect values like 2003-02-29T50:70:80. | |
| 244 // Or not, that may be a breaking change. | |
| 245 static DateTime parse(String formattedString) { | |
| 246 /* | |
| 247 * date ::= yeardate time_opt timezone_opt | |
| 248 * yeardate ::= year colon_opt month colon_opt day | |
| 249 * year ::= sign_opt digit{4,6} | |
| 250 * colon_opt :: <empty> | ':' | |
| 251 * sign ::= '+' | '-' | |
| 252 * sign_opt ::= <empty> | sign | |
| 253 * month ::= digit{2} | |
| 254 * day ::= digit{2} | |
| 255 * time_opt ::= <empty> | (' ' | 'T') hour minutes_opt | |
| 256 * minutes_opt ::= <empty> | colon_opt digit{2} seconds_opt | |
| 257 * seconds_opt ::= <empty> | colon_opt digit{2} millis_opt | |
| 258 * millis_opt ::= <empty> | '.' digit{1,6} | |
| 259 * timezone_opt ::= <empty> | space_opt timezone | |
| 260 * space_opt :: ' ' | <empty> | |
| 261 * timezone ::= 'z' | 'Z' | sign digit{2} timezonemins_opt | |
| 262 * timezonemins_opt ::= <empty> | colon_opt digit{2} | |
| 263 */ | |
| 264 final RegExp re = new RegExp( | |
| 265 r'^([+-]?\d{4,6})-?(\d\d)-?(\d\d)' // The day part. | |
| 266 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)?' // The time part | |
| 267 r'( ?[zZ]| ?([-+])(\d\d)(?::?(\d\d))?)?)?$'); // The timezone part | |
| 268 | |
| 269 Match match = re.firstMatch(formattedString); | |
| 270 if (match != null) { | |
| 271 int parseIntOrZero(String matched) { | |
| 272 if (matched == null) return 0; | |
| 273 return int.parse(matched); | |
| 274 } | |
| 275 | |
| 276 double parseDoubleOrZero(String matched) { | |
| 277 if (matched == null) return 0.0; | |
| 278 return double.parse(matched); | |
| 279 } | |
| 280 | |
| 281 int years = int.parse(match[1]); | |
| 282 int month = int.parse(match[2]); | |
| 283 int day = int.parse(match[3]); | |
| 284 int hour = parseIntOrZero(match[4]); | |
| 285 int minute = parseIntOrZero(match[5]); | |
| 286 int second = parseIntOrZero(match[6]); | |
| 287 bool addOneMillisecond = false; | |
| 288 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round(); | |
| 289 if (millisecond == 1000) { | |
| 290 addOneMillisecond = true; | |
| 291 millisecond = 999; | |
| 292 } | |
| 293 bool isUtc = false; | |
| 294 if (match[8] != null) { // timezone part | |
| 295 isUtc = true; | |
| 296 if (match[9] != null) { | |
| 297 // timezone other than 'Z' and 'z'. | |
| 298 int sign = (match[9] == '-') ? -1 : 1; | |
| 299 int hourDifference = int.parse(match[10]); | |
| 300 int minuteDifference = parseIntOrZero(match[11]); | |
| 301 minuteDifference += 60 * hourDifference; | |
| 302 minute -= sign * minuteDifference; | |
| 303 } | |
| 304 } | |
| 305 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | |
| 306 years, month, day, hour, minute, second, millisecond, isUtc); | |
| 307 if (millisecondsSinceEpoch == null) { | |
| 308 throw new FormatException("Time out of range", formattedString); | |
| 309 } | |
| 310 if (addOneMillisecond) millisecondsSinceEpoch++; | |
| 311 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | |
| 312 isUtc: isUtc); | |
| 313 } else { | |
| 314 throw new FormatException("Invalid date format", formattedString); | |
| 315 } | |
| 316 } | |
| 317 | |
| 318 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; | |
| 319 | |
| 320 /** | |
| 321 * Constructs a new [DateTime] instance | |
| 322 * with the given [millisecondsSinceEpoch]. | |
| 323 * | |
| 324 * If [isUtc] is false then the date is in the local time zone. | |
| 325 * | |
| 326 * The constructed [DateTime] represents | |
| 327 * 1970-01-01T00:00:00Z + [millisecondsSinceEpoch] ms in the given | |
| 328 * time zone (local or UTC). | |
| 329 */ | |
| 330 DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, | |
| 331 {bool isUtc: false}) | |
| 332 : this.millisecondsSinceEpoch = millisecondsSinceEpoch, | |
| 333 this.isUtc = isUtc { | |
| 334 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | |
| 335 throw new ArgumentError(millisecondsSinceEpoch); | |
| 336 } | |
| 337 if (isUtc == null) throw new ArgumentError(isUtc); | |
| 338 } | |
| 339 | |
| 340 /** | |
| 341 * Returns true if [other] is a [DateTime] at the same moment and in the | |
| 342 * same time zone (UTC or local). | |
| 343 * | |
| 344 * DateTime dDayUtc = new DateTime.utc(1944, DateTime.JUNE, 6); | |
| 345 * DateTime dDayLocal = new DateTime(1944, DateTime.JUNE, 6); | |
| 346 * | |
| 347 * assert(dDayUtc.isAtSameMomentAs(dDayLocal) == false); | |
| 348 * | |
| 349 * See [isAtSameMomentAs] for a comparison that adjusts for time zone. | |
| 350 */ | |
| 351 bool operator ==(other) { | |
| 352 if (!(other is DateTime)) return false; | |
| 353 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch && | |
| 354 isUtc == other.isUtc); | |
| 355 } | |
| 356 | |
| 357 /** | |
| 358 * Returns true if [this] occurs before [other]. | |
| 359 * | |
| 360 * The comparison is independent | |
| 361 * of whether the time is in UTC or in the local time zone. | |
| 362 * | |
| 363 * DateTime berlinWallFell = new DateTime(1989, 11, 9); | |
| 364 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 365 * | |
| 366 * assert(berlinWallFell.isBefore(moonLanding) == false); | |
| 367 * | |
| 368 */ | |
| 369 bool isBefore(DateTime other) { | |
| 370 return millisecondsSinceEpoch < other.millisecondsSinceEpoch; | |
| 371 } | |
| 372 | |
| 373 /** | |
| 374 * Returns true if [this] occurs after [other]. | |
| 375 * | |
| 376 * The comparison is independent | |
| 377 * of whether the time is in UTC or in the local time zone. | |
| 378 * | |
| 379 * DateTime berlinWallFell = new DateTime(1989, 11, 9); | |
| 380 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 381 * | |
| 382 * assert(berlinWallFell.isAfter(moonLanding) == true); | |
| 383 * | |
| 384 */ | |
| 385 bool isAfter(DateTime other) { | |
| 386 return millisecondsSinceEpoch > other.millisecondsSinceEpoch; | |
| 387 } | |
| 388 | |
| 389 /** | |
| 390 * Returns true if [this] occurs at the same moment as [other]. | |
| 391 * | |
| 392 * The comparison is independent of whether the time is in UTC or in the local | |
| 393 * time zone. | |
| 394 * | |
| 395 * DateTime berlinWallFell = new DateTime(1989, 11, 9); | |
| 396 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 397 * | |
| 398 * assert(berlinWallFell.isAtSameMomentAs(moonLanding) == false); | |
| 399 */ | |
| 400 bool isAtSameMomentAs(DateTime other) { | |
| 401 return millisecondsSinceEpoch == other.millisecondsSinceEpoch; | |
| 402 } | |
| 403 | |
| 404 /** | |
| 405 * Compares this DateTime object to [other], | |
| 406 * returning zero if the values are equal. | |
| 407 * | |
| 408 * This function returns a negative integer | |
| 409 * if this DateTime is smaller (earlier) than [other], | |
| 410 * or a positive integer if it is greater (later). | |
| 411 */ | |
| 412 int compareTo(DateTime other) | |
| 413 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch); | |
| 414 | |
| 415 int get hashCode => millisecondsSinceEpoch; | |
| 416 | |
| 417 /** | |
| 418 * Returns this DateTime value in the local time zone. | |
| 419 * | |
| 420 * Returns [this] if it is already in the local time zone. | |
| 421 * Otherwise this method is equivalent to: | |
| 422 * | |
| 423 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | |
| 424 * isUtc: false) | |
| 425 */ | |
| 426 DateTime toLocal() { | |
| 427 if (isUtc) { | |
| 428 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | |
| 429 isUtc: false); | |
| 430 } | |
| 431 return this; | |
| 432 } | |
| 433 | |
| 434 /** | |
| 435 * Returns this DateTime value in the UTC time zone. | |
| 436 * | |
| 437 * Returns [this] if it is already in UTC. | |
| 438 * Otherwise this method is equivalent to: | |
| 439 * | |
| 440 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | |
| 441 * isUtc: true) | |
| 442 */ | |
| 443 DateTime toUtc() { | |
| 444 if (isUtc) return this; | |
| 445 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | |
| 446 isUtc: true); | |
| 447 } | |
| 448 | |
| 449 static String _fourDigits(int n) { | |
| 450 int absN = n.abs(); | |
| 451 String sign = n < 0 ? "-" : ""; | |
| 452 if (absN >= 1000) return "$n"; | |
| 453 if (absN >= 100) return "${sign}0$absN"; | |
| 454 if (absN >= 10) return "${sign}00$absN"; | |
| 455 return "${sign}000$absN"; | |
| 456 } | |
| 457 | |
| 458 static String _sixDigits(int n) { | |
| 459 assert(n < -9999 || n > 9999); | |
| 460 int absN = n.abs(); | |
| 461 String sign = n < 0 ? "-" : "+"; | |
| 462 if (absN >= 100000) return "$sign$absN"; | |
| 463 return "${sign}0$absN"; | |
| 464 } | |
| 465 | |
| 466 static String _threeDigits(int n) { | |
| 467 if (n >= 100) return "${n}"; | |
| 468 if (n >= 10) return "0${n}"; | |
| 469 return "00${n}"; | |
| 470 } | |
| 471 | |
| 472 static String _twoDigits(int n) { | |
| 473 if (n >= 10) return "${n}"; | |
| 474 return "0${n}"; | |
| 475 } | |
| 476 | |
| 477 /** | |
| 478 * Returns a human-readable string for this instance. | |
| 479 * | |
| 480 * The returned string is constructed for the time zone of this instance. | |
| 481 * The `toString()` method provides a simply formatted string. | |
| 482 * It does not support internationalized strings. | |
| 483 * Use the [intl](http://pub.dartlang.org/packages/intl) package | |
| 484 * at the pub shared packages repo. | |
| 485 * | |
| 486 * The resulting string can be parsed back using [parse]. | |
| 487 */ | |
| 488 String toString() { | |
| 489 String y = _fourDigits(year); | |
| 490 String m = _twoDigits(month); | |
| 491 String d = _twoDigits(day); | |
| 492 String h = _twoDigits(hour); | |
| 493 String min = _twoDigits(minute); | |
| 494 String sec = _twoDigits(second); | |
| 495 String ms = _threeDigits(millisecond); | |
| 496 if (isUtc) { | |
| 497 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | |
| 498 } else { | |
| 499 return "$y-$m-$d $h:$min:$sec.$ms"; | |
| 500 } | |
| 501 } | |
| 502 | |
| 503 /** | |
| 504 * Returns an ISO-8601 full-precision extended format representation. | |
| 505 * | |
| 506 * The format is `yyyy-MM-ddTHH:mm:ss.sssZ` for UTC time, and | |
| 507 * `yyyy-MM-ddTHH:mm:ss.sss` (no trailing "Z") for local/non-UTC time, | |
| 508 * where: | |
| 509 * | |
| 510 * * `yyyy` is a, possibly negative, four digit representation of the year, | |
| 511 * if the year is in the range -9999 to 9999, | |
| 512 * otherwise it is a signed six digit representation of the year. | |
| 513 * * `MM` is the month in the range 01 to 12, | |
| 514 * * `dd` is the day of the month in the range 01 to 31, | |
| 515 * * `HH` are hours in the range 00 to 23, | |
| 516 * * `mm` are minutes in the range 00 to 59, | |
| 517 * * `ss` are seconds in the range 00 to 59 (no leap seconds), and | |
| 518 * * `sss` are milliseconds in the range 000 to 999. | |
| 519 * | |
| 520 * The resulting string can be parsed back using [parse]. | |
| 521 */ | |
| 522 String toIso8601String() { | |
| 523 String y = (year >= -9999 && year <= 9999) ? _fourDigits(year) | |
| 524 : _sixDigits(year); | |
| 525 String m = _twoDigits(month); | |
| 526 String d = _twoDigits(day); | |
| 527 String h = _twoDigits(hour); | |
| 528 String min = _twoDigits(minute); | |
| 529 String sec = _twoDigits(second); | |
| 530 String ms = _threeDigits(millisecond); | |
| 531 if (isUtc) { | |
| 532 return "$y-$m-${d}T$h:$min:$sec.${ms}Z"; | |
| 533 } else { | |
| 534 return "$y-$m-${d}T$h:$min:$sec.$ms"; | |
| 535 } | |
| 536 } | |
| 537 | |
| 538 /** | |
| 539 * Returns a new [DateTime] instance with [duration] added to [this]. | |
| 540 * | |
| 541 * DateTime today = new DateTime.now(); | |
| 542 * DateTime sixtyDaysFromNow = today.add(new Duration(days: 60)); | |
| 543 */ | |
| 544 DateTime add(Duration duration) { | |
| 545 int ms = millisecondsSinceEpoch; | |
| 546 return new DateTime.fromMillisecondsSinceEpoch( | |
| 547 ms + duration.inMilliseconds, isUtc: isUtc); | |
| 548 } | |
| 549 | |
| 550 /** | |
| 551 * Returns a new [DateTime] instance with [duration] subtracted from [this]. | |
| 552 * | |
| 553 * DateTime today = new DateTime.now(); | |
| 554 * DateTime sixtyDaysAgo = today.subtract(new Duration(days: 60)); | |
| 555 */ | |
| 556 DateTime subtract(Duration duration) { | |
| 557 int ms = millisecondsSinceEpoch; | |
| 558 return new DateTime.fromMillisecondsSinceEpoch( | |
| 559 ms - duration.inMilliseconds, isUtc: isUtc); | |
| 560 } | |
| 561 | |
| 562 /** | |
| 563 * Returns a [Duration] with the difference between [this] and [other]. | |
| 564 * | |
| 565 * DateTime berlinWallFell = new DateTime(1989, DateTime.NOVEMBER, 9); | |
| 566 * DateTime dDay = new DateTime(1944, DateTime.JUNE, 6); | |
| 567 * | |
| 568 * Duration difference = berlinWallFell.difference(dDay); | |
| 569 * assert(difference.inDays == 16592); | |
| 570 */ | |
| 571 | |
| 572 Duration difference(DateTime other) { | |
| 573 int ms = millisecondsSinceEpoch; | |
| 574 int otherMs = other.millisecondsSinceEpoch; | |
| 575 return new Duration(milliseconds: ms - otherMs); | |
| 576 } | |
| 577 | |
| 578 DateTime._internal(int year, | |
| 579 int month, | |
| 580 int day, | |
| 581 int hour, | |
| 582 int minute, | |
| 583 int second, | |
| 584 int millisecond, | |
| 585 bool isUtc) | |
| 586 // checkBool is manually inlined here because dart2js doesn't inline it | |
| 587 // and [isUtc] is usually a constant. | |
| 588 : this.isUtc = isUtc is bool ? isUtc : throw new ArgumentError(isUtc), | |
| 589 millisecondsSinceEpoch = checkInt(Primitives.valueFromDecomposedDate( | |
| 590 year, month, day, hour, minute, second, millisecond, isUtc)); | |
| 591 DateTime._now() | |
| 592 : isUtc = false, | |
| 593 millisecondsSinceEpoch = Primitives.dateNow(); | |
| 594 /// Returns the time as milliseconds since epoch, or null if the | |
| 595 /// values are out of range. | |
| 596 static int _brokenDownDateToMillisecondsSinceEpoch( | |
| 597 int year, int month, int day, int hour, int minute, int second, | |
| 598 int millisecond, bool isUtc) { | |
| 599 return Primitives.valueFromDecomposedDate( | |
| 600 year, month, day, hour, minute, second, millisecond, isUtc); | |
| 601 } | |
| 602 | |
| 603 /** | |
| 604 * The abbreviated time zone name—for example, | |
| 605 * [:"CET":] or [:"CEST":]. | |
| 606 */ | |
| 607 String get timeZoneName { | |
| 608 if (isUtc) return "UTC"; | |
| 609 return Primitives.getTimeZoneName(this); | |
| 610 } | |
| 611 | |
| 612 /** | |
| 613 * The time zone offset, which | |
| 614 * is the difference between local time and UTC. | |
| 615 * | |
| 616 * The offset is positive for time zones east of UTC. | |
| 617 * | |
| 618 * Note, that JavaScript, Python and C return the difference between UTC and | |
| 619 * local time. Java, C# and Ruby return the difference between local time and | |
| 620 * UTC. | |
| 621 */ | |
| 622 Duration get timeZoneOffset { | |
| 623 if (isUtc) return new Duration(); | |
| 624 return new Duration(minutes: Primitives.getTimeZoneOffsetInMinutes(this)); | |
| 625 } | |
| 626 | |
| 627 /** | |
| 628 * The year. | |
| 629 * | |
| 630 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 631 * assert(moonLanding.year == 1969); | |
| 632 */ | |
| 633 int get year => Primitives.getYear(this); | |
| 634 | |
| 635 /** | |
| 636 * The month [1..12]. | |
| 637 * | |
| 638 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 639 * assert(moonLanding.month == 7); | |
| 640 * assert(moonLanding.month == DateTime.JULY); | |
| 641 */ | |
| 642 int get month => Primitives.getMonth(this); | |
| 643 | |
| 644 /** | |
| 645 * The day of the month [1..31]. | |
| 646 * | |
| 647 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 648 * assert(moonLanding.day == 20); | |
| 649 */ | |
| 650 int get day => Primitives.getDay(this); | |
| 651 | |
| 652 /** | |
| 653 * The hour of the day, expressed as in a 24-hour clock [0..23]. | |
| 654 * | |
| 655 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 656 * assert(moonLanding.hour == 20); | |
| 657 */ | |
| 658 int get hour => Primitives.getHours(this); | |
| 659 | |
| 660 /** | |
| 661 * The minute [0...59]. | |
| 662 * | |
| 663 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 664 * assert(moonLanding.minute == 18); | |
| 665 */ | |
| 666 int get minute => Primitives.getMinutes(this); | |
| 667 | |
| 668 /** | |
| 669 * The second [0...59]. | |
| 670 * | |
| 671 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 672 * assert(moonLanding.second == 0); | |
| 673 */ | |
| 674 int get second => Primitives.getSeconds(this); | |
| 675 | |
| 676 /** | |
| 677 * The millisecond [0...999]. | |
| 678 * | |
| 679 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 680 * assert(moonLanding.millisecond == 0); | |
| 681 */ | |
| 682 int get millisecond => Primitives.getMilliseconds(this); | |
| 683 | |
| 684 /** | |
| 685 * The day of the week [MONDAY]..[SUNDAY]. | |
| 686 * | |
| 687 * In accordance with ISO 8601 | |
| 688 * a week starts with Monday, which has the value 1. | |
| 689 * | |
| 690 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | |
| 691 * assert(moonLanding.weekday == 7); | |
| 692 * assert(moonLanding.weekday == DateTime.SUNDAY); | |
| 693 * | |
| 694 */ | |
| 695 int get weekday => Primitives.getWeekday(this); | |
| 696 } | |
| OLD | NEW |