| 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 // TODO(jmesserly): the native class should be the real JS Date. | 5 // TODO(jmesserly): the native class should be the real JS Date. |
| 6 // TODO(jimhug): Making the date value non-lazy might be a good path there. | 6 // TODO(jimhug): Making the date value non-lazy might be a good path there. |
| 7 class DateImplementation implements Date { | 7 class DateImplementation implements Date { |
| 8 final int value; | 8 final int value; |
| 9 final TimeZone timeZone; | 9 final TimeZone timeZone; |
| 10 | 10 |
| 11 factory DateImplementation(int years, | 11 factory DateImplementation(int years, |
| 12 [int month = 1, | 12 [int month = 1, |
| 13 int day = 1, | 13 int day = 1, |
| 14 int hours = 0, | 14 int hours = 0, |
| 15 int minutes = 0, | 15 int minutes = 0, |
| 16 int seconds = 0, | 16 int seconds = 0, |
| 17 int milliseconds = 0]) { | 17 int milliseconds = 0, |
| 18 bool isUtc = false]) { |
| 18 return new DateImplementation.withTimeZone( | 19 return new DateImplementation.withTimeZone( |
| 19 years, month, day, | 20 years, month, day, |
| 20 hours, minutes, seconds, milliseconds, | 21 hours, minutes, seconds, milliseconds, |
| 21 new TimeZoneImplementation.local()); | 22 isUtc ? const TimeZoneImplementation.utc() |
| 23 : new TimeZoneImplementation.local()); |
| 22 } | 24 } |
| 23 | 25 |
| 24 DateImplementation.withTimeZone(int years, | 26 DateImplementation.withTimeZone(int years, |
| 25 int month, | 27 int month, |
| 26 int day, | 28 int day, |
| 27 int hours, | 29 int hours, |
| 28 int minutes, | 30 int minutes, |
| 29 int seconds, | 31 int seconds, |
| 30 int milliseconds, | 32 int milliseconds, |
| 31 TimeZone timeZone) | 33 TimeZone timeZone) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 int minutes = parseIntOrZero(match[5]); | 79 int minutes = parseIntOrZero(match[5]); |
| 78 int seconds = parseIntOrZero(match[6]); | 80 int seconds = parseIntOrZero(match[6]); |
| 79 bool addOneMillisecond = false; | 81 bool addOneMillisecond = false; |
| 80 int milliseconds = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); | 82 int milliseconds = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); |
| 81 if (milliseconds == 1000) { | 83 if (milliseconds == 1000) { |
| 82 addOneMillisecond = true; | 84 addOneMillisecond = true; |
| 83 milliseconds = 999; | 85 milliseconds = 999; |
| 84 } | 86 } |
| 85 // TODO(floitsch): we should not need to test against the empty string. | 87 // TODO(floitsch): we should not need to test against the empty string. |
| 86 bool isUtc = (match[8] !== null) && (match[8] != ""); | 88 bool isUtc = (match[8] !== null) && (match[8] != ""); |
| 87 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); | |
| 88 int epochValue = _valueFromDecomposed( | 89 int epochValue = _valueFromDecomposed( |
| 89 years, month, day, hours, minutes, seconds, milliseconds, isUtc); | 90 years, month, day, hours, minutes, seconds, milliseconds, isUtc); |
| 90 if (epochValue === null) { | 91 if (epochValue === null) { |
| 91 throw new IllegalArgumentException(formattedString); | 92 throw new IllegalArgumentException(formattedString); |
| 92 } | 93 } |
| 93 if (addOneMillisecond) epochValue++; | 94 if (addOneMillisecond) epochValue++; |
| 94 return new DateImplementation.fromEpoch(epochValue, timezone); | 95 return new DateImplementation.fromEpoch(epochValue, isUtc); |
| 95 } else { | 96 } else { |
| 96 throw new IllegalArgumentException(formattedString); | 97 throw new IllegalArgumentException(formattedString); |
| 97 } | 98 } |
| 98 } | 99 } |
| 99 | 100 |
| 100 const DateImplementation.fromEpoch(this.value, this.timeZone); | 101 DateImplementation.fromEpoch(this.value, [bool isUtc = false]) |
| 102 : this.timeZone = isUtc ? const TimeZoneImplementation.utc() |
| 103 : new TimeZoneImplementation.local(); |
| 101 | 104 |
| 102 bool operator ==(other) { | 105 bool operator ==(other) { |
| 103 if (!(other is DateImplementation)) return false; | 106 if (other is !DateImplementation) return false; |
| 104 return (value == other.value) && (timeZone == other.timeZone); | 107 return (value == other.value); |
| 105 } | 108 } |
| 106 | 109 |
| 107 bool operator <(Date other) => value < other.value; | 110 bool operator <(Date other) => value < other.value; |
| 108 | 111 |
| 109 bool operator <=(Date other) => value <= other.value; | 112 bool operator <=(Date other) => value <= other.value; |
| 110 | 113 |
| 111 bool operator >(Date other) => value > other.value; | 114 bool operator >(Date other) => value > other.value; |
| 112 | 115 |
| 113 bool operator >=(Date other) => value >= other.value; | 116 bool operator >=(Date other) => value >= other.value; |
| 114 | 117 |
| 115 int compareTo(Date other) { | 118 int compareTo(Date other) { |
| 116 return value.compareTo(other.value); | 119 return value.compareTo(other.value); |
| 117 } | 120 } |
| 118 | 121 |
| 119 int hashCode() { | 122 int hashCode() { |
| 120 return value; | 123 return value; |
| 121 } | 124 } |
| 122 | 125 |
| 126 Date toLocal() { |
| 127 if (isUtc()) return changeTimeZone(new TimeZoneImplementation.local()); |
| 128 return this; |
| 129 } |
| 130 |
| 131 Date toUtc() { |
| 132 if (isUtc()) return this; |
| 133 return changeTimeZone(const TimeZoneImplementation.utc()); |
| 134 } |
| 135 |
| 123 Date changeTimeZone(TimeZone targetTimeZone) { | 136 Date changeTimeZone(TimeZone targetTimeZone) { |
| 124 if (targetTimeZone == null) { | 137 if (targetTimeZone == null) { |
| 125 targetTimeZone = new TimeZoneImplementation.local(); | 138 targetTimeZone = new TimeZoneImplementation.local(); |
| 126 } | 139 } |
| 127 return new Date.fromEpoch(value, targetTimeZone); | 140 return new Date.fromEpoch(value, targetTimeZone.isUtc); |
| 128 } | 141 } |
| 129 | 142 |
| 130 String get timeZoneName() { throw "Unimplemented"; } | 143 String get timeZoneName() { throw "Unimplemented"; } |
| 131 Duration get timeZoneOffset() { throw "Unimplemented"; } | 144 Duration get timeZoneOffset() { throw "Unimplemented"; } |
| 132 | 145 |
| 133 int get year() native | 146 int get year() native |
| 134 '''return this.isUtc() ? this._asJs().getUTCFullYear() : | 147 '''return this.isUtc() ? this._asJs().getUTCFullYear() : |
| 135 this._asJs().getFullYear();''' { | 148 this._asJs().getFullYear();''' { |
| 136 isUtc(); | 149 isUtc(); |
| 137 _asJs(); | 150 _asJs(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 this._asJs().getMilliseconds();''' { | 190 this._asJs().getMilliseconds();''' { |
| 178 isUtc(); | 191 isUtc(); |
| 179 _asJs(); | 192 _asJs(); |
| 180 } | 193 } |
| 181 | 194 |
| 182 // Adjust by one because JS weeks start on Sunday. | 195 // Adjust by one because JS weeks start on Sunday. |
| 183 int get weekday() native ''' | 196 int get weekday() native ''' |
| 184 var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay(); | 197 var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay(); |
| 185 return (day + 6) % 7;'''; | 198 return (day + 6) % 7;'''; |
| 186 | 199 |
| 187 // TODO(jimhug): Could this please be getters? | |
| 188 bool isLocalTime() { | |
| 189 return !timeZone.isUtc; | |
| 190 } | |
| 191 | |
| 192 bool isUtc() { | 200 bool isUtc() { |
| 193 return timeZone.isUtc; | 201 return timeZone.isUtc; |
| 194 } | 202 } |
| 195 | 203 |
| 196 String toString() { | 204 String toString() { |
| 197 String fourDigits(int n) { | 205 String fourDigits(int n) { |
| 198 int absN = n.abs(); | 206 int absN = n.abs(); |
| 199 String sign = n < 0 ? "-" : ""; | 207 String sign = n < 0 ? "-" : ""; |
| 200 if (absN >= 1000) return "$n"; | 208 if (absN >= 1000) return "$n"; |
| 201 if (absN >= 100) return "${sign}0$absN"; | 209 if (absN >= 100) return "${sign}0$absN"; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 223 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | 231 return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| 224 } else { | 232 } else { |
| 225 return "$y-$m-$d $h:$min:$sec.$ms"; | 233 return "$y-$m-$d $h:$min:$sec.$ms"; |
| 226 } | 234 } |
| 227 } | 235 } |
| 228 | 236 |
| 229 // TODO(jimhug): Why not use operators here? | 237 // TODO(jimhug): Why not use operators here? |
| 230 // Adds the [duration] to this Date instance. | 238 // Adds the [duration] to this Date instance. |
| 231 Date add(Duration duration) { | 239 Date add(Duration duration) { |
| 232 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, | 240 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, |
| 233 timeZone); | 241 timeZone.isUtc); |
| 234 } | 242 } |
| 235 | 243 |
| 236 // Subtracts the [duration] from this Date instance. | 244 // Subtracts the [duration] from this Date instance. |
| 237 Date subtract(Duration duration) { | 245 Date subtract(Duration duration) { |
| 238 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, | 246 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, |
| 239 timeZone); | 247 timeZone.isUtc); |
| 240 } | 248 } |
| 241 | 249 |
| 242 // Returns a [Duration] with the difference of [this] and [other]. | 250 // Returns a [Duration] with the difference of [this] and [other]. |
| 243 Duration difference(Date other) { | 251 Duration difference(Date other) { |
| 244 return new Duration(milliseconds: value - other.value); | 252 return new Duration(milliseconds: value - other.value); |
| 245 } | 253 } |
| 246 | 254 |
| 247 // TODO(floitsch): Use real exception object. | 255 // TODO(floitsch): Use real exception object. |
| 248 static int _valueFromDecomposed(int years, int month, int day, | 256 static int _valueFromDecomposed(int years, int month, int day, |
| 249 int hours, int minutes, int seconds, | 257 int hours, int minutes, int seconds, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 275 } | 283 } |
| 276 return this.date;'''; | 284 return this.date;'''; |
| 277 } | 285 } |
| 278 | 286 |
| 279 // Trivial implementation of TimeZone | 287 // Trivial implementation of TimeZone |
| 280 class TimeZoneImplementation implements TimeZone { | 288 class TimeZoneImplementation implements TimeZone { |
| 281 const TimeZoneImplementation.utc() : this.isUtc = true; | 289 const TimeZoneImplementation.utc() : this.isUtc = true; |
| 282 const TimeZoneImplementation.local() : this.isUtc = false; | 290 const TimeZoneImplementation.local() : this.isUtc = false; |
| 283 | 291 |
| 284 bool operator ==(other) { | 292 bool operator ==(other) { |
| 285 if (!(other is TimeZoneImplementation)) return false; | 293 if (other is !TimeZoneImplementation) return false; |
| 286 return isUtc == other.isUtc; | 294 return isUtc == other.isUtc; |
| 287 } | 295 } |
| 288 | 296 |
| 289 String toString() { | 297 String toString() { |
| 290 if (isUtc) return "TimeZone (UTC)"; | 298 if (isUtc) return "TimeZone (UTC)"; |
| 291 return "TimeZone (Local)"; | 299 return "TimeZone (Local)"; |
| 292 } | 300 } |
| 293 | 301 |
| 294 final bool isUtc; | 302 final bool isUtc; |
| 295 } | 303 } |
| OLD | NEW |