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 --
11 -- Copyright (C) 1999-2001 Ada Core Technologies, Inc. --
13 -- This specification is derived from the Ada Reference Manual for use with --
14 -- GNAT. The copyright notice above, and the license provisions that follow --
15 -- apply solely to the contents of the part following the private keyword. --
17 -- GNAT is free software; you can redistribute it and/or modify it under --
18 -- terms of the GNU General Public License as published by the Free Soft- --
19 -- ware Foundation; either version 2, or (at your option) any later ver- --
20 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
21 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
22 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
23 -- for more details. You should have received a copy of the GNU General --
24 -- Public License distributed with GNAT; see file COPYING. If not, write --
25 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
26 -- MA 02111-1307, USA. --
28 -- As a special exception, if other files instantiate generics from this --
29 -- unit, or you link this unit with other files to produce an executable, --
30 -- this unit does not by itself cause the resulting executable to be --
31 -- covered by the GNU General Public License. This exception does not --
32 -- however invalidate any other reasons why the executable file might be --
33 -- covered by the GNU Public License. --
35 -- GNAT was originally developed by the GNAT team at New York University. --
36 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
38 ------------------------------------------------------------------------------
40 with Ada.Calendar; use Ada.Calendar;
41 with Ada.Characters.Handling;
42 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
45 package body GNAT.Calendar.Time_IO is
61 type Padding_Mode is (None, Zero, Space);
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Am_Pm (H : Natural) return String;
68 -- return AM or PM depending on the hour H
70 function Hour_12 (H : Natural) return Positive;
71 -- Convert a 1-24h format to a 0-12 hour format.
73 function Image (Str : String; Length : Natural := 0) return String;
74 -- Return Str capitalized and cut to length number of characters. If
75 -- length is set to 0 it does not cut it.
79 Padding : Padding_Mode := Zero;
80 Length : Natural := 0)
82 -- Return image of N. This number is eventually padded with zeros or
83 -- spaces depending of the length required. If length is 0 then no padding
88 Padding : Padding_Mode := Zero;
89 Length : Natural := 0)
91 -- As above with N provided in Integer format.
97 function Am_Pm (H : Natural) return String is
99 if H = 0 or else H > 12 then
110 function Hour_12 (H : Natural) return Positive is
127 Length : Natural := 0)
130 use Ada.Characters.Handling;
131 Local : String := To_Upper (Str (1)) & To_Lower (Str (2 .. Str'Last));
137 return Local (1 .. Length);
147 Padding : Padding_Mode := Zero;
148 Length : Natural := 0)
152 return Image (Long_Integer (N), Padding, Length);
157 Padding : Padding_Mode := Zero;
158 Length : Natural := 0)
161 function Pad_Char return String;
163 function Pad_Char return String is
166 when None => return "";
167 when Zero => return "00";
168 when Space => return " ";
172 NI : constant String := Long_Integer'Image (N);
173 NIP : constant String := Pad_Char & NI (2 .. NI'Last);
175 -- Start of processing for Image
178 if Length = 0 or else Padding = None then
179 return NI (2 .. NI'Last);
182 return NIP (NIP'Last - Length + 1 .. NIP'Last);
191 (Date : Ada.Calendar.Time;
192 Picture : Picture_String)
195 Padding : Padding_Mode := Zero;
196 -- Padding is set for one directive
198 Result : Unbounded_String;
201 Month : Month_Number;
204 Minute : Minute_Number;
205 Second : Second_Number;
206 Sub_Second : Second_Duration;
208 P : Positive := Picture'First;
211 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
214 -- A directive has the following format "%[-_]."
216 if Picture (P) = '%' then
220 if P = Picture'Last then
224 -- Check for GNU extension to change the padding
226 if Picture (P + 1) = '-' then
229 elsif Picture (P + 1) = '_' then
234 if P = Picture'Last then
238 case Picture (P + 1) is
243 Result := Result & '%';
248 Result := Result & ASCII.LF;
253 Result := Result & ASCII.HT;
258 Result := Result & Image (Hour, Padding, 2);
263 Result := Result & Image (Hour_12 (Hour), Padding, 2);
268 Result := Result & Image (Hour, Space, 2);
273 Result := Result & Image (Hour_12 (Hour), Space, 2);
278 Result := Result & Image (Minute, Padding, 2);
283 Result := Result & Am_Pm (Hour);
285 -- Time, 12-hour (hh:mm:ss [AP]M)
289 Image (Hour_12 (Hour), Padding, Length => 2) & ':' &
290 Image (Minute, Padding, Length => 2) & ':' &
291 Image (Second, Padding, Length => 2) & ' ' &
294 -- Seconds since 1970-01-01 00:00:00 UTC
295 -- (a nonstandard extension)
299 Sec : constant Long_Integer :=
301 ((Julian_Day (Year, Month, Day) -
302 Julian_Day (1970, 1, 1)) * 86_400 +
303 Hour * 3_600 + Minute * 60 + Second);
306 Result := Result & Image (Sec, None);
312 Result := Result & Image (Second, Padding, Length => 2);
314 -- Time, 24-hour (hh:mm:ss)
318 Image (Hour, Padding, Length => 2) & ':' &
319 Image (Minute, Padding, Length => 2) & ':' &
320 Image (Second, Padding, Length => 2);
322 -- Locale's abbreviated weekday name (Sun..Sat)
326 Image (Day_Name'Image (Day_Of_Week (Date)), 3);
328 -- Locale's full weekday name, variable length
329 -- (Sunday..Saturday)
333 Image (Day_Name'Image (Day_Of_Week (Date)));
335 -- Locale's abbreviated month name (Jan..Dec)
339 Image (Month_Name'Image (Month_Name'Val (Month - 1)), 3);
341 -- Locale's full month name, variable length
342 -- (January..December)
346 Image (Month_Name'Image (Month_Name'Val (Month - 1)));
348 -- Locale's date and time (Sat Nov 04 12:02:33 EST 1989)
353 Result := Result & Image (Date, "%a %b %d %T %Y");
355 Result := Result & Image (Date, "%a %b %_d %_T %Y");
357 Result := Result & Image (Date, "%a %b %-d %-T %Y");
360 -- Day of month (01..31)
363 Result := Result & Image (Day, Padding, 2);
369 Image (Month, Padding, 2) & '/' &
370 Image (Day, Padding, 2) & '/' &
371 Image (Year, Padding, 2);
373 -- Day of year (001..366)
376 Result := Result & Image (Day_In_Year (Date), Padding, 3);
381 Result := Result & Image (Month, Padding, 2);
383 -- Week number of year with Sunday as first day of week
388 Offset : constant Natural :=
389 (Julian_Day (Year, 1, 1) + 1) mod 7;
391 Week : constant Natural :=
392 1 + ((Day_In_Year (Date) - 1) + Offset) / 7;
395 Result := Result & Image (Week, Padding, 2);
398 -- Day of week (0..6) with 0 corresponding to Sunday
402 DOW : Natural range 0 .. 6;
405 if Day_Of_Week (Date) = Sunday then
408 DOW := Day_Name'Pos (Day_Of_Week (Date));
411 Result := Result & Image (DOW, Length => 1);
414 -- Week number of year with Monday as first day of week
418 Result := Result & Image (Week_In_Year (Date), Padding, 2);
420 -- Last two digits of year (00..99)
424 Y : constant Natural := Year - (Year / 100) * 100;
427 Result := Result & Image (Y, Padding, 2);
433 Result := Result & Image (Year, None, 4);
442 Result := Result & Picture (P);
446 exit when P > Picture'Last;
450 return To_String (Result);
458 (Date : Ada.Calendar.Time;
459 Picture : Picture_String)
462 Ada.Text_IO.Put (Image (Date, Picture));
465 end GNAT.Calendar.Time_IO;