1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/time.h"
#include <sys/time.h>
#include <time.h>
#include "base/basictypes.h"
#include "base/logging.h"
namespace base {
// The Time routines in this file use standard POSIX routines, or almost-
// standard routines in the case of timegm. We need to use a Mach-specific
// function for TimeTicks::Now() on Mac OS X.
// Time -----------------------------------------------------------------------
// The internal representation of Time uses time_t directly, so there is no
// offset. The epoch is 1970-01-01 00:00:00 UTC.
// static
const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0);
// static
Time Time::Now() {
struct timeval tv;
struct timezone tz = { 0, 0 }; // UTC
if (gettimeofday(&tv, &tz) != 0) {
DCHECK(0) << "Could not determine time of day";
}
// Combine seconds and microseconds in a 64-bit field containing microseconds
// since the epoch. That's enough for nearly 600 centuries.
return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec;
}
// static
Time Time::FromExploded(bool is_local, const Exploded& exploded) {
struct tm timestruct;
timestruct.tm_sec = exploded.second;
timestruct.tm_min = exploded.minute;
timestruct.tm_hour = exploded.hour;
timestruct.tm_mday = exploded.day_of_month;
timestruct.tm_mon = exploded.month - 1;
timestruct.tm_year = exploded.year - 1900;
timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this
timestruct.tm_yday = 0; // mktime/timegm ignore this
timestruct.tm_isdst = -1; // attempt to figure it out
timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore
timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore
time_t seconds;
if (is_local)
seconds = mktime(×truct);
else
seconds = timegm(×truct);
DCHECK(seconds >= 0) << "mktime/timegm could not convert from exploded";
uint64 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond;
return Time(milliseconds * kMicrosecondsPerMillisecond);
}
void Time::Explode(bool is_local, Exploded* exploded) const {
// Time stores times with microsecond resolution, but Exploded only carries
// millisecond resolution, so begin by being lossy.
uint64 milliseconds = us_ / kMicrosecondsPerMillisecond;
time_t seconds = milliseconds / kMillisecondsPerSecond;
struct tm timestruct;
if (is_local)
localtime_r(&seconds, ×truct);
else
gmtime_r(&seconds, ×truct);
exploded->year = timestruct.tm_year + 1900;
exploded->month = timestruct.tm_mon + 1;
exploded->day_of_week = timestruct.tm_wday;
exploded->day_of_month = timestruct.tm_mday;
exploded->hour = timestruct.tm_hour;
exploded->minute = timestruct.tm_min;
exploded->second = timestruct.tm_sec;
exploded->millisecond = milliseconds % kMillisecondsPerSecond;
}
// TimeTicks ------------------------------------------------------------------
// static
TimeTicks TimeTicks::Now() {
uint64_t absolute_micro;
#if defined(OS_POSIX) && \
defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed.";
return TimeTicks();
}
absolute_micro =
(static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) +
(static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond);
#else // _POSIX_MONOTONIC_CLOCK
#error No usable tick clock function on this platform.
#endif // _POSIX_MONOTONIC_CLOCK
return TimeTicks(absolute_micro);
}
// static
TimeTicks TimeTicks::HighResNow() {
return Now();
}
} // namespace base
|