* tree-cfg.c (tree_find_edge_insert_loc): Handle naked RETURN_EXPR.
[official-gcc.git] / gcc / ada / g-catiio.adb
blob147601dcb70aa7d5d817d6b925d938eff4163136
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-2005 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, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, 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 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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 February,
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) return String;
79 -- Return image of N. This number is eventually padded with zeros or spaces
80 -- depending of the length required. If length is 0 then no padding occurs.
82 function Image
83 (N : Integer;
84 Padding : Padding_Mode := Zero;
85 Length : Natural := 0) return String;
86 -- As above with N provided in Integer format
88 -----------
89 -- Am_Pm --
90 -----------
92 function Am_Pm (H : Natural) return String is
93 begin
94 if H = 0 or else H > 12 then
95 return "PM";
96 else
97 return "AM";
98 end if;
99 end Am_Pm;
101 -------------
102 -- Hour_12 --
103 -------------
105 function Hour_12 (H : Natural) return Positive is
106 begin
107 if H = 0 then
108 return 12;
109 elsif H <= 12 then
110 return H;
111 else -- H > 12
112 return H - 12;
113 end if;
114 end Hour_12;
116 -----------
117 -- Image --
118 -----------
120 function Image
121 (Str : String;
122 Length : Natural := 0) return String
124 use Ada.Characters.Handling;
125 Local : constant String :=
126 To_Upper (Str (1)) & To_Lower (Str (2 .. Str'Last));
128 begin
129 if Length = 0 then
130 return Local;
131 else
132 return Local (1 .. Length);
133 end if;
134 end Image;
136 -----------
137 -- Image --
138 -----------
140 function Image
141 (N : Integer;
142 Padding : Padding_Mode := Zero;
143 Length : Natural := 0) return String
145 begin
146 return Image (Long_Integer (N), Padding, Length);
147 end Image;
149 function Image
150 (N : Long_Integer;
151 Padding : Padding_Mode := Zero;
152 Length : Natural := 0) return String
154 function Pad_Char return String;
156 --------------
157 -- Pad_Char --
158 --------------
160 function Pad_Char return String is
161 begin
162 case Padding is
163 when None => return "";
164 when Zero => return "00";
165 when Space => return " ";
166 end case;
167 end Pad_Char;
169 NI : constant String := Long_Integer'Image (N);
170 NIP : constant String := Pad_Char & NI (2 .. NI'Last);
172 -- Start of processing for Image
174 begin
175 if Length = 0 or else Padding = None then
176 return NI (2 .. NI'Last);
178 else
179 return NIP (NIP'Last - Length + 1 .. NIP'Last);
180 end if;
181 end Image;
183 -----------
184 -- Image --
185 -----------
187 function Image
188 (Date : Ada.Calendar.Time;
189 Picture : Picture_String) return String
191 Padding : Padding_Mode := Zero;
192 -- Padding is set for one directive
194 Result : Unbounded_String;
196 Year : Year_Number;
197 Month : Month_Number;
198 Day : Day_Number;
199 Hour : Hour_Number;
200 Minute : Minute_Number;
201 Second : Second_Number;
202 Sub_Second : Second_Duration;
204 P : Positive := Picture'First;
206 begin
207 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
209 loop
210 -- A directive has the following format "%[-_]."
212 if Picture (P) = '%' then
214 Padding := Zero;
216 if P = Picture'Last then
217 raise Picture_Error;
218 end if;
220 -- Check for GNU extension to change the padding
222 if Picture (P + 1) = '-' then
223 Padding := None;
224 P := P + 1;
225 elsif Picture (P + 1) = '_' then
226 Padding := Space;
227 P := P + 1;
228 end if;
230 if P = Picture'Last then
231 raise Picture_Error;
232 end if;
234 case Picture (P + 1) is
236 -- Literal %
238 when '%' =>
239 Result := Result & '%';
241 -- A newline
243 when 'n' =>
244 Result := Result & ASCII.LF;
246 -- A horizontal tab
248 when 't' =>
249 Result := Result & ASCII.HT;
251 -- Hour (00..23)
253 when 'H' =>
254 Result := Result & Image (Hour, Padding, 2);
256 -- Hour (01..12)
258 when 'I' =>
259 Result := Result & Image (Hour_12 (Hour), Padding, 2);
261 -- Hour ( 0..23)
263 when 'k' =>
264 Result := Result & Image (Hour, Space, 2);
266 -- Hour ( 1..12)
268 when 'l' =>
269 Result := Result & Image (Hour_12 (Hour), Space, 2);
271 -- Minute (00..59)
273 when 'M' =>
274 Result := Result & Image (Minute, Padding, 2);
276 -- AM/PM
278 when 'p' =>
279 Result := Result & Am_Pm (Hour);
281 -- Time, 12-hour (hh:mm:ss [AP]M)
283 when 'r' =>
284 Result := Result &
285 Image (Hour_12 (Hour), Padding, Length => 2) & ':' &
286 Image (Minute, Padding, Length => 2) & ':' &
287 Image (Second, Padding, Length => 2) & ' ' &
288 Am_Pm (Hour);
290 -- Seconds since 1970-01-01 00:00:00 UTC
291 -- (a nonstandard extension)
293 when 's' =>
294 declare
295 Sec : constant Long_Integer :=
296 Long_Integer
297 ((Julian_Day (Year, Month, Day) -
298 Julian_Day (1970, 1, 1)) * 86_400 +
299 Hour * 3_600 + Minute * 60 + Second);
301 begin
302 Result := Result & Image (Sec, None);
303 end;
305 -- Second (00..59)
307 when 'S' =>
308 Result := Result & Image (Second, Padding, Length => 2);
310 -- Milliseconds (3 digits)
311 -- Microseconds (6 digits)
312 -- Nanoseconds (9 digits)
314 when 'i' | 'e' | 'o' =>
315 declare
316 Sub_Sec : constant Long_Integer :=
317 Long_Integer (Sub_Second * 1_000_000_000);
319 Img1 : constant String := Sub_Sec'Img;
320 Img2 : constant String :=
321 "00000000" & Img1 (Img1'First + 1 .. Img1'Last);
322 Nanos : constant String :=
323 Img2 (Img2'Last - 8 .. Img2'Last);
325 begin
326 case Picture (P + 1) is
327 when 'i' =>
328 Result := Result &
329 Nanos (Nanos'First .. Nanos'First + 2);
331 when 'e' =>
332 Result := Result &
333 Nanos (Nanos'First .. Nanos'First + 5);
335 when 'o' =>
336 Result := Result & Nanos;
338 when others =>
339 null;
340 end case;
341 end;
343 -- Time, 24-hour (hh:mm:ss)
345 when 'T' =>
346 Result := Result &
347 Image (Hour, Padding, Length => 2) & ':' &
348 Image (Minute, Padding, Length => 2) & ':' &
349 Image (Second, Padding, Length => 2);
351 -- Locale's abbreviated weekday name (Sun..Sat)
353 when 'a' =>
354 Result := Result &
355 Image (Day_Name'Image (Day_Of_Week (Date)), 3);
357 -- Locale's full weekday name, variable length
358 -- (Sunday..Saturday)
360 when 'A' =>
361 Result := Result &
362 Image (Day_Name'Image (Day_Of_Week (Date)));
364 -- Locale's abbreviated month name (Jan..Dec)
366 when 'b' | 'h' =>
367 Result := Result &
368 Image (Month_Name'Image (Month_Name'Val (Month - 1)), 3);
370 -- Locale's full month name, variable length
371 -- (January..December)
373 when 'B' =>
374 Result := Result &
375 Image (Month_Name'Image (Month_Name'Val (Month - 1)));
377 -- Locale's date and time (Sat Nov 04 12:02:33 EST 1989)
379 when 'c' =>
380 case Padding is
381 when Zero =>
382 Result := Result & Image (Date, "%a %b %d %T %Y");
383 when Space =>
384 Result := Result & Image (Date, "%a %b %_d %_T %Y");
385 when None =>
386 Result := Result & Image (Date, "%a %b %-d %-T %Y");
387 end case;
389 -- Day of month (01..31)
391 when 'd' =>
392 Result := Result & Image (Day, Padding, 2);
394 -- Date (mm/dd/yy)
396 when 'D' | 'x' =>
397 Result := Result &
398 Image (Month, Padding, 2) & '/' &
399 Image (Day, Padding, 2) & '/' &
400 Image (Year, Padding, 2);
402 -- Day of year (001..366)
404 when 'j' =>
405 Result := Result & Image (Day_In_Year (Date), Padding, 3);
407 -- Month (01..12)
409 when 'm' =>
410 Result := Result & Image (Month, Padding, 2);
412 -- Week number of year with Sunday as first day of week
413 -- (00..53)
415 when 'U' =>
416 declare
417 Offset : constant Natural :=
418 (Julian_Day (Year, 1, 1) + 1) mod 7;
420 Week : constant Natural :=
421 1 + ((Day_In_Year (Date) - 1) + Offset) / 7;
423 begin
424 Result := Result & Image (Week, Padding, 2);
425 end;
427 -- Day of week (0..6) with 0 corresponding to Sunday
429 when 'w' =>
430 declare
431 DOW : Natural range 0 .. 6;
433 begin
434 if Day_Of_Week (Date) = Sunday then
435 DOW := 0;
436 else
437 DOW := Day_Name'Pos (Day_Of_Week (Date));
438 end if;
440 Result := Result & Image (DOW, Length => 1);
441 end;
443 -- Week number of year with Monday as first day of week
444 -- (00..53)
446 when 'W' =>
447 Result := Result & Image (Week_In_Year (Date), Padding, 2);
449 -- Last two digits of year (00..99)
451 when 'y' =>
452 declare
453 Y : constant Natural := Year - (Year / 100) * 100;
454 begin
455 Result := Result & Image (Y, Padding, 2);
456 end;
458 -- Year (1970...)
460 when 'Y' =>
461 Result := Result & Image (Year, None, 4);
463 when others =>
464 raise Picture_Error;
465 end case;
467 P := P + 2;
469 else
470 Result := Result & Picture (P);
471 P := P + 1;
472 end if;
474 exit when P > Picture'Last;
476 end loop;
478 return To_String (Result);
479 end Image;
481 --------------
482 -- Put_Time --
483 --------------
485 procedure Put_Time
486 (Date : Ada.Calendar.Time;
487 Picture : Picture_String)
489 begin
490 Ada.Text_IO.Put (Image (Date, Picture));
491 end Put_Time;
493 end GNAT.Calendar.Time_IO;