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 // 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( |
|
ngeoffray
2012/05/22 08:58:45
If that one is deprecated, maybe don't use it.
floitsch
2012/05/22 14:50:38
This is the frog-implementation of the library. Gi
| |
| 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; |
|
ngeoffray
2012/05/22 08:58:45
other is !DateImplementation
floitsch
2012/05/22 14:50:38
Done.
| |
| 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()); | |
|
ngeoffray
2012/05/22 08:58:45
Don't use the deprecated method?
floitsch
2012/05/22 14:50:38
ditto.
| |
| 128 return this; | |
| 129 } | |
| 130 | |
| 131 Date toUtc() { | |
| 132 if (isUtc()) return this; | |
| 133 return changeTimeZone(const TimeZoneImplementation.utc()); | |
|
ngeoffray
2012/05/22 08:58:45
ditto
floitsch
2012/05/22 14:50:38
ditto.
| |
| 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 print(targetTimeZone.isUtc); |
|
ngeoffray
2012/05/22 08:58:45
Remove print
floitsch
2012/05/22 14:50:38
ouch.
done.
| |
| 141 return new Date.fromEpoch(value, targetTimeZone.isUtc); | |
| 128 } | 142 } |
| 129 | 143 |
| 130 String get timeZoneName() { throw "Unimplemented"; } | 144 String get timeZoneName() { throw "Unimplemented"; } |
| 131 Duration get timeZoneOffset() { throw "Unimplemented"; } | 145 Duration get timeZoneOffset() { throw "Unimplemented"; } |
| 132 | 146 |
| 133 int get year() native | 147 int get year() native |
| 134 '''return this.isUtc() ? this._asJs().getUTCFullYear() : | 148 '''return this.isUtc() ? this._asJs().getUTCFullYear() : |
| 135 this._asJs().getFullYear();''' { | 149 this._asJs().getFullYear();''' { |
| 136 isUtc(); | 150 isUtc(); |
| 137 _asJs(); | 151 _asJs(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 this._asJs().getMilliseconds();''' { | 191 this._asJs().getMilliseconds();''' { |
| 178 isUtc(); | 192 isUtc(); |
| 179 _asJs(); | 193 _asJs(); |
| 180 } | 194 } |
| 181 | 195 |
| 182 // Adjust by one because JS weeks start on Sunday. | 196 // Adjust by one because JS weeks start on Sunday. |
| 183 int get weekday() native ''' | 197 int get weekday() native ''' |
| 184 var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay(); | 198 var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay(); |
| 185 return (day + 6) % 7;'''; | 199 return (day + 6) % 7;'''; |
| 186 | 200 |
| 187 // TODO(jimhug): Could this please be getters? | |
| 188 bool isLocalTime() { | |
| 189 return !timeZone.isUtc; | |
| 190 } | |
| 191 | |
| 192 bool isUtc() { | 201 bool isUtc() { |
| 193 return timeZone.isUtc; | 202 return timeZone.isUtc; |
| 194 } | 203 } |
| 195 | 204 |
| 196 String toString() { | 205 String toString() { |
| 197 String fourDigits(int n) { | 206 String fourDigits(int n) { |
| 198 int absN = n.abs(); | 207 int absN = n.abs(); |
| 199 String sign = n < 0 ? "-" : ""; | 208 String sign = n < 0 ? "-" : ""; |
| 200 if (absN >= 1000) return "$n"; | 209 if (absN >= 1000) return "$n"; |
| 201 if (absN >= 100) return "${sign}0$absN"; | 210 if (absN >= 100) return "${sign}0$absN"; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 223 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | 232 return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| 224 } else { | 233 } else { |
| 225 return "$y-$m-$d $h:$min:$sec.$ms"; | 234 return "$y-$m-$d $h:$min:$sec.$ms"; |
| 226 } | 235 } |
| 227 } | 236 } |
| 228 | 237 |
| 229 // TODO(jimhug): Why not use operators here? | 238 // TODO(jimhug): Why not use operators here? |
| 230 // Adds the [duration] to this Date instance. | 239 // Adds the [duration] to this Date instance. |
| 231 Date add(Duration duration) { | 240 Date add(Duration duration) { |
| 232 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, | 241 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, |
| 233 timeZone); | 242 timeZone.isUtc); |
| 234 } | 243 } |
| 235 | 244 |
| 236 // Subtracts the [duration] from this Date instance. | 245 // Subtracts the [duration] from this Date instance. |
| 237 Date subtract(Duration duration) { | 246 Date subtract(Duration duration) { |
| 238 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, | 247 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, |
| 239 timeZone); | 248 timeZone.isUtc); |
| 240 } | 249 } |
| 241 | 250 |
| 242 // Returns a [Duration] with the difference of [this] and [other]. | 251 // Returns a [Duration] with the difference of [this] and [other]. |
| 243 Duration difference(Date other) { | 252 Duration difference(Date other) { |
| 244 return new Duration(milliseconds: value - other.value); | 253 return new Duration(milliseconds: value - other.value); |
| 245 } | 254 } |
| 246 | 255 |
| 247 // TODO(floitsch): Use real exception object. | 256 // TODO(floitsch): Use real exception object. |
| 248 static int _valueFromDecomposed(int years, int month, int day, | 257 static int _valueFromDecomposed(int years, int month, int day, |
| 249 int hours, int minutes, int seconds, | 258 int hours, int minutes, int seconds, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 return isUtc == other.isUtc; | 295 return isUtc == other.isUtc; |
| 287 } | 296 } |
| 288 | 297 |
| 289 String toString() { | 298 String toString() { |
| 290 if (isUtc) return "TimeZone (UTC)"; | 299 if (isUtc) return "TimeZone (UTC)"; |
| 291 return "TimeZone (Local)"; | 300 return "TimeZone (Local)"; |
| 292 } | 301 } |
| 293 | 302 |
| 294 final bool isUtc; | 303 final bool isUtc; |
| 295 } | 304 } |
| OLD | NEW |