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

Side by Side Diff: runtime/lib/date.cc

Issue 9969098: Set the TZ env to get the UTC-ms since epoch. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comment. Created 8 years, 7 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 | « no previous file | runtime/vm/os.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 4
5 #include <time.h>
6
5 #include "vm/bootstrap_natives.h" 7 #include "vm/bootstrap_natives.h"
6 8
7 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
8 #include "vm/native_entry.h" 10 #include "vm/native_entry.h"
9 #include "vm/object.h" 11 #include "vm/object.h"
10 #include "vm/os.h" 12 #include "vm/os.h"
11 13
12 namespace dart { 14 namespace dart {
13 15
16 typedef struct BrokenDownDate {
17 intptr_t year;
18 intptr_t month; // [1..12]
19 intptr_t day; // [1..31]
20 intptr_t hours;
21 intptr_t minutes;
22 intptr_t seconds;
23 } BrokenDownDate;
24
25
26 // Takes the seconds since epoch (midnight, January 1, 1970 UTC) and breaks it
27 // down into date and time.
28 // If 'dart_is_utc', then the broken down date and time are in the UTC timezone,
29 // otherwise the local timezone is used.
30 // The returned year is offset by 1900. The returned month is 0-based.
31 // Returns true if the conversion succeeds, false otherwise.
14 static bool BreakDownSecondsSinceEpoch(const Integer& dart_seconds, 32 static bool BreakDownSecondsSinceEpoch(const Integer& dart_seconds,
15 const Bool& dart_is_utc, 33 const Bool& dart_is_utc,
16 OS::BrokenDownDate* result) { 34 BrokenDownDate* result) {
35 // Always fill the result to avoid unitialized use warnings.
36 result->year = 0;
37 result->month = 0;
38 result->day = 0;
39 result->hours = 0;
40 result->minutes = 0;
41 result->seconds = 0;
42
17 bool is_utc = dart_is_utc.value(); 43 bool is_utc = dart_is_utc.value();
18 int64_t value = dart_seconds.AsInt64Value(); 44 int64_t seconds = dart_seconds.AsInt64Value();
19 time_t seconds = static_cast<time_t>(value); 45
20 return OS::BreakDownSecondsSinceEpoch(seconds, is_utc, result); 46 struct tm tm_result;
47 bool succeeded;
48 if (is_utc) {
49 succeeded = OS::GmTime(seconds, &tm_result);
50 } else {
51 succeeded = OS::LocalTime(seconds, &tm_result);
52 }
53 if (succeeded) {
54 result->year = tm_result.tm_year;
55 // C uses years since 1900, and not full years.
56 // Adding 1900 could overflow the intptr_t.
57 if (tm_result.tm_year > kIntptrMax - 1900) return false;
58 result->year += 1900;
59 // Dart has 1-based months (contrary to C's 0-based).
60 result->month= tm_result.tm_mon + 1;
61 result->day = tm_result.tm_mday;
62 result->hours = tm_result.tm_hour;
63 result->minutes = tm_result.tm_min;
64 result->seconds = tm_result.tm_sec;
65 }
66 return succeeded;
21 } 67 }
22 68
23 69
70 static bool BrokenDownToSecondsSinceEpoch(const BrokenDownDate& broken_down,
71 bool in_utc,
72 int64_t* result) {
73 // Always set the result to avoid unitialized use warnings.
74 *result = 0;
75
76 struct tm tm_broken_down;
77 intptr_t year = broken_down.year;
78 // C works with years since 1900.
79 // Removing 1900 could underflow the intptr_t.
80 if (year < kIntptrMin + 1900) return false;
81 year -= 1900;
82 intptr_t month = broken_down.month;
83 // C works with 0-based months.
84 // Avoid underflows (even though they should not matter since the date would
85 // be invalid anyways.
86 if (month < 0) return false;
87 month--;
88 tm_broken_down.tm_year = static_cast<int>(year);
89 tm_broken_down.tm_mon = static_cast<int>(month);
90 tm_broken_down.tm_mday = static_cast<int>(broken_down.day);
91 tm_broken_down.tm_hour = static_cast<int>(broken_down.hours);
92 tm_broken_down.tm_min = static_cast<int>(broken_down.minutes);
93 tm_broken_down.tm_sec = static_cast<int>(broken_down.seconds);
94 // Verify that casting to int did not change the value.
95 if (tm_broken_down.tm_year != year
96 || tm_broken_down.tm_mon != month
97 || tm_broken_down.tm_mday != broken_down.day
98 || tm_broken_down.tm_hour != broken_down.hours
99 || tm_broken_down.tm_min != broken_down.minutes
100 || tm_broken_down.tm_sec != broken_down.seconds) {
101 return false;
102 }
103 if (in_utc) {
104 return OS::MkGmTime(&tm_broken_down, result);
105 } else {
106 return OS::MkTime(&tm_broken_down, result);
107 }
108 }
109
110
24 DEFINE_NATIVE_ENTRY(DateNatives_brokenDownToSecondsSinceEpoch, 7) { 111 DEFINE_NATIVE_ENTRY(DateNatives_brokenDownToSecondsSinceEpoch, 7) {
25 GET_NATIVE_ARGUMENT(Integer, dart_years, arguments->At(0)); 112 GET_NATIVE_ARGUMENT(Integer, dart_years, arguments->At(0));
26 GET_NATIVE_ARGUMENT(Smi, dart_month, arguments->At(1)); 113 GET_NATIVE_ARGUMENT(Smi, dart_month, arguments->At(1));
27 GET_NATIVE_ARGUMENT(Smi, dart_day, arguments->At(2)); 114 GET_NATIVE_ARGUMENT(Smi, dart_day, arguments->At(2));
28 GET_NATIVE_ARGUMENT(Smi, dart_hours, arguments->At(3)); 115 GET_NATIVE_ARGUMENT(Smi, dart_hours, arguments->At(3));
29 GET_NATIVE_ARGUMENT(Smi, dart_minutes, arguments->At(4)); 116 GET_NATIVE_ARGUMENT(Smi, dart_minutes, arguments->At(4));
30 GET_NATIVE_ARGUMENT(Smi, dart_seconds, arguments->At(5)); 117 GET_NATIVE_ARGUMENT(Smi, dart_seconds, arguments->At(5));
31 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(6)); 118 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(6));
32 if (!dart_years.IsSmi()) { 119 if (!dart_years.IsSmi()) {
33 UNIMPLEMENTED(); 120 UNIMPLEMENTED();
34 } 121 }
35 Smi& smi_years = Smi::Handle(); 122 Smi& smi_years = Smi::Handle();
36 smi_years ^= dart_years.raw(); 123 smi_years ^= dart_years.raw();
37 OS::BrokenDownDate broken_down; 124 BrokenDownDate broken_down;
38 // mktime takes the years since 1900. 125 broken_down.year = smi_years.Value();
39 // TODO(floitsch): Removing 1900 could underflow the intptr_t. 126 broken_down.month = dart_month.Value();
40 intptr_t year = smi_years.Value() - 1900;
41 // TODO(1143): We don't handle the case yet where intptr_t and int have
42 // different sizes.
43 // ASSERT(sizeof(year) <= sizeof(broken_down.year));
44 broken_down.year = static_cast<int>(year);
45 // libc months are 0-based (contrary to Dart' 1-based months).
46 broken_down.month = dart_month.Value() - 1;
47 broken_down.day = dart_day.Value(); 127 broken_down.day = dart_day.Value();
48 broken_down.hours = dart_hours.Value(); 128 broken_down.hours = dart_hours.Value();
49 broken_down.minutes = dart_minutes.Value(); 129 broken_down.minutes = dart_minutes.Value();
50 broken_down.seconds = dart_seconds.Value(); 130 broken_down.seconds = dart_seconds.Value();
51 time_t value; 131 int64_t value;
52 bool succeeded = OS::BrokenDownToSecondsSinceEpoch(broken_down, 132 bool succeeded = BrokenDownToSecondsSinceEpoch(broken_down,
53 dart_is_utc.value(), 133 dart_is_utc.value(),
54 &value); 134 &value);
55 if (!succeeded) { 135 if (!succeeded) {
56 UNIMPLEMENTED(); 136 UNIMPLEMENTED();
57 } 137 }
58 arguments->SetReturn(Integer::Handle(Integer::New(value))); 138 arguments->SetReturn(Integer::Handle(Integer::New(value)));
59 } 139 }
60 140
61 141
62 DEFINE_NATIVE_ENTRY(DateNatives_currentTimeMillis, 0) { 142 DEFINE_NATIVE_ENTRY(DateNatives_currentTimeMillis, 0) {
63 const Integer& time = Integer::Handle( 143 const Integer& time = Integer::Handle(
64 Integer::New(OS::GetCurrentTimeMillis())); 144 Integer::New(OS::GetCurrentTimeMillis()));
65 arguments->SetReturn(time); 145 arguments->SetReturn(time);
66 } 146 }
67 147
68 148
69 DEFINE_NATIVE_ENTRY(DateNatives_getYear, 2) { 149 DEFINE_NATIVE_ENTRY(DateNatives_getYear, 2) {
70 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); 150 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0));
71 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); 151 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1));
72 OS::BrokenDownDate broken_down; 152 BrokenDownDate broken_down;
73 bool succeeded = 153 bool succeeded =
74 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); 154 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down);
75 if (!succeeded) { 155 if (!succeeded) {
76 UNIMPLEMENTED(); 156 UNIMPLEMENTED();
77 } 157 }
78 // C uses years since 1900, and not full years. 158 intptr_t year = broken_down.year;
79 // TODO(floitsch): adding 1900 could overflow the intptr_t.
80 intptr_t year = broken_down.year + 1900;
81 arguments->SetReturn(Integer::Handle(Integer::New(year))); 159 arguments->SetReturn(Integer::Handle(Integer::New(year)));
82 } 160 }
83 161
84 162
85 DEFINE_NATIVE_ENTRY(DateNatives_getMonth, 2) { 163 DEFINE_NATIVE_ENTRY(DateNatives_getMonth, 2) {
86 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); 164 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0));
87 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); 165 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1));
88 OS::BrokenDownDate broken_down; 166 BrokenDownDate broken_down;
89 bool succeeded = 167 bool succeeded =
90 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); 168 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down);
91 if (!succeeded) { 169 if (!succeeded) {
92 UNIMPLEMENTED(); 170 UNIMPLEMENTED();
93 } 171 }
94 // Dart has 1-based months (contrary to C's 0-based). 172 const Smi& result = Smi::Handle(Smi::New(broken_down.month));
95 const Smi& result = Smi::Handle(Smi::New(broken_down.month + 1));
96 arguments->SetReturn(result); 173 arguments->SetReturn(result);
97 } 174 }
98 175
99 176
100 DEFINE_NATIVE_ENTRY(DateNatives_getDay, 2) { 177 DEFINE_NATIVE_ENTRY(DateNatives_getDay, 2) {
101 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); 178 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0));
102 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); 179 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1));
103 OS::BrokenDownDate broken_down; 180 BrokenDownDate broken_down;
104 bool succeeded = 181 bool succeeded =
105 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); 182 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down);
106 if (!succeeded) { 183 if (!succeeded) {
107 UNIMPLEMENTED(); 184 UNIMPLEMENTED();
108 } 185 }
109 const Smi& result = Smi::Handle(Smi::New(broken_down.day)); 186 const Smi& result = Smi::Handle(Smi::New(broken_down.day));
110 arguments->SetReturn(result); 187 arguments->SetReturn(result);
111 } 188 }
112 189
113 190
114 DEFINE_NATIVE_ENTRY(DateNatives_getHours, 2) { 191 DEFINE_NATIVE_ENTRY(DateNatives_getHours, 2) {
115 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); 192 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0));
116 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); 193 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1));
117 OS::BrokenDownDate broken_down; 194 BrokenDownDate broken_down;
118 bool succeeded = 195 bool succeeded =
119 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); 196 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down);
120 if (!succeeded) { 197 if (!succeeded) {
121 UNIMPLEMENTED(); 198 UNIMPLEMENTED();
122 } 199 }
123 const Smi& result = Smi::Handle(Smi::New(broken_down.hours)); 200 const Smi& result = Smi::Handle(Smi::New(broken_down.hours));
124 arguments->SetReturn(result); 201 arguments->SetReturn(result);
125 } 202 }
126 203
127 204
128 DEFINE_NATIVE_ENTRY(DateNatives_getMinutes, 2) { 205 DEFINE_NATIVE_ENTRY(DateNatives_getMinutes, 2) {
129 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); 206 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0));
130 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); 207 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1));
131 OS::BrokenDownDate broken_down; 208 BrokenDownDate broken_down;
132 bool succeeded = 209 bool succeeded =
133 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); 210 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down);
134 if (!succeeded) { 211 if (!succeeded) {
135 UNIMPLEMENTED(); 212 UNIMPLEMENTED();
136 } 213 }
137 const Smi& result = Smi::Handle(Smi::New(broken_down.minutes)); 214 const Smi& result = Smi::Handle(Smi::New(broken_down.minutes));
138 arguments->SetReturn(result); 215 arguments->SetReturn(result);
139 } 216 }
140 217
141 218
142 DEFINE_NATIVE_ENTRY(DateNatives_getSeconds, 2) { 219 DEFINE_NATIVE_ENTRY(DateNatives_getSeconds, 2) {
143 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); 220 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0));
144 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); 221 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1));
145 OS::BrokenDownDate broken_down; 222 BrokenDownDate broken_down;
146 bool succeeded = 223 bool succeeded =
147 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); 224 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down);
148 if (!succeeded) { 225 if (!succeeded) {
149 UNIMPLEMENTED(); 226 UNIMPLEMENTED();
150 } 227 }
151 const Smi& result = Smi::Handle(Smi::New(broken_down.seconds)); 228 const Smi& result = Smi::Handle(Smi::New(broken_down.seconds));
152 arguments->SetReturn(result); 229 arguments->SetReturn(result);
153 } 230 }
154 231
155 } // namespace dart 232 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/os.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698