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 --
9 -- Copyright (C) 1999-2001 Ada Core Technologies, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
24 -- MA 02111-1307, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- GNAT was originally developed by the GNAT team at New York University. --
34 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
36 ------------------------------------------------------------------------------
38 with Ada
.Calendar
; use Ada
.Calendar
;
39 with Ada
.Characters
.Handling
;
40 with Ada
.Strings
.Unbounded
; use Ada
.Strings
.Unbounded
;
43 package body GNAT
.Calendar
.Time_IO
is
59 type Padding_Mode
is (None
, Zero
, Space
);
61 -----------------------
62 -- Local Subprograms --
63 -----------------------
65 function Am_Pm
(H
: Natural) return String;
66 -- return AM or PM depending on the hour H
68 function Hour_12
(H
: Natural) return Positive;
69 -- Convert a 1-24h format to a 0-12 hour format.
71 function Image
(Str
: String; Length
: Natural := 0) return String;
72 -- Return Str capitalized and cut to length number of characters. If
73 -- length is set to 0 it does not cut it.
77 Padding
: Padding_Mode
:= Zero
;
78 Length
: Natural := 0)
80 -- Return image of N. This number is eventually padded with zeros or
81 -- spaces depending of the length required. If length is 0 then no padding
86 Padding
: Padding_Mode
:= Zero
;
87 Length
: Natural := 0)
89 -- As above with N provided in Integer format.
95 function Am_Pm
(H
: Natural) return String is
97 if H
= 0 or else H
> 12 then
108 function Hour_12
(H
: Natural) return Positive is
125 Length
: Natural := 0)
128 use Ada
.Characters
.Handling
;
129 Local
: String := To_Upper
(Str
(1)) & To_Lower
(Str
(2 .. Str
'Last));
135 return Local
(1 .. Length
);
145 Padding
: Padding_Mode
:= Zero
;
146 Length
: Natural := 0)
150 return Image
(Long_Integer (N
), Padding
, Length
);
155 Padding
: Padding_Mode
:= Zero
;
156 Length
: Natural := 0)
159 function Pad_Char
return String;
161 function Pad_Char
return String is
164 when None
=> return "";
165 when Zero
=> return "00";
166 when Space
=> return " ";
170 NI
: constant String := Long_Integer'Image (N
);
171 NIP
: constant String := Pad_Char
& NI
(2 .. NI
'Last);
173 -- Start of processing for Image
176 if Length
= 0 or else Padding
= None
then
177 return NI
(2 .. NI
'Last);
180 return NIP
(NIP
'Last - Length
+ 1 .. NIP
'Last);
189 (Date
: Ada
.Calendar
.Time
;
190 Picture
: Picture_String
)
193 Padding
: Padding_Mode
:= Zero
;
194 -- Padding is set for one directive
196 Result
: Unbounded_String
;
199 Month
: Month_Number
;
202 Minute
: Minute_Number
;
203 Second
: Second_Number
;
204 Sub_Second
: Second_Duration
;
206 P
: Positive := Picture
'First;
209 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
212 -- A directive has the following format "%[-_]."
214 if Picture
(P
) = '%' then
218 if P
= Picture
'Last then
222 -- Check for GNU extension to change the padding
224 if Picture
(P
+ 1) = '-' then
227 elsif Picture
(P
+ 1) = '_' then
232 if P
= Picture
'Last then
236 case Picture
(P
+ 1) is
241 Result
:= Result
& '%';
246 Result
:= Result
& ASCII
.LF
;
251 Result
:= Result
& ASCII
.HT
;
256 Result
:= Result
& Image
(Hour
, Padding
, 2);
261 Result
:= Result
& Image
(Hour_12
(Hour
), Padding
, 2);
266 Result
:= Result
& Image
(Hour
, Space
, 2);
271 Result
:= Result
& Image
(Hour_12
(Hour
), Space
, 2);
276 Result
:= Result
& Image
(Minute
, Padding
, 2);
281 Result
:= Result
& Am_Pm
(Hour
);
283 -- Time, 12-hour (hh:mm:ss [AP]M)
287 Image
(Hour_12
(Hour
), Padding
, Length
=> 2) & ':' &
288 Image
(Minute
, Padding
, Length
=> 2) & ':' &
289 Image
(Second
, Padding
, Length
=> 2) & ' ' &
292 -- Seconds since 1970-01-01 00:00:00 UTC
293 -- (a nonstandard extension)
297 Sec
: constant Long_Integer :=
299 ((Julian_Day
(Year
, Month
, Day
) -
300 Julian_Day
(1970, 1, 1)) * 86_400
+
301 Hour
* 3_600
+ Minute
* 60 + Second
);
304 Result
:= Result
& Image
(Sec
, None
);
310 Result
:= Result
& Image
(Second
, Padding
, Length
=> 2);
312 -- Time, 24-hour (hh:mm:ss)
316 Image
(Hour
, Padding
, Length
=> 2) & ':' &
317 Image
(Minute
, Padding
, Length
=> 2) & ':' &
318 Image
(Second
, Padding
, Length
=> 2);
320 -- Locale's abbreviated weekday name (Sun..Sat)
324 Image
(Day_Name
'Image (Day_Of_Week
(Date
)), 3);
326 -- Locale's full weekday name, variable length
327 -- (Sunday..Saturday)
331 Image
(Day_Name
'Image (Day_Of_Week
(Date
)));
333 -- Locale's abbreviated month name (Jan..Dec)
337 Image
(Month_Name
'Image (Month_Name
'Val (Month
- 1)), 3);
339 -- Locale's full month name, variable length
340 -- (January..December)
344 Image
(Month_Name
'Image (Month_Name
'Val (Month
- 1)));
346 -- Locale's date and time (Sat Nov 04 12:02:33 EST 1989)
351 Result
:= Result
& Image
(Date
, "%a %b %d %T %Y");
353 Result
:= Result
& Image
(Date
, "%a %b %_d %_T %Y");
355 Result
:= Result
& Image
(Date
, "%a %b %-d %-T %Y");
358 -- Day of month (01..31)
361 Result
:= Result
& Image
(Day
, Padding
, 2);
367 Image
(Month
, Padding
, 2) & '/' &
368 Image
(Day
, Padding
, 2) & '/' &
369 Image
(Year
, Padding
, 2);
371 -- Day of year (001..366)
374 Result
:= Result
& Image
(Day_In_Year
(Date
), Padding
, 3);
379 Result
:= Result
& Image
(Month
, Padding
, 2);
381 -- Week number of year with Sunday as first day of week
386 Offset
: constant Natural :=
387 (Julian_Day
(Year
, 1, 1) + 1) mod 7;
389 Week
: constant Natural :=
390 1 + ((Day_In_Year
(Date
) - 1) + Offset
) / 7;
393 Result
:= Result
& Image
(Week
, Padding
, 2);
396 -- Day of week (0..6) with 0 corresponding to Sunday
400 DOW
: Natural range 0 .. 6;
403 if Day_Of_Week
(Date
) = Sunday
then
406 DOW
:= Day_Name
'Pos (Day_Of_Week
(Date
));
409 Result
:= Result
& Image
(DOW
, Length
=> 1);
412 -- Week number of year with Monday as first day of week
416 Result
:= Result
& Image
(Week_In_Year
(Date
), Padding
, 2);
418 -- Last two digits of year (00..99)
422 Y
: constant Natural := Year
- (Year
/ 100) * 100;
425 Result
:= Result
& Image
(Y
, Padding
, 2);
431 Result
:= Result
& Image
(Year
, None
, 4);
440 Result
:= Result
& Picture
(P
);
444 exit when P
> Picture
'Last;
448 return To_String
(Result
);
456 (Date
: Ada
.Calendar
.Time
;
457 Picture
: Picture_String
)
460 Ada
.Text_IO
.Put
(Image
(Date
, Picture
));
463 end GNAT
.Calendar
.Time_IO
;