[Ada] Implement Time_IO.Image with Time_Zone

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.
This commit is contained in:
Bob Duff 2020-05-08 18:37:50 -04:00 committed by Pierre-Marie de Rodat
parent 58d94a32b4
commit b1e78e8577
2 changed files with 30 additions and 2 deletions

View File

@ -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;
@ -209,6 +207,27 @@ package body GNAT.Calendar.Time_IO is
-- Image --
-----------
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

View File

@ -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