OLD | NEW |
| (Empty) |
1 part of dart.core; | |
2 class DateTime implements Comparable {static const int MONDAY = 1; | |
3 static const int TUESDAY = 2; | |
4 static const int WEDNESDAY = 3; | |
5 static const int THURSDAY = 4; | |
6 static const int FRIDAY = 5; | |
7 static const int SATURDAY = 6; | |
8 static const int SUNDAY = 7; | |
9 static const int DAYS_PER_WEEK = 7; | |
10 static const int JANUARY = 1; | |
11 static const int FEBRUARY = 2; | |
12 static const int MARCH = 3; | |
13 static const int APRIL = 4; | |
14 static const int MAY = 5; | |
15 static const int JUNE = 6; | |
16 static const int JULY = 7; | |
17 static const int AUGUST = 8; | |
18 static const int SEPTEMBER = 9; | |
19 static const int OCTOBER = 10; | |
20 static const int NOVEMBER = 11; | |
21 static const int DECEMBER = 12; | |
22 static const int MONTHS_PER_YEAR = 12; | |
23 final int millisecondsSinceEpoch; | |
24 final bool isUtc; | |
25 DateTime(int year, [int month = 1, int day = 1, int hour = 0, int minute = 0, i
nt second = 0, int millisecond = 0]) : this._internal(year, month, day, hour, mi
nute, second, millisecond, false); | |
26 DateTime.utc(int year, [int month = 1, int day = 1, int hour = 0, int minute =
0, int second = 0, int millisecond = 0]) : this._internal(year, month, day, hour
, minute, second, millisecond, true); | |
27 DateTime.now() : this._now(); | |
28 static DateTime parse(String formattedString) { | |
29 final RegExp re = new RegExp(r'^([+-]?\d{4,6})-?(\d\d)-?(\d\d)' r'(?:[ T](\d\d
)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)?' r'( ?[zZ]| ?([-+])(\d\d)(?::?(\d\d))?)?)
?$'); | |
30 Match match = re.firstMatch(formattedString); | |
31 if (match != null) { | |
32 int parseIntOrZero(String matched) { | |
33 if (matched == null) return 0; | |
34 return int.parse(matched); | |
35 } | |
36 double parseDoubleOrZero(String matched) { | |
37 if (matched == null) return 0.0; | |
38 return double.parse(matched); | |
39 } | |
40 int years = int.parse(match[1]); | |
41 int month = int.parse(match[2]); | |
42 int day = int.parse(match[3]); | |
43 int hour = parseIntOrZero(match[4]); | |
44 int minute = parseIntOrZero(match[5]); | |
45 int second = parseIntOrZero(match[6]); | |
46 bool addOneMillisecond = false; | |
47 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round(); | |
48 if (millisecond == 1000) { | |
49 addOneMillisecond = true; | |
50 millisecond = 999; | |
51 } | |
52 bool isUtc = false; | |
53 if (match[8] != null) { | |
54 isUtc = true; | |
55 if (match[9] != null) { | |
56 int sign = (match[9] == '-') ? -1 : 1; | |
57 int hourDifference = int.parse(match[10]); | |
58 int minuteDifference = parseIntOrZero(match[11]); | |
59 minuteDifference += 60 * hourDifference; | |
60 minute -= sign * minuteDifference; | |
61 } | |
62 } | |
63 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(years,
month, day, hour, minute, second, millisecond, isUtc); | |
64 if (millisecondsSinceEpoch == null) { | |
65 throw new FormatException("Time out of range", formattedString); | |
66 } | |
67 if (addOneMillisecond) millisecondsSinceEpoch++; | |
68 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUt
c: isUtc); | |
69 } | |
70 else { | |
71 throw new FormatException("Invalid date format", formattedString); | |
72 } | |
73 } | |
74 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; | |
75 DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, { | |
76 bool isUtc : false} | |
77 ) : this.millisecondsSinceEpoch = millisecondsSinceEpoch, this.isUtc = isUtc { | |
78 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | |
79 throw new ArgumentError(millisecondsSinceEpoch); | |
80 } | |
81 if (isUtc == null) throw new ArgumentError(isUtc); | |
82 } | |
83 bool operator ==(other) { | |
84 if (!(other is DateTime)) return false; | |
85 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch && isUtc == ot
her.isUtc); | |
86 } | |
87 bool isBefore(DateTime other) { | |
88 return millisecondsSinceEpoch < other.millisecondsSinceEpoch; | |
89 } | |
90 bool isAfter(DateTime other) { | |
91 return millisecondsSinceEpoch > other.millisecondsSinceEpoch; | |
92 } | |
93 bool isAtSameMomentAs(DateTime other) { | |
94 return millisecondsSinceEpoch == other.millisecondsSinceEpoch; | |
95 } | |
96 int compareTo(DateTime other) => millisecondsSinceEpoch.compareTo(other.millise
condsSinceEpoch); | |
97 int get hashCode => millisecondsSinceEpoch; | |
98 DateTime toLocal() { | |
99 if (isUtc) { | |
100 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc
: false); | |
101 } | |
102 return this; | |
103 } | |
104 DateTime toUtc() { | |
105 if (isUtc) return this; | |
106 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc:
true); | |
107 } | |
108 static String _fourDigits(int n) { | |
109 int absN = n.abs(); | |
110 String sign = n < 0 ? "-" : ""; | |
111 if (absN >= 1000) return "$n"; | |
112 if (absN >= 100) return "${sign}0$absN"; | |
113 if (absN >= 10) return "${sign}00$absN"; | |
114 return "${sign}000$absN"; | |
115 } | |
116 static String _sixDigits(int n) { | |
117 assert (n < -9999 || n > 9999); int absN = n.abs(); | |
118 String sign = n < 0 ? "-" : "+"; | |
119 if (absN >= 100000) return "$sign$absN"; | |
120 return "${sign}0$absN"; | |
121 } | |
122 static String _threeDigits(int n) { | |
123 if (n >= 100) return "${n}"; | |
124 if (n >= 10) return "0${n}"; | |
125 return "00${n}"; | |
126 } | |
127 static String _twoDigits(int n) { | |
128 if (n >= 10) return "${n}"; | |
129 return "0${n}"; | |
130 } | |
131 String toString() { | |
132 String y = _fourDigits(year); | |
133 String m = _twoDigits(month); | |
134 String d = _twoDigits(day); | |
135 String h = _twoDigits(hour); | |
136 String min = _twoDigits(minute); | |
137 String sec = _twoDigits(second); | |
138 String ms = _threeDigits(millisecond); | |
139 if (isUtc) { | |
140 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | |
141 } | |
142 else { | |
143 return "$y-$m-$d $h:$min:$sec.$ms"; | |
144 } | |
145 } | |
146 String toIso8601String() { | |
147 String y = (year >= -9999 && year <= 9999) ? _fourDigits(year) : _sixDigits(ye
ar); | |
148 String m = _twoDigits(month); | |
149 String d = _twoDigits(day); | |
150 String h = _twoDigits(hour); | |
151 String min = _twoDigits(minute); | |
152 String sec = _twoDigits(second); | |
153 String ms = _threeDigits(millisecond); | |
154 if (isUtc) { | |
155 return "$y-$m-${d}T$h:$min:$sec.${ms}Z"; | |
156 } | |
157 else { | |
158 return "$y-$m-${d}T$h:$min:$sec.$ms"; | |
159 } | |
160 } | |
161 DateTime add(Duration duration) { | |
162 int ms = millisecondsSinceEpoch; | |
163 return new DateTime.fromMillisecondsSinceEpoch(ms + duration.inMilliseconds,
isUtc: isUtc); | |
164 } | |
165 DateTime subtract(Duration duration) { | |
166 int ms = millisecondsSinceEpoch; | |
167 return new DateTime.fromMillisecondsSinceEpoch(ms - duration.inMilliseconds,
isUtc: isUtc); | |
168 } | |
169 Duration difference(DateTime other) { | |
170 int ms = millisecondsSinceEpoch; | |
171 int otherMs = other.millisecondsSinceEpoch; | |
172 return new Duration(milliseconds: ms - otherMs); | |
173 } | |
174 external DateTime._internal(int year, int month, int day, int hour, int minute,
int second, int millisecond, bool isUtc); | |
175 external DateTime._now(); | |
176 external static int _brokenDownDateToMillisecondsSinceEpoch(int year, int month
, int day, int hour, int minute, int second, int millisecond, bool isUtc); | |
177 external String get timeZoneName; | |
178 external Duration get timeZoneOffset; | |
179 external int get year; | |
180 external int get month; | |
181 external int get day; | |
182 external int get hour; | |
183 external int get minute; | |
184 external int get second; | |
185 external int get millisecond; | |
186 external int get weekday; | |
187 } | |
OLD | NEW |