OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <CoreFoundation/CFDate.h> | 7 #include <CoreFoundation/CFDate.h> |
8 #include <CoreFoundation/CFTimeZone.h> | 8 #include <CoreFoundation/CFTimeZone.h> |
9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <mach/mach_time.h> | 10 #include <mach/mach_time.h> |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 CFCalendarSetTimeZone(gregorian, time_zone); | 183 CFCalendarSetTimeZone(gregorian, time_zone); |
184 CFAbsoluteTime absolute_time; | 184 CFAbsoluteTime absolute_time; |
185 // 'S' is not defined in componentDesc in Apple documentation, but can be | 185 // 'S' is not defined in componentDesc in Apple documentation, but can be |
186 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c | 186 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c |
187 CFCalendarComposeAbsoluteTime( | 187 CFCalendarComposeAbsoluteTime( |
188 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, | 188 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, |
189 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, | 189 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, |
190 exploded.millisecond); | 190 exploded.millisecond); |
191 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; | 191 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; |
192 | 192 |
193 base::Time converted_time = | 193 // CFAbsolutTime is typedef of double. Convert seconds to |
194 Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + | 194 // microseconds and then cast to int64. If |
195 kWindowsEpochDeltaMicroseconds); | 195 // it cannot be suited to int64, then fail to avoid overflows. |
| 196 double microseconds = |
| 197 (seconds * kMicrosecondsPerSecond) + kWindowsEpochDeltaMicroseconds; |
| 198 if (microseconds > std::numeric_limits<int64_t>::max() || |
| 199 microseconds < std::numeric_limits<int64_t>::min()) { |
| 200 *time = Time(0); |
| 201 return false; |
| 202 } |
| 203 |
| 204 base::Time converted_time = Time(static_cast<int64_t>(microseconds)); |
196 | 205 |
197 // If |exploded.day_of_month| is set to 31 | 206 // If |exploded.day_of_month| is set to 31 |
198 // on a 28-30 day month, it will return the first day of the next month. | 207 // on a 28-30 day month, it will return the first day of the next month. |
199 // Thus round-trip the time and compare the initial |exploded| with | 208 // Thus round-trip the time and compare the initial |exploded| with |
200 // |utc_to_exploded| time. | 209 // |utc_to_exploded| time. |
201 base::Time::Exploded to_exploded; | 210 base::Time::Exploded to_exploded; |
202 if (!is_local) | 211 if (!is_local) |
203 converted_time.UTCExplode(&to_exploded); | 212 converted_time.UTCExplode(&to_exploded); |
204 else | 213 else |
205 converted_time.LocalExplode(&to_exploded); | 214 converted_time.LocalExplode(&to_exploded); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 return Clock::MAC_MACH_ABSOLUTE_TIME; | 292 return Clock::MAC_MACH_ABSOLUTE_TIME; |
284 #endif // defined(OS_IOS) | 293 #endif // defined(OS_IOS) |
285 } | 294 } |
286 | 295 |
287 // static | 296 // static |
288 ThreadTicks ThreadTicks::Now() { | 297 ThreadTicks ThreadTicks::Now() { |
289 return ThreadTicks(ComputeThreadTicks()); | 298 return ThreadTicks(ComputeThreadTicks()); |
290 } | 299 } |
291 | 300 |
292 } // namespace base | 301 } // namespace base |
OLD | NEW |