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.h" | 5 #include "base/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_time.h> | 9 #include <mach/mach_time.h> |
10 #include <sys/sysctl.h> | 10 #include <sys/sysctl.h> |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 132 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
133 CFGregorianDate date; | 133 CFGregorianDate date; |
134 date.second = exploded.second + | 134 date.second = exploded.second + |
135 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); | 135 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); |
136 date.minute = exploded.minute; | 136 date.minute = exploded.minute; |
137 date.hour = exploded.hour; | 137 date.hour = exploded.hour; |
138 date.day = exploded.day_of_month; | 138 date.day = exploded.day_of_month; |
139 date.month = exploded.month; | 139 date.month = exploded.month; |
140 date.year = exploded.year; | 140 date.year = exploded.year; |
141 | 141 |
142 base::mac::ScopedCFTypeRef<CFTimeZoneRef> | 142 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
143 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); | 143 is_local ? CFTimeZoneCopySystem() : NULL); |
144 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + | 144 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + |
145 kCFAbsoluteTimeIntervalSince1970; | 145 kCFAbsoluteTimeIntervalSince1970; |
146 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + | 146 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + |
147 kWindowsEpochDeltaMicroseconds); | 147 kWindowsEpochDeltaMicroseconds); |
148 } | 148 } |
149 | 149 |
150 void Time::Explode(bool is_local, Exploded* exploded) const { | 150 void Time::Explode(bool is_local, Exploded* exploded) const { |
151 // Avoid rounding issues, by only putting the integral number of seconds | 151 // Avoid rounding issues, by only putting the integral number of seconds |
152 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). | 152 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
153 int64 microsecond = us_ % kMicrosecondsPerSecond; | 153 int64 microsecond = us_ % kMicrosecondsPerSecond; |
154 if (microsecond < 0) | 154 if (microsecond < 0) |
155 microsecond += kMicrosecondsPerSecond; | 155 microsecond += kMicrosecondsPerSecond; |
156 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - | 156 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - |
157 kWindowsEpochDeltaSeconds - | 157 kWindowsEpochDeltaSeconds - |
158 kCFAbsoluteTimeIntervalSince1970; | 158 kCFAbsoluteTimeIntervalSince1970; |
159 | 159 |
160 base::mac::ScopedCFTypeRef<CFTimeZoneRef> | 160 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
161 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); | 161 is_local ? CFTimeZoneCopySystem() : NULL); |
162 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); | 162 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); |
163 // 1 = Monday, ..., 7 = Sunday. | 163 // 1 = Monday, ..., 7 = Sunday. |
164 int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone); | 164 int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone); |
165 | 165 |
166 exploded->year = date.year; | 166 exploded->year = date.year; |
167 exploded->month = date.month; | 167 exploded->month = date.month; |
168 exploded->day_of_week = cf_day_of_week % 7; | 168 exploded->day_of_week = cf_day_of_week % 7; |
169 exploded->day_of_month = date.day; | 169 exploded->day_of_month = date.day; |
170 exploded->hour = date.hour; | 170 exploded->hour = date.hour; |
171 exploded->minute = date.minute; | 171 exploded->minute = date.minute; |
(...skipping 18 matching lines...) Expand all Loading... |
190 TimeTicks TimeTicks::HighResNow() { | 190 TimeTicks TimeTicks::HighResNow() { |
191 return Now(); | 191 return Now(); |
192 } | 192 } |
193 | 193 |
194 // static | 194 // static |
195 TimeTicks TimeTicks::NowFromSystemTraceTime() { | 195 TimeTicks TimeTicks::NowFromSystemTraceTime() { |
196 return HighResNow(); | 196 return HighResNow(); |
197 } | 197 } |
198 | 198 |
199 } // namespace base | 199 } // namespace base |
OLD | NEW |