/src/util-linux/include/timeutils.h
Line  | Count  | Source (jump to first uncovered line)  | 
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 <sys/time.h>  | 
21  |  | #include <stdbool.h>  | 
22  |  |  | 
23  |  | typedef uint64_t usec_t;  | 
24  |  | typedef uint64_t nsec_t;  | 
25  |  |  | 
26  | 0  | #define MSEC_PER_SEC  1000ULL  | 
27  |  | #define USEC_PER_SEC  1000000ULL  | 
28  | 0  | #define USEC_PER_MSEC 1000ULL  | 
29  |  | #define NSEC_PER_SEC  1000000000ULL  | 
30  |  | #define NSEC_PER_MSEC 1000000ULL  | 
31  |  | #define NSEC_PER_USEC 1000ULL  | 
32  |  |  | 
33  |  | #define USEC_PER_MINUTE (60ULL*USEC_PER_SEC)  | 
34  |  | #define NSEC_PER_MINUTE (60ULL*NSEC_PER_SEC)  | 
35  |  | #define USEC_PER_HOUR (60ULL*USEC_PER_MINUTE)  | 
36  |  | #define NSEC_PER_HOUR (60ULL*NSEC_PER_MINUTE)  | 
37  |  | #define USEC_PER_DAY  (24ULL*USEC_PER_HOUR)  | 
38  |  | #define NSEC_PER_DAY  (24ULL*NSEC_PER_HOUR)  | 
39  |  | #define USEC_PER_WEEK (7ULL*USEC_PER_DAY)  | 
40  |  | #define NSEC_PER_WEEK (7ULL*NSEC_PER_DAY)  | 
41  |  | #define USEC_PER_MONTH  (2629800ULL*USEC_PER_SEC)  | 
42  |  | #define NSEC_PER_MONTH  (2629800ULL*NSEC_PER_SEC)  | 
43  |  | #define USEC_PER_YEAR (31557600ULL*USEC_PER_SEC)  | 
44  |  | #define NSEC_PER_YEAR (31557600ULL*NSEC_PER_SEC)  | 
45  |  |  | 
46  |  | #define FORMAT_TIMESTAMP_MAX ((4*4+1)+11+9+4+1) /* weekdays can be unicode */  | 
47  |  | #define FORMAT_TIMESTAMP_RELATIVE_MAX 256  | 
48  |  | #define FORMAT_TIMESPAN_MAX 64  | 
49  |  |  | 
50  |  | int ul_parse_timestamp(const char *t, usec_t *usec);  | 
51  |  | int get_gmtoff(const struct tm *tp);  | 
52  |  |  | 
53  |  | /* flags and masks for strxxx_iso() functions */  | 
54  |  | enum { | 
55  |  |   ISO_DATE    = (1 << 0),  | 
56  |  |   ISO_TIME    = (1 << 1),  | 
57  |  |   ISO_TIMEZONE    = (1 << 2),  | 
58  |  |   ISO_DOTUSEC   = (1 << 3),  | 
59  |  |   ISO_COMMAUSEC   = (1 << 4),  | 
60  |  |   ISO_DOTNSEC   = (1 << 5),  | 
61  |  |   ISO_COMMANSEC   = (1 << 6),  | 
62  |  |   ISO_T     = (1 << 7),  | 
63  |  |   ISO_GMTIME    = (1 << 8),  | 
64  |  |   ISO_TIMESTAMP   = ISO_DATE | ISO_TIME | ISO_TIMEZONE,  | 
65  |  |   ISO_TIMESTAMP_T   = ISO_TIMESTAMP | ISO_T,  | 
66  |  |   ISO_TIMESTAMP_DOT = ISO_TIMESTAMP | ISO_DOTUSEC,  | 
67  |  |   ISO_TIMESTAMP_DOT_T = ISO_TIMESTAMP_DOT | ISO_T,  | 
68  |  |   ISO_TIMESTAMP_COMMA = ISO_TIMESTAMP | ISO_COMMAUSEC,  | 
69  |  |   ISO_TIMESTAMP_COMMA_T = ISO_TIMESTAMP_COMMA | ISO_T,  | 
70  |  |   ISO_TIMESTAMP_COMMA_G = ISO_TIMESTAMP_COMMA | ISO_GMTIME,  | 
71  |  |   ISO_TIMESTAMP_COMMA_GT  = ISO_TIMESTAMP_COMMA_G | ISO_T  | 
72  |  | };  | 
73  |  |  | 
74  |  | #define CTIME_BUFSIZ  26  | 
75  |  | #define ISO_BUFSIZ  42  | 
76  |  |  | 
77  |  | int strtimeval_iso(const struct timeval *tv, int flags, char *buf, size_t bufsz);  | 
78  |  | int strtm_iso(const struct tm *tm, int flags, char *buf, size_t bufsz);  | 
79  |  | int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz);  | 
80  |  | int strtimespec_iso(const struct timespec *t, int flags, char *buf, size_t bufsz);  | 
81  |  | int strtimespec_relative(const struct timespec *ts, char *buf, size_t bufsz);  | 
82  |  |  | 
83  |  | #define UL_SHORTTIME_THISYEAR_HHMM (1 << 1)  | 
84  |  |  | 
85  |  | int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz);  | 
86  |  |  | 
87  |  | #ifndef HAVE_TIMEGM  | 
88  |  | extern time_t timegm(struct tm *tm);  | 
89  |  | #endif  | 
90  |  |  | 
91  |  | static inline usec_t timeval_to_usec(const struct timeval *t)  | 
92  | 0  | { | 
93  | 0  |   return t->tv_sec * USEC_PER_SEC + t->tv_usec;  | 
94  | 0  | }  | 
95  |  |  | 
96  |  | static inline usec_t timespec_to_usec(const struct timespec *t)  | 
97  | 0  | { | 
98  | 0  |   return t->tv_sec * USEC_PER_SEC + t->tv_nsec / NSEC_PER_USEC;  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | static inline struct timeval usec_to_timeval(usec_t t)  | 
102  | 0  | { | 
103  | 0  |   struct timeval r = { | 
104  | 0  |     .tv_sec = t / USEC_PER_SEC,  | 
105  | 0  |     .tv_usec = t % USEC_PER_SEC,  | 
106  | 0  |   };  | 
107  | 0  |   return r;  | 
108  | 0  | }  | 
109  |  |  | 
110  |  | static inline bool is_timespecset(const struct timespec *t)  | 
111  | 0  | { | 
112  | 0  |   return t->tv_sec || t->tv_nsec;  | 
113  | 0  | }  | 
114  |  |  | 
115  |  | static inline double time_diff(const struct timeval *a, const struct timeval *b)  | 
116  | 0  | { | 
117  | 0  |   return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / (double) USEC_PER_SEC;  | 
118  | 0  | }  | 
119  |  | #endif /* UTIL_LINUX_TIME_UTIL_H */  |