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 | |
48a40005 SM |
16 | #if defined(__cplusplus) |
17 | extern "C" { | |
18 | #endif | |
19 | ||
81684730 JR |
20 | #define MSEC_PER_SEC 1000ULL |
21 | #define NSEC_PER_SEC 1000000000ULL | |
22 | #define NSEC_PER_MSEC 1000000ULL | |
23 | #define NSEC_PER_USEC 1000ULL | |
24 | #define USEC_PER_SEC 1000000ULL | |
25 | #define USEC_PER_MSEC 1000ULL | |
26 | ||
27 | #define SEC_PER_MINUTE 60ULL | |
28 | #define MINUTE_PER_HOUR 60ULL | |
29 | ||
30 | #define USEC_PER_MINUTE (USEC_PER_SEC * SEC_PER_MINUTE) | |
31 | #define USEC_PER_HOURS (USEC_PER_MINUTE * MINUTE_PER_HOUR) | |
395d6b02 | 32 | |
274ca939 | 33 | #define ISO8601_STR_LEN sizeof("YYYYmmddTHHMMSS+HHMM") |
a8b66566 | 34 | #define DATETIME_STR_LEN sizeof("YYYYmmdd-HHMMSS") |
274ca939 | 35 | |
2a1135fa JG |
36 | bool locale_supports_utf8(void); |
37 | ||
38 | #define NSEC_UNIT "ns" | |
39 | #define USEC_UNIT (locale_supports_utf8() ? "µs" : "us") | |
40 | #define MSEC_UNIT "ms" | |
41 | #define SEC_UNIT "s" | |
42 | #define MIN_UNIT "m" | |
43 | #define HR_UNIT "h" | |
44 | ||
2daf6502 MD |
45 | /* |
46 | * timespec_to_ms: Convert timespec to milliseconds. | |
47 | * | |
48 | * Returns 0 on success, else -1 on error. errno is set to EOVERFLOW if | |
49 | * input would overflow the output in milliseconds. | |
50 | */ | |
51 | int timespec_to_ms(struct timespec ts, unsigned long *ms); | |
52 | ||
53 | /* | |
54 | * timespec_abs_diff: Absolute difference between timespec. | |
55 | */ | |
56 | struct timespec timespec_abs_diff(struct timespec ts_a, struct timespec ts_b); | |
57 | ||
274ca939 JG |
58 | /* |
59 | * Format a Unix timestamp to an ISO 8601 compatible timestamp of | |
60 | * the form "YYYYmmddTHHMMSS+HHMM" in local time. `len` must >= to | |
61 | * ISO8601_STR_LEN. | |
62 | * | |
63 | * Returns 0 on success, else -1 on error. | |
64 | */ | |
274ca939 JG |
65 | int time_to_iso8601_str(time_t time, char *str, size_t len); |
66 | ||
a8b66566 JR |
67 | int time_to_datetime_str(time_t time, char *str, size_t len); |
68 | ||
48a40005 SM |
69 | #if defined(__cplusplus) |
70 | } | |
71 | #endif | |
72 | ||
395d6b02 | 73 | #endif /* LTTNG_TIME_H */ |