Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: corelib/unified/core/implementation/date.dart

Issue 10829102: Create 'unified' version of Date, with a dart2js implementation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add dart2js patch file for coreimpl. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 external DateImplementation(int year,
10 [int month,
11 int day,
12 int hour,
13 int minute,
14 int second,
15 int millisecond,
16 bool isUtc]);
17
18 external DateImplementation.now();
19
20 factory DateImplementation.fromString(String formattedString) {
21 // Read in (a subset of) ISO 8601.
22 // Examples:
23 // - "2012-02-27 13:27:00"
24 // - "2012-02-27 13:27:00.423z"
25 // - "20120227 13:27:00"
26 // - "20120227T132700"
27 // - "20120227"
28 // - "2012-02-27T14Z"
29 // - "-123450101 00:00:00 Z" // In the year -12345.
30 final RegExp re = const RegExp(
31 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part.
32 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$');
33 Match match = re.firstMatch(formattedString);
34 if (match !== null) {
35 int parseIntOrZero(String matched) {
36 // TODO(floitsch): we should not need to test against the empty string.
37 if (matched === null || matched == "") return 0;
38 return Math.parseInt(matched);
39 }
40
41 double parseDoubleOrZero(String matched) {
42 // TODO(floitsch): we should not need to test against the empty string.
43 if (matched === null || matched == "") return 0.0;
44 return Math.parseDouble(matched);
45 }
46
47 int years = Math.parseInt(match[1]);
48 int month = Math.parseInt(match[2]);
49 int day = Math.parseInt(match[3]);
50 int hour = parseIntOrZero(match[4]);
51 int minute = parseIntOrZero(match[5]);
52 int second = parseIntOrZero(match[6]);
53 bool addOneMillisecond = false;
54 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt();
55 if (millisecond == 1000) {
56 addOneMillisecond = true;
57 millisecond = 999;
58 }
59 // TODO(floitsch): we should not need to test against the empty string.
60 bool isUtc = (match[8] !== null) && (match[8] != "");
61 int millisecondsSinceEpoch = Primitives.valueFromDecomposedDate(
62 years, month, day, hour, minute, second, millisecond, isUtc);
63 if (millisecondsSinceEpoch === null) {
64 throw new IllegalArgumentException(formattedString);
65 }
66 if (addOneMillisecond) millisecondsSinceEpoch++;
67 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc);
68 } else {
69 throw new IllegalArgumentException(formattedString);
70 }
71 }
72
73 static final int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
74
75 DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch,
76 [this.isUtc = false]) {
77 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
78 throw new IllegalArgumentException(millisecondsSinceEpoch);
79 }
80 if (isUtc === null) throw new IllegalArgumentException(isUtc);
81 }
82
83 bool operator ==(other) {
84 if (!(other is Date)) return false;
85 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch);
86 }
87
88 bool operator <(Date other)
89 => millisecondsSinceEpoch < other.millisecondsSinceEpoch;
90
91 bool operator <=(Date other)
92 => millisecondsSinceEpoch <= other.millisecondsSinceEpoch;
93
94 bool operator >(Date other)
95 => millisecondsSinceEpoch > other.millisecondsSinceEpoch;
96
97 bool operator >=(Date other)
98 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch;
99
100 int compareTo(Date other)
101 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch);
102
103 int hashCode() => millisecondsSinceEpoch;
104
105 Date toLocal() {
106 if (isUtc) {
107 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, false);
108 }
109 return this;
110 }
111
112 Date toUtc() {
113 if (isUtc) return this;
114 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, true);
115 }
116
117 external String get timeZoneName();
Mads Ager (google) 2012/08/01 08:08:31 Maybe group all of the external methods at the end
Anders Johnsen 2012/08/01 08:35:50 As I see it, we have two options. Keeping them in
118
119 external Duration get timeZoneOffset();
120
121 external int get year();
122
123 external int get month();
124
125 external int get day();
126
127 external int get hour();
128
129 external int get minute();
130
131 external int get second();
132
133 external int get millisecond();
134
135 external int get weekday();
136
137 String toString() {
138 String fourDigits(int n) {
139 int absN = n.abs();
140 String sign = n < 0 ? "-" : "";
141 if (absN >= 1000) return "$n";
142 if (absN >= 100) return "${sign}0$absN";
143 if (absN >= 10) return "${sign}00$absN";
144 return "${sign}000$absN";
145 }
146
147 String threeDigits(int n) {
148 if (n >= 100) return "${n}";
149 if (n >= 10) return "0${n}";
150 return "00${n}";
151 }
152
153 String twoDigits(int n) {
154 if (n >= 10) return "${n}";
155 return "0${n}";
156 }
157
158 String y = fourDigits(year);
159 String m = twoDigits(month);
160 String d = twoDigits(day);
161 String h = twoDigits(hour);
162 String min = twoDigits(minute);
163 String sec = twoDigits(second);
164 String ms = threeDigits(millisecond);
165 if (isUtc) {
166 return "$y-$m-$d $h:$min:$sec.${ms}Z";
167 } else {
168 return "$y-$m-$d $h:$min:$sec.$ms";
169 }
170 }
171
172 // Adds the [duration] to this Date instance.
173 Date add(Duration duration) {
174 int ms = millisecondsSinceEpoch;
175 return new Date.fromMillisecondsSinceEpoch(
176 ms + duration.inMilliseconds, isUtc);
177 }
178
179 // Subtracts the [duration] from this Date instance.
180 Date subtract(Duration duration) {
181 int ms = millisecondsSinceEpoch;
182 return new Date.fromMillisecondsSinceEpoch(
183 ms - duration.inMilliseconds, isUtc);
184 }
185
186 // Returns a [Duration] with the difference of [this] and [other].
187 Duration difference(Date other) {
188 int ms = millisecondsSinceEpoch;
189 int otherMs = other.millisecondsSinceEpoch;
190 return new Duration(milliseconds: ms - otherMs);
191 }
192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698