Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class DateImplementation implements Date { | 5 class DateImplementation implements Date { |
| 6 final int millisecondsSinceEpoch; | 6 final int millisecondsSinceEpoch; |
| 7 final bool isUtc; | 7 final bool isUtc; |
| 8 | 8 |
| 9 factory DateImplementation.fromString(String formattedString) { | 9 factory DateImplementation.fromString(String formattedString) { |
| 10 // Read in (a subset of) ISO 8601. | 10 // Read in (a subset of) ISO 8601. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 millisecond = 999; | 46 millisecond = 999; |
| 47 } | 47 } |
| 48 // TODO(floitsch): we should not need to test against the empty string. | 48 // TODO(floitsch): we should not need to test against the empty string. |
| 49 bool isUtc = (match[8] !== null) && (match[8] != ""); | 49 bool isUtc = (match[8] !== null) && (match[8] != ""); |
| 50 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 50 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
| 51 years, month, day, hour, minute, second, millisecond, isUtc); | 51 years, month, day, hour, minute, second, millisecond, isUtc); |
| 52 if (millisecondsSinceEpoch === null) { | 52 if (millisecondsSinceEpoch === null) { |
| 53 throw new ArgumentError(formattedString); | 53 throw new ArgumentError(formattedString); |
| 54 } | 54 } |
| 55 if (addOneMillisecond) millisecondsSinceEpoch++; | 55 if (addOneMillisecond) millisecondsSinceEpoch++; |
| 56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc); | 56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, |
| 57 isUtc: isUtc); | |
| 57 } else { | 58 } else { |
| 58 throw new ArgumentError(formattedString); | 59 throw new ArgumentError(formattedString); |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 | 62 |
| 62 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; | 63 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; |
| 63 | 64 |
| 64 DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, | 65 DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, |
| 65 this.isUtc) { | 66 this.isUtc) { |
| 66 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | 67 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 86 bool operator >=(Date other) | 87 bool operator >=(Date other) |
| 87 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch; | 88 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch; |
| 88 | 89 |
| 89 int compareTo(Date other) | 90 int compareTo(Date other) |
| 90 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch); | 91 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch); |
| 91 | 92 |
| 92 int hashCode() => millisecondsSinceEpoch; | 93 int hashCode() => millisecondsSinceEpoch; |
| 93 | 94 |
| 94 Date toLocal() { | 95 Date toLocal() { |
| 95 if (isUtc) { | 96 if (isUtc) { |
| 96 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, false); | 97 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, |
| 98 isUtc: false); | |
| 97 } | 99 } |
| 98 return this; | 100 return this; |
| 99 } | 101 } |
| 100 | 102 |
| 101 Date toUtc() { | 103 Date toUtc() { |
| 102 if (isUtc) return this; | 104 if (isUtc) return this; |
| 103 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, true); | 105 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, |
| 106 isUtc: true); | |
| 104 } | 107 } |
| 105 | 108 |
| 106 String toString() { | 109 String toString() { |
| 107 String fourDigits(int n) { | 110 String fourDigits(int n) { |
| 108 int absN = n.abs(); | 111 int absN = n.abs(); |
| 109 String sign = n < 0 ? "-" : ""; | 112 String sign = n < 0 ? "-" : ""; |
| 110 if (absN >= 1000) return "$n"; | 113 if (absN >= 1000) return "$n"; |
| 111 if (absN >= 100) return "${sign}0$absN"; | 114 if (absN >= 100) return "${sign}0$absN"; |
| 112 if (absN >= 10) return "${sign}00$absN"; | 115 if (absN >= 10) return "${sign}00$absN"; |
| 113 return "${sign}000$absN"; | 116 return "${sign}000$absN"; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 135 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | 138 return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| 136 } else { | 139 } else { |
| 137 return "$y-$m-$d $h:$min:$sec.$ms"; | 140 return "$y-$m-$d $h:$min:$sec.$ms"; |
| 138 } | 141 } |
| 139 } | 142 } |
| 140 | 143 |
| 141 /** Returns a new [Date] with the [duration] added to [this]. */ | 144 /** Returns a new [Date] with the [duration] added to [this]. */ |
| 142 Date add(Duration duration) { | 145 Date add(Duration duration) { |
| 143 int ms = millisecondsSinceEpoch; | 146 int ms = millisecondsSinceEpoch; |
| 144 return new Date.fromMillisecondsSinceEpoch( | 147 return new Date.fromMillisecondsSinceEpoch( |
| 145 ms + duration.inMilliseconds, isUtc); | 148 ms + duration.inMilliseconds, isUtc: isUtc); |
| 146 } | 149 } |
| 147 | 150 |
| 148 /** Returns a new [Date] with the [duration] subtracted from [this]. */ | 151 /** Returns a new [Date] with the [duration] subtracted from [this]. */ |
| 149 Date subtract(Duration duration) { | 152 Date subtract(Duration duration) { |
| 150 int ms = millisecondsSinceEpoch; | 153 int ms = millisecondsSinceEpoch; |
| 151 return new Date.fromMillisecondsSinceEpoch( | 154 return new Date.fromMillisecondsSinceEpoch( |
| 152 ms - duration.inMilliseconds, isUtc); | 155 ms - duration.inMilliseconds, isUtc: isUtc); |
| 153 } | 156 } |
| 154 | 157 |
| 155 /** Returns a [Duration] with the difference of [this] and [other]. */ | 158 /** Returns a [Duration] with the difference of [this] and [other]. */ |
| 156 Duration difference(Date other) { | 159 Duration difference(Date other) { |
| 157 int ms = millisecondsSinceEpoch; | 160 int ms = millisecondsSinceEpoch; |
| 158 int otherMs = other.millisecondsSinceEpoch; | 161 int otherMs = other.millisecondsSinceEpoch; |
| 159 return new Duration(milliseconds: ms - otherMs); | 162 return new Duration(milliseconds: ms - otherMs); |
| 160 } | 163 } |
| 161 | 164 |
| 162 // TODO(lrn): Make parameters not optional for the implementation class. | 165 // TODO(lrn): Make parameters not optional for the implementation class. |
|
Lasse Reichstein Nielsen
2012/10/09 12:35:00
Do the TODO :)
regis
2012/10/11 01:33:56
Done.
| |
| 163 external DateImplementation(int year, | 166 external DateImplementation(int year, |
| 164 [int month = 1, | 167 {int month: 1, |
| 165 int day = 1, | 168 int day: 1, |
| 166 int hour = 0, | 169 int hour: 0, |
| 167 int minute = 0, | 170 int minute: 0, |
| 168 int second = 0, | 171 int second: 0, |
| 169 int millisecond = 0, | 172 int millisecond: 0, |
| 170 bool isUtc = false]); | 173 bool isUtc: false}); |
| 171 external DateImplementation.now(); | 174 external DateImplementation.now(); |
| 172 external static int _brokenDownDateToMillisecondsSinceEpoch( | 175 external static int _brokenDownDateToMillisecondsSinceEpoch( |
| 173 int year, int month, int day, int hour, int minute, int second, | 176 int year, int month, int day, int hour, int minute, int second, |
| 174 int millisecond, bool isUtc); | 177 int millisecond, bool isUtc); |
| 175 external String get timeZoneName; | 178 external String get timeZoneName; |
| 176 external Duration get timeZoneOffset; | 179 external Duration get timeZoneOffset; |
| 177 external int get year; | 180 external int get year; |
| 178 external int get month; | 181 external int get month; |
| 179 external int get day; | 182 external int get day; |
| 180 external int get hour; | 183 external int get hour; |
| 181 external int get minute; | 184 external int get minute; |
| 182 external int get second; | 185 external int get second; |
| 183 external int get millisecond; | 186 external int get millisecond; |
| 184 external int get weekday; | 187 external int get weekday; |
| 185 } | 188 } |
| OLD | NEW |