1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . C A L E N D A R . T I M E _ I O --
10 -- Copyright (C) 1999-2001 Ada Core Technologies, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
25 -- MA 02111-1307, USA. --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
34 -- GNAT was originally developed by the GNAT team at New York University. --
35 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
37 ------------------------------------------------------------------------------
39 with Ada.Calendar; use Ada.Calendar;
40 with Ada.Characters.Handling;
41 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
44 package body GNAT.Calendar.Time_IO is
60 type Padding_Mode is (None, Zero, Space);
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 function Am_Pm (H : Natural) return String;
67 -- return AM or PM depending on the hour H
69 function Hour_12 (H : Natural) return Positive;
70 -- Convert a 1-24h format to a 0-12 hour format.
72 function Image (Str : String; Length : Natural := 0) return String;
73 -- Return Str capitalized and cut to length number of characters. If
74 -- length is set to 0 it does not cut it.
78 Padding : Padding_Mode := Zero;
79 Length : Natural := 0)
81 -- Return image of N. This number is eventually padded with zeros or
82 -- spaces depending of the length required. If length is 0 then no padding
87 Padding : Padding_Mode := Zero;
88 Length : Natural := 0)
90 -- As above with N provided in Integer format.
96 function Am_Pm (H : Natural) return String is
98 if H = 0 or else H > 12 then
109 function Hour_12 (H : Natural) return Positive is
126 Length : Natural := 0)
129 use Ada.Characters.Handling;
130 Local : String := To_Upper (Str (1)) & To_Lower (Str (2 .. Str'Last));
136 return Local (1 .. Length);
146 Padding : Padding_Mode := Zero;
147 Length : Natural := 0)
151 return Image (Long_Integer (N), Padding, Length);
156 Padding : Padding_Mode := Zero;
157 Length : Natural := 0)
160 function Pad_Char return String;
162 function Pad_Char return String is
165 when None => return "";
166 when Zero => return "00";
167 when Space => return " ";
171 NI : constant String := Long_Integer'Image (N);
172 NIP : constant String := Pad_Char & NI (2 .. NI'Last);
174 -- Start of processing for Image
177 if Length = 0 or else Padding = None then
178 return NI (2 .. NI'Last);
181 return NIP (NIP'Last - Length + 1 .. NIP'Last);
190 (Date : Ada.Calendar.Time;
191 Picture : Picture_String)
194 Padding : Padding_Mode := Zero;
195 -- Padding is set for one directive
197 Result : Unbounded_String;
200 Month : Month_Number;
203 Minute : Minute_Number;
204 Second : Second_Number;
205 Sub_Second : Second_Duration;
207 P : Positive := Picture'First;
210 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
213 -- A directive has the following format "%[-_]."
215 if Picture (P) = '%' then
219 if P = Picture'Last then
223 -- Check for GNU extension to change the padding
225 if Picture (P + 1) = '-' then
228 elsif Picture (P + 1) = '_' then
233 if P = Picture'Last then
237 case Picture (P + 1) is
242 Result := Result & '%';
247 Result := Result & ASCII.LF;
252 Result := Result & ASCII.HT;
257 Result := Result & Image (Hour, Padding, 2);
262 Result := Result & Image (Hour_12 (Hour), Padding, 2);
267 Result := Result & Image (Hour, Space, 2);
272 Result := Result & Image (Hour_12 (Hour), Space, 2);
277 Result := Result & Image (Minute, Padding, 2);
282 Result := Result & Am_Pm (Hour);
284 -- Time, 12-hour (hh:mm:ss [AP]M)
288 Image (Hour_12 (Hour), Padding, Length => 2) & ':' &
289 Image (Minute, Padding, Length => 2) & ':' &
290 Image (Second, Padding, Length => 2) & ' ' &
293 -- Seconds since 1970-01-01 00:00:00 UTC
294 -- (a nonstandard extension)
298 Sec : constant Long_Integer :=
300 ((Julian_Day (Year, Month, Day) -
301 Julian_Day (1970, 1, 1)) * 86_400 +
302 Hour * 3_600 + Minute * 60 + Second);
305 Result := Result & Image (Sec, None);
311 Result := Result & Image (Second, Padding, Length => 2);
313 -- Time, 24-hour (hh:mm:ss)
317 Image (Hour, Padding, Length => 2) & ':' &
318 Image (Minute, Padding, Length => 2) & ':' &
319 Image (Second, Padding, Length => 2);
321 -- Locale's abbreviated weekday name (Sun..Sat)
325 Image (Day_Name'Image (Day_Of_Week (Date)), 3);
327 -- Locale's full weekday name, variable length
328 -- (Sunday..Saturday)
332 Image (Day_Name'Image (Day_Of_Week (Date)));
334 -- Locale's abbreviated month name (Jan..Dec)
338 Image (Month_Name'Image (Month_Name'Val (Month - 1)), 3);
340 -- Locale's full month name, variable length
341 -- (January..December)
345 Image (Month_Name'Image (Month_Name'Val (Month - 1)));
347 -- Locale's date and time (Sat Nov 04 12:02:33 EST 1989)
352 Result := Result & Image (Date, "%a %b %d %T %Y");
354 Result := Result & Image (Date, "%a %b %_d %_T %Y");
356 Result := Result & Image (Date, "%a %b %-d %-T %Y");
359 -- Day of month (01..31)
362 Result := Result & Image (Day, Padding, 2);
368 Image (Month, Padding, 2) & '/' &
369 Image (Day, Padding, 2) & '/' &
370 Image (Year, Padding, 2);
372 -- Day of year (001..366)
375 Result := Result & Image (Day_In_Year (Date), Padding, 3);
380 Result := Result & Image (Month, Padding, 2);
382 -- Week number of year with Sunday as first day of week
387 Offset : constant Natural :=
388 (Julian_Day (Year, 1, 1) + 1) mod 7;
390 Week : constant Natural :=
391 1 + ((Day_In_Year (Date) - 1) + Offset) / 7;
394 Result := Result & Image (Week, Padding, 2);
397 -- Day of week (0..6) with 0 corresponding to Sunday
401 DOW : Natural range 0 .. 6;
404 if Day_Of_Week (Date) = Sunday then
407 DOW := Day_Name'Pos (Day_Of_Week (Date));
410 Result := Result & Image (DOW, Length => 1);
413 -- Week number of year with Monday as first day of week
417 Result := Result & Image (Week_In_Year (Date), Padding, 2);
419 -- Last two digits of year (00..99)
423 Y : constant Natural := Year - (Year / 100) * 100;
426 Result := Result & Image (Y, Padding, 2);
432 Result := Result & Image (Year, None, 4);
441 Result := Result & Picture (P);
445 exit when P > Picture'Last;
449 return To_String (Result);
457 (Date : Ada.Calendar.Time;
458 Picture : Picture_String)
461 Ada.Text_IO.Put (Image (Date, Picture));
464 end GNAT.Calendar.Time_IO;