add_params: Embed inode table and sd_set directly
[wimlib] / include / wimlib / timestamp.h
1 #ifndef _WIMLIB_TIMESTAMP_H
2 #define _WIMLIB_TIMESTAMP_H
3
4 #include "wimlib/types.h"
5 #include <sys/types.h>
6 #include <sys/time.h>
7 #include <time.h>
8
9 #define intervals_per_second (1000000000ULL / 100ULL)
10 #define intervals_per_microsecond (10)
11 #define nanoseconds_per_interval (100)
12 #define days_per_year (365ULL)
13 #define seconds_per_day (3600ULL * 24ULL)
14 #define intervals_per_day (seconds_per_day * intervals_per_second)
15 #define intervals_per_year (intervals_per_day * days_per_year)
16 #define years_1601_to_1970 (1970ULL - 1601ULL)
17 #define leap_years_1601_to_1970 (years_1601_to_1970 / 4ULL - 3ULL)
18 #define intervals_1601_to_1970 (years_1601_to_1970 * intervals_per_year \
19                                 + leap_years_1601_to_1970 * intervals_per_day)
20
21 static inline u64
22 unix_timestamp_to_wim(time_t t)
23 {
24         return (u64)intervals_1601_to_1970 + t * intervals_per_second;
25 }
26
27 /* Converts a timestamp as used in the WIM file to a UNIX timestamp as used in
28  * the time() function. */
29 static inline time_t
30 wim_timestamp_to_unix(u64 timestamp)
31 {
32         return (timestamp - intervals_1601_to_1970) / intervals_per_second;
33 }
34
35 static inline u64
36 timeval_to_wim_timestamp(const struct timeval tv)
37 {
38         return intervals_1601_to_1970
39                + (u64)tv.tv_sec * intervals_per_second
40                + (u64)tv.tv_usec * intervals_per_microsecond;
41 }
42
43 static inline struct timeval
44 wim_timestamp_to_timeval(u64 timestamp)
45 {
46         struct timeval tv;
47         tv.tv_sec = (timestamp - intervals_1601_to_1970) / intervals_per_second;
48         tv.tv_usec = ((timestamp - intervals_1601_to_1970) /
49                         intervals_per_microsecond) % 1000000;
50         return tv;
51 }
52
53 static inline u64
54 timespec_to_wim_timestamp(const struct timespec ts)
55 {
56         return intervals_1601_to_1970
57                + (u64)ts.tv_sec * intervals_per_second
58                + (u64)ts.tv_nsec / nanoseconds_per_interval;
59 }
60
61 static inline struct timespec
62 wim_timestamp_to_timespec(u64 timestamp)
63 {
64         struct timespec ts;
65         ts.tv_sec = (timestamp - intervals_1601_to_1970) / intervals_per_second;
66         ts.tv_nsec = ((timestamp - intervals_1601_to_1970) % intervals_per_second) *
67                         nanoseconds_per_interval;
68         return ts;
69 }
70
71 extern u64
72 get_wim_timestamp(void);
73
74 extern void
75 wim_timestamp_to_str(u64 timestamp, tchar *buf, size_t len);
76
77 #endif