[email protected] | 91665412 | 2011-04-26 19:07:08 | [diff] [blame] | 1 | // Copyright (c) 2009 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] | a3153c47 | 2009-02-04 19:38:30 | [diff] [blame] | 6 | #include "base/sys_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | #include "base/third_party/nspr/prtime.h" |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 11 | namespace base { |
| 12 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | // TimeDelta ------------------------------------------------------------------ |
| 14 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | int TimeDelta::InDays() const { |
| 16 | return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); |
| 17 | } |
| 18 | |
| 19 | int TimeDelta::InHours() const { |
| 20 | return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); |
| 21 | } |
| 22 | |
[email protected] | 9f25130 | 2008-08-19 09:16:49 | [diff] [blame] | 23 | int TimeDelta::InMinutes() const { |
| 24 | return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); |
| 25 | } |
| 26 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | double TimeDelta::InSecondsF() const { |
| 28 | return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; |
| 29 | } |
| 30 | |
| 31 | int64 TimeDelta::InSeconds() const { |
| 32 | return delta_ / Time::kMicrosecondsPerSecond; |
| 33 | } |
| 34 | |
| 35 | double TimeDelta::InMillisecondsF() const { |
| 36 | return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; |
| 37 | } |
| 38 | |
| 39 | int64 TimeDelta::InMilliseconds() const { |
| 40 | return delta_ / Time::kMicrosecondsPerMillisecond; |
| 41 | } |
| 42 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 43 | int64 TimeDelta::InMillisecondsRoundedUp() const { |
| 44 | return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / |
| 45 | Time::kMicrosecondsPerMillisecond; |
| 46 | } |
| 47 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | int64 TimeDelta::InMicroseconds() const { |
| 49 | return delta_; |
| 50 | } |
| 51 | |
| 52 | // Time ----------------------------------------------------------------------- |
| 53 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | // static |
| 55 | Time Time::FromTimeT(time_t tt) { |
| 56 | if (tt == 0) |
| 57 | return Time(); // Preserve 0 so we can tell it doesn't exist. |
[email protected] | b842d4c6 | 2009-09-14 18:49:03 | [diff] [blame] | 58 | return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | time_t Time::ToTimeT() const { |
| 62 | if (us_ == 0) |
| 63 | return 0; // Preserve 0 so we can tell it doesn't exist. |
| 64 | return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; |
| 65 | } |
| 66 | |
[email protected] | c20210e6 | 2009-04-03 21:39:26 | [diff] [blame] | 67 | // static |
| 68 | Time Time::FromDoubleT(double dt) { |
[email protected] | c925ad1 | 2010-09-09 17:17:55 | [diff] [blame] | 69 | if (dt == 0) |
| 70 | return Time(); // Preserve 0 so we can tell it doesn't exist. |
[email protected] | b842d4c6 | 2009-09-14 18:49:03 | [diff] [blame] | 71 | return Time(static_cast<int64>((dt * |
| 72 | static_cast<double>(kMicrosecondsPerSecond)) + |
| 73 | kTimeTToMicrosecondsOffset)); |
[email protected] | c20210e6 | 2009-04-03 21:39:26 | [diff] [blame] | 74 | } |
| 75 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | double Time::ToDoubleT() const { |
| 77 | if (us_ == 0) |
| 78 | return 0; // Preserve 0 so we can tell it doesn't exist. |
| 79 | return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 80 | static_cast<double>(kMicrosecondsPerSecond)); |
| 81 | } |
| 82 | |
[email protected] | c925ad1 | 2010-09-09 17:17:55 | [diff] [blame] | 83 | // static |
| 84 | Time Time::UnixEpoch() { |
| 85 | Time time; |
| 86 | time.us_ = kTimeTToMicrosecondsOffset; |
| 87 | return time; |
| 88 | } |
| 89 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 90 | Time Time::LocalMidnight() const { |
| 91 | Exploded exploded; |
| 92 | LocalExplode(&exploded); |
| 93 | exploded.hour = 0; |
| 94 | exploded.minute = 0; |
| 95 | exploded.second = 0; |
| 96 | exploded.millisecond = 0; |
| 97 | return FromLocalExploded(exploded); |
| 98 | } |
| 99 | |
| 100 | // static |
[email protected] | 91665412 | 2011-04-26 19:07:08 | [diff] [blame] | 101 | bool Time::FromString(const wchar_t* time_string, Time* parsed_time) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 102 | DCHECK((time_string != NULL) && (parsed_time != NULL)); |
[email protected] | 91665412 | 2011-04-26 19:07:08 | [diff] [blame] | 103 | std::string ascii_time_string = SysWideToUTF8(time_string); |
| 104 | if (ascii_time_string.length() == 0) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | return false; |
| 106 | PRTime result_time = 0; |
[email protected] | 91665412 | 2011-04-26 19:07:08 | [diff] [blame] | 107 | PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 108 | &result_time); |
| 109 | if (PR_SUCCESS != result) |
| 110 | return false; |
| 111 | result_time += kTimeTToMicrosecondsOffset; |
| 112 | *parsed_time = Time(result_time); |
| 113 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 114 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 115 | |
[email protected] | e241908 | 2010-07-27 02:43:20 | [diff] [blame] | 116 | // Time::Exploded ------------------------------------------------------------- |
| 117 | |
| 118 | inline bool is_in_range(int value, int lo, int hi) { |
| 119 | return lo <= value && value <= hi; |
| 120 | } |
| 121 | |
| 122 | bool Time::Exploded::HasValidValues() const { |
| 123 | return is_in_range(month, 1, 12) && |
| 124 | is_in_range(day_of_week, 0, 6) && |
| 125 | is_in_range(day_of_month, 1, 31) && |
| 126 | is_in_range(hour, 0, 23) && |
| 127 | is_in_range(minute, 0, 59) && |
| 128 | is_in_range(second, 0, 60) && |
| 129 | is_in_range(millisecond, 0, 999); |
| 130 | } |
| 131 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 132 | } // namespace base |