X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftimestamp.h;h=91006fc0ac849f8f3274822714551f6a392ecb4c;hb=cf373e59a7f6ff7d1fd007c1f22defe508aa67d4;hp=6f08fc2e03fe641d993eabbedb684dee442212df;hpb=14c65f15f708c27dc434db1f0d112fad2a0b007c;p=wimlib diff --git a/src/timestamp.h b/src/timestamp.h index 6f08fc2e..91006fc0 100644 --- a/src/timestamp.h +++ b/src/timestamp.h @@ -3,6 +3,8 @@ #include "util.h" #include +#include +#include #define intervals_per_second (1000000000ULL / 100ULL) #define intervals_per_microsecond (10) @@ -16,41 +18,60 @@ #define intervals_1601_to_1970 (years_1601_to_1970 * intervals_per_year \ + leap_years_1601_to_1970 * intervals_per_day) -static inline u64 unix_timestamp_to_wim(time_t t) +static inline u64 +unix_timestamp_to_wim(time_t t) { return (u64)intervals_1601_to_1970 + t * intervals_per_second; } - /* Converts a timestamp as used in the WIM file to a UNIX timestamp as used in * the time() function. */ -static inline time_t wim_timestamp_to_unix(u64 timestamp) +static inline time_t +wim_timestamp_to_unix(u64 timestamp) { return (timestamp - intervals_1601_to_1970) / intervals_per_second; } -static inline u64 timeval_to_wim_timestamp(const struct timeval *tv) +static inline u64 +timeval_to_wim_timestamp(const struct timeval tv) { return intervals_1601_to_1970 - + (u64)tv->tv_sec * intervals_per_second - + (u64)tv->tv_usec * intervals_per_microsecond; + + (u64)tv.tv_sec * intervals_per_second + + (u64)tv.tv_usec * intervals_per_microsecond; } -static inline void wim_timestamp_to_timeval(u64 timestamp, struct timeval *tv) +static inline struct timeval +wim_timestamp_to_timeval(u64 timestamp) { - tv->tv_sec = (timestamp - intervals_1601_to_1970) / intervals_per_second; - tv->tv_usec = ((timestamp - intervals_1601_to_1970) / + struct timeval tv; + tv.tv_sec = (timestamp - intervals_1601_to_1970) / intervals_per_second; + tv.tv_usec = ((timestamp - intervals_1601_to_1970) / intervals_per_microsecond) % 1000000; + return tv; } -static inline u64 timespec_to_wim_timestamp(const struct timespec *ts) +static inline u64 +timespec_to_wim_timestamp(const struct timespec ts) { return intervals_1601_to_1970 - + (u64)ts->tv_sec * intervals_per_second - + (u64)ts->tv_nsec / nanoseconds_per_interval; + + (u64)ts.tv_sec * intervals_per_second + + (u64)ts.tv_nsec / nanoseconds_per_interval; +} + +static inline struct timespec +wim_timestamp_to_timespec(u64 timestamp) +{ + struct timespec ts; + ts.tv_sec = (timestamp - intervals_1601_to_1970) / intervals_per_second; + ts.tv_nsec = ((timestamp - intervals_1601_to_1970) % intervals_per_second) * + nanoseconds_per_interval; + return ts; } +extern u64 +get_wim_timestamp(); -extern u64 get_wim_timestamp(); +extern void +wim_timestamp_to_str(u64 timestamp, tchar *buf, size_t len); #endif