[flang-commits] [flang] FDATE extension implementation: get date and time in ctime format (PR #71222)

David Truby via flang-commits flang-commits at lists.llvm.org
Fri Nov 10 07:01:07 PST 2023


================
@@ -13,8 +13,25 @@
 #include "tools.h"
 #include "flang/Runtime/descriptor.h"
 #include <cstdlib>
+#include <ctime>
 #include <limits>
 
+#ifdef _WIN32
+inline const char *ctime_alloc(
+    char *buffer, size_t bufsize, const time_t cur_time) {
+  int error = ctime_s(buffer, bufsize, &cur_time);
+  assert(error == 0 && "ctime_s returned an error");
+  return buffer;
+}
+#else
+inline const char *ctime_alloc(
+    char *buffer, size_t bufsize, const time_t cur_time) {
+  const char *res = ctime_r(&cur_time, buffer);
----------------
DavidTruby wrote:

> Can we not just use ctime_s everywhere? It's a standard C function so it should be available. 

Never mind, I just checked on my linux system and it doesn't exist. Very odd. I'm surprised MS implements this from C11 but not glibc. `ctime_r` should be fine on POSIX platforms. 

I don't think we support any platforms that are not-posix and not-win32 but since `ctime_s` is actually the standard function and we might want to future proof, can we swap the test macro round? i.e.
```
#ifdef _POSIX_C_SOURCE
// use ctime_r
#else
// use ctime_s
```

https://github.com/llvm/llvm-project/pull/71222


More information about the flang-commits mailing list