ipc/chromium/src/base/time.cc

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

mercurial