public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] To_GM_Time returning invalid value for Invalid_Time
@ 2020-11-27  9:18 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2020-11-27  9:18 UTC (permalink / raw)
  To: gcc-patches; +Cc: Arnaud Charlet

[-- Attachment #1: Type: text/plain, Size: 533 bytes --]

Some code currently relies on To_GM_Time returning some reasonable value
in this case rather than e.g. raising an exception and it appears that
Windows returns 1970-01-01 except with second set to -1 (triggering an
exception), while linux returns 1970-01-01 minus 1 second (so 1969-12-31
at 23:59:59). Since the Linux behavior is friendlier, special case the
code to that effect.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* libgnat/s-os_lib.adb (To_GM_Time): Return valid and consistent
	values for Invalid_Time.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 1145 bytes --]

diff --git a/gcc/ada/libgnat/s-os_lib.adb b/gcc/ada/libgnat/s-os_lib.adb
--- a/gcc/ada/libgnat/s-os_lib.adb
+++ b/gcc/ada/libgnat/s-os_lib.adb
@@ -1365,6 +1365,21 @@ package body System.OS_Lib is
       S  : Integer;
 
    begin
+      --  Special case Invalid_Time which is handled differently between
+      --  Windows and Linux: Linux will set to 1 second before 1970-01-01
+      --  while Windows will set the time to 1970-01-01 with Second set to -1,
+      --  which is not a valid value.
+
+      if Date = Invalid_Time then
+         Year   := 1969;
+         Month  := 12;
+         Day    := 31;
+         Hour   := 23;
+         Minute := 59;
+         Second := 59;
+         return;
+      end if;
+
       --  Use the global lock because To_GM_Time is not thread safe
 
       Locked_Processing : begin
@@ -1387,7 +1402,15 @@ package body System.OS_Lib is
 
       Year   := Y + 1900;
       Month  := Mo + 1;
-      Day    := D;
+
+      --  May happen if To_GM_Time fails
+
+      if D = 0 then
+         Day := 1;
+      else
+         Day := D;
+      end if;
+
       Hour   := H;
       Minute := Mn;
       Second := S;



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-11-27  9:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-27  9:18 [Ada] To_GM_Time returning invalid value for Invalid_Time Pierre-Marie de Rodat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).