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 <limits> | 7 #include <limits> |
8 #include <ostream> | 8 #include <ostream> |
9 | 9 |
10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
| 11 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/third_party/nspr/prtime.h" | 13 #include "base/third_party/nspr/prtime.h" |
13 #include "base/third_party/nspr/prtypes.h" | 14 #include "base/third_party/nspr/prtypes.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 // TimeDelta ------------------------------------------------------------------ | 18 // TimeDelta ------------------------------------------------------------------ |
18 | 19 |
19 int TimeDelta::InDays() const { | 20 int TimeDelta::InDays() const { |
20 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); | 21 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 is_local ? PR_FALSE : PR_TRUE, | 183 is_local ? PR_FALSE : PR_TRUE, |
183 &result_time); | 184 &result_time); |
184 if (PR_SUCCESS != result) | 185 if (PR_SUCCESS != result) |
185 return false; | 186 return false; |
186 | 187 |
187 result_time += kTimeTToMicrosecondsOffset; | 188 result_time += kTimeTToMicrosecondsOffset; |
188 *parsed_time = Time(result_time); | 189 *parsed_time = Time(result_time); |
189 return true; | 190 return true; |
190 } | 191 } |
191 | 192 |
| 193 // Local helper class to hold the conversion from Time to TickTime at the |
| 194 // time of the Unix epoch. |
| 195 class UnixEpochSingleton { |
| 196 public: |
| 197 UnixEpochSingleton() |
| 198 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {} |
| 199 |
| 200 TimeTicks unix_epoch() const { return unix_epoch_; } |
| 201 |
| 202 private: |
| 203 const TimeTicks unix_epoch_; |
| 204 |
| 205 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton); |
| 206 }; |
| 207 |
| 208 static LazyInstance<UnixEpochSingleton>::Leaky |
| 209 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER; |
| 210 |
| 211 // Static |
| 212 TimeTicks TimeTicks::UnixEpoch() { |
| 213 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); |
| 214 } |
| 215 |
192 // Time::Exploded ------------------------------------------------------------- | 216 // Time::Exploded ------------------------------------------------------------- |
193 | 217 |
194 inline bool is_in_range(int value, int lo, int hi) { | 218 inline bool is_in_range(int value, int lo, int hi) { |
195 return lo <= value && value <= hi; | 219 return lo <= value && value <= hi; |
196 } | 220 } |
197 | 221 |
198 bool Time::Exploded::HasValidValues() const { | 222 bool Time::Exploded::HasValidValues() const { |
199 return is_in_range(month, 1, 12) && | 223 return is_in_range(month, 1, 12) && |
200 is_in_range(day_of_week, 0, 6) && | 224 is_in_range(day_of_week, 0, 6) && |
201 is_in_range(day_of_month, 1, 31) && | 225 is_in_range(day_of_month, 1, 31) && |
202 is_in_range(hour, 0, 23) && | 226 is_in_range(hour, 0, 23) && |
203 is_in_range(minute, 0, 59) && | 227 is_in_range(minute, 0, 59) && |
204 is_in_range(second, 0, 60) && | 228 is_in_range(second, 0, 60) && |
205 is_in_range(millisecond, 0, 999); | 229 is_in_range(millisecond, 0, 999); |
206 } | 230 } |
207 | 231 |
208 } // namespace base | 232 } // namespace base |
OLD | NEW |