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 TimeZoneImplementation timeZone; | 9 final TimeZoneImplementation timeZone; |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 timeZone.isUtc) { | 35 timeZone.isUtc) { |
36 _asJs(); | 36 _asJs(); |
37 } | 37 } |
38 | 38 |
39 DateImplementation.now() | 39 DateImplementation.now() |
40 : timeZone = new TimeZone.local(), | 40 : timeZone = new TimeZone.local(), |
41 value = _now() { | 41 value = _now() { |
42 _asJs(); | 42 _asJs(); |
43 } | 43 } |
44 | 44 |
45 DateImplementation.fromString(String formattedString) | 45 factory DateImplementation.fromString(String formattedString) { |
46 : timeZone = new TimeZone.local(), | 46 // Always try to read in (a subset of) ISO 8601 first. If that fails fall |
47 value = _valueFromString(formattedString) { | 47 // back to JavaScript's implementation. |
48 _asJs(); | 48 final RegExp re = |
| 49 const RegExp(@'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d) (\d\d):(\d\d):(\d\d)(
?:.(\d{1,3}))? ?([zZ]?)$'); |
| 50 Match match = re.firstMatch(formattedString); |
| 51 if (match !== null) { |
| 52 int years = Math.parseInt(match[1]); |
| 53 int month = Math.parseInt(match[2]); |
| 54 int day = Math.parseInt(match[3]); |
| 55 int hours = Math.parseInt(match[4]); |
| 56 int minutes = Math.parseInt(match[5]); |
| 57 int seconds = Math.parseInt(match[6]); |
| 58 int milliseconds = 0; |
| 59 if (match[7] !== null) { |
| 60 milliseconds = Math.parseInt(match[7]); |
| 61 if (match[7].length == 1) { |
| 62 milliseconds *= 100; |
| 63 } else if (match[7].length == 2) { |
| 64 milliseconds *= 10; |
| 65 } else { |
| 66 assert(match[7].length == 3); |
| 67 } |
| 68 } |
| 69 bool isUtc = match[8] !== null; |
| 70 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); |
| 71 return new DateImplementation.withTimeZone( |
| 72 years, month, day, hours, minutes, seconds, milliseconds, timezone); |
| 73 } else { |
| 74 return new DateImplementation.fromEpoch(formattedString, |
| 75 new TimeZone.local()); |
| 76 } |
49 } | 77 } |
50 | 78 |
51 const DateImplementation.fromEpoch(this.value, this.timeZone); | 79 const DateImplementation.fromEpoch(this.value, this.timeZone); |
52 | 80 |
53 bool operator ==(other) { | 81 bool operator ==(other) { |
54 if (!(other is DateImplementation)) return false; | 82 if (!(other is DateImplementation)) return false; |
55 return (value == other.value) && (timeZone == other.timeZone); | 83 return (value == other.value) && (timeZone == other.timeZone); |
56 } | 84 } |
57 | 85 |
58 int compareTo(Date other) { | 86 int compareTo(Date other) { |
59 return value.compareTo(other.value); | 87 return value.compareTo(other.value); |
60 } | 88 } |
61 | 89 |
62 Date changeTimeZone(TimeZone targetTimeZone) { | 90 Date changeTimeZone(TimeZone targetTimeZone) { |
63 if (targetTimeZone == null) { | 91 if (targetTimeZone == null) { |
64 targetTimeZone = new TimeZoneImplementation.local(); | 92 targetTimeZone = new TimeZoneImplementation.local(); |
65 } | 93 } |
66 return new Date.fromEpoch(value, targetTimeZone); | 94 return new Date.fromEpoch(value, targetTimeZone); |
67 } | 95 } |
68 | 96 |
69 int get year() native | 97 int get year() native |
70 '''return this.isUtc ? this._asJs().getUTCFullYear() : | 98 '''return this.isUtc() ? this._asJs().getUTCFullYear() : |
71 this._asJs().getFullYear();'''; | 99 this._asJs().getFullYear();''' { |
| 100 isUtc(); |
| 101 _asJs(); |
| 102 } |
72 | 103 |
73 int get month() native | 104 int get month() native |
74 '''return this.isUtc ? this._asJs().getMonth() + 1 : | 105 '''return this.isUtc() ? this._asJs().getUTCMonth() + 1 : |
75 this._asJs().getMonth() + 1;'''; | 106 this._asJs().getMonth() + 1;''' { |
| 107 isUtc(); |
| 108 _asJs(); |
| 109 } |
76 | 110 |
77 int get day() native | 111 int get day() native |
78 'return this.isUtc ? this._asJs().getUTCDate() : this._asJs().getDate()'; | 112 '''return this.isUtc() ? this._asJs().getUTCDate() : |
| 113 this._asJs().getDate();''' { |
| 114 isUtc(); |
| 115 _asJs(); |
| 116 } |
79 | 117 |
80 int get hours() native | 118 int get hours() native |
81 'return this.isUtc ? this._asJs().getUTCHours() : this._asJs().getHours()'; | 119 '''return this.isUtc() ? this._asJs().getUTCHours() : |
| 120 this._asJs().getHours();''' { |
| 121 isUtc(); |
| 122 _asJs(); |
| 123 } |
82 | 124 |
83 int get minutes() native | 125 int get minutes() native |
84 'return this.isUtc ? this._asJs().getUTCMinutes() : this._asJs().getMinutes(
)'; | 126 '''return this.isUtc() ? this._asJs().getUTCMinutes() : |
| 127 this._asJs().getMinutes();''' { |
| 128 isUtc(); |
| 129 _asJs(); |
| 130 } |
85 | 131 |
86 int get seconds() native | 132 int get seconds() native |
87 'return this.isUtc ? this._asJs().getUTCSeconds() : this._asJs().getSeconds(
)'; | 133 '''return this.isUtc() ? this._asJs().getUTCSeconds() : |
| 134 this._asJs().getSeconds();''' { |
| 135 isUtc(); |
| 136 _asJs(); |
| 137 } |
88 | 138 |
89 int get milliseconds() native | 139 int get milliseconds() native |
90 '''return this.isUtc ? this._asJs().getUTCMilliseconds() : | 140 '''return this.isUtc() ? this._asJs().getUTCMilliseconds() : |
91 this._asJs().getMilliseconds();'''; | 141 this._asJs().getMilliseconds();''' { |
| 142 isUtc(); |
| 143 _asJs(); |
| 144 } |
92 | 145 |
93 // Adjust by one because JS weeks start on Sunday. | 146 // Adjust by one because JS weeks start on Sunday. |
94 int get weekday() native ''' | 147 int get weekday() native ''' |
95 var day = this.isUtc ? this._asJs().getUTCDay() : this._asJs().getDay(); | 148 var day = this.isUtc() ? this._asJs().getUTCDay() : this._asJs().getDay(); |
96 return (day + 6) % 7;'''; | 149 return (day + 6) % 7;'''; |
97 | 150 |
98 // TODO(jimhug): Could this please be getters? | 151 // TODO(jimhug): Could this please be getters? |
99 bool isLocalTime() { | 152 bool isLocalTime() { |
100 return !timeZone.isUtc; | 153 return !timeZone.isUtc; |
101 } | 154 } |
102 | 155 |
103 bool isUtc() { | 156 bool isUtc() { |
104 return timeZone.isUtc; | 157 return timeZone.isUtc; |
105 } | 158 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 return isUtc == other.isUtc; | 237 return isUtc == other.isUtc; |
185 } | 238 } |
186 | 239 |
187 String toString() { | 240 String toString() { |
188 if (isUtc) return "TimeZone (UTC)"; | 241 if (isUtc) return "TimeZone (UTC)"; |
189 return "TimeZone (Local)"; | 242 return "TimeZone (Local)"; |
190 } | 243 } |
191 | 244 |
192 final bool isUtc; | 245 final bool isUtc; |
193 } | 246 } |
OLD | NEW |