| 1 | #ifndef AWS_COMMON_CLOCK_H |
| 2 | #define AWS_COMMON_CLOCK_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/common/common.h> |
| 10 | #include <aws/common/math.h> |
| 11 | |
| 12 | AWS_PUSH_SANE_WARNING_LEVEL |
| 13 | |
| 14 | enum aws_timestamp_unit { |
| 15 | AWS_TIMESTAMP_SECS = 1, |
| 16 | AWS_TIMESTAMP_MILLIS = 1000, |
| 17 | AWS_TIMESTAMP_MICROS = 1000000, |
| 18 | AWS_TIMESTAMP_NANOS = 1000000000, |
| 19 | }; |
| 20 | |
| 21 | AWS_EXTERN_C_BEGIN |
| 22 | |
| 23 | /** |
| 24 | * Converts 'timestamp' from unit 'convert_from' to unit 'convert_to', if the units are the same then 'timestamp' is |
| 25 | * returned. If 'remainder' is NOT NULL, it will be set to the remainder if convert_from is a more precise unit than |
| 26 | * convert_to. To avoid unnecessary branching, 'remainder' is not zero initialized in this function, be sure to set it |
| 27 | * to 0 first if you care about that kind of thing. If conversion would lead to integer overflow, the timestamp |
| 28 | * returned will be the highest possible time that is representable, i.e. UINT64_MAX. |
| 29 | */ |
| 30 | AWS_STATIC_IMPL uint64_t aws_timestamp_convert( |
| 31 | uint64_t timestamp, |
| 32 | enum aws_timestamp_unit convert_from, |
| 33 | enum aws_timestamp_unit convert_to, |
| 34 | uint64_t *remainder); |
| 35 | |
| 36 | /** |
| 37 | * More general form of aws_timestamp_convert that takes arbitrary frequencies rather than the timestamp enum. |
| 38 | */ |
| 39 | AWS_STATIC_IMPL uint64_t |
| 40 | aws_timestamp_convert_u64(uint64_t ticks, uint64_t old_frequency, uint64_t new_frequency, uint64_t *remainder); |
| 41 | |
| 42 | /** |
| 43 | * Get ticks in nanoseconds (usually 100 nanosecond precision) on the high resolution clock (most-likely TSC). This |
| 44 | * clock has no bearing on the actual system time. On success, timestamp will be set. |
| 45 | */ |
| 46 | AWS_COMMON_API |
| 47 | int aws_high_res_clock_get_ticks(uint64_t *timestamp); |
| 48 | |
| 49 | /** |
| 50 | * Get ticks in nanoseconds (usually 100 nanosecond precision) on the system clock. Reflects actual system time via |
| 51 | * nanoseconds since unix epoch. Use with care since an inaccurately set clock will probably cause bugs. On success, |
| 52 | * timestamp will be set. |
| 53 | */ |
| 54 | AWS_COMMON_API |
| 55 | int aws_sys_clock_get_ticks(uint64_t *timestamp); |
| 56 | |
| 57 | AWS_EXTERN_C_END |
| 58 | |
| 59 | #ifndef AWS_NO_STATIC_IMPL |
| 60 | # include <aws/common/clock.inl> |
| 61 | #endif /* AWS_NO_STATIC_IMPL */ |
| 62 | |
| 63 | AWS_POP_SANE_WARNING_LEVEL |
| 64 | |
| 65 | #endif /* AWS_COMMON_CLOCK_H */ |