/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 | | #define USEC_PER_MSEC 1000ULL |
31 | 0 | #define NSEC_PER_SEC 1000000000ULL |
32 | | #define NSEC_PER_MSEC 1000000ULL |
33 | 0 | #define NSEC_PER_USEC 1000ULL |
34 | | |
35 | | #define USEC_PER_MINUTE (60ULL*USEC_PER_SEC) |
36 | | #define NSEC_PER_MINUTE (60ULL*NSEC_PER_SEC) |
37 | | #define USEC_PER_HOUR (60ULL*USEC_PER_MINUTE) |
38 | | #define NSEC_PER_HOUR (60ULL*NSEC_PER_MINUTE) |
39 | | #define USEC_PER_DAY (24ULL*USEC_PER_HOUR) |
40 | | #define NSEC_PER_DAY (24ULL*NSEC_PER_HOUR) |
41 | | #define USEC_PER_WEEK (7ULL*USEC_PER_DAY) |
42 | | #define NSEC_PER_WEEK (7ULL*NSEC_PER_DAY) |
43 | | #define USEC_PER_MONTH (2629800ULL*USEC_PER_SEC) |
44 | | #define NSEC_PER_MONTH (2629800ULL*NSEC_PER_SEC) |
45 | | #define USEC_PER_YEAR (31557600ULL*USEC_PER_SEC) |
46 | | #define NSEC_PER_YEAR (31557600ULL*NSEC_PER_SEC) |
47 | | |
48 | | #define TIME_T_MAX ((time_t) ((1ULL << (sizeof(time_t) * 8 - 1)) - 1)) |
49 | | |
50 | | #define FORMAT_TIMESTAMP_MAX ((4*4+1)+11+9+4+1) /* weekdays can be unicode */ |
51 | | #define FORMAT_TIMESTAMP_RELATIVE_MAX 256 |
52 | | #define FORMAT_TIMESPAN_MAX 64 |
53 | | |
54 | | int ul_parse_timestamp(const char *t, usec_t *usec); |
55 | | int get_gmtoff(const struct tm *tp); |
56 | | |
57 | | /* flags and masks for strxxx_iso() functions */ |
58 | | enum { |
59 | | ISO_DATE = (1 << 0), |
60 | | ISO_TIME = (1 << 1), |
61 | | ISO_TIMEZONE = (1 << 2), |
62 | | ISO_DOTUSEC = (1 << 3), |
63 | | ISO_COMMAUSEC = (1 << 4), |
64 | | ISO_DOTNSEC = (1 << 5), |
65 | | ISO_COMMANSEC = (1 << 6), |
66 | | ISO_T = (1 << 7), |
67 | | ISO_GMTIME = (1 << 8), |
68 | | ISO_TIMESTAMP = ISO_DATE | ISO_TIME | ISO_TIMEZONE, |
69 | | ISO_TIMESTAMP_T = ISO_TIMESTAMP | ISO_T, |
70 | | ISO_TIMESTAMP_DOT = ISO_TIMESTAMP | ISO_DOTUSEC, |
71 | | ISO_TIMESTAMP_DOT_T = ISO_TIMESTAMP_DOT | ISO_T, |
72 | | ISO_TIMESTAMP_COMMA = ISO_TIMESTAMP | ISO_COMMAUSEC, |
73 | | ISO_TIMESTAMP_COMMA_T = ISO_TIMESTAMP_COMMA | ISO_T, |
74 | | ISO_TIMESTAMP_COMMA_G = ISO_TIMESTAMP_COMMA | ISO_GMTIME, |
75 | | ISO_TIMESTAMP_COMMA_GT = ISO_TIMESTAMP_COMMA_G | ISO_T |
76 | | }; |
77 | | |
78 | | #define CTIME_BUFSIZ 26 |
79 | | #define ISO_BUFSIZ 42 |
80 | | |
81 | | int strtimeval_iso(const struct timeval *tv, int flags, char *buf, size_t bufsz); |
82 | | int strtm_iso(const struct tm *tm, int flags, char *buf, size_t bufsz); |
83 | | int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz); |
84 | | int strtimespec_iso(const struct timespec *t, int flags, char *buf, size_t bufsz); |
85 | | int strtimespec_relative(const struct timespec *ts, char *buf, size_t bufsz); |
86 | | |
87 | | #define UL_SHORTTIME_THISYEAR_HHMM (1 << 1) |
88 | | |
89 | | int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz); |
90 | | |
91 | | #ifndef HAVE_TIMEGM |
92 | | extern time_t timegm(struct tm *tm); |
93 | | #endif |
94 | | |
95 | | static inline usec_t timeval_to_usec(const struct timeval *t) |
96 | 0 | { |
97 | 0 | return t->tv_sec * USEC_PER_SEC + t->tv_usec; |
98 | 0 | } Unexecuted instantiation: canonicalize.c:timeval_to_usec Unexecuted instantiation: env.c:timeval_to_usec Unexecuted instantiation: fileutils.c:timeval_to_usec Unexecuted instantiation: loopdev.c:timeval_to_usec Unexecuted instantiation: path.c:timeval_to_usec Unexecuted instantiation: strutils.c:timeval_to_usec Unexecuted instantiation: sysfs.c:timeval_to_usec Unexecuted instantiation: blkdev.c:timeval_to_usec Unexecuted instantiation: probe.c:timeval_to_usec Unexecuted instantiation: verify.c:timeval_to_usec |
99 | | |
100 | | static inline usec_t timespec_to_usec(const struct timespec *t) |
101 | 0 | { |
102 | 0 | return t->tv_sec * USEC_PER_SEC + t->tv_nsec / NSEC_PER_USEC; |
103 | 0 | } Unexecuted instantiation: canonicalize.c:timespec_to_usec Unexecuted instantiation: env.c:timespec_to_usec Unexecuted instantiation: fileutils.c:timespec_to_usec Unexecuted instantiation: loopdev.c:timespec_to_usec Unexecuted instantiation: path.c:timespec_to_usec Unexecuted instantiation: strutils.c:timespec_to_usec Unexecuted instantiation: sysfs.c:timespec_to_usec Unexecuted instantiation: blkdev.c:timespec_to_usec Unexecuted instantiation: probe.c:timespec_to_usec Unexecuted instantiation: verify.c:timespec_to_usec |
104 | | |
105 | | static inline struct timeval usec_to_timeval(usec_t t) |
106 | 0 | { |
107 | 0 | struct timeval r = { |
108 | 0 | .tv_sec = t / USEC_PER_SEC, |
109 | 0 | .tv_usec = t % USEC_PER_SEC, |
110 | 0 | }; |
111 | 0 | return r; |
112 | 0 | } Unexecuted instantiation: canonicalize.c:usec_to_timeval Unexecuted instantiation: env.c:usec_to_timeval Unexecuted instantiation: fileutils.c:usec_to_timeval Unexecuted instantiation: loopdev.c:usec_to_timeval Unexecuted instantiation: path.c:usec_to_timeval Unexecuted instantiation: strutils.c:usec_to_timeval Unexecuted instantiation: sysfs.c:usec_to_timeval Unexecuted instantiation: blkdev.c:usec_to_timeval Unexecuted instantiation: probe.c:usec_to_timeval Unexecuted instantiation: verify.c:usec_to_timeval |
113 | | |
114 | | static inline bool is_timespecset(const struct timespec *t) |
115 | 0 | { |
116 | 0 | return t->tv_sec || t->tv_nsec; |
117 | 0 | } Unexecuted instantiation: canonicalize.c:is_timespecset Unexecuted instantiation: env.c:is_timespecset Unexecuted instantiation: fileutils.c:is_timespecset Unexecuted instantiation: loopdev.c:is_timespecset Unexecuted instantiation: path.c:is_timespecset Unexecuted instantiation: strutils.c:is_timespecset Unexecuted instantiation: sysfs.c:is_timespecset Unexecuted instantiation: blkdev.c:is_timespecset Unexecuted instantiation: probe.c:is_timespecset Unexecuted instantiation: verify.c:is_timespecset |
118 | | |
119 | | static inline double time_diff(const struct timeval *a, const struct timeval *b) |
120 | 0 | { |
121 | 0 | return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / (double) USEC_PER_SEC; |
122 | 0 | } Unexecuted instantiation: canonicalize.c:time_diff Unexecuted instantiation: env.c:time_diff Unexecuted instantiation: fileutils.c:time_diff Unexecuted instantiation: loopdev.c:time_diff Unexecuted instantiation: path.c:time_diff Unexecuted instantiation: strutils.c:time_diff Unexecuted instantiation: sysfs.c:time_diff Unexecuted instantiation: blkdev.c:time_diff Unexecuted instantiation: probe.c:time_diff Unexecuted instantiation: verify.c:time_diff |
123 | | |
124 | | /* |
125 | | * The usleep function was marked obsolete in POSIX.1-2001 and was removed |
126 | | * in POSIX.1-2008. It was replaced with nanosleep() that provides more |
127 | | * advantages (like no interaction with signals and other timer functions). |
128 | | */ |
129 | | static inline int xusleep(usec_t usec) |
130 | 0 | { |
131 | 0 | #ifdef HAVE_NANOSLEEP |
132 | 0 | struct timespec waittime = { |
133 | 0 | .tv_sec = usec / USEC_PER_SEC, |
134 | 0 | .tv_nsec = (usec % USEC_PER_SEC) * NSEC_PER_USEC |
135 | 0 | }; |
136 | 0 | return nanosleep(&waittime, NULL); |
137 | | #elif defined(HAVE_USLEEP) |
138 | | return usleep(usec); |
139 | | #else |
140 | | # error "System with usleep() or nanosleep() required!" |
141 | | #endif |
142 | 0 | } Unexecuted instantiation: canonicalize.c:xusleep Unexecuted instantiation: env.c:xusleep Unexecuted instantiation: fileutils.c:xusleep Unexecuted instantiation: loopdev.c:xusleep Unexecuted instantiation: path.c:xusleep Unexecuted instantiation: strutils.c:xusleep Unexecuted instantiation: sysfs.c:xusleep Unexecuted instantiation: blkdev.c:xusleep Unexecuted instantiation: probe.c:xusleep Unexecuted instantiation: verify.c:xusleep |
143 | | |
144 | | #endif /* UTIL_LINUX_TIME_UTIL_H */ |