]> wimlib.net Git - wimlib/blob - src/timestamp.h
Test suite
[wimlib] / src / timestamp.h
1 #ifndef _WIMLIB_TIMESTAMP_H
2 #define _WIMLIB_TIMESTAMP_H
3
4 #include "util.h"
5 #include <time.h>
6
7 #define intervals_per_second (1000000000ULL / 100ULL)
8 #define days_per_year (365ULL)
9 #define seconds_per_day (3600ULL * 24ULL)
10 #define intervals_per_day (seconds_per_day * intervals_per_second)
11 #define intervals_per_year (intervals_per_day * days_per_year)
12 #define years_1601_to_1970 (1970ULL - 1601ULL)
13 #define leap_years_1601_to_1970 (years_1601_to_1970 / 4ULL - 3ULL)
14 #define intervals_1601_to_1970 (years_1601_to_1970 * intervals_per_year \
15                                 + leap_years_1601_to_1970 * intervals_per_day)
16
17 static inline u64 unix_timestamp_to_ms(time_t t)
18 {
19         return (u64)intervals_1601_to_1970 + t * intervals_per_second;
20 }
21 /* 
22  * Returns the number of 100-nanosecond intervals that have elapsed since
23  * 12:00 A.M., January 1, 1601 UTC.
24  */
25 static inline u64 get_timestamp()
26 {
27         return unix_timestamp_to_ms(time(NULL));
28 }
29
30 /* Converts a timestamp as used in the WIM file to a UNIX timestamp as used in
31  * the time() function. */
32 static inline time_t ms_timestamp_to_unix(u64 timestamp)
33 {
34         return (timestamp - intervals_1601_to_1970) / intervals_per_second;
35 }
36
37 #endif