1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2019 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/*
19 * Header including time portability declarations
20 */
21#pragma once
22
23#include <platform/visibility.h>
24
25#include <stdint.h>
26#include <time.h>
27
28#include <folly/portability/SysTime.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#ifdef WIN32
35typedef unsigned __int64 hrtime_t;
36#else
37#include <sys/time.h>
38typedef uint64_t hrtime_t;
39#endif // WIN32
40
41/**
42 * return a monotonically increasing value with a seconds frequency.
43 */
44PLATFORM_PUBLIC_API
45uint64_t cb_get_monotonic_seconds(void);
46
47/**
48 * obtain a timeval structure containing the current time since EPOCH.
49 */
50PLATFORM_PUBLIC_API
51int cb_get_timeofday(struct timeval* tv);
52
53/**
54 * Set an offset (in seconds) added to cb_get_timeofday before returned
55 * to the caller.
56 *
57 * This is intended for testing of time jumps.
58 *
59 * @param offset the number of seconds to add (a negative value results in
60 *               jumping back in time)
61 */
62PLATFORM_PUBLIC_API
63void cb_set_timeofday_offset(int offset);
64
65/**
66 * Get the offset being added to the cb_get_timeofday()
67 */
68PLATFORM_PUBLIC_API
69int cb_get_timeofday_offset(void);
70
71/**
72 * Set an uptime offset to be added to memcached_uptime
73 *
74 * This is intended for the testing of expiry.
75 *
76 * @param offset the number of seconds to add
77 *
78 */
79PLATFORM_PUBLIC_API
80void cb_set_uptime_offset(uint64_t offset);
81
82/**
83 * Get the offset to add to the uptime.
84 */
85PLATFORM_PUBLIC_API
86uint64_t cb_get_uptime_offset();
87
88/**
89 * Travel in time by updating the timeofday_offset with a relative
90 * value
91 *
92 * @param secs the number of seconds to travel
93 */
94PLATFORM_PUBLIC_API
95void cb_timeofday_timetravel(int offset);
96
97/**
98 * Convert time_t to a structure
99 *
100 * @param clock the input value
101 * @param result the output value
102 * @return 0 for success, -1 on failure
103 */
104PLATFORM_PUBLIC_API
105int cb_gmtime_r(const time_t* clock, struct tm* result);
106
107/**
108 * Convert a time value with adjustments for the local time zone
109 *
110 * @param clock the input value
111 * @param result the output value
112 * @return 0 for success, -1 on failure
113 */
114PLATFORM_PUBLIC_API
115int cb_localtime_r(const time_t* clock, struct tm* result);
116
117#ifdef __cplusplus
118}
119#endif
120