1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "base/clock.h"
31
32 #ifdef OS_WIN
33 #include <Windows.h>
34 #include <time.h>
35 #else // OS_WIN
36 #ifdef OS_MACOSX
37 #include <mach/mach_time.h>
38 #elif defined(OS_NACL) // OS_MACOSX
39 #include <irt.h>
40 #endif // OS_MACOSX or OS_NACL
41 #include <sys/time.h>
42 #endif // OS_WIN
43
44 #include "base/singleton.h"
45
46 namespace mozc {
47 namespace {
48
49 class ClockImpl : public ClockInterface {
50 public:
51 #ifndef OS_NACL
ClockImpl()52 ClockImpl() {}
53 #else // OS_NACL
54 ClockImpl() : timezone_offset_sec_(0) {}
55 #endif // OS_NACL
56
~ClockImpl()57 virtual ~ClockImpl() {}
58
GetTimeOfDay(uint64 * sec,uint32 * usec)59 virtual void GetTimeOfDay(uint64 *sec, uint32 *usec) {
60 #ifdef OS_WIN
61 FILETIME file_time;
62 GetSystemTimeAsFileTime(&file_time);
63 ULARGE_INTEGER time_value;
64 time_value.HighPart = file_time.dwHighDateTime;
65 time_value.LowPart = file_time.dwLowDateTime;
66 // Convert into microseconds
67 time_value.QuadPart /= 10;
68 // kDeltaEpochInMicroSecs is difference between January 1, 1970 and
69 // January 1, 1601 in microsecond.
70 // This number is calculated as follows.
71 // ((1970 - 1601) * 365 + 89) * 24 * 60 * 60 * 1000000
72 // 89 is the number of leap years between 1970 and 1601.
73 const uint64 kDeltaEpochInMicroSecs = 11644473600000000ULL;
74 // Convert file time to unix epoch
75 time_value.QuadPart -= kDeltaEpochInMicroSecs;
76 *sec = static_cast<uint64>(time_value.QuadPart / 1000000UL);
77 *usec = static_cast<uint32>(time_value.QuadPart % 1000000UL);
78 #else // OS_WIN
79 struct timeval tv;
80 gettimeofday(&tv, nullptr);
81 *sec = tv.tv_sec;
82 *usec = tv.tv_usec;
83 #endif // OS_WIN
84 }
85
GetTime()86 virtual uint64 GetTime() {
87 #ifdef OS_WIN
88 return static_cast<uint64>(_time64(nullptr));
89 #else
90 return static_cast<uint64>(time(nullptr));
91 #endif // OS_WIN
92 }
93
GetTmWithOffsetSecond(time_t offset_sec,tm * output)94 virtual bool GetTmWithOffsetSecond(time_t offset_sec, tm *output) {
95 const time_t current_sec = static_cast<time_t>(this->GetTime());
96 const time_t modified_sec = current_sec + offset_sec;
97
98 #ifdef OS_WIN
99 if (_localtime64_s(output, &modified_sec) != 0) {
100 return false;
101 }
102 #elif defined(OS_NACL)
103 const time_t localtime_sec = modified_sec + timezone_offset_sec_;
104 if (gmtime_r(&localtime_sec, output) == nullptr) {
105 return false;
106 }
107 #else // !OS_WIN && !OS_NACL
108 if (localtime_r(&modified_sec, output) == nullptr) {
109 return false;
110 }
111 #endif // OS_WIN
112 return true;
113 }
114
GetFrequency()115 virtual uint64 GetFrequency() {
116 #if defined(OS_WIN)
117 LARGE_INTEGER timestamp;
118 // TODO(yukawa): Consider the case where QueryPerformanceCounter is not
119 // available.
120 const BOOL result = ::QueryPerformanceFrequency(×tamp);
121 return static_cast<uint64>(timestamp.QuadPart);
122 #elif defined(OS_MACOSX)
123 static mach_timebase_info_data_t timebase_info;
124 mach_timebase_info(&timebase_info);
125 return static_cast<uint64>(
126 1.0e9 * timebase_info.denom / timebase_info.numer);
127 #elif defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_NACL)
128 return 1000000uLL;
129 #else // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...)
130 #error "Not supported platform"
131 #endif // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...)
132 }
133
GetTicks()134 virtual uint64 GetTicks() {
135 // TODO(team): Use functions in <chrono> once the use of it is approved.
136 #if defined(OS_WIN)
137 LARGE_INTEGER timestamp;
138 // TODO(yukawa): Consider the case where QueryPerformanceCounter is not
139 // available.
140 const BOOL result = ::QueryPerformanceCounter(×tamp);
141 return static_cast<uint64>(timestamp.QuadPart);
142 #elif defined(OS_MACOSX)
143 return static_cast<uint64>(mach_absolute_time());
144 #elif defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_NACL)
145 uint64 sec;
146 uint32 usec;
147 GetTimeOfDay(&sec, &usec);
148 return sec * 1000000 + usec;
149 #else // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...)
150 #error "Not supported platform"
151 #endif // platforms (OS_WIN, OS_MACOSX, OS_LINUX, ...)
152 }
153
154 #ifdef OS_NACL
SetTimezoneOffset(int32 timezone_offset_sec)155 virtual void SetTimezoneOffset(int32 timezone_offset_sec) {
156 timezone_offset_sec_ = timezone_offset_sec;
157 }
158
159 private:
160 int32 timezone_offset_sec_;
161 #endif // OS_NACL
162 };
163
164 ClockInterface *g_clock = nullptr;
165
GetClock()166 inline ClockInterface *GetClock() {
167 return g_clock != nullptr ? g_clock : Singleton<ClockImpl>::get();
168 }
169
170 } // namespace
171
GetTimeOfDay(uint64 * sec,uint32 * usec)172 void Clock::GetTimeOfDay(uint64 *sec, uint32 *usec) {
173 GetClock()->GetTimeOfDay(sec, usec);
174 }
175
GetTime()176 uint64 Clock::GetTime() {
177 return GetClock()->GetTime();
178 }
179
GetTmWithOffsetSecond(tm * time_with_offset,int offset_sec)180 bool Clock::GetTmWithOffsetSecond(tm *time_with_offset, int offset_sec) {
181 return GetClock()->GetTmWithOffsetSecond(offset_sec, time_with_offset);
182 }
183
GetFrequency()184 uint64 Clock::GetFrequency() {
185 return GetClock()->GetFrequency();
186 }
187
GetTicks()188 uint64 Clock::GetTicks() {
189 return GetClock()->GetTicks();
190 }
191
192 #ifdef OS_NACL
SetTimezoneOffset(int32 timezone_offset_sec)193 void Clock::SetTimezoneOffset(int32 timezone_offset_sec) {
194 return GetClock()->SetTimezoneOffset(timezone_offset_sec);
195 }
196 #endif // OS_NACL
197
SetClockForUnitTest(ClockInterface * clock)198 void Clock::SetClockForUnitTest(ClockInterface *clock) {
199 g_clock = clock;
200 }
201
202 } // namespace mozc
203