Line data Source code
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.
6 :
7 : #include "base/time.h"
8 : #include "base/string_util.h"
9 : #include "base/sys_string_conversions.h"
10 : #include "prtime.h"
11 :
12 : #include "base/logging.h"
13 :
14 : namespace base {
15 :
16 : // TimeDelta ------------------------------------------------------------------
17 :
18 0 : int TimeDelta::InDays() const {
19 0 : return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
20 : }
21 :
22 0 : int TimeDelta::InHours() const {
23 0 : return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
24 : }
25 :
26 0 : int TimeDelta::InMinutes() const {
27 0 : return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
28 : }
29 :
30 0 : double TimeDelta::InSecondsF() const {
31 0 : return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
32 : }
33 :
34 0 : int64_t TimeDelta::InSeconds() const {
35 0 : return delta_ / Time::kMicrosecondsPerSecond;
36 : }
37 :
38 0 : double TimeDelta::InMillisecondsF() const {
39 0 : return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
40 : }
41 :
42 0 : int64_t TimeDelta::InMilliseconds() const {
43 0 : return delta_ / Time::kMicrosecondsPerMillisecond;
44 : }
45 :
46 527 : int64_t TimeDelta::InMicroseconds() const {
47 527 : return delta_;
48 : }
49 :
50 : // Time -----------------------------------------------------------------------
51 :
52 : // static
53 0 : Time Time::FromTimeT(time_t tt) {
54 0 : if (tt == 0)
55 0 : return Time(); // Preserve 0 so we can tell it doesn't exist.
56 0 : return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
57 : }
58 :
59 0 : time_t Time::ToTimeT() const {
60 0 : if (us_ == 0)
61 0 : return 0; // Preserve 0 so we can tell it doesn't exist.
62 0 : return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
63 : }
64 :
65 : // static
66 0 : Time Time::FromDoubleT(double dt) {
67 0 : return Time((dt * static_cast<double>(kMicrosecondsPerSecond)) +
68 0 : kTimeTToMicrosecondsOffset);
69 : }
70 :
71 0 : double Time::ToDoubleT() const {
72 0 : if (us_ == 0)
73 0 : return 0; // Preserve 0 so we can tell it doesn't exist.
74 0 : return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
75 0 : static_cast<double>(kMicrosecondsPerSecond));
76 : }
77 :
78 0 : Time Time::LocalMidnight() const {
79 : Exploded exploded;
80 0 : LocalExplode(&exploded);
81 0 : exploded.hour = 0;
82 0 : exploded.minute = 0;
83 0 : exploded.second = 0;
84 0 : exploded.millisecond = 0;
85 0 : return FromLocalExploded(exploded);
86 : }
87 :
88 : // static
89 0 : bool Time::FromString(const wchar_t* time_string, Time* parsed_time) {
90 0 : DCHECK((time_string != NULL) && (parsed_time != NULL));
91 0 : std::string ascii_time_string = SysWideToUTF8(time_string);
92 0 : if (ascii_time_string.length() == 0)
93 0 : return false;
94 0 : PRTime result_time = 0;
95 0 : PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE,
96 0 : &result_time);
97 0 : if (PR_SUCCESS != result)
98 0 : return false;
99 0 : result_time += kTimeTToMicrosecondsOffset;
100 0 : *parsed_time = Time(result_time);
101 0 : return true;
102 : }
103 :
104 : } // namespace base
|