Line data Source code
1 : #ifndef AWS_COMMON_CLOCK_INL
2 : #define AWS_COMMON_CLOCK_INL
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/clock.h>
10 : #include <aws/common/common.h>
11 : #include <aws/common/math.h>
12 :
13 : AWS_EXTERN_C_BEGIN
14 :
15 : /**
16 : * Converts 'timestamp' from unit 'convert_from' to unit 'convert_to', if the units are the same then 'timestamp' is
17 : * returned. If 'remainder' is NOT NULL, it will be set to the remainder if convert_from is a more precise unit than
18 : * convert_to. To avoid unnecessary branching, 'remainder' is not zero initialized in this function, be sure to set it
19 : * to 0 first if you care about that kind of thing. If conversion would lead to integer overflow, the timestamp
20 : * returned will be the highest possible time that is representable, i.e. UINT64_MAX.
21 : */
22 : AWS_STATIC_IMPL uint64_t aws_timestamp_convert(
23 : uint64_t timestamp,
24 : enum aws_timestamp_unit convert_from,
25 : enum aws_timestamp_unit convert_to,
26 0 : uint64_t *remainder) {
27 0 : uint64_t diff = 0;
28 0 :
29 0 : if (convert_to > convert_from) {
30 0 : diff = convert_to / convert_from;
31 0 : return aws_mul_u64_saturating(timestamp, diff);
32 0 : } else if (convert_to < convert_from) {
33 0 : diff = convert_from / convert_to;
34 0 :
35 0 : if (remainder) {
36 0 : *remainder = timestamp % diff;
37 0 : }
38 0 :
39 0 : return timestamp / diff;
40 0 : } else {
41 0 : return timestamp;
42 0 : }
43 0 : }
44 :
45 : AWS_EXTERN_C_END
46 :
47 : #endif /* AWS_COMMON_CLOCK_INL */
|