| 6 |
| 7 | #include <CoreFoundation/CFDate.h> |
| 8 | #include <CoreFoundation/CFCalendar.h> |
| 9 | #include <CoreFoundation/CFTimeZone.h> |
| 10 |
| 11 | #include "base/mac/scoped_cftyperef.h" |
| 12 |
| 13 | #if __LP64__ |
| 14 | #error Use posix implementation on 64-bit platforms. |
| 15 | #endif // __LP64__ |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | // Note: These implementations of Time::FromExploded() and Time::Explode() are |
| 20 | // only used on iOS now. Since Mac is now always 64-bit, we can use the POSIX |
| 21 | // versions of these functions as time_t is not capped at year 2038 on 64-bit |
| 22 | // builds. The POSIX functions are preferred since they don't suffer from some |
| 23 | // performance problems that are present in these implementations. |
| 24 | // See crbug.com/781601, crbug.com/985061 for more details. |
| 25 |
| 26 | // static |
| 27 | bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) { |
| 28 | base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
| 29 | is_local |
| 30 | ? CFTimeZoneCopySystem() |
| 31 | : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); |
| 32 | base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier( |
| 33 | kCFAllocatorDefault, kCFGregorianCalendar)); |
| 34 | CFCalendarSetTimeZone(gregorian, time_zone); |
| 35 | CFAbsoluteTime absolute_time; |
| 36 | // 'S' is not defined in componentDesc in Apple documentation, but can be |
| 37 | // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c |
| 38 | CFCalendarComposeAbsoluteTime( |
| 39 | gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, |
| 40 | exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, |
| 41 | exploded.millisecond); |
| 42 | CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; |
| 43 |
| 44 | // CFAbsolutTime is typedef of double. Convert seconds to |
| 45 | // microseconds and then cast to int64. If |
| 46 | // it cannot be suited to int64, then fail to avoid overflows. |
| 47 | double microseconds = |
| 48 | (seconds * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset; |
| 49 | if (microseconds > std::numeric_limits<int64_t>::max() || |
| 50 | microseconds < std::numeric_limits<int64_t>::min()) { |
| 51 | *time = Time(0); |
| 52 | return false; |
| 53 | } |
| 54 |
| 55 | base::Time converted_time = Time(static_cast<int64_t>(microseconds)); |
| 56 |
| 57 | // If |exploded.day_of_month| is set to 31 |
| 58 | // on a 28-30 day month, it will return the first day of the next month. |
| 59 | // Thus round-trip the time and compare the initial |exploded| with |
| 60 | // |utc_to_exploded| time. |
| 61 | base::Time::Exploded to_exploded; |
| 62 | if (!is_local) |
| 63 | converted_time.UTCExplode(&to_exploded); |
| 64 | else |
| 65 | converted_time.LocalExplode(&to_exploded); |
| 66 |
| 67 | if (ExplodedMostlyEquals(to_exploded, exploded)) { |
| 68 | *time = converted_time; |
| 69 | return true; |
| 70 | } |
| 71 |
| 72 | *time = Time(0); |
| 73 | return false; |
| 74 | } |
| 75 |
| 76 | void Time::Explode(bool is_local, Exploded* exploded) const { |
| 77 | // Avoid rounding issues, by only putting the integral number of seconds |
| 78 | // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
| 79 | int64_t microsecond = us_ % kMicrosecondsPerSecond; |
| 80 | if (microsecond < 0) |
| 81 | microsecond += kMicrosecondsPerSecond; |
| 82 | CFAbsoluteTime seconds = ((us_ - microsecond - kTimeTToMicrosecondsOffset) / |
| 83 | kMicrosecondsPerSecond) - |
| 84 | kCFAbsoluteTimeIntervalSince1970; |
| 85 |
| 86 | base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
| 87 | is_local |
| 88 | ? CFTimeZoneCopySystem() |
| 89 | : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); |
| 90 | base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier( |
| 91 | kCFAllocatorDefault, kCFGregorianCalendar)); |
| 92 | CFCalendarSetTimeZone(gregorian, time_zone); |
| 93 | int second, day_of_week; |
| 94 | // 'E' sets the day of week, but is not defined in componentDesc in Apple |
| 95 | // documentation. It can be found in open source code here: |
| 96 | // http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c |
| 97 | CFCalendarDecomposeAbsoluteTime(gregorian, seconds, "yMdHmsE", |
| 98 | &exploded->year, &exploded->month, |
| 99 | &exploded->day_of_month, &exploded->hour, |
| 100 | &exploded->minute, &second, &day_of_week); |
| 101 | // Make sure seconds are rounded down towards -infinity. |
| 102 | exploded->second = floor(second); |
| 103 | // |Exploded|'s convention for day of week is 0 = Sunday, i.e. different |
| 104 | // from CF's 1 = Sunday. |
| 105 | exploded->day_of_week = (day_of_week - 1) % 7; |
| 106 | // Calculate milliseconds ourselves, since we rounded the |seconds|, making |
| 107 | // sure to round towards -infinity. |
| 108 | exploded->millisecond = |
| 109 | (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond |
| 110 | : ((microsecond + 1) / kMicrosecondsPerMillisecond - 1); |
| 111 | } |
| 112 |
| 113 | } // namespace base |
| 114 |