1#ifndef AWS_COMMON_CLOCK_H
2#define AWS_COMMON_CLOCK_H
3
4/*
5 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License").
8 * You may not use this file except in compliance with the License.
9 * A copy of the License is located at
10 *
11 * http://aws.amazon.com/apache2.0
12 *
13 * or in the "license" file accompanying this file. This file is distributed
14 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 * express or implied. See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18
19#include <aws/common/common.h>
20#include <aws/common/math.h>
21
22enum aws_timestamp_unit {
23 AWS_TIMESTAMP_SECS = 1,
24 AWS_TIMESTAMP_MILLIS = 1000,
25 AWS_TIMESTAMP_MICROS = 1000000,
26 AWS_TIMESTAMP_NANOS = 1000000000,
27};
28
29AWS_EXTERN_C_BEGIN
30
31/**
32 * Converts 'timestamp' from unit 'convert_from' to unit 'convert_to', if the units are the same then 'timestamp' is
33 * returned. If 'remainder' is NOT NULL, it will be set to the remainder if convert_from is a more precise unit than
34 * convert_to. To avoid unnecessary branching, 'remainder' is not zero initialized in this function, be sure to set it
35 * to 0 first if you care about that kind of thing. If conversion would lead to integer overflow, the timestamp
36 * returned will be the highest possible time that is representable, i.e. UINT64_MAX.
37 */
38AWS_STATIC_IMPL uint64_t aws_timestamp_convert(
39 uint64_t timestamp,
40 enum aws_timestamp_unit convert_from,
41 enum aws_timestamp_unit convert_to,
42 uint64_t *remainder);
43
44/**
45 * Get ticks in nanoseconds (usually 100 nanosecond precision) on the high resolution clock (most-likely TSC). This
46 * clock has no bearing on the actual system time. On success, timestamp will be set.
47 */
48AWS_COMMON_API
49int aws_high_res_clock_get_ticks(uint64_t *timestamp);
50
51/**
52 * Get ticks in nanoseconds (usually 100 nanosecond precision) on the system clock. Reflects actual system time via
53 * nanoseconds since unix epoch. Use with care since an inaccurately set clock will probably cause bugs. On success,
54 * timestamp will be set.
55 */
56AWS_COMMON_API
57int aws_sys_clock_get_ticks(uint64_t *timestamp);
58
59#ifndef AWS_NO_STATIC_IMPL
60# include <aws/common/clock.inl>
61#endif /* AWS_NO_STATIC_IMPL */
62
63AWS_EXTERN_C_END
64
65#endif /* AWS_COMMON_CLOCK_H */
66