These changes cause genapi.cc to use whichever of clock_gettime() or
    gettimeofday() are available.  This prevents compilation errors on
    systems where clock_gettime() is not available.

    gcc/cobol/ChangeLog:

            PR cobol/119975
            * genapi.cc (parser_intrinsic_call_0): Use get_time_64()
function.
            * genutil.cc (get_time_64): Definition created.
            * genutil.h (get_time_64): Declaration created.


>From 4c958c0c5160cd3b4162737613ba6c40574c4d0a Mon Sep 17 00:00:00 2001
From: Robert Dubner <rdub...@symas.com>
Date: Mon, 2 Jun 2025 15:55:20 -0400
Subject: [PATCH] cobol: [PR119975]

---
 gcc/cobol/genapi.cc  |  4 +++-
 gcc/cobol/genutil.cc | 23 +++++++++++++++++++++++
 gcc/cobol/genutil.h  |  3 +++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/gcc/cobol/genapi.cc b/gcc/cobol/genapi.cc
index 51ecf819aac..1202e9eb239 100644
--- a/gcc/cobol/genapi.cc
+++ b/gcc/cobol/genapi.cc
@@ -10567,7 +10567,9 @@ parser_intrinsic_call_0(cbl_field_t *tgt,
     {
     // Pass __gg__when_compiled() the time from right now.
     struct timespec tp;
-    clock_gettime(CLOCK_REALTIME, &tp); // time_t tv_sec; long tv_nsec
+    uint64_t now = get_time_64();
+    tp.tv_sec  = now / 1000000000;
+    tp.tv_nsec = now % 1000000000;
 
     store_location_stuff(function_name);
     gg_call(VOID,
diff --git a/gcc/cobol/genutil.cc b/gcc/cobol/genutil.cc
index d0aaf2b3215..e971043164c 100644
--- a/gcc/cobol/genutil.cc
+++ b/gcc/cobol/genutil.cc
@@ -2119,3 +2119,26 @@ qualified_data_location(cbl_refer_t &refer)
   return gg_add(member(refer.field->var_decl_node, "data"),
                 refer_offset(refer));
   }
+
+uint64_t
+get_time_64()
+{
+  // This code was unabashedly stolen from gcc/timevar.cc.
+  // It returns the Unix epoch with nine decimal places.
+
+  uint64_t retval = 0;
+
+#ifdef HAVE_CLOCK_GETTIME
+  struct timespec ts;
+  clock_gettime (CLOCK_REALTIME, &ts);
+  retval = ts.tv_sec * 1000000000 + ts.tv_nsec;
+  return retval;
+#endif
+#ifdef HAVE_GETTIMEOFDAY
+  struct timeval tv;
+  gettimeofday (&tv, NULL);
+  retval = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
+  return retval;
+#endif
+  return retval;
+}
\ No newline at end of file
diff --git a/gcc/cobol/genutil.h b/gcc/cobol/genutil.h
index 2f4bc36eace..43102d7cc54 100644
--- a/gcc/cobol/genutil.h
+++ b/gcc/cobol/genutil.h
@@ -155,4 +155,7 @@ void      build_array_of_fourplets( int ngroup,
                                     size_t N,
                                     cbl_refer_t *refers);
 void      get_depending_on_value_from_odo(tree retval, cbl_field_t *odo);
+uint64_t  get_time_64();
+
+
 #endif
-- 
2.34.1

Reply via email to