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

Side by Side Diff: lib/coreimpl/date.dart

Issue 11227055: Move DateImplementation from coreimpl to core, making it private. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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
« no previous file with comments | « lib/coreimpl/corelib_impl_sources.gypi ('k') | runtime/lib/date_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 factory DateImplementation.fromString(String formattedString) {
10 // Read in (a subset of) ISO 8601.
11 // Examples:
12 // - "2012-02-27 13:27:00"
13 // - "2012-02-27 13:27:00.423z"
14 // - "20120227 13:27:00"
15 // - "20120227T132700"
16 // - "20120227"
17 // - "2012-02-27T14Z"
18 // - "-123450101 00:00:00 Z" // In the year -12345.
19 final RegExp re = const RegExp(
20 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part.
21 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$');
22 Match match = re.firstMatch(formattedString);
23 if (match !== null) {
24 int parseIntOrZero(String matched) {
25 // TODO(floitsch): we should not need to test against the empty string.
26 if (matched === null || matched == "") return 0;
27 return int.parse(matched);
28 }
29
30 double parseDoubleOrZero(String matched) {
31 // TODO(floitsch): we should not need to test against the empty string.
32 if (matched === null || matched == "") return 0.0;
33 return double.parse(matched);
34 }
35
36 int years = int.parse(match[1]);
37 int month = int.parse(match[2]);
38 int day = int.parse(match[3]);
39 int hour = parseIntOrZero(match[4]);
40 int minute = parseIntOrZero(match[5]);
41 int second = parseIntOrZero(match[6]);
42 bool addOneMillisecond = false;
43 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt();
44 if (millisecond == 1000) {
45 addOneMillisecond = true;
46 millisecond = 999;
47 }
48 // TODO(floitsch): we should not need to test against the empty string.
49 bool isUtc = (match[8] !== null) && (match[8] != "");
50 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
51 years, month, day, hour, minute, second, millisecond, isUtc);
52 if (millisecondsSinceEpoch === null) {
53 throw new ArgumentError(formattedString);
54 }
55 if (addOneMillisecond) millisecondsSinceEpoch++;
56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
57 isUtc: isUtc);
58 } else {
59 throw new ArgumentError(formattedString);
60 }
61 }
62
63 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
64
65 DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch,
66 this.isUtc) {
67 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
68 throw new ArgumentError(millisecondsSinceEpoch);
69 }
70 if (isUtc === null) throw new ArgumentError(isUtc);
71 }
72
73 bool operator ==(other) {
74 if (!(other is Date)) return false;
75 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch);
76 }
77
78 bool operator <(Date other)
79 => millisecondsSinceEpoch < other.millisecondsSinceEpoch;
80
81 bool operator <=(Date other)
82 => millisecondsSinceEpoch <= other.millisecondsSinceEpoch;
83
84 bool operator >(Date other)
85 => millisecondsSinceEpoch > other.millisecondsSinceEpoch;
86
87 bool operator >=(Date other)
88 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch;
89
90 int compareTo(Date other)
91 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch);
92
93 int get hashCode => millisecondsSinceEpoch;
94
95 Date toLocal() {
96 if (isUtc) {
97 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
98 isUtc: false);
99 }
100 return this;
101 }
102
103 Date toUtc() {
104 if (isUtc) return this;
105 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
106 isUtc: true);
107 }
108
109 String toString() {
110 String fourDigits(int n) {
111 int absN = n.abs();
112 String sign = n < 0 ? "-" : "";
113 if (absN >= 1000) return "$n";
114 if (absN >= 100) return "${sign}0$absN";
115 if (absN >= 10) return "${sign}00$absN";
116 return "${sign}000$absN";
117 }
118
119 String threeDigits(int n) {
120 if (n >= 100) return "${n}";
121 if (n >= 10) return "0${n}";
122 return "00${n}";
123 }
124
125 String twoDigits(int n) {
126 if (n >= 10) return "${n}";
127 return "0${n}";
128 }
129
130 String y = fourDigits(year);
131 String m = twoDigits(month);
132 String d = twoDigits(day);
133 String h = twoDigits(hour);
134 String min = twoDigits(minute);
135 String sec = twoDigits(second);
136 String ms = threeDigits(millisecond);
137 if (isUtc) {
138 return "$y-$m-$d $h:$min:$sec.${ms}Z";
139 } else {
140 return "$y-$m-$d $h:$min:$sec.$ms";
141 }
142 }
143
144 /** Returns a new [Date] with the [duration] added to [this]. */
145 Date add(Duration duration) {
146 int ms = millisecondsSinceEpoch;
147 return new Date.fromMillisecondsSinceEpoch(
148 ms + duration.inMilliseconds, isUtc: isUtc);
149 }
150
151 /** Returns a new [Date] with the [duration] subtracted from [this]. */
152 Date subtract(Duration duration) {
153 int ms = millisecondsSinceEpoch;
154 return new Date.fromMillisecondsSinceEpoch(
155 ms - duration.inMilliseconds, isUtc: isUtc);
156 }
157
158 /** Returns a [Duration] with the difference of [this] and [other]. */
159 Duration difference(Date other) {
160 int ms = millisecondsSinceEpoch;
161 int otherMs = other.millisecondsSinceEpoch;
162 return new Duration(milliseconds: ms - otherMs);
163 }
164
165 external DateImplementation(int year,
166 int month,
167 int day,
168 int hour,
169 int minute,
170 int second,
171 int millisecond,
172 bool isUtc);
173 external DateImplementation.now();
174 external static int _brokenDownDateToMillisecondsSinceEpoch(
175 int year, int month, int day, int hour, int minute, int second,
176 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 }
OLDNEW
« no previous file with comments | « lib/coreimpl/corelib_impl_sources.gypi ('k') | runtime/lib/date_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698