Chromium Code Reviews| 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 } | 160 } |
| 161 | 161 |
| 162 // static | 162 // static |
| 163 Time Time::NowFromSystemTime() { | 163 Time Time::NowFromSystemTime() { |
| 164 // Just use Now() because Now() returns the system time. | 164 // Just use Now() because Now() returns the system time. |
| 165 return Now(); | 165 return Now(); |
| 166 } | 166 } |
| 167 | 167 |
| 168 // static | 168 // static |
| 169 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 169 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
| 170 CFGregorianDate date; | |
| 171 date.second = exploded.second + | |
| 172 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); | |
| 173 date.minute = exploded.minute; | |
| 174 date.hour = exploded.hour; | |
| 175 date.day = exploded.day_of_month; | |
| 176 date.month = exploded.month; | |
| 177 date.year = exploded.year; | |
| 178 | |
| 179 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | 170 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
| 180 is_local ? CFTimeZoneCopySystem() : NULL); | 171 is_local |
| 181 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + | 172 ? CFTimeZoneCopySystem() |
| 182 kCFAbsoluteTimeIntervalSince1970; | 173 : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); |
| 174 base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier( | |
| 175 kCFAllocatorDefault, kCFGregorianCalendar)); | |
| 176 CFCalendarSetTimeZone(gregorian, time_zone); | |
| 177 CFAbsoluteTime absolute_time; | |
| 178 // 'S' is not defined in componentDesc in Apple documentation, but can be | |
| 179 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c | |
| 180 CFCalendarComposeAbsoluteTime( | |
| 181 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, | |
| 182 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, | |
| 183 exploded.millisecond); | |
| 184 // Milliseconds from |exploded| is added back to |absolute_time| here | |
|
sdefresne
2016/02/12 13:49:57
Remove this comment as it is no longer valid (i.e.
| |
| 185 // because CFCalendar parameters are integer values. | |
| 186 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; | |
| 183 return Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + | 187 return Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + |
| 184 kWindowsEpochDeltaMicroseconds); | 188 kWindowsEpochDeltaMicroseconds); |
| 185 } | 189 } |
| 186 | 190 |
| 187 void Time::Explode(bool is_local, Exploded* exploded) const { | 191 void Time::Explode(bool is_local, Exploded* exploded) const { |
| 188 // Avoid rounding issues, by only putting the integral number of seconds | 192 // Avoid rounding issues, by only putting the integral number of seconds |
| 189 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). | 193 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
| 190 int64_t microsecond = us_ % kMicrosecondsPerSecond; | 194 int64_t microsecond = us_ % kMicrosecondsPerSecond; |
| 191 if (microsecond < 0) | 195 if (microsecond < 0) |
| 192 microsecond += kMicrosecondsPerSecond; | 196 microsecond += kMicrosecondsPerSecond; |
| 193 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - | 197 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - |
| 194 kWindowsEpochDeltaSeconds - | 198 kWindowsEpochDeltaSeconds - |
| 195 kCFAbsoluteTimeIntervalSince1970; | 199 kCFAbsoluteTimeIntervalSince1970; |
| 196 | 200 |
| 197 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | 201 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
| 198 is_local ? CFTimeZoneCopySystem() : NULL); | 202 is_local |
| 199 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); | 203 ? CFTimeZoneCopySystem() |
| 200 // 1 = Monday, ..., 7 = Sunday. | 204 : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); |
| 201 int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone); | 205 base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier( |
| 202 | 206 kCFAllocatorDefault, kCFGregorianCalendar)); |
| 203 exploded->year = date.year; | 207 CFCalendarSetTimeZone(gregorian, time_zone); |
| 204 exploded->month = date.month; | 208 int second, day_of_week; |
| 205 exploded->day_of_week = cf_day_of_week % 7; | 209 // 'E' sets the day of week, but is not defined in componentDesc in Apple |
| 206 exploded->day_of_month = date.day; | 210 // documentation. It can be found in open source code here: |
| 207 exploded->hour = date.hour; | 211 // http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c |
| 208 exploded->minute = date.minute; | 212 CFCalendarDecomposeAbsoluteTime(gregorian, seconds, "yMdHmsE", |
| 213 &exploded->year, &exploded->month, | |
| 214 &exploded->day_of_month, &exploded->hour, | |
| 215 &exploded->minute, &second, &day_of_week); | |
| 209 // Make sure seconds are rounded down towards -infinity. | 216 // Make sure seconds are rounded down towards -infinity. |
| 210 exploded->second = floor(date.second); | 217 exploded->second = floor(second); |
| 218 // |Exploded|'s convention for day of week is 0 = Sunday, i.e. different | |
| 219 // from CF's 1 = Sunday. | |
| 220 exploded->day_of_week = (day_of_week - 1) % 7; | |
| 211 // Calculate milliseconds ourselves, since we rounded the |seconds|, making | 221 // Calculate milliseconds ourselves, since we rounded the |seconds|, making |
| 212 // sure to round towards -infinity. | 222 // sure to round towards -infinity. |
| 213 exploded->millisecond = | 223 exploded->millisecond = |
| 214 (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond : | 224 (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond : |
| 215 (microsecond - kMicrosecondsPerMillisecond + 1) / | 225 (microsecond - kMicrosecondsPerMillisecond + 1) / |
| 216 kMicrosecondsPerMillisecond; | 226 kMicrosecondsPerMillisecond; |
| 217 } | 227 } |
| 218 | 228 |
| 219 // TimeTicks ------------------------------------------------------------------ | 229 // TimeTicks ------------------------------------------------------------------ |
| 220 | 230 |
| 221 // static | 231 // static |
| 222 TimeTicks TimeTicks::Now() { | 232 TimeTicks TimeTicks::Now() { |
| 223 return TimeTicks(ComputeCurrentTicks()); | 233 return TimeTicks(ComputeCurrentTicks()); |
| 224 } | 234 } |
| 225 | 235 |
| 226 // static | 236 // static |
| 227 bool TimeTicks::IsHighResolution() { | 237 bool TimeTicks::IsHighResolution() { |
| 228 return true; | 238 return true; |
| 229 } | 239 } |
| 230 | 240 |
| 231 // static | 241 // static |
| 232 ThreadTicks ThreadTicks::Now() { | 242 ThreadTicks ThreadTicks::Now() { |
| 233 return ThreadTicks(ComputeThreadTicks()); | 243 return ThreadTicks(ComputeThreadTicks()); |
| 234 } | 244 } |
| 235 | 245 |
| 236 } // namespace base | 246 } // namespace base |
| OLD | NEW |