[email protected] | 527c7f3 | 2012-06-06 05:04:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include "base/time.h" |
[email protected] | 527c7f3 | 2012-06-06 05:04:22 | [diff] [blame] | 6 | |
| 7 | #include <math.h> |
| 8 | #if defined(OS_WIN) |
| 9 | #include <float.h> |
| 10 | #endif |
| 11 | |
[email protected] | c462caf | 2012-08-27 18:44:03 | [diff] [blame] | 12 | #include <limits> |
| 13 | |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 14 | #include "base/strings/sys_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | #include "base/third_party/nspr/prtime.h" |
| 16 | |
| 17 | #include "base/logging.h" |
| 18 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 19 | namespace base { |
| 20 | |
[email protected] | 527c7f3 | 2012-06-06 05:04:22 | [diff] [blame] | 21 | namespace { |
| 22 | #if defined(OS_WIN) |
| 23 | inline bool isnan(double num) { return !!_isnan(num); } |
| 24 | #endif |
| 25 | } |
| 26 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | // TimeDelta ------------------------------------------------------------------ |
| 28 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | int TimeDelta::InDays() const { |
| 30 | return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); |
| 31 | } |
| 32 | |
| 33 | int TimeDelta::InHours() const { |
| 34 | return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); |
| 35 | } |
| 36 | |
[email protected] | 9f25130 | 2008-08-19 09:16:49 | [diff] [blame] | 37 | int TimeDelta::InMinutes() const { |
| 38 | return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); |
| 39 | } |
| 40 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | double TimeDelta::InSecondsF() const { |
| 42 | return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; |
| 43 | } |
| 44 | |
| 45 | int64 TimeDelta::InSeconds() const { |
| 46 | return delta_ / Time::kMicrosecondsPerSecond; |
| 47 | } |
| 48 | |
| 49 | double TimeDelta::InMillisecondsF() const { |
| 50 | return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; |
| 51 | } |
| 52 | |
| 53 | int64 TimeDelta::InMilliseconds() const { |
| 54 | return delta_ / Time::kMicrosecondsPerMillisecond; |
| 55 | } |
| 56 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 57 | int64 TimeDelta::InMillisecondsRoundedUp() const { |
| 58 | return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / |
| 59 | Time::kMicrosecondsPerMillisecond; |
| 60 | } |
| 61 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | int64 TimeDelta::InMicroseconds() const { |
| 63 | return delta_; |
| 64 | } |
| 65 | |
| 66 | // Time ----------------------------------------------------------------------- |
| 67 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | // static |
[email protected] | c462caf | 2012-08-27 18:44:03 | [diff] [blame] | 69 | Time Time::Max() { |
| 70 | return Time(std::numeric_limits<int64>::max()); |
| 71 | } |
| 72 | |
| 73 | // static |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | Time Time::FromTimeT(time_t tt) { |
| 75 | if (tt == 0) |
| 76 | return Time(); // Preserve 0 so we can tell it doesn't exist. |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 77 | if (tt == std::numeric_limits<time_t>::max()) |
| 78 | return Max(); |
[email protected] | b842d4c6 | 2009-09-14 18:49:03 | [diff] [blame] | 79 | return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | time_t Time::ToTimeT() const { |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 83 | if (is_null()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 84 | return 0; // Preserve 0 so we can tell it doesn't exist. |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 85 | if (is_max()) { |
| 86 | // Preserve max without offset to prevent overflow. |
| 87 | return std::numeric_limits<time_t>::max(); |
| 88 | } |
| 89 | if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) { |
| 90 | DLOG(WARNING) << "Overflow when converting base::Time with internal " << |
| 91 | "value " << us_ << " to time_t."; |
| 92 | return std::numeric_limits<time_t>::max(); |
| 93 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; |
| 95 | } |
| 96 | |
[email protected] | c20210e6 | 2009-04-03 21:39:26 | [diff] [blame] | 97 | // static |
| 98 | Time Time::FromDoubleT(double dt) { |
[email protected] | 527c7f3 | 2012-06-06 05:04:22 | [diff] [blame] | 99 | if (dt == 0 || isnan(dt)) |
[email protected] | c925ad1 | 2010-09-09 17:17:55 | [diff] [blame] | 100 | return Time(); // Preserve 0 so we can tell it doesn't exist. |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 101 | if (dt == std::numeric_limits<double>::max()) |
| 102 | return Max(); |
[email protected] | b842d4c6 | 2009-09-14 18:49:03 | [diff] [blame] | 103 | return Time(static_cast<int64>((dt * |
| 104 | static_cast<double>(kMicrosecondsPerSecond)) + |
| 105 | kTimeTToMicrosecondsOffset)); |
[email protected] | c20210e6 | 2009-04-03 21:39:26 | [diff] [blame] | 106 | } |
| 107 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 108 | double Time::ToDoubleT() const { |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 109 | if (is_null()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | return 0; // Preserve 0 so we can tell it doesn't exist. |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 111 | if (is_max()) { |
| 112 | // Preserve max without offset to prevent overflow. |
| 113 | return std::numeric_limits<double>::max(); |
| 114 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 116 | static_cast<double>(kMicrosecondsPerSecond)); |
| 117 | } |
| 118 | |
[email protected] | c925ad1 | 2010-09-09 17:17:55 | [diff] [blame] | 119 | // static |
[email protected] | 42132be | 2012-06-08 22:59:33 | [diff] [blame] | 120 | Time Time::FromJsTime(double ms_since_epoch) { |
| 121 | // The epoch is a valid time, so this constructor doesn't interpret |
| 122 | // 0 as the null time. |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 123 | if (ms_since_epoch == std::numeric_limits<double>::max()) |
| 124 | return Max(); |
[email protected] | 42132be | 2012-06-08 22:59:33 | [diff] [blame] | 125 | return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + |
| 126 | kTimeTToMicrosecondsOffset); |
| 127 | } |
| 128 | |
| 129 | double Time::ToJsTime() const { |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 130 | if (is_null()) { |
[email protected] | 42132be | 2012-06-08 22:59:33 | [diff] [blame] | 131 | // Preserve 0 so the invalid result doesn't depend on the platform. |
| 132 | return 0; |
| 133 | } |
[email protected] | a9e5f044 | 2012-09-08 17:50:07 | [diff] [blame] | 134 | if (is_max()) { |
| 135 | // Preserve max without offset to prevent overflow. |
| 136 | return std::numeric_limits<double>::max(); |
| 137 | } |
[email protected] | 42132be | 2012-06-08 22:59:33 | [diff] [blame] | 138 | return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 139 | kMicrosecondsPerMillisecond); |
| 140 | } |
| 141 | |
| 142 | // static |
[email protected] | c925ad1 | 2010-09-09 17:17:55 | [diff] [blame] | 143 | Time Time::UnixEpoch() { |
| 144 | Time time; |
| 145 | time.us_ = kTimeTToMicrosecondsOffset; |
| 146 | return time; |
| 147 | } |
| 148 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | Time Time::LocalMidnight() const { |
| 150 | Exploded exploded; |
| 151 | LocalExplode(&exploded); |
| 152 | exploded.hour = 0; |
| 153 | exploded.minute = 0; |
| 154 | exploded.second = 0; |
| 155 | exploded.millisecond = 0; |
| 156 | return FromLocalExploded(exploded); |
| 157 | } |
| 158 | |
| 159 | // static |
[email protected] | 6e6acc10 | 2012-10-29 22:54:58 | [diff] [blame] | 160 | bool Time::FromStringInternal(const char* time_string, |
| 161 | bool is_local, |
| 162 | Time* parsed_time) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | DCHECK((time_string != NULL) && (parsed_time != NULL)); |
[email protected] | 46470aa | 2011-08-03 05:28:10 | [diff] [blame] | 164 | |
| 165 | if (time_string[0] == '\0') |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | return false; |
[email protected] | 46470aa | 2011-08-03 05:28:10 | [diff] [blame] | 167 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 168 | PRTime result_time = 0; |
[email protected] | 6e6acc10 | 2012-10-29 22:54:58 | [diff] [blame] | 169 | PRStatus result = PR_ParseTimeString(time_string, |
| 170 | is_local ? PR_FALSE : PR_TRUE, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 171 | &result_time); |
| 172 | if (PR_SUCCESS != result) |
| 173 | return false; |
[email protected] | 46470aa | 2011-08-03 05:28:10 | [diff] [blame] | 174 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 175 | result_time += kTimeTToMicrosecondsOffset; |
| 176 | *parsed_time = Time(result_time); |
| 177 | return true; |
| 178 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 179 | |
[email protected] | e241908 | 2010-07-27 02:43:20 | [diff] [blame] | 180 | // Time::Exploded ------------------------------------------------------------- |
| 181 | |
| 182 | inline bool is_in_range(int value, int lo, int hi) { |
| 183 | return lo <= value && value <= hi; |
| 184 | } |
| 185 | |
| 186 | bool Time::Exploded::HasValidValues() const { |
| 187 | return is_in_range(month, 1, 12) && |
| 188 | is_in_range(day_of_week, 0, 6) && |
| 189 | is_in_range(day_of_month, 1, 31) && |
| 190 | is_in_range(hour, 0, 23) && |
| 191 | is_in_range(minute, 0, 59) && |
| 192 | is_in_range(second, 0, 60) && |
| 193 | is_in_range(millisecond, 0, 999); |
| 194 | } |
| 195 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 196 | } // namespace base |