From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id 7EC833971835 for ; Fri, 27 Nov 2020 09:18:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7EC833971835 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=derodat@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 1D82E116406; Fri, 27 Nov 2020 04:18:01 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Ey1pMCmxIwAN; Fri, 27 Nov 2020 04:18:01 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 0B0551173E0; Fri, 27 Nov 2020 04:18:01 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4862) id 09B48114; Fri, 27 Nov 2020 04:18:01 -0500 (EST) Date: Fri, 27 Nov 2020 04:18:01 -0500 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Arnaud Charlet Subject: [Ada] To_GM_Time returning invalid value for Invalid_Time Message-ID: <20201127091801.GA63272@adacore.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="45Z9DzgjV8m4Oswq" Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Nov 2020 09:18:08 -0000 --45Z9DzgjV8m4Oswq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. --45Z9DzgjV8m4Oswq Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" 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; --45Z9DzgjV8m4Oswq--