/src/util-linux/include/timeutils.h
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
3 | | * |
4 | | * First set of functions in this file are part of systemd, and were |
5 | | * copied to util-linux at August 2013. |
6 | | * |
7 | | * Copyright 2010 Lennart Poettering |
8 | | * Copyright (C) 2014 Karel Zak <kzak@redhat.com> |
9 | | * |
10 | | * This is free software; you can redistribute it and/or modify it under the |
11 | | * terms of the GNU Lesser General Public License as published by the Free |
12 | | * Software Foundation; either version 2.1 of the License, or (at your option) |
13 | | * any later version. |
14 | | */ |
15 | | #ifndef UTIL_LINUX_TIME_UTIL_H |
16 | | #define UTIL_LINUX_TIME_UTIL_H |
17 | | |
18 | | #include <stdio.h> |
19 | | #include <inttypes.h> |
20 | | #include <time.h> |
21 | | #include <sys/time.h> |
22 | | #include <stdbool.h> |
23 | | |
24 | | typedef uint64_t msec_t; |
25 | | typedef uint64_t usec_t; |
26 | | typedef uint64_t nsec_t; |
27 | | |
28 | | #define MSEC_PER_SEC 1000ULL |
29 | 0 | #define USEC_PER_SEC 1000000ULL |
30 | 0 | #define USEC_PER_MSEC 1000ULL |
31 | 0 | #define NSEC_PER_SEC 1000000000ULL |
32 | 0 | #define NSEC_PER_MSEC 1000000ULL |
33 | 0 | #define NSEC_PER_USEC 1000ULL |
34 | | |
35 | 0 | #define USEC_PER_MINUTE (60ULL*USEC_PER_SEC) |
36 | 0 | #define NSEC_PER_MINUTE (60ULL*NSEC_PER_SEC) |
37 | 0 | #define USEC_PER_HOUR (60ULL*USEC_PER_MINUTE) |
38 | 0 | #define NSEC_PER_HOUR (60ULL*NSEC_PER_MINUTE) |
39 | 0 | #define USEC_PER_DAY (24ULL*USEC_PER_HOUR) |
40 | 0 | #define NSEC_PER_DAY (24ULL*NSEC_PER_HOUR) |
41 | 0 | #define USEC_PER_WEEK (7ULL*USEC_PER_DAY) |
42 | | #define NSEC_PER_WEEK (7ULL*NSEC_PER_DAY) |
43 | 0 | #define USEC_PER_MONTH (2629800ULL*USEC_PER_SEC) |
44 | | #define NSEC_PER_MONTH (2629800ULL*NSEC_PER_SEC) |
45 | 0 | #define USEC_PER_YEAR (31557600ULL*USEC_PER_SEC) |
46 | 0 | #define NSEC_PER_YEAR (31557600ULL*NSEC_PER_SEC) |
47 | | |
48 | | #define FORMAT_TIMESTAMP_MAX ((4*4+1)+11+9+4+1) /* weekdays can be unicode */ |
49 | | #define FORMAT_TIMESTAMP_RELATIVE_MAX 256 |
50 | | #define FORMAT_TIMESPAN_MAX 64 |
51 | | |
52 | | int ul_parse_timestamp(const char *t, usec_t *usec); |
53 | | int get_gmtoff(const struct tm *tp); |
54 | | |
55 | | /* flags and masks for strxxx_iso() functions */ |
56 | | enum { |
57 | | ISO_DATE = (1 << 0), |
58 | | ISO_TIME = (1 << 1), |
59 | | ISO_TIMEZONE = (1 << 2), |
60 | | ISO_DOTUSEC = (1 << 3), |
61 | | ISO_COMMAUSEC = (1 << 4), |
62 | | ISO_DOTNSEC = (1 << 5), |
63 | | ISO_COMMANSEC = (1 << 6), |
64 | | ISO_T = (1 << 7), |
65 | | ISO_GMTIME = (1 << 8), |
66 | | ISO_TIMESTAMP = ISO_DATE | ISO_TIME | ISO_TIMEZONE, |
67 | | ISO_TIMESTAMP_T = ISO_TIMESTAMP | ISO_T, |
68 | | ISO_TIMESTAMP_DOT = ISO_TIMESTAMP | ISO_DOTUSEC, |
69 | | ISO_TIMESTAMP_DOT_T = ISO_TIMESTAMP_DOT | ISO_T, |
70 | | ISO_TIMESTAMP_COMMA = ISO_TIMESTAMP | ISO_COMMAUSEC, |
71 | | ISO_TIMESTAMP_COMMA_T = ISO_TIMESTAMP_COMMA | ISO_T, |
72 | | ISO_TIMESTAMP_COMMA_G = ISO_TIMESTAMP_COMMA | ISO_GMTIME, |
73 | | ISO_TIMESTAMP_COMMA_GT = ISO_TIMESTAMP_COMMA_G | ISO_T |
74 | | }; |
75 | | |
76 | | #define CTIME_BUFSIZ 26 |
77 | | #define ISO_BUFSIZ 42 |
78 | | |
79 | | int strtimeval_iso(const struct timeval *tv, int flags, char *buf, size_t bufsz); |
80 | | int strtm_iso(const struct tm *tm, int flags, char *buf, size_t bufsz); |
81 | | int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz); |
82 | | int strtimespec_iso(const struct timespec *t, int flags, char *buf, size_t bufsz); |
83 | | int strtimespec_relative(const struct timespec *ts, char *buf, size_t bufsz); |
84 | | |
85 | 0 | #define UL_SHORTTIME_THISYEAR_HHMM (1 << 1) |
86 | | |
87 | | int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz); |
88 | | |
89 | | #ifndef HAVE_TIMEGM |
90 | | extern time_t timegm(struct tm *tm); |
91 | | #endif |
92 | | |
93 | | static inline usec_t timeval_to_usec(const struct timeval *t) |
94 | 0 | { |
95 | 0 | return t->tv_sec * USEC_PER_SEC + t->tv_usec; |
96 | 0 | } Unexecuted instantiation: last.c:timeval_to_usec Unexecuted instantiation: fileutils.c:timeval_to_usec Unexecuted instantiation: timeutils.c:timeval_to_usec |
97 | | |
98 | | static inline usec_t timespec_to_usec(const struct timespec *t) |
99 | 0 | { |
100 | 0 | return t->tv_sec * USEC_PER_SEC + t->tv_nsec / NSEC_PER_USEC; |
101 | 0 | } Unexecuted instantiation: last.c:timespec_to_usec Unexecuted instantiation: fileutils.c:timespec_to_usec Unexecuted instantiation: timeutils.c:timespec_to_usec |
102 | | |
103 | | static inline struct timeval usec_to_timeval(usec_t t) |
104 | 0 | { |
105 | 0 | struct timeval r = { |
106 | 0 | .tv_sec = t / USEC_PER_SEC, |
107 | 0 | .tv_usec = t % USEC_PER_SEC, |
108 | 0 | }; |
109 | 0 | return r; |
110 | 0 | } Unexecuted instantiation: last.c:usec_to_timeval Unexecuted instantiation: fileutils.c:usec_to_timeval Unexecuted instantiation: timeutils.c:usec_to_timeval |
111 | | |
112 | | static inline bool is_timespecset(const struct timespec *t) |
113 | 0 | { |
114 | 0 | return t->tv_sec || t->tv_nsec; |
115 | 0 | } Unexecuted instantiation: last.c:is_timespecset Unexecuted instantiation: fileutils.c:is_timespecset Unexecuted instantiation: timeutils.c:is_timespecset |
116 | | |
117 | | static inline double time_diff(const struct timeval *a, const struct timeval *b) |
118 | 0 | { |
119 | 0 | return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / (double) USEC_PER_SEC; |
120 | 0 | } Unexecuted instantiation: last.c:time_diff Unexecuted instantiation: fileutils.c:time_diff Unexecuted instantiation: timeutils.c:time_diff |
121 | | |
122 | | /* |
123 | | * The usleep function was marked obsolete in POSIX.1-2001 and was removed |
124 | | * in POSIX.1-2008. It was replaced with nanosleep() that provides more |
125 | | * advantages (like no interaction with signals and other timer functions). |
126 | | */ |
127 | | static inline int xusleep(usec_t usec) |
128 | 0 | { |
129 | 0 | #ifdef HAVE_NANOSLEEP |
130 | 0 | struct timespec waittime = { |
131 | 0 | .tv_sec = usec / USEC_PER_SEC, |
132 | 0 | .tv_nsec = (usec % USEC_PER_SEC) * NSEC_PER_USEC |
133 | 0 | }; |
134 | 0 | return nanosleep(&waittime, NULL); |
135 | | #elif defined(HAVE_USLEEP) |
136 | | return usleep(usec); |
137 | | #else |
138 | | # error "System with usleep() or nanosleep() required!" |
139 | | #endif |
140 | 0 | } Unexecuted instantiation: last.c:xusleep Unexecuted instantiation: fileutils.c:xusleep Unexecuted instantiation: timeutils.c:xusleep |
141 | | |
142 | | #endif /* UTIL_LINUX_TIME_UTIL_H */ |