diff options
author | Max Kellermann <max@musicpd.org> | 2021-08-10 14:18:57 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2021-08-10 15:13:22 +0200 |
commit | 9fc3c6091036373497c2cab52708b059a65af8df (patch) | |
tree | 038293fab53fd6ef4f4293f51577f0c3b9c82086 | |
parent | 1976003e91170b53d3d22d506d862f46db162df5 (diff) |
time/FileTime: add FileTimeToChronoDuration()
-rw-r--r-- | src/time/FileTime.hxx | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/time/FileTime.hxx b/src/time/FileTime.hxx index a134433e3..4ac323331 100644 --- a/src/time/FileTime.hxx +++ b/src/time/FileTime.hxx @@ -53,6 +53,24 @@ ToInt64(FILETIME ft) noexcept return ToUint64(ft); } +/* "A file time is a 64-bit value that represents the number of + 100-nanosecond intervals" + https://docs.microsoft.com/en-us/windows/win32/sysinfo/file-times */ +using FileTimeResolution = std::ratio<1, 10000000>; + +using FileTimeDuration = std::chrono::duration<int_least64_t, + FileTimeResolution>; + +/** + * Calculate a std::chrono::duration specifying the duration of the + * FILETIME since its epoch (1601-01-01T00:00). + */ +constexpr auto +FileTimeToChronoDuration(FILETIME ft) noexcept +{ + return FileTimeDuration(ToInt64(ft)); +} + constexpr time_t FileTimeToTimeT(FILETIME ft) noexcept { @@ -69,7 +87,8 @@ FileTimeToChrono(FILETIME ft) noexcept constexpr std::chrono::seconds DeltaFileTimeS(FILETIME a, FILETIME b) noexcept { - return std::chrono::seconds((ToInt64(a) - ToInt64(b)) / 10000000); + return std::chrono::duration_cast<std::chrono::seconds> + (FileTimeToChronoDuration(a) - FileTimeToChronoDuration(b)); } #endif |