]> git.ipfire.org Git - thirdparty/util-linux.git/blame - include/timeutils.h
libmount: check for linux/mount.h
[thirdparty/util-linux.git] / include / timeutils.h
CommitLineData
79feaa60
KZ
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 */
2659a49e
SK
15#ifndef UTIL_LINUX_TIME_UTIL_H
16#define UTIL_LINUX_TIME_UTIL_H
17
18#include <stdio.h>
19#include <inttypes.h>
3c201431 20#include <sys/time.h>
765b4a14 21#include <stdbool.h>
2659a49e
SK
22
23typedef uint64_t usec_t;
24typedef uint64_t nsec_t;
25
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
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
50int parse_timestamp(const char *t, usec_t *usec);
9fd0a7a9 51int get_gmtoff(const struct tm *tp);
2659a49e 52
4111bb3a 53/* flags and masks for strxxx_iso() functions */
3c201431 54enum {
4111bb3a
WP
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),
136f4874
TW
60 ISO_DOTNSEC = (1 << 5),
61 ISO_COMMANSEC = (1 << 6),
62 ISO_T = (1 << 7),
63 ISO_GMTIME = (1 << 8),
4111bb3a
WP
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
3c201431 72};
33c7ffa3 73
3160589d 74#define CTIME_BUFSIZ 26
4111bb3a 75#define ISO_BUFSIZ 42
33c7ffa3 76
971244fd
TW
77int strtimeval_iso(const struct timeval *tv, int flags, char *buf, size_t bufsz);
78int strtm_iso(const struct tm *tm, int flags, char *buf, size_t bufsz);
33c7ffa3 79int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz);
b9abaae3 80int strtimespec_iso(const struct timespec *t, int flags, char *buf, size_t bufsz);
00ddcaf1 81int strtimespec_relative(const struct timespec *ts, char *buf, size_t bufsz);
3c201431 82
eee665bb
KZ
83#define UL_SHORTTIME_THISYEAR_HHMM (1 << 1)
84
eee665bb
KZ
85int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz);
86
b72a75e9
SK
87#ifndef HAVE_TIMEGM
88extern time_t timegm(struct tm *tm);
89#endif
90
a8ec2e15
TW
91static inline usec_t timeval_to_usec(const struct timeval *t)
92{
93 return t->tv_sec * USEC_PER_SEC + t->tv_usec;
94}
95
96static inline usec_t timespec_to_usec(const struct timespec *t)
97{
98 return t->tv_sec * USEC_PER_SEC + t->tv_nsec / NSEC_PER_USEC;
99}
100
101static inline struct timeval usec_to_timeval(usec_t t)
102{
103 struct timeval r = {
104 .tv_sec = t / USEC_PER_SEC,
105 .tv_usec = t % USEC_PER_SEC,
106 };
107 return r;
108}
109
765b4a14
MY
110static inline bool is_timespecset(const struct timespec *t)
111{
112 return t->tv_sec || t->tv_nsec;
113}
114
2659a49e 115#endif /* UTIL_LINUX_TIME_UTIL_H */