• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "system_timer.h"
17 
18 #include <atomic>
19 #include <sys/time.h>
20 
21 #include "log_print.h"
22 #include "platform_specific.h"
23 
24 namespace {
25 #ifdef RUNNING_ON_SIMULATED_ENV
26     const uint64_t MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS = 1000000;
27 #endif
28     std::atomic<int64_t> g_timeOffset(0);
29 }
30 
31 namespace DistributedDB {
32 namespace OS {
33 
34 #ifdef RUNNING_ON_SIMULATED_ENV
GetCurrentSysTimeInMicrosecond(uint64_t & outTime)35 int GetCurrentSysTimeInMicrosecond(uint64_t &outTime)
36 {
37     struct timeval rawTime;
38     int errCode = gettimeofday(&rawTime, nullptr);
39     if (errCode < 0) {
40         LOGE("[GetSysTime] Fail:%d.", errCode);
41         return errCode;
42     }
43     outTime = static_cast<uint64_t>(rawTime.tv_sec) * MULTIPLES_BETWEEN_SECONDS_AND_MICROSECONDS +
44         static_cast<uint64_t>(rawTime.tv_usec);
45     outTime = outTime + g_timeOffset.load();
46     return 0;
47 }
48 #endif // RUNNING_ON_SIMULATED_ENV
49 
SetOffsetBySecond(int64_t inSecond)50 void SetOffsetBySecond(int64_t inSecond)
51 {
52     int64_t microSecond = static_cast<int64_t>(inSecond) * 1000 * 1000; // to ms
53     g_timeOffset.store(microSecond);
54     LOGD("[SetTimeOffset] offset : %llds", inSecond);
55 }
56 }
57 }