Commit | Line | Data |
---|---|---|
395d6b02 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
395d6b02 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-or-later |
395d6b02 | 5 | * |
395d6b02 JG |
6 | */ |
7 | ||
8 | #ifndef LTTNG_TIME_H | |
9 | #define LTTNG_TIME_H | |
10 | ||
2daf6502 | 11 | #include <time.h> |
2a1135fa | 12 | #include <stdbool.h> |
03ab1b42 | 13 | #include <common/macros.h> |
efef32e9 | 14 | #include <common/compat/time.h> |
2daf6502 | 15 | |
81684730 JR |
16 | #define MSEC_PER_SEC 1000ULL |
17 | #define NSEC_PER_SEC 1000000000ULL | |
18 | #define NSEC_PER_MSEC 1000000ULL | |
19 | #define NSEC_PER_USEC 1000ULL | |
20 | #define USEC_PER_SEC 1000000ULL | |
21 | #define USEC_PER_MSEC 1000ULL | |
22 | ||
23 | #define SEC_PER_MINUTE 60ULL | |
24 | #define MINUTE_PER_HOUR 60ULL | |
25 | ||
26 | #define USEC_PER_MINUTE (USEC_PER_SEC * SEC_PER_MINUTE) | |
27 | #define USEC_PER_HOURS (USEC_PER_MINUTE * MINUTE_PER_HOUR) | |
395d6b02 | 28 | |
274ca939 | 29 | #define ISO8601_STR_LEN sizeof("YYYYmmddTHHMMSS+HHMM") |
a8b66566 | 30 | #define DATETIME_STR_LEN sizeof("YYYYmmdd-HHMMSS") |
274ca939 | 31 | |
03ab1b42 | 32 | LTTNG_HIDDEN |
2a1135fa JG |
33 | bool locale_supports_utf8(void); |
34 | ||
35 | #define NSEC_UNIT "ns" | |
36 | #define USEC_UNIT (locale_supports_utf8() ? "µs" : "us") | |
37 | #define MSEC_UNIT "ms" | |
38 | #define SEC_UNIT "s" | |
39 | #define MIN_UNIT "m" | |
40 | #define HR_UNIT "h" | |
41 | ||
2daf6502 MD |
42 | /* |
43 | * timespec_to_ms: Convert timespec to milliseconds. | |
44 | * | |
45 | * Returns 0 on success, else -1 on error. errno is set to EOVERFLOW if | |
46 | * input would overflow the output in milliseconds. | |
47 | */ | |
03ab1b42 | 48 | LTTNG_HIDDEN |
2daf6502 MD |
49 | int timespec_to_ms(struct timespec ts, unsigned long *ms); |
50 | ||
51 | /* | |
52 | * timespec_abs_diff: Absolute difference between timespec. | |
53 | */ | |
03ab1b42 | 54 | LTTNG_HIDDEN |
2daf6502 MD |
55 | struct timespec timespec_abs_diff(struct timespec ts_a, struct timespec ts_b); |
56 | ||
274ca939 JG |
57 | /* |
58 | * Format a Unix timestamp to an ISO 8601 compatible timestamp of | |
59 | * the form "YYYYmmddTHHMMSS+HHMM" in local time. `len` must >= to | |
60 | * ISO8601_STR_LEN. | |
61 | * | |
62 | * Returns 0 on success, else -1 on error. | |
63 | */ | |
64 | LTTNG_HIDDEN | |
65 | int time_to_iso8601_str(time_t time, char *str, size_t len); | |
66 | ||
a8b66566 JR |
67 | LTTNG_HIDDEN |
68 | int time_to_datetime_str(time_t time, char *str, size_t len); | |
69 | ||
395d6b02 | 70 | #endif /* LTTNG_TIME_H */ |