michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/time.h" michael@0: #include "base/string_util.h" michael@0: #include "base/sys_string_conversions.h" michael@0: #include "prtime.h" michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: namespace base { michael@0: michael@0: // TimeDelta ------------------------------------------------------------------ michael@0: michael@0: int TimeDelta::InDays() const { michael@0: return static_cast(delta_ / Time::kMicrosecondsPerDay); michael@0: } michael@0: michael@0: int TimeDelta::InHours() const { michael@0: return static_cast(delta_ / Time::kMicrosecondsPerHour); michael@0: } michael@0: michael@0: int TimeDelta::InMinutes() const { michael@0: return static_cast(delta_ / Time::kMicrosecondsPerMinute); michael@0: } michael@0: michael@0: double TimeDelta::InSecondsF() const { michael@0: return static_cast(delta_) / Time::kMicrosecondsPerSecond; michael@0: } michael@0: michael@0: int64_t TimeDelta::InSeconds() const { michael@0: return delta_ / Time::kMicrosecondsPerSecond; michael@0: } michael@0: michael@0: double TimeDelta::InMillisecondsF() const { michael@0: return static_cast(delta_) / Time::kMicrosecondsPerMillisecond; michael@0: } michael@0: michael@0: int64_t TimeDelta::InMilliseconds() const { michael@0: return delta_ / Time::kMicrosecondsPerMillisecond; michael@0: } michael@0: michael@0: int64_t TimeDelta::InMicroseconds() const { michael@0: return delta_; michael@0: } michael@0: michael@0: // Time ----------------------------------------------------------------------- michael@0: michael@0: // static michael@0: Time Time::FromTimeT(time_t tt) { michael@0: if (tt == 0) michael@0: return Time(); // Preserve 0 so we can tell it doesn't exist. michael@0: return (tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset; michael@0: } michael@0: michael@0: time_t Time::ToTimeT() const { michael@0: if (us_ == 0) michael@0: return 0; // Preserve 0 so we can tell it doesn't exist. michael@0: return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; michael@0: } michael@0: michael@0: // static michael@0: Time Time::FromDoubleT(double dt) { michael@0: return (dt * static_cast(kMicrosecondsPerSecond)) + michael@0: kTimeTToMicrosecondsOffset; michael@0: } michael@0: michael@0: double Time::ToDoubleT() const { michael@0: if (us_ == 0) michael@0: return 0; // Preserve 0 so we can tell it doesn't exist. michael@0: return (static_cast(us_ - kTimeTToMicrosecondsOffset) / michael@0: static_cast(kMicrosecondsPerSecond)); michael@0: } michael@0: michael@0: Time Time::LocalMidnight() const { michael@0: Exploded exploded; michael@0: LocalExplode(&exploded); michael@0: exploded.hour = 0; michael@0: exploded.minute = 0; michael@0: exploded.second = 0; michael@0: exploded.millisecond = 0; michael@0: return FromLocalExploded(exploded); michael@0: } michael@0: michael@0: // static michael@0: bool Time::FromString(const wchar_t* time_string, Time* parsed_time) { michael@0: DCHECK((time_string != NULL) && (parsed_time != NULL)); michael@0: std::string ascii_time_string = SysWideToUTF8(time_string); michael@0: if (ascii_time_string.length() == 0) michael@0: return false; michael@0: PRTime result_time = 0; michael@0: PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE, michael@0: &result_time); michael@0: if (PR_SUCCESS != result) michael@0: return false; michael@0: result_time += kTimeTToMicrosecondsOffset; michael@0: *parsed_time = Time(result_time); michael@0: return true; michael@0: } michael@0: michael@0: } // namespace base