Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: base/time/time_mac.cc

Issue 1988663002: Add: check exploded time is properly converted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: handling local and utc times + unittests Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 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>
11 #include <stddef.h> 11 #include <stddef.h>
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 =
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.
mmenke 2016/05/18 17:32:00 We should figure out what platforms this happens o
eroman 2016/05/18 19:35:17 In the case of our _posix implementation, the spec
mmenke 2016/05/18 20:11:32 That's a good point...I was mostly thinking (hopin
maksims (do not use this acc) 2016/05/19 09:57:20 What's then? Can you run trybots to see what envir
194 // Thus round-trip the time and compare the initial |exploded| with
195 // |utc_to_exploded| time.
196 // If they are not same, return Time(0).
197 // Windows and Posix implementations return Time(0) on failure.
mmenke 2016/05/18 17:32:00 These last two sentences seem not to be needed.
maksims (do not use this acc) 2016/05/19 09:57:20 Done.
198 base::Time::Exploded to_exploded = {0};
Mark Mentovai 2016/05/18 17:42:49 No need to waste time {0}-initializing.
maksims (do not use this acc) 2016/05/19 09:57:20 Done.
199 if (!is_local)
200 t_time.UTCExplode(&to_exploded);
201 else
202 t_time.LocalExplode(&to_exploded);
203
204 return to_exploded != exploded ? Time(0) : t_time;
189 } 205 }
190 206
191 void Time::Explode(bool is_local, Exploded* exploded) const { 207 void Time::Explode(bool is_local, Exploded* exploded) const {
192 // Avoid rounding issues, by only putting the integral number of seconds 208 // Avoid rounding issues, by only putting the integral number of seconds
193 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). 209 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|).
194 int64_t microsecond = us_ % kMicrosecondsPerSecond; 210 int64_t microsecond = us_ % kMicrosecondsPerSecond;
195 if (microsecond < 0) 211 if (microsecond < 0)
196 microsecond += kMicrosecondsPerSecond; 212 microsecond += kMicrosecondsPerSecond;
197 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - 213 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) -
198 kWindowsEpochDeltaSeconds - 214 kWindowsEpochDeltaSeconds -
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 return Clock::MAC_MACH_ABSOLUTE_TIME; 262 return Clock::MAC_MACH_ABSOLUTE_TIME;
247 #endif // defined(OS_IOS) 263 #endif // defined(OS_IOS)
248 } 264 }
249 265
250 // static 266 // static
251 ThreadTicks ThreadTicks::Now() { 267 ThreadTicks ThreadTicks::Now() {
252 return ThreadTicks(ComputeThreadTicks()); 268 return ThreadTicks(ComputeThreadTicks());
253 } 269 }
254 270
255 } // namespace base 271 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698