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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: base/time/time_mac.cc
diff --git a/base/time/time_mac.cc b/base/time/time_mac.cc
index c23c4917e757f492ab7a6cc151158a55901878ef..9863f871a0b99c6f065afcc120511b915fd7ef39 100644
--- a/base/time/time_mac.cc
+++ b/base/time/time_mac.cc
@@ -184,8 +184,61 @@ Time Time::FromExploded(bool is_local, const Exploded& exploded) {
exploded.day_of_month, exploded.hour, exploded.minute, exploded.second,
exploded.millisecond);
CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970;
- return Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) +
- kWindowsEpochDeltaMicroseconds);
+
+ base::Time t_time =
mmenke 2016/05/20 19:44:56 nit: t_time seems like a weird variable name - it
maksims (do not use this acc) 2016/05/24 09:14:21 Done. I already have a "time" in the function argu
+ Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) +
+ kWindowsEpochDeltaMicroseconds);
+
+ // If |exploded.day_of_month| is set to 31
+ // on a 28-30 day month, it will return the first day of the next month.
+ // Thus round-trip the time and compare the initial |exploded| with
+ // |utc_to_exploded| time.
mmenke 2016/05/20 19:44:56 nit: Comment can be reformatted - first line of t
maksims (do not use this acc) 2016/05/24 09:14:21 Done.
+ base::Time::Exploded to_exploded;
+ if (!is_local)
+ t_time.UTCExplode(&to_exploded);
+ else
+ t_time.LocalExplode(&to_exploded);
+
+ return to_exploded != exploded ? Time(0) : t_time;
+}
+
+// static
+bool Time::FromExploded(bool is_local, const Exploded& exploded, Time& time) {
+ base::ScopedCFTypeRef<CFTimeZoneRef> time_zone(
+ is_local
+ ? CFTimeZoneCopySystem()
+ : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0));
+ base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier(
+ kCFAllocatorDefault, kCFGregorianCalendar));
+ CFCalendarSetTimeZone(gregorian, time_zone);
+ CFAbsoluteTime absolute_time;
+ // 'S' is not defined in componentDesc in Apple documentation, but can be
+ // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c
+ CFCalendarComposeAbsoluteTime(
+ gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month,
+ exploded.day_of_month, exploded.hour, exploded.minute, exploded.second,
+ exploded.millisecond);
+ CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970;
+
+ base::Time t_time =
+ Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) +
+ kWindowsEpochDeltaMicroseconds);
+
+ // If |exploded.day_of_month| is set to 31
+ // on a 28-30 day month, it will return the first day of the next month.
+ // Thus round-trip the time and compare the initial |exploded| with
+ // |utc_to_exploded| time.
+ base::Time::Exploded to_exploded;
+ if (!is_local)
+ t_time.UTCExplode(&to_exploded);
+ else
+ t_time.LocalExplode(&to_exploded);
+
+ time = to_exploded != exploded ? Time(0) : t_time;
+
+ // If time is null, return false
+ // which means time conversion failed
+ return time.is_null() ? false : true;
}
void Time::Explode(bool is_local, Exploded* exploded) const {

Powered by Google App Engine
This is Rietveld 408576698