* diagnostic.c (announce_function): Move to toplev.c.
[official-gcc.git] / gcc / ada / g-catiio.adb
blobe3f45f815febef73d14e35794eaa831a3d45840b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . C A L E N D A R . T I M E _ I O --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2001 Ada Core Technologies, Inc. --
10 -- --
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. --
14 -- --
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. --
25 -- --
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. --
32 -- --
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). --
35 -- --
36 ------------------------------------------------------------------------------
38 with Ada.Calendar; use Ada.Calendar;
39 with Ada.Characters.Handling;
40 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
41 with Ada.Text_IO;
43 package body GNAT.Calendar.Time_IO is
45 type Month_Name is
46 (January,
47 Febuary,
48 March,
49 April,
50 May,
51 June,
52 July,
53 August,
54 September,
55 October,
56 November,
57 December);
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.
75 function Image
76 (N : Long_Integer;
77 Padding : Padding_Mode := Zero;
78 Length : Natural := 0)
79 return String;
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
82 -- occurs.
84 function Image
85 (N : Integer;
86 Padding : Padding_Mode := Zero;
87 Length : Natural := 0)
88 return String;
89 -- As above with N provided in Integer format.
91 -----------
92 -- Am_Pm --
93 -----------
95 function Am_Pm (H : Natural) return String is
96 begin
97 if H = 0 or else H > 12 then
98 return "PM";
99 else
100 return "AM";
101 end if;
102 end Am_Pm;
104 -------------
105 -- Hour_12 --
106 -------------
108 function Hour_12 (H : Natural) return Positive is
109 begin
110 if H = 0 then
111 return 12;
112 elsif H <= 12 then
113 return H;
114 else -- H > 12
115 return H - 12;
116 end if;
117 end Hour_12;
119 -----------
120 -- Image --
121 -----------
123 function Image
124 (Str : String;
125 Length : Natural := 0)
126 return String
128 use Ada.Characters.Handling;
129 Local : String := To_Upper (Str (1)) & To_Lower (Str (2 .. Str'Last));
131 begin
132 if Length = 0 then
133 return Local;
134 else
135 return Local (1 .. Length);
136 end if;
137 end Image;
139 -----------
140 -- Image --
141 -----------
143 function Image
144 (N : Integer;
145 Padding : Padding_Mode := Zero;
146 Length : Natural := 0)
147 return String
149 begin
150 return Image (Long_Integer (N), Padding, Length);
151 end Image;
153 function Image
154 (N : Long_Integer;
155 Padding : Padding_Mode := Zero;
156 Length : Natural := 0)
157 return String
159 function Pad_Char return String;
161 function Pad_Char return String is
162 begin
163 case Padding is
164 when None => return "";
165 when Zero => return "00";
166 when Space => return " ";
167 end case;
168 end Pad_Char;
170 NI : constant String := Long_Integer'Image (N);
171 NIP : constant String := Pad_Char & NI (2 .. NI'Last);
173 -- Start of processing for Image
175 begin
176 if Length = 0 or else Padding = None then
177 return NI (2 .. NI'Last);
179 else
180 return NIP (NIP'Last - Length + 1 .. NIP'Last);
181 end if;
182 end Image;
184 -----------
185 -- Image --
186 -----------
188 function Image
189 (Date : Ada.Calendar.Time;
190 Picture : Picture_String)
191 return String
193 Padding : Padding_Mode := Zero;
194 -- Padding is set for one directive
196 Result : Unbounded_String;
198 Year : Year_Number;
199 Month : Month_Number;
200 Day : Day_Number;
201 Hour : Hour_Number;
202 Minute : Minute_Number;
203 Second : Second_Number;
204 Sub_Second : Second_Duration;
206 P : Positive := Picture'First;
208 begin
209 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
211 loop
212 -- A directive has the following format "%[-_]."
214 if Picture (P) = '%' then
216 Padding := Zero;
218 if P = Picture'Last then
219 raise Picture_Error;
220 end if;
222 -- Check for GNU extension to change the padding
224 if Picture (P + 1) = '-' then
225 Padding := None;
226 P := P + 1;
227 elsif Picture (P + 1) = '_' then
228 Padding := Space;
229 P := P + 1;
230 end if;
232 if P = Picture'Last then
233 raise Picture_Error;
234 end if;
236 case Picture (P + 1) is
238 -- Literal %
240 when '%' =>
241 Result := Result & '%';
243 -- A newline
245 when 'n' =>
246 Result := Result & ASCII.LF;
248 -- A horizontal tab
250 when 't' =>
251 Result := Result & ASCII.HT;
253 -- Hour (00..23)
255 when 'H' =>
256 Result := Result & Image (Hour, Padding, 2);
258 -- Hour (01..12)
260 when 'I' =>
261 Result := Result & Image (Hour_12 (Hour), Padding, 2);
263 -- Hour ( 0..23)
265 when 'k' =>
266 Result := Result & Image (Hour, Space, 2);
268 -- Hour ( 1..12)
270 when 'l' =>
271 Result := Result & Image (Hour_12 (Hour), Space, 2);
273 -- Minute (00..59)
275 when 'M' =>
276 Result := Result & Image (Minute, Padding, 2);
278 -- AM/PM
280 when 'p' =>
281 Result := Result & Am_Pm (Hour);
283 -- Time, 12-hour (hh:mm:ss [AP]M)
285 when 'r' =>
286 Result := Result &
287 Image (Hour_12 (Hour), Padding, Length => 2) & ':' &
288 Image (Minute, Padding, Length => 2) & ':' &
289 Image (Second, Padding, Length => 2) & ' ' &
290 Am_Pm (Hour);
292 -- Seconds since 1970-01-01 00:00:00 UTC
293 -- (a nonstandard extension)
295 when 's' =>
296 declare
297 Sec : constant Long_Integer :=
298 Long_Integer
299 ((Julian_Day (Year, Month, Day) -
300 Julian_Day (1970, 1, 1)) * 86_400 +
301 Hour * 3_600 + Minute * 60 + Second);
303 begin
304 Result := Result & Image (Sec, None);
305 end;
307 -- Second (00..59)
309 when 'S' =>
310 Result := Result & Image (Second, Padding, Length => 2);
312 -- Time, 24-hour (hh:mm:ss)
314 when 'T' =>
315 Result := Result &
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)
322 when 'a' =>
323 Result := Result &
324 Image (Day_Name'Image (Day_Of_Week (Date)), 3);
326 -- Locale's full weekday name, variable length
327 -- (Sunday..Saturday)
329 when 'A' =>
330 Result := Result &
331 Image (Day_Name'Image (Day_Of_Week (Date)));
333 -- Locale's abbreviated month name (Jan..Dec)
335 when 'b' | 'h' =>
336 Result := Result &
337 Image (Month_Name'Image (Month_Name'Val (Month - 1)), 3);
339 -- Locale's full month name, variable length
340 -- (January..December)
342 when 'B' =>
343 Result := Result &
344 Image (Month_Name'Image (Month_Name'Val (Month - 1)));
346 -- Locale's date and time (Sat Nov 04 12:02:33 EST 1989)
348 when 'c' =>
349 case Padding is
350 when Zero =>
351 Result := Result & Image (Date, "%a %b %d %T %Y");
352 when Space =>
353 Result := Result & Image (Date, "%a %b %_d %_T %Y");
354 when None =>
355 Result := Result & Image (Date, "%a %b %-d %-T %Y");
356 end case;
358 -- Day of month (01..31)
360 when 'd' =>
361 Result := Result & Image (Day, Padding, 2);
363 -- Date (mm/dd/yy)
365 when 'D' | 'x' =>
366 Result := Result &
367 Image (Month, Padding, 2) & '/' &
368 Image (Day, Padding, 2) & '/' &
369 Image (Year, Padding, 2);
371 -- Day of year (001..366)
373 when 'j' =>
374 Result := Result & Image (Day_In_Year (Date), Padding, 3);
376 -- Month (01..12)
378 when 'm' =>
379 Result := Result & Image (Month, Padding, 2);
381 -- Week number of year with Sunday as first day of week
382 -- (00..53)
384 when 'U' =>
385 declare
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;
392 begin
393 Result := Result & Image (Week, Padding, 2);
394 end;
396 -- Day of week (0..6) with 0 corresponding to Sunday
398 when 'w' =>
399 declare
400 DOW : Natural range 0 .. 6;
402 begin
403 if Day_Of_Week (Date) = Sunday then
404 DOW := 0;
405 else
406 DOW := Day_Name'Pos (Day_Of_Week (Date));
407 end if;
409 Result := Result & Image (DOW, Length => 1);
410 end;
412 -- Week number of year with Monday as first day of week
413 -- (00..53)
415 when 'W' =>
416 Result := Result & Image (Week_In_Year (Date), Padding, 2);
418 -- Last two digits of year (00..99)
420 when 'y' =>
421 declare
422 Y : constant Natural := Year - (Year / 100) * 100;
424 begin
425 Result := Result & Image (Y, Padding, 2);
426 end;
428 -- Year (1970...)
430 when 'Y' =>
431 Result := Result & Image (Year, None, 4);
433 when others =>
434 raise Picture_Error;
435 end case;
437 P := P + 2;
439 else
440 Result := Result & Picture (P);
441 P := P + 1;
442 end if;
444 exit when P > Picture'Last;
446 end loop;
448 return To_String (Result);
449 end Image;
451 --------------
452 -- Put_Time --
453 --------------
455 procedure Put_Time
456 (Date : Ada.Calendar.Time;
457 Picture : Picture_String)
459 begin
460 Ada.Text_IO.Put (Image (Date, Picture));
461 end Put_Time;
463 end GNAT.Calendar.Time_IO;