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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 kCFAllocatorDefault, kCFGregorianCalendar)); | 177 kCFAllocatorDefault, kCFGregorianCalendar)); |
178 CFCalendarSetTimeZone(gregorian, time_zone); | 178 CFCalendarSetTimeZone(gregorian, time_zone); |
179 CFAbsoluteTime absolute_time; | 179 CFAbsoluteTime absolute_time; |
180 // 'S' is not defined in componentDesc in Apple documentation, but can be | 180 // 'S' is not defined in componentDesc in Apple documentation, but can be |
181 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c | 181 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c |
182 CFCalendarComposeAbsoluteTime( | 182 CFCalendarComposeAbsoluteTime( |
183 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, | 183 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, |
184 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, | 184 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, |
185 exploded.millisecond); | 185 exploded.millisecond); |
186 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; | 186 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; |
187 return Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + | 187 |
188 kWindowsEpochDeltaMicroseconds); | 188 base::Time t_time = |
mmenke
2016/05/20 19:44:56
nit: t_time seems like a weird variable name - it
maksims (do not use this acc)
2016/05/24 09:14:21
Done. I already have a "time" in the function argu
| |
189 Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + | |
190 kWindowsEpochDeltaMicroseconds); | |
191 | |
192 // If |exploded.day_of_month| is set to 31 | |
193 // on a 28-30 day month, it will return the first day of the next month. | |
194 // Thus round-trip the time and compare the initial |exploded| with | |
195 // |utc_to_exploded| time. | |
mmenke
2016/05/20 19:44:56
nit: Comment can be reformatted - first line of t
maksims (do not use this acc)
2016/05/24 09:14:21
Done.
| |
196 base::Time::Exploded to_exploded; | |
197 if (!is_local) | |
198 t_time.UTCExplode(&to_exploded); | |
199 else | |
200 t_time.LocalExplode(&to_exploded); | |
201 | |
202 return to_exploded != exploded ? Time(0) : t_time; | |
203 } | |
204 | |
205 // static | |
206 bool Time::FromExploded(bool is_local, const Exploded& exploded, Time& time) { | |
207 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | |
208 is_local | |
209 ? CFTimeZoneCopySystem() | |
210 : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); | |
211 base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier( | |
212 kCFAllocatorDefault, kCFGregorianCalendar)); | |
213 CFCalendarSetTimeZone(gregorian, time_zone); | |
214 CFAbsoluteTime absolute_time; | |
215 // 'S' is not defined in componentDesc in Apple documentation, but can be | |
216 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c | |
217 CFCalendarComposeAbsoluteTime( | |
218 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, | |
219 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, | |
220 exploded.millisecond); | |
221 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; | |
222 | |
223 base::Time t_time = | |
224 Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + | |
225 kWindowsEpochDeltaMicroseconds); | |
226 | |
227 // If |exploded.day_of_month| is set to 31 | |
228 // on a 28-30 day month, it will return the first day of the next month. | |
229 // Thus round-trip the time and compare the initial |exploded| with | |
230 // |utc_to_exploded| time. | |
231 base::Time::Exploded to_exploded; | |
232 if (!is_local) | |
233 t_time.UTCExplode(&to_exploded); | |
234 else | |
235 t_time.LocalExplode(&to_exploded); | |
236 | |
237 time = to_exploded != exploded ? Time(0) : t_time; | |
238 | |
239 // If time is null, return false | |
240 // which means time conversion failed | |
241 return time.is_null() ? false : true; | |
189 } | 242 } |
190 | 243 |
191 void Time::Explode(bool is_local, Exploded* exploded) const { | 244 void Time::Explode(bool is_local, Exploded* exploded) const { |
192 // Avoid rounding issues, by only putting the integral number of seconds | 245 // Avoid rounding issues, by only putting the integral number of seconds |
193 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). | 246 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
194 int64_t microsecond = us_ % kMicrosecondsPerSecond; | 247 int64_t microsecond = us_ % kMicrosecondsPerSecond; |
195 if (microsecond < 0) | 248 if (microsecond < 0) |
196 microsecond += kMicrosecondsPerSecond; | 249 microsecond += kMicrosecondsPerSecond; |
197 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - | 250 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - |
198 kWindowsEpochDeltaSeconds - | 251 kWindowsEpochDeltaSeconds - |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 return Clock::MAC_MACH_ABSOLUTE_TIME; | 299 return Clock::MAC_MACH_ABSOLUTE_TIME; |
247 #endif // defined(OS_IOS) | 300 #endif // defined(OS_IOS) |
248 } | 301 } |
249 | 302 |
250 // static | 303 // static |
251 ThreadTicks ThreadTicks::Now() { | 304 ThreadTicks ThreadTicks::Now() { |
252 return ThreadTicks(ComputeThreadTicks()); | 305 return ThreadTicks(ComputeThreadTicks()); |
253 } | 306 } |
254 | 307 |
255 } // namespace base | 308 } // namespace base |
OLD | NEW |