]> wimlib.net Git - wimlib/blob - src/timestamp.h
6f08fc2e03fe641d993eabbedb684dee442212df
[wimlib] / src / timestamp.h
1 #ifndef _WIMLIB_TIMESTAMP_H
2 #define _WIMLIB_TIMESTAMP_H
3
4 #include "util.h"
5 #include <sys/types.h>
6
7 #define intervals_per_second (1000000000ULL / 100ULL)
8 #define intervals_per_microsecond (10)
9 #define nanoseconds_per_interval (100)
10 #define days_per_year (365ULL)
11 #define seconds_per_day (3600ULL * 24ULL)
12 #define intervals_per_day (seconds_per_day * intervals_per_second)
13 #define intervals_per_year (intervals_per_day * days_per_year)
14 #define years_1601_to_1970 (1970ULL - 1601ULL)
15 #define leap_years_1601_to_1970 (years_1601_to_1970 / 4ULL - 3ULL)
16 #define intervals_1601_to_1970 (years_1601_to_1970 * intervals_per_year \
17                                 + leap_years_1601_to_1970 * intervals_per_day)
18
19 static inline u64 unix_timestamp_to_wim(time_t t)
20 {
21         return (u64)intervals_1601_to_1970 + t * intervals_per_second;
22 }
23
24
25 /* Converts a timestamp as used in the WIM file to a UNIX timestamp as used in
26  * the time() function. */
27 static inline time_t wim_timestamp_to_unix(u64 timestamp)
28 {
29         return (timestamp - intervals_1601_to_1970) / intervals_per_second;
30 }
31
32 static inline u64 timeval_to_wim_timestamp(const struct timeval *tv)
33 {
34         return intervals_1601_to_1970
35                + (u64)tv->tv_sec * intervals_per_second
36                + (u64)tv->tv_usec * intervals_per_microsecond;
37 }
38
39 static inline void wim_timestamp_to_timeval(u64 timestamp, struct timeval *tv)
40 {
41         tv->tv_sec = (timestamp - intervals_1601_to_1970) / intervals_per_second;
42         tv->tv_usec = ((timestamp - intervals_1601_to_1970) /
43                         intervals_per_microsecond) % 1000000;
44 }
45
46 static inline u64 timespec_to_wim_timestamp(const struct timespec *ts)
47 {
48         return intervals_1601_to_1970
49                + (u64)ts->tv_sec * intervals_per_second
50                + (u64)ts->tv_nsec / nanoseconds_per_interval;
51 }
52
53
54 extern u64 get_wim_timestamp();
55
56 #endif