OLD | NEW |
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 <cmath> | 7 #include <cmath> |
8 #include <ios> | 8 #include <ios> |
9 #include <limits> | 9 #include <limits> |
10 #include <ostream> | 10 #include <ostream> |
11 #include <sstream> | 11 #include <sstream> |
12 | 12 |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/third_party/nspr/prtime.h" | 17 #include "base/third_party/nspr/prtime.h" |
| 18 #include "build/build_config.h" |
17 | 19 |
18 namespace base { | 20 namespace base { |
19 | 21 |
20 // TimeDelta ------------------------------------------------------------------ | 22 // TimeDelta ------------------------------------------------------------------ |
21 | 23 |
22 // static | 24 // static |
23 TimeDelta TimeDelta::Max() { | 25 TimeDelta TimeDelta::Max() { |
24 return TimeDelta(std::numeric_limits<int64>::max()); | 26 return TimeDelta(std::numeric_limits<int64_t>::max()); |
25 } | 27 } |
26 | 28 |
27 int TimeDelta::InDays() const { | 29 int TimeDelta::InDays() const { |
28 if (is_max()) { | 30 if (is_max()) { |
29 // Preserve max to prevent overflow. | 31 // Preserve max to prevent overflow. |
30 return std::numeric_limits<int>::max(); | 32 return std::numeric_limits<int>::max(); |
31 } | 33 } |
32 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); | 34 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); |
33 } | 35 } |
34 | 36 |
(...skipping 14 matching lines...) Expand all Loading... |
49 } | 51 } |
50 | 52 |
51 double TimeDelta::InSecondsF() const { | 53 double TimeDelta::InSecondsF() const { |
52 if (is_max()) { | 54 if (is_max()) { |
53 // Preserve max to prevent overflow. | 55 // Preserve max to prevent overflow. |
54 return std::numeric_limits<double>::infinity(); | 56 return std::numeric_limits<double>::infinity(); |
55 } | 57 } |
56 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; | 58 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; |
57 } | 59 } |
58 | 60 |
59 int64 TimeDelta::InSeconds() const { | 61 int64_t TimeDelta::InSeconds() const { |
60 if (is_max()) { | 62 if (is_max()) { |
61 // Preserve max to prevent overflow. | 63 // Preserve max to prevent overflow. |
62 return std::numeric_limits<int64>::max(); | 64 return std::numeric_limits<int64_t>::max(); |
63 } | 65 } |
64 return delta_ / Time::kMicrosecondsPerSecond; | 66 return delta_ / Time::kMicrosecondsPerSecond; |
65 } | 67 } |
66 | 68 |
67 double TimeDelta::InMillisecondsF() const { | 69 double TimeDelta::InMillisecondsF() const { |
68 if (is_max()) { | 70 if (is_max()) { |
69 // Preserve max to prevent overflow. | 71 // Preserve max to prevent overflow. |
70 return std::numeric_limits<double>::infinity(); | 72 return std::numeric_limits<double>::infinity(); |
71 } | 73 } |
72 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; | 74 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; |
73 } | 75 } |
74 | 76 |
75 int64 TimeDelta::InMilliseconds() const { | 77 int64_t TimeDelta::InMilliseconds() const { |
76 if (is_max()) { | 78 if (is_max()) { |
77 // Preserve max to prevent overflow. | 79 // Preserve max to prevent overflow. |
78 return std::numeric_limits<int64>::max(); | 80 return std::numeric_limits<int64_t>::max(); |
79 } | 81 } |
80 return delta_ / Time::kMicrosecondsPerMillisecond; | 82 return delta_ / Time::kMicrosecondsPerMillisecond; |
81 } | 83 } |
82 | 84 |
83 int64 TimeDelta::InMillisecondsRoundedUp() const { | 85 int64_t TimeDelta::InMillisecondsRoundedUp() const { |
84 if (is_max()) { | 86 if (is_max()) { |
85 // Preserve max to prevent overflow. | 87 // Preserve max to prevent overflow. |
86 return std::numeric_limits<int64>::max(); | 88 return std::numeric_limits<int64_t>::max(); |
87 } | 89 } |
88 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / | 90 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / |
89 Time::kMicrosecondsPerMillisecond; | 91 Time::kMicrosecondsPerMillisecond; |
90 } | 92 } |
91 | 93 |
92 int64 TimeDelta::InMicroseconds() const { | 94 int64_t TimeDelta::InMicroseconds() const { |
93 if (is_max()) { | 95 if (is_max()) { |
94 // Preserve max to prevent overflow. | 96 // Preserve max to prevent overflow. |
95 return std::numeric_limits<int64>::max(); | 97 return std::numeric_limits<int64_t>::max(); |
96 } | 98 } |
97 return delta_; | 99 return delta_; |
98 } | 100 } |
99 | 101 |
100 namespace time_internal { | 102 namespace time_internal { |
101 | 103 |
102 int64 SaturatedAdd(TimeDelta delta, int64 value) { | 104 int64_t SaturatedAdd(TimeDelta delta, int64_t value) { |
103 CheckedNumeric<int64> rv(delta.delta_); | 105 CheckedNumeric<int64_t> rv(delta.delta_); |
104 rv += value; | 106 rv += value; |
105 return FromCheckedNumeric(rv); | 107 return FromCheckedNumeric(rv); |
106 } | 108 } |
107 | 109 |
108 int64 SaturatedSub(TimeDelta delta, int64 value) { | 110 int64_t SaturatedSub(TimeDelta delta, int64_t value) { |
109 CheckedNumeric<int64> rv(delta.delta_); | 111 CheckedNumeric<int64_t> rv(delta.delta_); |
110 rv -= value; | 112 rv -= value; |
111 return FromCheckedNumeric(rv); | 113 return FromCheckedNumeric(rv); |
112 } | 114 } |
113 | 115 |
114 int64 FromCheckedNumeric(const CheckedNumeric<int64> value) { | 116 int64_t FromCheckedNumeric(const CheckedNumeric<int64_t> value) { |
115 if (value.IsValid()) | 117 if (value.IsValid()) |
116 return value.ValueUnsafe(); | 118 return value.ValueUnsafe(); |
117 | 119 |
118 // We could return max/min but we don't really expose what the maximum delta | 120 // We could return max/min but we don't really expose what the maximum delta |
119 // is. Instead, return max/(-max), which is something that clients can reason | 121 // is. Instead, return max/(-max), which is something that clients can reason |
120 // about. | 122 // about. |
121 // TODO(rvargas) crbug.com/332611: don't use internal values. | 123 // TODO(rvargas) crbug.com/332611: don't use internal values. |
122 int64 limit = std::numeric_limits<int64>::max(); | 124 int64_t limit = std::numeric_limits<int64_t>::max(); |
123 if (value.validity() == internal::RANGE_UNDERFLOW) | 125 if (value.validity() == internal::RANGE_UNDERFLOW) |
124 limit = -limit; | 126 limit = -limit; |
125 return value.ValueOrDefault(limit); | 127 return value.ValueOrDefault(limit); |
126 } | 128 } |
127 | 129 |
128 } // namespace time_internal | 130 } // namespace time_internal |
129 | 131 |
130 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { | 132 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { |
131 return os << time_delta.InSecondsF() << "s"; | 133 return os << time_delta.InSecondsF() << "s"; |
132 } | 134 } |
133 | 135 |
134 // Time ----------------------------------------------------------------------- | 136 // Time ----------------------------------------------------------------------- |
135 | 137 |
136 // static | 138 // static |
137 Time Time::Max() { | 139 Time Time::Max() { |
138 return Time(std::numeric_limits<int64>::max()); | 140 return Time(std::numeric_limits<int64_t>::max()); |
139 } | 141 } |
140 | 142 |
141 // static | 143 // static |
142 Time Time::FromTimeT(time_t tt) { | 144 Time Time::FromTimeT(time_t tt) { |
143 if (tt == 0) | 145 if (tt == 0) |
144 return Time(); // Preserve 0 so we can tell it doesn't exist. | 146 return Time(); // Preserve 0 so we can tell it doesn't exist. |
145 if (tt == std::numeric_limits<time_t>::max()) | 147 if (tt == std::numeric_limits<time_t>::max()) |
146 return Max(); | 148 return Max(); |
147 return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSeconds(tt); | 149 return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSeconds(tt); |
148 } | 150 } |
149 | 151 |
150 time_t Time::ToTimeT() const { | 152 time_t Time::ToTimeT() const { |
151 if (is_null()) | 153 if (is_null()) |
152 return 0; // Preserve 0 so we can tell it doesn't exist. | 154 return 0; // Preserve 0 so we can tell it doesn't exist. |
153 if (is_max()) { | 155 if (is_max()) { |
154 // Preserve max without offset to prevent overflow. | 156 // Preserve max without offset to prevent overflow. |
155 return std::numeric_limits<time_t>::max(); | 157 return std::numeric_limits<time_t>::max(); |
156 } | 158 } |
157 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) { | 159 if (std::numeric_limits<int64_t>::max() - kTimeTToMicrosecondsOffset <= us_) { |
158 DLOG(WARNING) << "Overflow when converting base::Time with internal " << | 160 DLOG(WARNING) << "Overflow when converting base::Time with internal " << |
159 "value " << us_ << " to time_t."; | 161 "value " << us_ << " to time_t."; |
160 return std::numeric_limits<time_t>::max(); | 162 return std::numeric_limits<time_t>::max(); |
161 } | 163 } |
162 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; | 164 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; |
163 } | 165 } |
164 | 166 |
165 // static | 167 // static |
166 Time Time::FromDoubleT(double dt) { | 168 Time Time::FromDoubleT(double dt) { |
167 if (dt == 0 || std::isnan(dt)) | 169 if (dt == 0 || std::isnan(dt)) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 return 0; | 205 return 0; |
204 } | 206 } |
205 if (is_max()) { | 207 if (is_max()) { |
206 // Preserve max without offset to prevent overflow. | 208 // Preserve max without offset to prevent overflow. |
207 return std::numeric_limits<double>::infinity(); | 209 return std::numeric_limits<double>::infinity(); |
208 } | 210 } |
209 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / | 211 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
210 kMicrosecondsPerMillisecond); | 212 kMicrosecondsPerMillisecond); |
211 } | 213 } |
212 | 214 |
213 int64 Time::ToJavaTime() const { | 215 int64_t Time::ToJavaTime() const { |
214 if (is_null()) { | 216 if (is_null()) { |
215 // Preserve 0 so the invalid result doesn't depend on the platform. | 217 // Preserve 0 so the invalid result doesn't depend on the platform. |
216 return 0; | 218 return 0; |
217 } | 219 } |
218 if (is_max()) { | 220 if (is_max()) { |
219 // Preserve max without offset to prevent overflow. | 221 // Preserve max without offset to prevent overflow. |
220 return std::numeric_limits<int64>::max(); | 222 return std::numeric_limits<int64_t>::max(); |
221 } | 223 } |
222 return ((us_ - kTimeTToMicrosecondsOffset) / | 224 return ((us_ - kTimeTToMicrosecondsOffset) / |
223 kMicrosecondsPerMillisecond); | 225 kMicrosecondsPerMillisecond); |
224 } | 226 } |
225 | 227 |
226 // static | 228 // static |
227 Time Time::UnixEpoch() { | 229 Time Time::UnixEpoch() { |
228 Time time; | 230 Time time; |
229 time.us_ = kTimeTToMicrosecondsOffset; | 231 time.us_ = kTimeTToMicrosecondsOffset; |
230 return time; | 232 return time; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 return is_in_range(month, 1, 12) && | 338 return is_in_range(month, 1, 12) && |
337 is_in_range(day_of_week, 0, 6) && | 339 is_in_range(day_of_week, 0, 6) && |
338 is_in_range(day_of_month, 1, 31) && | 340 is_in_range(day_of_month, 1, 31) && |
339 is_in_range(hour, 0, 23) && | 341 is_in_range(hour, 0, 23) && |
340 is_in_range(minute, 0, 59) && | 342 is_in_range(minute, 0, 59) && |
341 is_in_range(second, 0, 60) && | 343 is_in_range(second, 0, 60) && |
342 is_in_range(millisecond, 0, 999); | 344 is_in_range(millisecond, 0, 999); |
343 } | 345 } |
344 | 346 |
345 } // namespace base | 347 } // namespace base |
OLD | NEW |