/src/dovecot/src/lib/time-util.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef TIME_UTIL_H |
2 | | #define TIME_UTIL_H |
3 | | |
4 | | #include <sys/time.h> /* for struct timeval */ |
5 | | |
6 | | /* Same as gettimeofday(), but call i_fatal() if the call fails. */ |
7 | | void i_gettimeofday(struct timeval *tv_r); |
8 | | /* Return nanoseconds since UNIX epoch (1970-01-01). */ |
9 | | uint64_t i_nanoseconds(void); |
10 | | /* Return microseconds since UNIX epoch (1970-01-01). */ |
11 | 0 | static inline uint64_t i_microseconds(void) { |
12 | 0 | return i_nanoseconds() / 1000; |
13 | 0 | } Unexecuted instantiation: var-expand.c:i_microseconds Unexecuted instantiation: connection.c:i_microseconds Unexecuted instantiation: failures.c:i_microseconds Unexecuted instantiation: iostream-rawlog.c:i_microseconds Unexecuted instantiation: ioloop.c:i_microseconds Unexecuted instantiation: ioloop-notify-inotify.c:i_microseconds Unexecuted instantiation: lib-event.c:i_microseconds Unexecuted instantiation: net.c:i_microseconds Unexecuted instantiation: time-util.c:i_microseconds |
14 | | |
15 | | /* Returns -1 if tv1<tv2, 1 if tv1>tv2, 0 if they're equal. */ |
16 | | int timeval_cmp(const struct timeval *tv1, const struct timeval *tv2); |
17 | | /* Same as timeval_cmp, but tv->usecs must differ by at least usec_margin */ |
18 | | int timeval_cmp_margin(const struct timeval *tv1, const struct timeval *tv2, |
19 | | unsigned int usec_margin); |
20 | | /* Returns tv1-tv2 in microseconds. */ |
21 | | long long timeval_diff_usecs(const struct timeval *tv1, |
22 | | const struct timeval *tv2); |
23 | | /* Returns tv1-tv2 in milliseconds. */ |
24 | | static inline long long timeval_diff_msecs(const struct timeval *tv1, |
25 | 0 | const struct timeval *tv2) { |
26 | 0 | return timeval_diff_usecs(tv1, tv2) / 1000; |
27 | 0 | } Unexecuted instantiation: var-expand.c:timeval_diff_msecs Unexecuted instantiation: connection.c:timeval_diff_msecs Unexecuted instantiation: failures.c:timeval_diff_msecs Unexecuted instantiation: iostream-rawlog.c:timeval_diff_msecs Unexecuted instantiation: ioloop.c:timeval_diff_msecs Unexecuted instantiation: ioloop-notify-inotify.c:timeval_diff_msecs Unexecuted instantiation: lib-event.c:timeval_diff_msecs Unexecuted instantiation: net.c:timeval_diff_msecs Unexecuted instantiation: time-util.c:timeval_diff_msecs |
28 | | |
29 | | static inline void |
30 | | timeval_from_usecs(struct timeval *tv_r, unsigned long usecs) |
31 | 0 | { |
32 | 0 | i_assert(usecs != ULLONG_MAX); |
33 | 0 | tv_r->tv_sec = (time_t)(usecs / 1000000); |
34 | 0 | tv_r->tv_usec = usecs % 1000000; |
35 | 0 | } Unexecuted instantiation: var-expand.c:timeval_from_usecs Unexecuted instantiation: connection.c:timeval_from_usecs Unexecuted instantiation: failures.c:timeval_from_usecs Unexecuted instantiation: iostream-rawlog.c:timeval_from_usecs Unexecuted instantiation: ioloop.c:timeval_from_usecs Unexecuted instantiation: ioloop-notify-inotify.c:timeval_from_usecs Unexecuted instantiation: lib-event.c:timeval_from_usecs Unexecuted instantiation: net.c:timeval_from_usecs Unexecuted instantiation: time-util.c:timeval_from_usecs |
36 | | |
37 | | static inline void |
38 | | timeval_add_usecs(struct timeval *tv, suseconds_t usecs) |
39 | 0 | { |
40 | 0 | i_assert(usecs >= 0); |
41 | 0 | tv->tv_sec += (time_t)(usecs / 1000000); |
42 | 0 | tv->tv_usec += (usecs % 1000000); |
43 | 0 | if (tv->tv_usec >= 1000000) { |
44 | 0 | tv->tv_sec++; |
45 | 0 | tv->tv_usec -= 1000000; |
46 | 0 | } |
47 | 0 | } Unexecuted instantiation: var-expand.c:timeval_add_usecs Unexecuted instantiation: connection.c:timeval_add_usecs Unexecuted instantiation: failures.c:timeval_add_usecs Unexecuted instantiation: iostream-rawlog.c:timeval_add_usecs Unexecuted instantiation: ioloop.c:timeval_add_usecs Unexecuted instantiation: ioloop-notify-inotify.c:timeval_add_usecs Unexecuted instantiation: lib-event.c:timeval_add_usecs Unexecuted instantiation: net.c:timeval_add_usecs Unexecuted instantiation: time-util.c:timeval_add_usecs |
48 | | |
49 | | static inline void |
50 | | timeval_sub_usecs(struct timeval *tv, suseconds_t usecs) |
51 | 0 | { |
52 | 0 | i_assert(usecs >= 0); |
53 | 0 | tv->tv_sec -= (time_t)(usecs / 1000000); |
54 | 0 | tv->tv_usec -= (usecs % 1000000); |
55 | 0 | if (tv->tv_usec < 0) { |
56 | 0 | tv->tv_sec--; |
57 | 0 | tv->tv_usec += 1000000; |
58 | 0 | } |
59 | 0 | } Unexecuted instantiation: var-expand.c:timeval_sub_usecs Unexecuted instantiation: connection.c:timeval_sub_usecs Unexecuted instantiation: failures.c:timeval_sub_usecs Unexecuted instantiation: iostream-rawlog.c:timeval_sub_usecs Unexecuted instantiation: ioloop.c:timeval_sub_usecs Unexecuted instantiation: ioloop-notify-inotify.c:timeval_sub_usecs Unexecuted instantiation: lib-event.c:timeval_sub_usecs Unexecuted instantiation: net.c:timeval_sub_usecs Unexecuted instantiation: time-util.c:timeval_sub_usecs |
60 | | |
61 | | static inline void |
62 | | timeval_add_msecs(struct timeval *tv, unsigned int msecs) |
63 | 39.9k | { |
64 | 39.9k | tv->tv_sec += msecs / 1000; |
65 | 39.9k | tv->tv_usec += (long)(msecs % 1000) * 1000; |
66 | 39.9k | if (tv->tv_usec >= 1000000) { |
67 | 310 | tv->tv_sec++; |
68 | 310 | tv->tv_usec -= 1000000; |
69 | 310 | } |
70 | 39.9k | } Unexecuted instantiation: var-expand.c:timeval_add_msecs Unexecuted instantiation: connection.c:timeval_add_msecs Unexecuted instantiation: failures.c:timeval_add_msecs Unexecuted instantiation: iostream-rawlog.c:timeval_add_msecs ioloop.c:timeval_add_msecs Line | Count | Source | 63 | 39.9k | { | 64 | 39.9k | tv->tv_sec += msecs / 1000; | 65 | 39.9k | tv->tv_usec += (long)(msecs % 1000) * 1000; | 66 | 39.9k | if (tv->tv_usec >= 1000000) { | 67 | 310 | tv->tv_sec++; | 68 | 310 | tv->tv_usec -= 1000000; | 69 | 310 | } | 70 | 39.9k | } |
Unexecuted instantiation: ioloop-notify-inotify.c:timeval_add_msecs Unexecuted instantiation: lib-event.c:timeval_add_msecs Unexecuted instantiation: net.c:timeval_add_msecs Unexecuted instantiation: time-util.c:timeval_add_msecs |
71 | | |
72 | | static inline void |
73 | | timeval_sub_msecs(struct timeval *tv, unsigned int msecs) |
74 | 0 | { |
75 | 0 | tv->tv_sec -= msecs / 1000; |
76 | 0 | tv->tv_usec -= (long)(msecs % 1000) * 1000; |
77 | 0 | if (tv->tv_usec < 0) { |
78 | 0 | tv->tv_sec--; |
79 | 0 | tv->tv_usec += 1000000; |
80 | 0 | } |
81 | 0 | } Unexecuted instantiation: var-expand.c:timeval_sub_msecs Unexecuted instantiation: connection.c:timeval_sub_msecs Unexecuted instantiation: failures.c:timeval_sub_msecs Unexecuted instantiation: iostream-rawlog.c:timeval_sub_msecs Unexecuted instantiation: ioloop.c:timeval_sub_msecs Unexecuted instantiation: ioloop-notify-inotify.c:timeval_sub_msecs Unexecuted instantiation: lib-event.c:timeval_sub_msecs Unexecuted instantiation: net.c:timeval_sub_msecs Unexecuted instantiation: time-util.c:timeval_sub_msecs |
82 | | |
83 | | static inline unsigned long long timeval_to_usecs(const struct timeval *tv) |
84 | 0 | { |
85 | 0 | return (tv->tv_sec * 1000000ULL + tv->tv_usec); |
86 | 0 | } Unexecuted instantiation: var-expand.c:timeval_to_usecs Unexecuted instantiation: connection.c:timeval_to_usecs Unexecuted instantiation: failures.c:timeval_to_usecs Unexecuted instantiation: iostream-rawlog.c:timeval_to_usecs Unexecuted instantiation: ioloop.c:timeval_to_usecs Unexecuted instantiation: ioloop-notify-inotify.c:timeval_to_usecs Unexecuted instantiation: lib-event.c:timeval_to_usecs Unexecuted instantiation: net.c:timeval_to_usecs Unexecuted instantiation: time-util.c:timeval_to_usecs |
87 | | |
88 | | static inline void timeval_add(struct timeval *tv, const struct timeval *val) |
89 | 0 | { |
90 | 0 | i_assert(val->tv_usec < 1000000); |
91 | 0 | tv->tv_sec += val->tv_sec; |
92 | 0 | tv->tv_usec += val->tv_usec; |
93 | 0 | if (tv->tv_usec >= 1000000) { |
94 | 0 | tv->tv_sec++; |
95 | 0 | tv->tv_usec -= 1000000; |
96 | 0 | } |
97 | 0 | } Unexecuted instantiation: var-expand.c:timeval_add Unexecuted instantiation: connection.c:timeval_add Unexecuted instantiation: failures.c:timeval_add Unexecuted instantiation: iostream-rawlog.c:timeval_add Unexecuted instantiation: ioloop.c:timeval_add Unexecuted instantiation: ioloop-notify-inotify.c:timeval_add Unexecuted instantiation: lib-event.c:timeval_add Unexecuted instantiation: net.c:timeval_add Unexecuted instantiation: time-util.c:timeval_add |
98 | | |
99 | | static inline time_t timeval_round(struct timeval *tv) |
100 | 0 | { |
101 | 0 | return (tv->tv_usec < 500000 ? tv->tv_sec : tv->tv_sec + 1); |
102 | 0 | } Unexecuted instantiation: var-expand.c:timeval_round Unexecuted instantiation: connection.c:timeval_round Unexecuted instantiation: failures.c:timeval_round Unexecuted instantiation: iostream-rawlog.c:timeval_round Unexecuted instantiation: ioloop.c:timeval_round Unexecuted instantiation: ioloop-notify-inotify.c:timeval_round Unexecuted instantiation: lib-event.c:timeval_round Unexecuted instantiation: net.c:timeval_round Unexecuted instantiation: time-util.c:timeval_round |
103 | | |
104 | | /* Convert t to local time and return timestamp on that day at 00:00:00. */ |
105 | | time_t time_to_local_day_start(time_t t); |
106 | | |
107 | | /* Wrappers to strftime() */ |
108 | | const char *t_strftime(const char *fmt, const struct tm *tm) ATTR_STRFTIME(1); |
109 | | const char *t_strflocaltime(const char *fmt, time_t t) ATTR_STRFTIME(1); |
110 | | const char *t_strfgmtime(const char *fmt, time_t t) ATTR_STRFTIME(1); |
111 | | |
112 | | /* Parse string as <unix timestamp>[.<usecs>] into timeval. <usecs> must not |
113 | | have higher precision time, i.e. a maximum of 6 digits is allowed. Note that |
114 | | ".1" is handled as ".1000000" so the string should have been written using |
115 | | "%06u" printf format. */ |
116 | | int str_to_timeval(const char *str, struct timeval *tv_r) |
117 | | ATTR_WARN_UNUSED_RESULT; |
118 | | |
119 | | #endif |