Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / ipc / chromium / src / base / time.cc
blob2d7687800535283cd93eec2d04e1191ef05af06d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 #include "base/time.h"
8 #include "base/string_util.h"
9 #include "base/sys_string_conversions.h"
10 #include "prtime.h"
12 #include "base/logging.h"
14 namespace base {
16 // TimeDelta ------------------------------------------------------------------
18 int TimeDelta::InDays() const {
19 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
22 int TimeDelta::InHours() const {
23 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
26 int TimeDelta::InMinutes() const {
27 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
30 double TimeDelta::InSecondsF() const {
31 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
34 int64_t TimeDelta::InSeconds() const {
35 return delta_ / Time::kMicrosecondsPerSecond;
38 double TimeDelta::InMillisecondsF() const {
39 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
42 int64_t TimeDelta::InMilliseconds() const {
43 return delta_ / Time::kMicrosecondsPerMillisecond;
46 int64_t TimeDelta::InMicroseconds() const { return delta_; }
48 // Time -----------------------------------------------------------------------
50 // static
51 Time Time::FromTimeT(time_t tt) {
52 if (tt == 0) return Time(); // Preserve 0 so we can tell it doesn't exist.
53 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
56 time_t Time::ToTimeT() const {
57 if (us_ == 0) return 0; // Preserve 0 so we can tell it doesn't exist.
58 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
61 // static
62 Time Time::FromDoubleT(double dt) {
63 return Time((dt * static_cast<double>(kMicrosecondsPerSecond)) +
64 kTimeTToMicrosecondsOffset);
67 double Time::ToDoubleT() const {
68 if (us_ == 0) return 0; // Preserve 0 so we can tell it doesn't exist.
69 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
70 static_cast<double>(kMicrosecondsPerSecond));
73 Time Time::LocalMidnight() const {
74 Exploded exploded;
75 LocalExplode(&exploded);
76 exploded.hour = 0;
77 exploded.minute = 0;
78 exploded.second = 0;
79 exploded.millisecond = 0;
80 return FromLocalExploded(exploded);
83 // static
84 bool Time::FromString(const wchar_t* time_string, Time* parsed_time) {
85 DCHECK((time_string != NULL) && (parsed_time != NULL));
86 std::string ascii_time_string = SysWideToUTF8(time_string);
87 if (ascii_time_string.length() == 0) return false;
88 PRTime result_time = 0;
89 PRStatus result =
90 PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE, &result_time);
91 if (PR_SUCCESS != result) return false;
92 result_time += kTimeTToMicrosecondsOffset;
93 *parsed_time = Time(result_time);
94 return true;
97 } // namespace base