[Ada] Implement Time_IO.Image with Time_Zone

Message ID 20200706113855.GA135623@adacore.com
State New
Headers show
Series
  • [Ada] Implement Time_IO.Image with Time_Zone
Related show

Commit Message

Pierre-Marie de Rodat July 6, 2020, 11:38 a.m.
This patch adds a new function Image in GNAT.Time_IO that takes a
Time_Zone parameter rather than using local time.

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

gcc/ada/

	* libgnat/g-catiio.ads, libgnat/g-catiio.adb (Image): New
	function. It might seem like the local-time Image should be
	implemented in terms of the Time_Zone Image, but that would be
	far harder to implement, so we do it the other way around.

Patch

diff --git a/gcc/ada/libgnat/g-catiio.adb b/gcc/ada/libgnat/g-catiio.adb
--- a/gcc/ada/libgnat/g-catiio.adb
+++ b/gcc/ada/libgnat/g-catiio.adb
@@ -29,8 +29,6 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with Ada.Calendar;            use Ada.Calendar;
-with Ada.Calendar.Time_Zones;
 with Ada.Characters.Handling;
 with Ada.Strings.Unbounded;   use Ada.Strings.Unbounded;
 with Ada.Text_IO;
@@ -210,6 +208,27 @@  package body GNAT.Calendar.Time_IO is
    -----------
 
    function Image
+     (Date      : Ada.Calendar.Time;
+      Picture   : Picture_String;
+      Time_Zone : Time_Zones.Time_Offset) return String
+   is
+      --  We subtract off the local time zone, and add in the requested
+      --  Time_Zone, and then pass it on to the version of Image that uses
+      --  the local time zone.
+
+      use Time_Zones;
+      Local_TZ : constant Time_Offset := Local_Time_Offset (Date);
+      Minute_Offset : constant Integer := Integer (Time_Zone - Local_TZ);
+      Second_Offset : constant Integer := Minute_Offset * 60;
+   begin
+      return Image (Date + Duration (Second_Offset), Picture);
+   end Image;
+
+   -----------
+   -- Image --
+   -----------
+
+   function Image
      (Date    : Ada.Calendar.Time;
       Picture : Picture_String) return String
    is


diff --git a/gcc/ada/libgnat/g-catiio.ads b/gcc/ada/libgnat/g-catiio.ads
--- a/gcc/ada/libgnat/g-catiio.ads
+++ b/gcc/ada/libgnat/g-catiio.ads
@@ -32,6 +32,8 @@ 
 --  This package augments standard Ada.Text_IO with facilities for input
 --  and output of time values in standardized format.
 
+with Ada.Calendar.Time_Zones; use Ada.Calendar;
+
 package GNAT.Calendar.Time_IO is
 
    Picture_Error : exception;
@@ -115,6 +117,13 @@  package GNAT.Calendar.Time_IO is
    --  with format Picture. Raise Picture_Error if picture string is null or
    --  has an incorrect format.
 
+   function Image
+     (Date      : Ada.Calendar.Time;
+      Picture   : Picture_String;
+      Time_Zone : Time_Zones.Time_Offset) return String;
+   --  Same as previous Image, except it uses the specified time zone instead
+   --  of the local time zone.
+
    function Value (Date : String) return Ada.Calendar.Time;
    --  Parse the string Date, interpreted as a time representation in the
    --  current local time zone, and return the corresponding Time value. The