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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 } | 158 } |
| 159 | 159 |
| 160 // static | 160 // static |
| 161 Time Time::NowFromSystemTime() { | 161 Time Time::NowFromSystemTime() { |
| 162 // Just use Now() because Now() returns the system time. | 162 // Just use Now() because Now() returns the system time. |
| 163 return Now(); | 163 return Now(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 // static | 166 // static |
| 167 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 167 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
| 168 CFGregorianDate date; | 168 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
|
Nico
2015/12/04 20:18:59
I wondered why we don't just use time_posix.cc on
sdefresne
2016/01/07 16:36:28
iOS still target 32-bit platforms so the fact that
pkl (ping after 24h if needed)
2016/02/03 06:42:31
I've taken the route of keeping time_mac.cc based
| |
| 169 date.second = exploded.second + | 169 is_local ? CFTimeZoneCopySystem() : |
| 170 CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); | |
| 171 base::ScopedCFTypeRef<CFCalendarRef> gregorian( | |
| 172 CFCalendarCreateWithIdentifier(kCFAllocatorDefault, | |
| 173 kCFGregorianCalendar)); | |
| 174 CFCalendarSetTimeZone(gregorian, time_zone); | |
| 175 CFAbsoluteTime absolute_time; | |
| 176 CFCalendarComposeAbsoluteTime(gregorian, &absolute_time, "yMdHms", | |
|
sdefresne
2016/01/07 16:36:28
nit: you can use "yMdHmS" to also pass the "explod
pkl (ping after 24h if needed)
2016/02/03 06:42:32
Done. Also added a comment to explain 'S'.
| |
| 177 static_cast<int>(exploded.year), | |
|
sdefresne
2016/01/07 16:36:28
nit: exploded.year, ... are all int, so the static
pkl (ping after 24h if needed)
2016/02/03 06:42:31
Done.
| |
| 178 static_cast<int>(exploded.month), | |
| 179 static_cast<int>(exploded.day_of_month), | |
| 180 static_cast<int>(exploded.hour), | |
| 181 static_cast<int>(exploded.minute), | |
| 182 static_cast<int>(exploded.second)); | |
| 183 // Milliseconds from |exploded| is added back to |absolute_time| here | |
| 184 // because CFCalendar parameters are integer values. | |
| 185 double milliseconds = | |
| 170 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); | 186 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); |
| 171 date.minute = exploded.minute; | 187 CFAbsoluteTime seconds = |
| 172 date.hour = exploded.hour; | 188 absolute_time + milliseconds + kCFAbsoluteTimeIntervalSince1970; |
| 173 date.day = exploded.day_of_month; | |
| 174 date.month = exploded.month; | |
| 175 date.year = exploded.year; | |
| 176 | |
| 177 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | |
| 178 is_local ? CFTimeZoneCopySystem() : NULL); | |
| 179 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + | |
| 180 kCFAbsoluteTimeIntervalSince1970; | |
| 181 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + | 189 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + |
| 182 kWindowsEpochDeltaMicroseconds); | 190 kWindowsEpochDeltaMicroseconds); |
| 183 } | 191 } |
| 184 | 192 |
| 185 void Time::Explode(bool is_local, Exploded* exploded) const { | 193 void Time::Explode(bool is_local, Exploded* exploded) const { |
| 186 // Avoid rounding issues, by only putting the integral number of seconds | 194 // Avoid rounding issues, by only putting the integral number of seconds |
| 187 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). | 195 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
| 188 int64 microsecond = us_ % kMicrosecondsPerSecond; | 196 int64 microsecond = us_ % kMicrosecondsPerSecond; |
| 189 if (microsecond < 0) | 197 if (microsecond < 0) |
| 190 microsecond += kMicrosecondsPerSecond; | 198 microsecond += kMicrosecondsPerSecond; |
| 191 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - | 199 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - |
| 192 kWindowsEpochDeltaSeconds - | 200 kWindowsEpochDeltaSeconds - |
| 193 kCFAbsoluteTimeIntervalSince1970; | 201 kCFAbsoluteTimeIntervalSince1970; |
| 194 | 202 |
| 195 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | 203 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
| 196 is_local ? CFTimeZoneCopySystem() : NULL); | 204 is_local ? CFTimeZoneCopySystem() : |
| 197 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); | 205 CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); |
| 198 // 1 = Monday, ..., 7 = Sunday. | 206 base::ScopedCFTypeRef<CFCalendarRef> gregorian( |
| 199 int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone); | 207 CFCalendarCreateWithIdentifier(kCFAllocatorDefault, |
| 208 kCFGregorianCalendar)); | |
| 209 CFCalendarSetTimeZone(gregorian, time_zone); | |
| 210 int year, month, day, hour, minute, second; | |
| 211 CFCalendarDecomposeAbsoluteTime(gregorian, seconds, "yMdHms", &year, &month, | |
|
sdefresne
2016/01/07 16:36:28
nit: you can use "yMdHmsE" to also extract cf_day_
pkl (ping after 24h if needed)
2016/02/03 06:42:31
Done. Added 'E'.
| |
| 212 &day, &hour, &minute, &second); | |
|
sdefresne
2016/01/07 16:36:28
nit: you can also use &exploded->year, ... to avoi
pkl (ping after 24h if needed)
2016/02/03 06:42:31
Changed to use &exploded->year etc. where appropri
| |
| 200 | 213 |
| 201 exploded->year = date.year; | 214 // Calculate the day of week using CFCalendar, which returns 1 = Sunday, |
| 202 exploded->month = date.month; | 215 // 2 = Monday, etc., then convert to |Exploded|'s 0 = Sunday convention. |
| 203 exploded->day_of_week = cf_day_of_week % 7; | 216 CFIndex cf_day_of_week = CFCalendarGetOrdinalityOfUnit( |
| 204 exploded->day_of_month = date.day; | 217 gregorian, kCFCalendarUnitWeekday, kCFCalendarUnitWeekOfMonth, seconds); |
| 205 exploded->hour = date.hour; | 218 |
| 206 exploded->minute = date.minute; | 219 exploded->year = year; |
| 220 exploded->month = month; | |
| 221 exploded->day_of_week = (cf_day_of_week - 1) % 7; | |
| 222 exploded->day_of_month = day; | |
| 223 exploded->hour = hour; | |
| 224 exploded->minute = minute; | |
| 207 // Make sure seconds are rounded down towards -infinity. | 225 // Make sure seconds are rounded down towards -infinity. |
| 208 exploded->second = floor(date.second); | 226 exploded->second = floor(second); |
| 209 // Calculate milliseconds ourselves, since we rounded the |seconds|, making | 227 // Calculate milliseconds ourselves, since we rounded the |seconds|, making |
| 210 // sure to round towards -infinity. | 228 // sure to round towards -infinity. |
| 211 exploded->millisecond = | 229 exploded->millisecond = |
| 212 (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond : | 230 (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond : |
| 213 (microsecond - kMicrosecondsPerMillisecond + 1) / | 231 (microsecond - kMicrosecondsPerMillisecond + 1) / |
| 214 kMicrosecondsPerMillisecond; | 232 kMicrosecondsPerMillisecond; |
| 215 } | 233 } |
| 216 | 234 |
| 217 // TimeTicks ------------------------------------------------------------------ | 235 // TimeTicks ------------------------------------------------------------------ |
| 218 | 236 |
| 219 // static | 237 // static |
| 220 TimeTicks TimeTicks::Now() { | 238 TimeTicks TimeTicks::Now() { |
| 221 return TimeTicks(ComputeCurrentTicks()); | 239 return TimeTicks(ComputeCurrentTicks()); |
| 222 } | 240 } |
| 223 | 241 |
| 224 // static | 242 // static |
| 225 bool TimeTicks::IsHighResolution() { | 243 bool TimeTicks::IsHighResolution() { |
| 226 return true; | 244 return true; |
| 227 } | 245 } |
| 228 | 246 |
| 229 // static | 247 // static |
| 230 ThreadTicks ThreadTicks::Now() { | 248 ThreadTicks ThreadTicks::Now() { |
| 231 return ThreadTicks(ComputeThreadTicks()); | 249 return ThreadTicks(ComputeThreadTicks()); |
| 232 } | 250 } |
| 233 | 251 |
| 234 } // namespace base | 252 } // namespace base |
| OLD | NEW |