diff options
author | sath <sath@yandex-team.com> | 2024-05-13 12:52:23 +0300 |
---|---|---|
committer | sath <sath@yandex-team.com> | 2024-05-13 13:02:13 +0300 |
commit | 7b31bb09a816431b42dc0a7d01398445ca700477 (patch) | |
tree | c48cacc78775095b641a028c29036104cb250fd4 /util/datetime | |
parent | 5b22fadb0f035a3b82c328e0ae710ad2b92f6eac (diff) | |
download | ydb-7b31bb09a816431b42dc0a7d01398445ca700477.tar.gz |
Add mtime/atime/ctime with nanoseconds to TFileStat.
3523ab3f5aade2bdf4c0efd5dd2defbe19f124ff
Diffstat (limited to 'util/datetime')
-rw-r--r-- | util/datetime/systime.cpp | 40 | ||||
-rw-r--r-- | util/datetime/systime.h | 4 |
2 files changed, 36 insertions, 8 deletions
diff --git a/util/datetime/systime.cpp b/util/datetime/systime.cpp index aa935b7cf5..cb12cfdcf2 100644 --- a/util/datetime/systime.cpp +++ b/util/datetime/systime.cpp @@ -5,15 +5,39 @@ #ifdef _win_ +namespace { + // Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 + constexpr ui64 NUMBER_OF_100_NANO_BETWEEN_1601_1970 = + ULL(116444736000000000); + constexpr ui64 NUMBER_OF_100_NANO_IN_SECOND = ULL(10000000); + + union TFTUnion { + ui64 FTScalar; + FILETIME FTStruct; + }; +} // namespace + void FileTimeToTimeval(const FILETIME* ft, timeval* tv) { - const i64 NANOINTERVAL = LL(116444736000000000); // Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 - union { - ui64 ft_scalar; - FILETIME ft_struct; - } nt_time; - nt_time.ft_struct = *ft; - tv->tv_sec = (long)((nt_time.ft_scalar - NANOINTERVAL) / LL(10000000)); - tv->tv_usec = (i32)((nt_time.ft_scalar / LL(10)) % LL(1000000)); + Y_ASSERT(ft); + Y_ASSERT(tv); + TFTUnion ntTime; + ntTime.FTStruct = *ft; + ntTime.FTScalar -= NUMBER_OF_100_NANO_BETWEEN_1601_1970; + tv->tv_sec = + static_cast<long>(ntTime.FTScalar / NUMBER_OF_100_NANO_IN_SECOND); + tv->tv_usec = static_cast<long>( + (ntTime.FTScalar % NUMBER_OF_100_NANO_IN_SECOND) / LL(10)); +} + +void FileTimeToTimespec(const FILETIME& ft, struct timespec* ts) { + Y_ASSERT(ts); + TFTUnion ntTime; + ntTime.FTStruct = ft; + ntTime.FTScalar -= NUMBER_OF_100_NANO_BETWEEN_1601_1970; + ts->tv_sec = + static_cast<time_t>(ntTime.FTScalar / NUMBER_OF_100_NANO_IN_SECOND); + ts->tv_nsec = static_cast<long>( + (ntTime.FTScalar % NUMBER_OF_100_NANO_IN_SECOND) * LL(100)); } int gettimeofday(timeval* tp, void*) { diff --git a/util/datetime/systime.h b/util/datetime/systime.h index 491d36e802..760c28d6e6 100644 --- a/util/datetime/systime.h +++ b/util/datetime/systime.h @@ -15,8 +15,12 @@ TString CTimeR(const time_t* timer); #include <util/system/winint.h> #include <winsock2.h> +// Convert FILETIME to timeval - seconds and microseconds. void FileTimeToTimeval(const FILETIME* ft, struct timeval* tv); +// Convert FILETIME to timespec - seconds and nanoseconds. +void FileTimeToTimespec(const FILETIME& ft, struct timespec* ts); + // obtains the current time, expressed as seconds and microseconds since 00:00 UTC, January 1, 1970 int gettimeofday(struct timeval* tp, void*); |