| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class DateImplementation implements Date { | |
| 6 final int millisecondsSinceEpoch; | |
| 7 final bool isUtc; | |
| 8 | |
| 9 factory DateImplementation.fromString(String formattedString) { | |
| 10 // Read in (a subset of) ISO 8601. | |
| 11 // Examples: | |
| 12 // - "2012-02-27 13:27:00" | |
| 13 // - "2012-02-27 13:27:00.423z" | |
| 14 // - "20120227 13:27:00" | |
| 15 // - "20120227T132700" | |
| 16 // - "20120227" | |
| 17 // - "2012-02-27T14Z" | |
| 18 // - "-123450101 00:00:00 Z" // In the year -12345. | |
| 19 final RegExp re = const RegExp( | |
| 20 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. | |
| 21 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); | |
| 22 Match match = re.firstMatch(formattedString); | |
| 23 if (match !== null) { | |
| 24 int parseIntOrZero(String matched) { | |
| 25 // TODO(floitsch): we should not need to test against the empty string. | |
| 26 if (matched === null || matched == "") return 0; | |
| 27 return Math.parseInt(matched); | |
| 28 } | |
| 29 | |
| 30 double parseDoubleOrZero(String matched) { | |
| 31 // TODO(floitsch): we should not need to test against the empty string. | |
| 32 if (matched === null || matched == "") return 0.0; | |
| 33 return Math.parseDouble(matched); | |
| 34 } | |
| 35 | |
| 36 int years = Math.parseInt(match[1]); | |
| 37 int month = Math.parseInt(match[2]); | |
| 38 int day = Math.parseInt(match[3]); | |
| 39 int hour = parseIntOrZero(match[4]); | |
| 40 int minute = parseIntOrZero(match[5]); | |
| 41 int second = parseIntOrZero(match[6]); | |
| 42 bool addOneMillisecond = false; | |
| 43 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); | |
| 44 if (millisecond == 1000) { | |
| 45 addOneMillisecond = true; | |
| 46 millisecond = 999; | |
| 47 } | |
| 48 // TODO(floitsch): we should not need to test against the empty string. | |
| 49 bool isUtc = (match[8] !== null) && (match[8] != ""); | |
| 50 int millisecondsSinceEpoch = Primitives.valueFromDecomposedDate( | |
| 51 years, month, day, hour, minute, second, millisecond, isUtc); | |
| 52 if (millisecondsSinceEpoch === null) { | |
| 53 throw new IllegalArgumentException(formattedString); | |
| 54 } | |
| 55 if (addOneMillisecond) millisecondsSinceEpoch++; | |
| 56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc); | |
| 57 } else { | |
| 58 throw new IllegalArgumentException(formattedString); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 static final int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; | |
| 63 | |
| 64 DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, | |
| 65 [this.isUtc = false]) { | |
| 66 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | |
| 67 throw new IllegalArgumentException(millisecondsSinceEpoch); | |
| 68 } | |
| 69 if (isUtc === null) throw new IllegalArgumentException(isUtc); | |
| 70 } | |
| 71 | |
| 72 bool operator ==(other) { | |
| 73 if (!(other is Date)) return false; | |
| 74 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch); | |
| 75 } | |
| 76 | |
| 77 bool operator <(Date other) | |
| 78 => millisecondsSinceEpoch < other.millisecondsSinceEpoch; | |
| 79 | |
| 80 bool operator <=(Date other) | |
| 81 => millisecondsSinceEpoch <= other.millisecondsSinceEpoch; | |
| 82 | |
| 83 bool operator >(Date other) | |
| 84 => millisecondsSinceEpoch > other.millisecondsSinceEpoch; | |
| 85 | |
| 86 bool operator >=(Date other) | |
| 87 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch; | |
| 88 | |
| 89 int compareTo(Date other) | |
| 90 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch); | |
| 91 | |
| 92 int hashCode() => millisecondsSinceEpoch; | |
| 93 | |
| 94 Date toLocal() { | |
| 95 if (isUtc) { | |
| 96 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, false); | |
| 97 } | |
| 98 return this; | |
| 99 } | |
| 100 | |
| 101 Date toUtc() { | |
| 102 if (isUtc) return this; | |
| 103 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, true); | |
| 104 } | |
| 105 | |
| 106 String toString() { | |
| 107 String fourDigits(int n) { | |
| 108 int absN = n.abs(); | |
| 109 String sign = n < 0 ? "-" : ""; | |
| 110 if (absN >= 1000) return "$n"; | |
| 111 if (absN >= 100) return "${sign}0$absN"; | |
| 112 if (absN >= 10) return "${sign}00$absN"; | |
| 113 return "${sign}000$absN"; | |
| 114 } | |
| 115 | |
| 116 String threeDigits(int n) { | |
| 117 if (n >= 100) return "${n}"; | |
| 118 if (n >= 10) return "0${n}"; | |
| 119 return "00${n}"; | |
| 120 } | |
| 121 | |
| 122 String twoDigits(int n) { | |
| 123 if (n >= 10) return "${n}"; | |
| 124 return "0${n}"; | |
| 125 } | |
| 126 | |
| 127 String y = fourDigits(year); | |
| 128 String m = twoDigits(month); | |
| 129 String d = twoDigits(day); | |
| 130 String h = twoDigits(hour); | |
| 131 String min = twoDigits(minute); | |
| 132 String sec = twoDigits(second); | |
| 133 String ms = threeDigits(millisecond); | |
| 134 if (isUtc) { | |
| 135 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | |
| 136 } else { | |
| 137 return "$y-$m-$d $h:$min:$sec.$ms"; | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 // Adds the [duration] to this Date instance. | |
| 142 Date add(Duration duration) { | |
| 143 int ms = millisecondsSinceEpoch; | |
| 144 return new Date.fromMillisecondsSinceEpoch( | |
| 145 ms + duration.inMilliseconds, isUtc); | |
| 146 } | |
| 147 | |
| 148 // Subtracts the [duration] from this Date instance. | |
| 149 Date subtract(Duration duration) { | |
| 150 int ms = millisecondsSinceEpoch; | |
| 151 return new Date.fromMillisecondsSinceEpoch( | |
| 152 ms - duration.inMilliseconds, isUtc); | |
| 153 } | |
| 154 | |
| 155 // Returns a [Duration] with the difference of [this] and [other]. | |
| 156 Duration difference(Date other) { | |
| 157 int ms = millisecondsSinceEpoch; | |
| 158 int otherMs = other.millisecondsSinceEpoch; | |
| 159 return new Duration(milliseconds: ms - otherMs); | |
| 160 } | |
| 161 | |
| 162 external DateImplementation(int year, | |
| 163 [int month, | |
| 164 int day, | |
| 165 int hour, | |
| 166 int minute, | |
| 167 int second, | |
| 168 int millisecond, | |
| 169 bool isUtc]); | |
| 170 external DateImplementation.now(); | |
| 171 external String get timeZoneName(); | |
| 172 external Duration get timeZoneOffset(); | |
| 173 external int get year(); | |
| 174 external int get month(); | |
| 175 external int get day(); | |
| 176 external int get hour(); | |
| 177 external int get minute(); | |
| 178 external int get second(); | |
| 179 external int get millisecond(); | |
| 180 external int get weekday(); | |
| 181 } | |
| OLD | NEW |