| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Dart core library. |
| 6 |
| 7 // JavaScript implementation of DateImplementation. |
| 8 class DateImplementation implements Date { |
| 9 factory DateImplementation(int years, |
| 10 int month, |
| 11 int day, |
| 12 int hours, |
| 13 int minutes, |
| 14 int seconds, |
| 15 int milliseconds) { |
| 16 return new DateImplementation.withTimeZone( |
| 17 years, month, day, |
| 18 hours, minutes, seconds, milliseconds, |
| 19 new TimeZoneImplementation.local()); |
| 20 } |
| 21 |
| 22 DateImplementation.withTimeZone(int years, |
| 23 int month, |
| 24 int day, |
| 25 int hours, |
| 26 int minutes, |
| 27 int seconds, |
| 28 int milliseconds, |
| 29 TimeZone timeZone) |
| 30 : this.timeZone = timeZone, |
| 31 value = _valueFromDecomposed(years, month, day, |
| 32 hours, minutes, seconds, milliseconds, |
| 33 timeZone.isUtc) { |
| 34 } |
| 35 |
| 36 DateImplementation.now() |
| 37 : timeZone = new TimeZone.local(), |
| 38 value = _now() { |
| 39 } |
| 40 |
| 41 DateImplementation.fromString(String formattedString) |
| 42 : timeZone = new TimeZone.local(), |
| 43 value = _valueFromString(formattedString) { |
| 44 } |
| 45 |
| 46 const DateImplementation.fromEpoch(int this.value, TimeZone this.timeZone); |
| 47 |
| 48 bool operator ==(other) { |
| 49 if (!(other is DateImplementation)) return false; |
| 50 return (value == other.value) && (timeZone == other.timeZone); |
| 51 } |
| 52 |
| 53 int compareTo(Date other) { |
| 54 return value.compareTo(other.value); |
| 55 } |
| 56 |
| 57 Date changeTimeZone(TimeZone targetTimeZone) { |
| 58 if (targetTimeZone == null) { |
| 59 targetTimeZone = new TimeZoneImplementation.local(); |
| 60 } |
| 61 return new Date.fromEpoch(value, targetTimeZone); |
| 62 } |
| 63 |
| 64 int get year() { |
| 65 return _getYear(value, isUtc()); |
| 66 } |
| 67 |
| 68 int get month() { |
| 69 return _getMonth(value, isUtc()); |
| 70 } |
| 71 |
| 72 int get day() { |
| 73 return _getDay(value, isUtc()); |
| 74 } |
| 75 |
| 76 int get hours() { |
| 77 return _getHours(value, isUtc()); |
| 78 } |
| 79 |
| 80 int get minutes() { |
| 81 return _getMinutes(value, isUtc()); |
| 82 } |
| 83 |
| 84 int get seconds() { |
| 85 return _getSeconds(value, isUtc()); |
| 86 } |
| 87 |
| 88 int get milliseconds() { |
| 89 return _getMilliseconds(value, isUtc()); |
| 90 } |
| 91 |
| 92 int get weekday() { |
| 93 final Date unixTimeStart = |
| 94 new Date.withTimeZone(1970, 1, 1, 0, 0, 0, 0, timeZone); |
| 95 int msSince1970 = this.difference(unixTimeStart).inMilliseconds; |
| 96 // Adjust the milliseconds to avoid problems with summer-time. |
| 97 if (hours < 2) { |
| 98 msSince1970 += 2 * Duration.MILLISECONDS_PER_HOUR; |
| 99 } |
| 100 int daysSince1970 = |
| 101 (msSince1970 / Duration.MILLISECONDS_PER_DAY).floor().toInt(); |
| 102 // 1970-1-1 was a Thursday. |
| 103 return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); |
| 104 } |
| 105 |
| 106 bool isLocalTime() { |
| 107 return !timeZone.isUtc; |
| 108 } |
| 109 |
| 110 bool isUtc() { |
| 111 return timeZone.isUtc; |
| 112 } |
| 113 |
| 114 String toString() { |
| 115 String threeDigits(int n) { |
| 116 if (n >= 100) return "${n}"; |
| 117 if (n > 10) return "0${n}"; |
| 118 return "00${n}"; |
| 119 } |
| 120 String twoDigits(int n) { |
| 121 if (n >= 10) return "${n}"; |
| 122 return "0${n}"; |
| 123 } |
| 124 |
| 125 String m = twoDigits(month); |
| 126 String d = twoDigits(day); |
| 127 String h = twoDigits(hours); |
| 128 String min = twoDigits(minutes); |
| 129 String sec = twoDigits(seconds); |
| 130 String ms = threeDigits(milliseconds); |
| 131 if (timeZone.isUtc) { |
| 132 return "$year-$m-$d $h:$min:$sec.${ms}Z"; |
| 133 } else { |
| 134 return "$year-$m-$d $h:$min:$sec.$ms"; |
| 135 } |
| 136 } |
| 137 |
| 138 // Adds the [duration] to this Date instance. |
| 139 Date add(Duration duration) { |
| 140 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, |
| 141 timeZone); |
| 142 } |
| 143 |
| 144 // Subtracts the [duration] from this Date instance. |
| 145 Date subtract(Duration duration) { |
| 146 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, |
| 147 timeZone); |
| 148 } |
| 149 |
| 150 // Returns a [Duration] with the difference of [this] and [other]. |
| 151 Duration difference(Date other) { |
| 152 return new Duration(milliseconds: value - other.value); |
| 153 } |
| 154 |
| 155 final int value; |
| 156 final TimeZoneImplementation timeZone; |
| 157 |
| 158 static int _valueFromDecomposed(int years, int month, int day, |
| 159 int hours, int minutes, int seconds, |
| 160 int milliseconds, bool isUtc) native; |
| 161 static int _valueFromString(String str) native; |
| 162 static int _now() native; |
| 163 int _getYear(int value, bool isUtc) native; |
| 164 int _getMonth(int value, bool isUtc) native; |
| 165 int _getDay(int value, bool isUtc) native; |
| 166 int _getHours(int value, bool isUtc) native; |
| 167 int _getMinutes(int value, bool isUtc) native; |
| 168 int _getSeconds(int value, bool isUtc) native; |
| 169 int _getMilliseconds(int value, bool isUtc) native; |
| 170 } |
| OLD | NEW |