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_mock.h"
31 
32 #include <ctime>
33 
34 namespace mozc {
35 
ClockMock(uint64 sec,uint32 usec)36 ClockMock::ClockMock(uint64 sec, uint32 usec)
37     : seconds_(sec), micro_seconds_(usec),
38       frequency_(1000000000), ticks_(0),
39 #ifdef OS_NACL
40       timezone_offset_sec_(0),
41 #endif  // OS_NACL
42       delta_seconds_(0), delta_micro_seconds_(0) {
43 }
44 
~ClockMock()45 ClockMock::~ClockMock() {}
46 
GetTimeOfDay(uint64 * sec,uint32 * usec)47 void ClockMock::GetTimeOfDay(uint64 *sec, uint32 *usec) {
48   *sec = seconds_;
49   *usec = micro_seconds_;
50   PutClockForward(delta_seconds_, delta_micro_seconds_);
51 }
52 
GetTime()53 uint64 ClockMock::GetTime() {
54   const uint64 ret_sec = seconds_;
55   PutClockForward(delta_seconds_, delta_micro_seconds_);
56   return ret_sec;
57 }
58 
GetTmWithOffsetSecond(time_t offset_sec,tm * output)59 bool ClockMock::GetTmWithOffsetSecond(time_t offset_sec, tm *output) {
60   const time_t current_sec = static_cast<time_t>(seconds_);
61   const time_t modified_sec = current_sec + offset_sec;
62 
63 #ifdef OS_WIN
64   if (_gmtime64_s(output, &modified_sec) != 0) {
65     return false;
66   }
67 #elif defined(OS_NACL)
68   const time_t localtime_sec = modified_sec + timezone_offset_sec_;
69   if (gmtime_r(&localtime_sec, output) == NULL) {
70     return false;
71   }
72 #else  // !OS_WIN && !OS_NACL
73   if (gmtime_r(&modified_sec, output) == NULL) {
74     return false;
75   }
76 #endif
77   PutClockForward(delta_seconds_, delta_micro_seconds_);
78 
79   return true;
80 }
81 
GetFrequency()82 uint64 ClockMock::GetFrequency() {
83   return frequency_;
84 }
85 
GetTicks()86 uint64 ClockMock::GetTicks() {
87   return ticks_;
88 }
89 
90 #ifdef OS_NACL
SetTimezoneOffset(int32 timezone_offset_sec)91 void ClockMock::SetTimezoneOffset(int32 timezone_offset_sec) {
92   timezone_offset_sec_ = timezone_offset_sec;
93 }
94 #endif  // OS_NACL
95 
PutClockForward(uint64 delta_sec,uint32 delta_usec)96 void ClockMock::PutClockForward(uint64 delta_sec, uint32 delta_usec) {
97   const uint32 one_second = 1000000u;
98 
99   if (micro_seconds_ + delta_usec < one_second) {
100     seconds_ += delta_sec;
101     micro_seconds_ += delta_usec;
102   } else {
103     seconds_ += delta_sec + 1uLL;
104     micro_seconds_ += delta_usec - one_second;
105   }
106 }
107 
PutClockForwardByTicks(uint64 ticks)108 void ClockMock::PutClockForwardByTicks(uint64 ticks) {
109   ticks_ += ticks;
110 }
111 
SetAutoPutClockForward(uint64 delta_sec,uint32 delta_usec)112 void ClockMock::SetAutoPutClockForward(uint64 delta_sec,
113                                        uint32 delta_usec) {
114   delta_seconds_ = delta_sec;
115   delta_micro_seconds_ = delta_usec;
116 }
117 
SetTime(uint64 sec,uint32 usec)118 void ClockMock::SetTime(uint64 sec, uint32 usec) {
119   seconds_ = sec;
120   micro_seconds_ = usec;
121 }
122 
SetFrequency(uint64 frequency)123 void ClockMock::SetFrequency(uint64 frequency) {
124   frequency_ = frequency;
125 }
126 
SetTicks(uint64 ticks)127 void ClockMock::SetTicks(uint64 ticks) {
128   ticks_ = ticks;
129 }
130 
131 }  // namespace mozc
132