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