[flang-commits] [flang] [llvm] [flang-rt] Add implementation for date_and_time on Windows (PR #190174)
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Thu Apr 2 07:35:08 PDT 2026
================
@@ -399,6 +410,41 @@ template <typename TM = struct tm> struct GmtOffsetHelper {
};
};
+#ifdef _WIN32
+struct timeval {
+ std::int64_t tv_sec;
+ std::int64_t tv_usec;
+};
+
+// gettimeofday half-implementation for win32; ignore the timezone as we don't
+// use it anyway
+int gettimeofday(timeval *tv, void *) {
+ constexpr std::uint64_t epoch_offset{116444736000000000ull};
+
+ FILETIME ftime;
+ GetSystemTimePreciseAsFileTime(&ftime);
+
+ // Convert ftime to a real 64-bit integer
+ std::uint64_t time{
+ ULARGE_INTEGER{{ftime.dwHighDateTime, ftime.dwHighDateTime}}.QuadPart};
+ // Convert to Unix epoch time
+ time -= epoch_offset;
+
+ auto [sec, usec] = std::lldiv(time, 10000000L);
+ tv->tv_sec = sec;
+ tv->tv_usec = usec;
+ return 0;
+}
+
+// localtime_s on Windows does the same thing as localtime_r but swaps the
+// arguments
+struct tm *localtime_r(const time_t *timer, struct tm *buf) {
+ _localtime64_s(buf, timer);
----------------
tblah wrote:
nit: check the return code was zero, if not return nullptr.
https://github.com/llvm/llvm-project/pull/190174
More information about the flang-commits
mailing list