xref: /4.0.0/platform/src/cb_time.c (revision bcdf4026)
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2014 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#include "config.h"
19
20#if defined(__APPLE__)
21#include <mach/mach_time.h>
22#endif
23
24#if defined(WIN32)
25#include <Windows.h>
26#endif
27
28#include <platform/platform.h>
29
30static uint64_t timeofday_offset = 0;
31
32/*
33    return a monotonically increasing value with a seconds frequency.
34*/
35uint64_t cb_get_monotonic_seconds() {
36    uint64_t seconds = 0;
37#if defined(WIN32)
38    /* GetTickCound64 gives us near 60years of ticks...*/
39    seconds =  (GetTickCount64() / 1000);
40#elif defined(__APPLE__)
41    uint64_t time = mach_absolute_time();
42
43    static mach_timebase_info_data_t timebase;
44    if (timebase.denom == 0) {
45      mach_timebase_info(&timebase);
46    }
47
48    seconds = (double)time * timebase.numer / timebase.denom * 1e-9;
49#elif defined(__linux__) || defined(__sun)
50    /* Linux and Solaris can use clock_gettime */
51    struct timespec tm;
52    if (clock_gettime(CLOCK_MONOTONIC, &tm) == -1) {
53        abort();
54    }
55    seconds = tm.tv_sec;
56#else
57#error "Don't know how to build cb_get_monotonic_seconds"
58#endif
59
60    return seconds;
61}
62
63/*
64    obtain a timeval structure containing the current time since EPOCH.
65*/
66int cb_get_timeofday(struct timeval *tv) {
67    int rv = gettimeofday(tv, NULL);
68#if defined(WIN32)
69    // WIN32: tv_sec is less precise than normal (it's a long); so explicitly
70    // downcast to silence implicit downcast warning.
71    tv->tv_sec += (long)timeofday_offset;
72#else
73    tv->tv_sec += timeofday_offset;
74#endif
75    return rv;
76}
77
78/*
79    set an offset so that cb_get_timeofday returns an offsetted time.
80    This is intended for testing of time jumps.
81*/
82void cb_set_timeofday_offset(uint64_t offset) {
83    timeofday_offset = offset;
84}
85
86
87int cb_gmtime_r(const time_t *clock, struct tm *result)
88{
89#ifdef WIN32
90    return gmtime_s(result, clock) == 0 ? 0 : -1;
91#else
92    return gmtime_r(clock, result) == NULL ? -1 : 0;
93#endif
94}
95
96int cb_localtime_r(const time_t *clock, struct tm *result)
97{
98#ifdef WIN32
99    return localtime_s(result, clock) == 0 ? 0 : -1;
100#else
101    return localtime_r(clock, result) == NULL ? -1 : 0;
102#endif
103}
104