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

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

Issue 1988663002: Add: check exploded time is properly converted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mmenke comments 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) 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 <stdint.h> 7 #include <stdint.h>
8 #include <sys/time.h> 8 #include <sys/time.h>
9 #include <time.h> 9 #include <time.h>
10 #if defined(OS_ANDROID) && !defined(__LP64__) 10 #if defined(OS_ANDROID) && !defined(__LP64__)
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 exploded->month = timestruct.tm_mon + 1; 204 exploded->month = timestruct.tm_mon + 1;
205 exploded->day_of_week = timestruct.tm_wday; 205 exploded->day_of_week = timestruct.tm_wday;
206 exploded->day_of_month = timestruct.tm_mday; 206 exploded->day_of_month = timestruct.tm_mday;
207 exploded->hour = timestruct.tm_hour; 207 exploded->hour = timestruct.tm_hour;
208 exploded->minute = timestruct.tm_min; 208 exploded->minute = timestruct.tm_min;
209 exploded->second = timestruct.tm_sec; 209 exploded->second = timestruct.tm_sec;
210 exploded->millisecond = millisecond; 210 exploded->millisecond = millisecond;
211 } 211 }
212 212
213 // static 213 // static
214 Time Time::FromExploded(bool is_local, const Exploded& exploded) { 214 bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {
215 struct tm timestruct; 215 struct tm timestruct;
216 timestruct.tm_sec = exploded.second; 216 timestruct.tm_sec = exploded.second;
217 timestruct.tm_min = exploded.minute; 217 timestruct.tm_min = exploded.minute;
218 timestruct.tm_hour = exploded.hour; 218 timestruct.tm_hour = exploded.hour;
219 timestruct.tm_mday = exploded.day_of_month; 219 timestruct.tm_mday = exploded.day_of_month;
220 timestruct.tm_mon = exploded.month - 1; 220 timestruct.tm_mon = exploded.month - 1;
221 timestruct.tm_year = exploded.year - 1900; 221 timestruct.tm_year = exploded.year - 1900;
222 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this 222 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this
223 timestruct.tm_yday = 0; // mktime/timegm ignore this 223 timestruct.tm_yday = 0; // mktime/timegm ignore this
224 timestruct.tm_isdst = -1; // attempt to figure it out 224 timestruct.tm_isdst = -1; // attempt to figure it out
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 milliseconds = min_seconds * kMillisecondsPerSecond; 294 milliseconds = min_seconds * kMillisecondsPerSecond;
295 } else { 295 } else {
296 milliseconds = max_seconds * kMillisecondsPerSecond; 296 milliseconds = max_seconds * kMillisecondsPerSecond;
297 milliseconds += (kMillisecondsPerSecond - 1); 297 milliseconds += (kMillisecondsPerSecond - 1);
298 } 298 }
299 } else { 299 } else {
300 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; 300 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond;
301 } 301 }
302 302
303 // Adjust from Unix (1970) to Windows (1601) epoch. 303 // Adjust from Unix (1970) to Windows (1601) epoch.
304 return Time((milliseconds * kMicrosecondsPerMillisecond) + 304 base::Time out_time = Time((milliseconds * kMicrosecondsPerMillisecond) +
305 kWindowsEpochDeltaMicroseconds); 305 kWindowsEpochDeltaMicroseconds);
306
307 // If |exploded.day_of_month| is set to 31 on a 28-30 day month, it will
308 // return the first day of the next month. Thus round-trip the time and
309 // compare the initial |exploded| with |utc_to_exploded| time.
310 base::Time::Exploded to_exploded;
311 if (!is_local)
312 out_time.UTCExplode(&to_exploded);
313 else
314 out_time.LocalExplode(&to_exploded);
315
316 *time = to_exploded != exploded ? Time(0) : out_time;
317
318 // If time is null, return false
319 // which means time conversion failed
320 return (*time).is_null() ? false : true;
mmenke 2016/05/24 16:08:33 See comment in mac file.
maksims (do not use this acc) 2016/05/26 04:38:18 Done.
306 } 321 }
307 322
308 // TimeTicks ------------------------------------------------------------------ 323 // TimeTicks ------------------------------------------------------------------
309 // static 324 // static
310 TimeTicks TimeTicks::Now() { 325 TimeTicks TimeTicks::Now() {
311 return TimeTicks(ClockNow(CLOCK_MONOTONIC)); 326 return TimeTicks(ClockNow(CLOCK_MONOTONIC));
312 } 327 }
313 328
314 // static 329 // static
315 TimeTicks::Clock TimeTicks::GetClock() { 330 TimeTicks::Clock TimeTicks::GetClock() {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 374 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
360 return result; 375 return result;
361 } 376 }
362 int64_t us = us_ - kTimeTToMicrosecondsOffset; 377 int64_t us = us_ - kTimeTToMicrosecondsOffset;
363 result.tv_sec = us / Time::kMicrosecondsPerSecond; 378 result.tv_sec = us / Time::kMicrosecondsPerSecond;
364 result.tv_usec = us % Time::kMicrosecondsPerSecond; 379 result.tv_usec = us % Time::kMicrosecondsPerSecond;
365 return result; 380 return result;
366 } 381 }
367 382
368 } // namespace base 383 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698