]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - include/timeutils.h
2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * First set of functions in this file are part of systemd, and were
5 * copied to util-linux at August 2013.
7 * Copyright 2010 Lennart Poettering
8 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
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)
15 #ifndef UTIL_LINUX_TIME_UTIL_H
16 #define UTIL_LINUX_TIME_UTIL_H
23 typedef uint64_t usec_t
;
24 typedef uint64_t nsec_t
;
26 #define MSEC_PER_SEC 1000ULL
27 #define USEC_PER_SEC 1000000ULL
28 #define USEC_PER_MSEC 1000ULL
29 #define NSEC_PER_SEC 1000000000ULL
30 #define NSEC_PER_MSEC 1000000ULL
31 #define NSEC_PER_USEC 1000ULL
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)
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
50 int parse_timestamp(const char *t
, usec_t
*usec
);
51 int get_gmtoff(const struct tm
*tp
);
53 /* flags and masks for strxxx_iso() functions */
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),
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
74 #define CTIME_BUFSIZ 26
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
);
83 #define UL_SHORTTIME_THISYEAR_HHMM (1 << 1)
85 int strtime_short(const time_t *t
, struct timeval
*now
, int flags
, char *buf
, size_t bufsz
);
88 extern time_t timegm(struct tm
*tm
);
91 static inline usec_t
timeval_to_usec(const struct timeval
*t
)
93 return t
->tv_sec
* USEC_PER_SEC
+ t
->tv_usec
;
96 static inline usec_t
timespec_to_usec(const struct timespec
*t
)
98 return t
->tv_sec
* USEC_PER_SEC
+ t
->tv_nsec
/ NSEC_PER_USEC
;
101 static inline struct timeval
usec_to_timeval(usec_t t
)
104 .tv_sec
= t
/ USEC_PER_SEC
,
105 .tv_usec
= t
% USEC_PER_SEC
,
110 static inline bool is_timespecset(const struct timespec
*t
)
112 return t
->tv_sec
|| t
->tv_nsec
;
115 #endif /* UTIL_LINUX_TIME_UTIL_H */