1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/time.h" 6 #include "base/string_util.h" 7 #include "base/sys_string_conversions.h" 8 #include "base/third_party/nspr/prtime.h" 9 10 #include "base/logging.h" 11 12 namespace base { 13 14 // TimeDelta ------------------------------------------------------------------ 15 InDays() const16int TimeDelta::InDays() const { 17 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); 18 } 19 InHours() const20int TimeDelta::InHours() const { 21 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); 22 } 23 InMinutes() const24int TimeDelta::InMinutes() const { 25 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); 26 } 27 InSecondsF() const28double TimeDelta::InSecondsF() const { 29 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; 30 } 31 InSeconds() const32int64 TimeDelta::InSeconds() const { 33 return delta_ / Time::kMicrosecondsPerSecond; 34 } 35 InMillisecondsF() const36double TimeDelta::InMillisecondsF() const { 37 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; 38 } 39 InMilliseconds() const40int64 TimeDelta::InMilliseconds() const { 41 return delta_ / Time::kMicrosecondsPerMillisecond; 42 } 43 InMillisecondsRoundedUp() const44int64 TimeDelta::InMillisecondsRoundedUp() const { 45 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / 46 Time::kMicrosecondsPerMillisecond; 47 } 48 InMicroseconds() const49int64 TimeDelta::InMicroseconds() const { 50 return delta_; 51 } 52 53 // Time ----------------------------------------------------------------------- 54 55 // static FromTimeT(time_t tt)56Time Time::FromTimeT(time_t tt) { 57 if (tt == 0) 58 return Time(); // Preserve 0 so we can tell it doesn't exist. 59 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); 60 } 61 ToTimeT() const62time_t Time::ToTimeT() const { 63 if (us_ == 0) 64 return 0; // Preserve 0 so we can tell it doesn't exist. 65 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; 66 } 67 68 // static FromDoubleT(double dt)69Time Time::FromDoubleT(double dt) { 70 return Time(static_cast<int64>((dt * 71 static_cast<double>(kMicrosecondsPerSecond)) + 72 kTimeTToMicrosecondsOffset)); 73 } 74 ToDoubleT() const75double Time::ToDoubleT() const { 76 if (us_ == 0) 77 return 0; // Preserve 0 so we can tell it doesn't exist. 78 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 79 static_cast<double>(kMicrosecondsPerSecond)); 80 } 81 LocalMidnight() const82Time Time::LocalMidnight() const { 83 Exploded exploded; 84 LocalExplode(&exploded); 85 exploded.hour = 0; 86 exploded.minute = 0; 87 exploded.second = 0; 88 exploded.millisecond = 0; 89 return FromLocalExploded(exploded); 90 } 91 92 // static FromString(const wchar_t * time_string,Time * parsed_time)93bool Time::FromString(const wchar_t* time_string, Time* parsed_time) { 94 DCHECK((time_string != NULL) && (parsed_time != NULL)); 95 std::string ascii_time_string = SysWideToUTF8(time_string); 96 if (ascii_time_string.length() == 0) 97 return false; 98 PRTime result_time = 0; 99 PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE, 100 &result_time); 101 if (PR_SUCCESS != result) 102 return false; 103 result_time += kTimeTToMicrosecondsOffset; 104 *parsed_time = Time(result_time); 105 return true; 106 } 107 108 } // namespace base 109