OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time.h" | 5 #include "base/time.h" |
6 | 6 |
7 #include <sys/time.h> | 7 #include <sys/time.h> |
8 #include <time.h> | 8 #include <time.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
| 17 struct timespec TimeDelta::ToTimeSpec() const { |
| 18 int64 microseconds = InMicroseconds(); |
| 19 time_t seconds = 0; |
| 20 if (microseconds >= Time::kMicrosecondsPerSecond) { |
| 21 seconds = InSeconds(); |
| 22 microseconds -= seconds * Time::kMicrosecondsPerSecond; |
| 23 } |
| 24 struct timespec result = |
| 25 {seconds, |
| 26 microseconds * Time::kNanosecondsPerMicrosecond}; |
| 27 return result; |
| 28 } |
| 29 |
17 #if !defined(OS_MACOSX) | 30 #if !defined(OS_MACOSX) |
18 // The Time routines in this file use standard POSIX routines, or almost- | 31 // The Time routines in this file use standard POSIX routines, or almost- |
19 // standard routines in the case of timegm. We need to use a Mach-specific | 32 // standard routines in the case of timegm. We need to use a Mach-specific |
20 // function for TimeTicks::Now() on Mac OS X. | 33 // function for TimeTicks::Now() on Mac OS X. |
21 | 34 |
22 // Time ----------------------------------------------------------------------- | 35 // Time ----------------------------------------------------------------------- |
23 | 36 |
24 // Windows uses a Gregorian epoch of 1601. We need to match this internally | 37 // Windows uses a Gregorian epoch of 1601. We need to match this internally |
25 // so that our time representations match across all platforms. See bug 14734. | 38 // so that our time representations match across all platforms. See bug 14734. |
26 // irb(main):010:0> Time.at(0).getutc() | 39 // irb(main):010:0> Time.at(0).getutc() |
(...skipping 26 matching lines...) Expand all Loading... |
53 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + | 66 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + |
54 kWindowsEpochDeltaMicroseconds); | 67 kWindowsEpochDeltaMicroseconds); |
55 } | 68 } |
56 | 69 |
57 // static | 70 // static |
58 Time Time::NowFromSystemTime() { | 71 Time Time::NowFromSystemTime() { |
59 // Just use Now() because Now() returns the system time. | 72 // Just use Now() because Now() returns the system time. |
60 return Now(); | 73 return Now(); |
61 } | 74 } |
62 | 75 |
| 76 void Time::Explode(bool is_local, Exploded* exploded) const { |
| 77 // Time stores times with microsecond resolution, but Exploded only carries |
| 78 // millisecond resolution, so begin by being lossy. Adjust from Windows |
| 79 // epoch (1601) to Unix epoch (1970); |
| 80 int64 milliseconds = (us_ - kWindowsEpochDeltaMicroseconds) / |
| 81 kMicrosecondsPerMillisecond; |
| 82 time_t seconds = milliseconds / kMillisecondsPerSecond; |
| 83 |
| 84 struct tm timestruct; |
| 85 if (is_local) |
| 86 localtime_r(&seconds, ×truct); |
| 87 else |
| 88 gmtime_r(&seconds, ×truct); |
| 89 |
| 90 exploded->year = timestruct.tm_year + 1900; |
| 91 exploded->month = timestruct.tm_mon + 1; |
| 92 exploded->day_of_week = timestruct.tm_wday; |
| 93 exploded->day_of_month = timestruct.tm_mday; |
| 94 exploded->hour = timestruct.tm_hour; |
| 95 exploded->minute = timestruct.tm_min; |
| 96 exploded->second = timestruct.tm_sec; |
| 97 exploded->millisecond = milliseconds % kMillisecondsPerSecond; |
| 98 } |
| 99 |
63 // static | 100 // static |
64 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 101 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
65 struct tm timestruct; | 102 struct tm timestruct; |
66 timestruct.tm_sec = exploded.second; | 103 timestruct.tm_sec = exploded.second; |
67 timestruct.tm_min = exploded.minute; | 104 timestruct.tm_min = exploded.minute; |
68 timestruct.tm_hour = exploded.hour; | 105 timestruct.tm_hour = exploded.hour; |
69 timestruct.tm_mday = exploded.day_of_month; | 106 timestruct.tm_mday = exploded.day_of_month; |
70 timestruct.tm_mon = exploded.month - 1; | 107 timestruct.tm_mon = exploded.month - 1; |
71 timestruct.tm_year = exploded.year - 1900; | 108 timestruct.tm_year = exploded.year - 1900; |
72 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this | 109 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 149 } |
113 } else { | 150 } else { |
114 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; | 151 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
115 } | 152 } |
116 | 153 |
117 // Adjust from Unix (1970) to Windows (1601) epoch. | 154 // Adjust from Unix (1970) to Windows (1601) epoch. |
118 return Time((milliseconds * kMicrosecondsPerMillisecond) + | 155 return Time((milliseconds * kMicrosecondsPerMillisecond) + |
119 kWindowsEpochDeltaMicroseconds); | 156 kWindowsEpochDeltaMicroseconds); |
120 } | 157 } |
121 | 158 |
122 void Time::Explode(bool is_local, Exploded* exploded) const { | |
123 // Time stores times with microsecond resolution, but Exploded only carries | |
124 // millisecond resolution, so begin by being lossy. Adjust from Windows | |
125 // epoch (1601) to Unix epoch (1970); | |
126 int64 milliseconds = (us_ - kWindowsEpochDeltaMicroseconds) / | |
127 kMicrosecondsPerMillisecond; | |
128 time_t seconds = milliseconds / kMillisecondsPerSecond; | |
129 | |
130 struct tm timestruct; | |
131 if (is_local) | |
132 localtime_r(&seconds, ×truct); | |
133 else | |
134 gmtime_r(&seconds, ×truct); | |
135 | |
136 exploded->year = timestruct.tm_year + 1900; | |
137 exploded->month = timestruct.tm_mon + 1; | |
138 exploded->day_of_week = timestruct.tm_wday; | |
139 exploded->day_of_month = timestruct.tm_mday; | |
140 exploded->hour = timestruct.tm_hour; | |
141 exploded->minute = timestruct.tm_min; | |
142 exploded->second = timestruct.tm_sec; | |
143 exploded->millisecond = milliseconds % kMillisecondsPerSecond; | |
144 } | |
145 | |
146 // TimeTicks ------------------------------------------------------------------ | 159 // TimeTicks ------------------------------------------------------------------ |
147 // FreeBSD 6 has CLOCK_MONOLITHIC but defines _POSIX_MONOTONIC_CLOCK to -1. | 160 // FreeBSD 6 has CLOCK_MONOLITHIC but defines _POSIX_MONOTONIC_CLOCK to -1. |
148 #if (defined(OS_POSIX) && \ | 161 #if (defined(OS_POSIX) && \ |
149 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ | 162 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
150 defined(OS_FREEBSD) || defined(OS_OPENBSD) | 163 defined(OS_FREEBSD) || defined(OS_OPENBSD) |
151 | 164 |
152 // static | 165 // static |
153 TimeTicks TimeTicks::Now() { | 166 TimeTicks TimeTicks::Now() { |
154 uint64_t absolute_micro; | 167 uint64_t absolute_micro; |
155 | 168 |
(...skipping 14 matching lines...) Expand all Loading... |
170 #error No usable tick clock function on this platform. | 183 #error No usable tick clock function on this platform. |
171 #endif // _POSIX_MONOTONIC_CLOCK | 184 #endif // _POSIX_MONOTONIC_CLOCK |
172 | 185 |
173 // static | 186 // static |
174 TimeTicks TimeTicks::HighResNow() { | 187 TimeTicks TimeTicks::HighResNow() { |
175 return Now(); | 188 return Now(); |
176 } | 189 } |
177 | 190 |
178 #endif // !OS_MACOSX | 191 #endif // !OS_MACOSX |
179 | 192 |
180 struct timespec TimeDelta::ToTimeSpec() const { | |
181 int64 microseconds = InMicroseconds(); | |
182 time_t seconds = 0; | |
183 if (microseconds >= Time::kMicrosecondsPerSecond) { | |
184 seconds = InSeconds(); | |
185 microseconds -= seconds * Time::kMicrosecondsPerSecond; | |
186 } | |
187 struct timespec result = | |
188 {seconds, | |
189 microseconds * Time::kNanosecondsPerMicrosecond}; | |
190 return result; | |
191 } | |
192 | |
193 struct timeval Time::ToTimeVal() const { | 193 struct timeval Time::ToTimeVal() const { |
194 struct timeval result; | 194 struct timeval result; |
195 int64 us = us_ - kTimeTToMicrosecondsOffset; | 195 int64 us = us_ - kTimeTToMicrosecondsOffset; |
196 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 196 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
197 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 197 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
198 return result; | 198 return result; |
199 } | 199 } |
200 | 200 |
201 } // namespace base | 201 } // namespace base |
OLD | NEW |