From patchwork Fri Dec 13 09:55:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1209054 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-515853-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="jKKGpRSf"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47Z5fK47wGz9sP6 for ; Fri, 13 Dec 2019 20:56:25 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=nOukkdV6aAg/MYVUR2FCDW+TUR3LC2xsSPRibmvetxVm7zeJNc ZuiVrJUE+WThPO/Y84HckHfFhrRvOw9p4F0qGoouGJ+l6sAIvF5ftgd+j6T91N7c iZcPjCJIK/NzAO6EBsKgL0ESJfuHMZ0fLk/frHbwryJE6rVn3xWWK91Hs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=hKpWPtlTi5C7+Ae5gvKaV4Pg31k=; b=jKKGpRSfRw2bCu9IOPsJ vzrznbFoOVpb6Myo7C3l1qz4RaDsMtEGp2WRWUT+DOx0xVcO7poPZz8Ds+6NFFyw jBt8Ek84e57ZZrByjZpaqjTjtP6fb4Lr8Wp/QzCs+mIKTmmQ9DamuNdOb5rjpofw 5IvwZyEPNVW5NZnM5Czzhd8= Received: (qmail 36936 invoked by alias); 13 Dec 2019 09:55:11 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 36816 invoked by uid 89); 13 Dec 2019 09:55:10 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 13 Dec 2019 09:55:08 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 44E50560DB; Fri, 13 Dec 2019 04:55:02 -0500 (EST) 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 vzGnN0Uh55uh; Fri, 13 Dec 2019 04:55:02 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id F03F4560D6; Fri, 13 Dec 2019 04:55:01 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4862) id EF4B4157; Fri, 13 Dec 2019 04:55:01 -0500 (EST) Date: Fri, 13 Dec 2019 04:55:01 -0500 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Arnaud Charlet Subject: [Ada] Fix support for > 24 hours image in Ada.Calendar.Formatting Message-ID: <20191213095501.GA14043@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes The Ada.Calendar.Formatting.Image function is supposed to support at least durations up to (excluding) 100 hours. Fixes the implementation so that it does and clarify in the spec the behavior when the duration exceeds 100 hours (raise Time_Error). Tested on x86_64-pc-linux-gnu, committed on trunk 2019-12-13 Arnaud Charlet gcc/ada/ * libgnat/a-calfor.ads, libgnat/a-calfor.adb (Split_Duration): New procedure. (Split, Image): Use Split_Duration. Update Image spec. --- gcc/ada/libgnat/a-calfor.adb +++ gcc/ada/libgnat/a-calfor.adb @@ -52,6 +52,15 @@ package body Ada.Calendar.Formatting is -- such as 1983-*1-j3 u5:n7:k9 which should be 1983-01-03 05:07:09. Raise -- Constraint_Error if there is a mismatch. + procedure Split_Duration + (Seconds : Duration; + Hour : out Natural; + Minute : out Minute_Number; + Second : out Second_Number; + Sub_Second : out Second_Duration); + -- Version of Split that allows durations < 100 hours. + -- Will raise Time_Error if Seconds >= 100 hours. + ---------------- -- Check_Char -- ---------------- @@ -140,7 +149,7 @@ package body Ada.Calendar.Formatting is Include_Time_Fraction : Boolean := False) return String is To_Char : constant array (0 .. 9) of Character := "0123456789"; - Hour : Hour_Number; + Hour : Natural; Minute : Minute_Number; Second : Second_Number; Sub_Second : Duration; @@ -155,7 +164,7 @@ package body Ada.Calendar.Formatting is Result : String := "-00:00:00.00"; begin - Split (abs (Elapsed_Time), Hour, Minute, Second, Sub_Second); + Split_Duration (abs Elapsed_Time, Hour, Minute, Second, Sub_Second); -- Hour processing, positions 2 and 3 @@ -361,6 +370,34 @@ package body Ada.Calendar.Formatting is Sub_Second; end Seconds_Of; + -------------------- + -- Split_Duration -- + -------------------- + + procedure Split_Duration + (Seconds : Duration; + Hour : out Natural; + Minute : out Minute_Number; + Second : out Second_Number; + Sub_Second : out Second_Duration) + is + Secs : Natural; + begin + -- Check that Seconds is below 100 hours + + if Seconds >= 3600.0 * 100.0 then + raise Time_Error; + end if; + + Secs := (if Seconds = 0.0 then 0 else Natural (Seconds - 0.5)); + + Sub_Second := Second_Duration (Seconds - Duration (Secs)); + Hour := Natural (Secs / 3_600); + Secs := Secs mod 3_600; + Minute := Minute_Number (Secs / 60); + Second := Second_Number (Secs mod 60); + end Split_Duration; + ----------- -- Split -- ----------- @@ -372,8 +409,7 @@ package body Ada.Calendar.Formatting is Second : out Second_Number; Sub_Second : out Second_Duration) is - Secs : Natural; - + Unchecked_Hour : Natural; begin -- Validity checks @@ -381,23 +417,13 @@ package body Ada.Calendar.Formatting is raise Constraint_Error; end if; - Secs := (if Seconds = 0.0 then 0 else Natural (Seconds - 0.5)); - - Sub_Second := Second_Duration (Seconds - Day_Duration (Secs)); - Hour := Hour_Number (Secs / 3_600); - Secs := Secs mod 3_600; - Minute := Minute_Number (Secs / 60); - Second := Second_Number (Secs mod 60); - - -- Validity checks + Split_Duration (Seconds, Unchecked_Hour, Minute, Second, Sub_Second); - if not Hour'Valid - or else not Minute'Valid - or else not Second'Valid - or else not Sub_Second'Valid - then + if Unchecked_Hour > Hour_Number'Last then raise Time_Error; end if; + + Hour := Unchecked_Hour; end Split; ----------- --- gcc/ada/libgnat/a-calfor.ads +++ gcc/ada/libgnat/a-calfor.ads @@ -204,7 +204,7 @@ package Ada.Calendar.Formatting is -- to the string as a point followed by a 2-digit value. If Elapsed_Time < -- 0.0, the result is Image (abs Elapsed_Time, Include_Time_Fraction) -- prefixed with a minus sign. If abs Elapsed_Time represents 100 hours or - -- more, the result is implementation-defined. + -- more, Time_Error is raised. function Value (Elapsed_Time : String) return Duration; -- Returns a Duration value for the image given as Elapsed_Time.