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 // Dart core library. | 4 // Dart core library. |
5 | 5 |
6 // VM implementation of DateImplementation. | 6 // VM implementation of DateImplementation. |
7 patch class DateImplementation { | 7 patch class DateImplementation { |
8 /* patch */ DateImplementation(int year, | 8 /* patch */ DateImplementation(int year, |
9 [int month = 1, | 9 [int month = 1, |
10 int day = 1, | 10 int day = 1, |
11 int hour = 0, | 11 int hour = 0, |
12 int minute = 0, | 12 int minute = 0, |
13 int second = 0, | 13 int second = 0, |
14 int millisecond = 0, | 14 int millisecond = 0, |
15 bool isUtc = false]) | 15 bool isUtc = false]) |
16 : this.isUtc = isUtc, | 16 : this.isUtc = isUtc, |
17 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 17 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
18 year, month, day, hour, minute, second, millisecond, isUtc) { | 18 year, month, day, hour, minute, second, millisecond, isUtc) { |
19 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException(); | 19 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException(); |
20 if (isUtc === null) throw new IllegalArgumentException(); | 20 if (isUtc === null) throw new IllegalArgumentException(); |
21 } | 21 } |
22 | 22 |
23 /* patch */ DateImplementation.now() | 23 /* patch */ DateImplementation.now() |
24 : isUtc = false, | 24 : isUtc = false, |
25 millisecondsSinceEpoch = _getCurrentMs() { | 25 millisecondsSinceEpoch = _getCurrentMs() { |
26 } | 26 } |
27 | 27 |
28 /* patch */ String get timeZoneName() { | 28 /* patch */ String get timeZoneName { |
29 if (isUtc) return "UTC"; | 29 if (isUtc) return "UTC"; |
30 return _timeZoneName(millisecondsSinceEpoch); | 30 return _timeZoneName(millisecondsSinceEpoch); |
31 } | 31 } |
32 | 32 |
33 /* patch */ Duration get timeZoneOffset() { | 33 /* patch */ Duration get timeZoneOffset { |
34 if (isUtc) return new Duration(0); | 34 if (isUtc) return new Duration(0); |
35 int offsetInSeconds = _timeZoneOffsetInSeconds(millisecondsSinceEpoch); | 35 int offsetInSeconds = _timeZoneOffsetInSeconds(millisecondsSinceEpoch); |
36 return new Duration(seconds: offsetInSeconds); | 36 return new Duration(seconds: offsetInSeconds); |
37 } | 37 } |
38 | 38 |
39 /* patch */ int get year() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[0]
; | 39 /* patch */ int get year => _decomposeIntoYearMonthDay(_localDateInUtcMs)[0]; |
40 | 40 |
41 /* patch */ int get month() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[1
]; | 41 /* patch */ int get month => _decomposeIntoYearMonthDay(_localDateInUtcMs)[1]; |
42 | 42 |
43 /* patch */ int get day() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[2]; | 43 /* patch */ int get day => _decomposeIntoYearMonthDay(_localDateInUtcMs)[2]; |
44 | 44 |
45 /* patch */ int get hour() { | 45 /* patch */ int get hour { |
46 int valueInHours = _flooredDivision(_localDateInUtcMs, | 46 int valueInHours = _flooredDivision(_localDateInUtcMs, |
47 Duration.MILLISECONDS_PER_HOUR); | 47 Duration.MILLISECONDS_PER_HOUR); |
48 return valueInHours % Duration.HOURS_PER_DAY; | 48 return valueInHours % Duration.HOURS_PER_DAY; |
49 } | 49 } |
50 | 50 |
51 /* patch */ int get minute() { | 51 /* patch */ int get minute { |
52 int valueInMinutes = _flooredDivision(_localDateInUtcMs, | 52 int valueInMinutes = _flooredDivision(_localDateInUtcMs, |
53 Duration.MILLISECONDS_PER_MINUTE); | 53 Duration.MILLISECONDS_PER_MINUTE); |
54 return valueInMinutes % Duration.MINUTES_PER_HOUR; | 54 return valueInMinutes % Duration.MINUTES_PER_HOUR; |
55 } | 55 } |
56 | 56 |
57 /* patch */ int get second() { | 57 /* patch */ int get second { |
58 // Seconds are unaffected by the timezone the user is in. So we can | 58 // Seconds are unaffected by the timezone the user is in. So we can |
59 // directly use the millisecondsSinceEpoch and not [_localDateInUtcMs]. | 59 // directly use the millisecondsSinceEpoch and not [_localDateInUtcMs]. |
60 int valueInSeconds = | 60 int valueInSeconds = |
61 _flooredDivision(millisecondsSinceEpoch, | 61 _flooredDivision(millisecondsSinceEpoch, |
62 Duration.MILLISECONDS_PER_SECOND); | 62 Duration.MILLISECONDS_PER_SECOND); |
63 return valueInSeconds % Duration.SECONDS_PER_MINUTE; | 63 return valueInSeconds % Duration.SECONDS_PER_MINUTE; |
64 } | 64 } |
65 | 65 |
66 /* patch */ int get millisecond() { | 66 /* patch */ int get millisecond { |
67 // Milliseconds are unaffected by the timezone the user is in. So we can | 67 // Milliseconds are unaffected by the timezone the user is in. So we can |
68 // directly use the value and not the [_localDateInUtcValue]. | 68 // directly use the value and not the [_localDateInUtcValue]. |
69 return millisecondsSinceEpoch % Duration.MILLISECONDS_PER_SECOND; | 69 return millisecondsSinceEpoch % Duration.MILLISECONDS_PER_SECOND; |
70 } | 70 } |
71 | 71 |
72 /** Returns the weekday of [this]. In accordance with ISO 8601 a week | 72 /** Returns the weekday of [this]. In accordance with ISO 8601 a week |
73 * starts with Monday. Monday has the value 1 up to Sunday with 7. */ | 73 * starts with Monday. Monday has the value 1 up to Sunday with 7. */ |
74 /* patch */ int get weekday() { | 74 /* patch */ int get weekday { |
75 int daysSince1970 = | 75 int daysSince1970 = |
76 _flooredDivision(_localDateInUtcMs, Duration.MILLISECONDS_PER_DAY); | 76 _flooredDivision(_localDateInUtcMs, Duration.MILLISECONDS_PER_DAY); |
77 // 1970-1-1 was a Thursday. | 77 // 1970-1-1 was a Thursday. |
78 return ((daysSince1970 + Date.THU - Date.MON) % Date.DAYS_IN_WEEK) + | 78 return ((daysSince1970 + Date.THU - Date.MON) % Date.DAYS_IN_WEEK) + |
79 Date.MON; | 79 Date.MON; |
80 } | 80 } |
81 | 81 |
82 | 82 |
83 /** The first list contains the days until each month in non-leap years. The | 83 /** The first list contains the days until each month in non-leap years. The |
84 * second list contains the days in leap years. */ | 84 * second list contains the days in leap years. */ |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 * * [:this.year == new Date.fromMillisecondsSinceEpoch(t, true).year:], | 143 * * [:this.year == new Date.fromMillisecondsSinceEpoch(t, true).year:], |
144 * * [:this.month == new Date.fromMillisecondsSinceEpoch(t, true).month:], | 144 * * [:this.month == new Date.fromMillisecondsSinceEpoch(t, true).month:], |
145 * * [:this.day == new Date.fromMillisecondsSinceEpoch(t, true).day:], | 145 * * [:this.day == new Date.fromMillisecondsSinceEpoch(t, true).day:], |
146 * * [:this.hour == new Date.fromMillisecondsSinceEpoch(t, true).hour:], | 146 * * [:this.hour == new Date.fromMillisecondsSinceEpoch(t, true).hour:], |
147 * * ... | 147 * * ... |
148 * | 148 * |
149 * Daylight savings is computed as if the date was computed in [1970..2037]. | 149 * Daylight savings is computed as if the date was computed in [1970..2037]. |
150 * If [this] lies outside this range then it is a year with similar | 150 * If [this] lies outside this range then it is a year with similar |
151 * properties (leap year, weekdays) is used instead. | 151 * properties (leap year, weekdays) is used instead. |
152 */ | 152 */ |
153 int get _localDateInUtcMs() { | 153 int get _localDateInUtcMs { |
154 int ms = millisecondsSinceEpoch; | 154 int ms = millisecondsSinceEpoch; |
155 if (isUtc) return ms; | 155 if (isUtc) return ms; |
156 int offset = | 156 int offset = |
157 _timeZoneOffsetInSeconds(ms) * Duration.MILLISECONDS_PER_SECOND; | 157 _timeZoneOffsetInSeconds(ms) * Duration.MILLISECONDS_PER_SECOND; |
158 return ms + offset; | 158 return ms + offset; |
159 } | 159 } |
160 | 160 |
161 static int _flooredDivision(int a, int b) { | 161 static int _flooredDivision(int a, int b) { |
162 return (a - (a < 0 ? b - 1 : 0)) ~/ b; | 162 return (a - (a < 0 ? b - 1 : 0)) ~/ b; |
163 } | 163 } |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 | 315 |
316 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) | 316 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) |
317 native "DateNatives_timeZoneName"; | 317 native "DateNatives_timeZoneName"; |
318 | 318 |
319 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) | 319 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) |
320 native "DateNatives_timeZoneOffsetInSeconds"; | 320 native "DateNatives_timeZoneOffsetInSeconds"; |
321 | 321 |
322 static int _localTimeZoneAdjustmentInSeconds() | 322 static int _localTimeZoneAdjustmentInSeconds() |
323 native "DateNatives_localTimeZoneAdjustmentInSeconds"; | 323 native "DateNatives_localTimeZoneAdjustmentInSeconds"; |
324 } | 324 } |
OLD | NEW |