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-2007, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Calendar; use Ada.Calendar;
35 with Ada.Characters.Handling;
36 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
39 package body GNAT.Calendar.Time_IO is
55 type Padding_Mode is (None, Zero, Space);
57 type Sec_Number is mod 2 ** 64;
58 -- Type used to compute the number of seconds since 01/01/1970. A 32 bit
59 -- number will cover only a period of 136 years. This means that for date
60 -- past 2106 the computation is not possible. A 64 bits number should be
61 -- enough for a very large period of time.
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 0, then no cut operation is performed.
79 Padding : Padding_Mode := Zero;
80 Length : Natural := 0) return String;
81 -- Return image of N. This number is eventually padded with zeros or spaces
82 -- depending of the length required. If length is 0 then no padding occurs.
86 Padding : Padding_Mode := Zero;
87 Length : Natural := 0) return String;
88 -- As above with N provided in Integer format
94 function Am_Pm (H : Natural) return String is
96 if H = 0 or else H > 12 then
107 function Hour_12 (H : Natural) return Positive is
124 Length : Natural := 0) return String
126 use Ada.Characters.Handling;
127 Local : constant String :=
128 To_Upper (Str (Str'First)) &
129 To_Lower (Str (Str'First + 1 .. Str'Last));
134 return Local (1 .. Length);
144 Padding : Padding_Mode := Zero;
145 Length : Natural := 0) return String
148 return Image (Sec_Number (N), Padding, Length);
153 Padding : Padding_Mode := Zero;
154 Length : Natural := 0) return String
156 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 := Sec_Number'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);
180 return NIP (NIP'Last - Length + 1 .. NIP'Last);
189 (Date : Ada.Calendar.Time;
190 Picture : Picture_String) return String
192 Padding : Padding_Mode := Zero;
193 -- Padding is set for one directive
195 Result : Unbounded_String;
198 Month : Month_Number;
201 Minute : Minute_Number;
202 Second : Second_Number;
203 Sub_Second : Second_Duration;
208 -- Get current time in split format
210 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
212 -- Null picture string is error
215 raise Picture_Error with "null picture string";
218 -- Loop through characters of picture string, building result
220 Result := Null_Unbounded_String;
222 while P <= Picture'Last loop
224 -- A directive has the following format "%[-_]."
226 if Picture (P) = '%' then
229 if P = Picture'Last then
230 raise Picture_Error with "picture string ends with '%";
233 -- Check for GNU extension to change the padding
235 if Picture (P + 1) = '-' then
239 elsif Picture (P + 1) = '_' then
244 if P = Picture'Last then
245 raise Picture_Error with "picture string ends with '- or '_";
248 case Picture (P + 1) is
253 Result := Result & '%';
258 Result := Result & ASCII.LF;
263 Result := Result & ASCII.HT;
268 Result := Result & Image (Hour, Padding, 2);
273 Result := Result & Image (Hour_12 (Hour), Padding, 2);
278 Result := Result & Image (Hour, Space, 2);
283 Result := Result & Image (Hour_12 (Hour), Space, 2);
288 Result := Result & Image (Minute, Padding, 2);
293 Result := Result & Am_Pm (Hour);
295 -- Time, 12-hour (hh:mm:ss [AP]M)
299 Image (Hour_12 (Hour), Padding, Length => 2) & ':' &
300 Image (Minute, Padding, Length => 2) & ':' &
301 Image (Second, Padding, Length => 2) & ' ' &
304 -- Seconds since 1970-01-01 00:00:00 UTC
305 -- (a nonstandard extension)
309 Sec : constant Sec_Number :=
310 Sec_Number (Julian_Day (Year, Month, Day)
311 - Julian_Day (1970, 1, 1)) * 86_400
312 + Sec_Number (Hour) * 3_600
313 + Sec_Number (Minute) * 60
314 + Sec_Number (Second);
317 Result := Result & Image (Sec, None);
323 Result := Result & Image (Second, Padding, Length => 2);
325 -- Milliseconds (3 digits)
326 -- Microseconds (6 digits)
327 -- Nanoseconds (9 digits)
329 when 'i' | 'e' | 'o' =>
331 Sub_Sec : constant Long_Integer :=
332 Long_Integer (Sub_Second * 1_000_000_000);
334 Img1 : constant String := Sub_Sec'Img;
335 Img2 : constant String :=
336 "00000000" & Img1 (Img1'First + 1 .. Img1'Last);
337 Nanos : constant String :=
338 Img2 (Img2'Last - 8 .. Img2'Last);
341 case Picture (P + 1) is
344 Nanos (Nanos'First .. Nanos'First + 2);
348 Nanos (Nanos'First .. Nanos'First + 5);
351 Result := Result & Nanos;
358 -- Time, 24-hour (hh:mm:ss)
362 Image (Hour, Padding, Length => 2) & ':' &
363 Image (Minute, Padding, Length => 2) & ':' &
364 Image (Second, Padding, Length => 2);
366 -- Locale's abbreviated weekday name (Sun..Sat)
370 Image (Day_Name'Image (Day_Of_Week (Date)), 3);
372 -- Locale's full weekday name, variable length
373 -- (Sunday..Saturday)
377 Image (Day_Name'Image (Day_Of_Week (Date)));
379 -- Locale's abbreviated month name (Jan..Dec)
383 Image (Month_Name'Image (Month_Name'Val (Month - 1)), 3);
385 -- Locale's full month name, variable length
386 -- (January..December).
390 Image (Month_Name'Image (Month_Name'Val (Month - 1)));
392 -- Locale's date and time (Sat Nov 04 12:02:33 EST 1989)
397 Result := Result & Image (Date, "%a %b %d %T %Y");
399 Result := Result & Image (Date, "%a %b %_d %_T %Y");
401 Result := Result & Image (Date, "%a %b %-d %-T %Y");
404 -- Day of month (01..31)
407 Result := Result & Image (Day, Padding, 2);
413 Image (Month, Padding, 2) & '/' &
414 Image (Day, Padding, 2) & '/' &
415 Image (Year, Padding, 2);
417 -- Day of year (001..366)
420 Result := Result & Image (Day_In_Year (Date), Padding, 3);
425 Result := Result & Image (Month, Padding, 2);
427 -- Week number of year with Sunday as first day of week
432 Offset : constant Natural :=
433 (Julian_Day (Year, 1, 1) + 1) mod 7;
435 Week : constant Natural :=
436 1 + ((Day_In_Year (Date) - 1) + Offset) / 7;
439 Result := Result & Image (Week, Padding, 2);
442 -- Day of week (0..6) with 0 corresponding to Sunday
446 DOW : Natural range 0 .. 6;
449 if Day_Of_Week (Date) = Sunday then
452 DOW := Day_Name'Pos (Day_Of_Week (Date));
455 Result := Result & Image (DOW, Length => 1);
458 -- Week number of year with Monday as first day of week
462 Result := Result & Image (Week_In_Year (Date), Padding, 2);
464 -- Last two digits of year (00..99)
468 Y : constant Natural := Year - (Year / 100) * 100;
470 Result := Result & Image (Y, Padding, 2);
476 Result := Result & Image (Year, None, 4);
479 raise Picture_Error with
480 "unknown format character in picture string";
484 -- Skip past % and format character
488 -- Character other than % is copied into the result
491 Result := Result & Picture (P);
496 return To_String (Result);
503 function Value (Date : String) return Ada.Calendar.Time is
504 D : String (1 .. 19);
505 D_Length : constant Natural := Date'Length;
508 Month : Month_Number;
511 Minute : Minute_Number;
512 Second : Second_Number;
513 Sub_Second : Second_Duration;
515 procedure Extract_Date
516 (Year : out Year_Number;
517 Month : out Month_Number;
518 Day : out Day_Number;
519 Y2K : Boolean := False);
520 -- Try and extract a date value from string D. Set Y2K to True to
521 -- account for the 20YY case. Raise Constraint_Error if the portion
522 -- of D corresponding to the date is not well formatted.
524 procedure Extract_Time
526 Hour : out Hour_Number;
527 Minute : out Minute_Number;
528 Second : out Second_Number;
529 Check_Space : Boolean := False);
530 -- Try and extract a time value from string D starting from position
531 -- Index. Set Check_Space to True to check whether the character at
532 -- Index - 1 is a space. Raise Constraint_Error if the portion of D
533 -- corresponding to the date is not well formatted.
539 procedure Extract_Date
540 (Year : out Year_Number;
541 Month : out Month_Number;
542 Day : out Day_Number;
543 Y2K : Boolean := False)
545 Delim_Index : Positive := 5;
552 if (D (Delim_Index) /= '-' or else D (Delim_Index + 3) /= '-')
554 (D (Delim_Index) /= '/' or else D (Delim_Index + 3) /= '/')
556 raise Constraint_Error;
560 Year := Year_Number'Value ("20" & D (1 .. 2));
561 Month := Month_Number'Value (D (4 .. 5));
562 Day := Day_Number'Value (D (7 .. 8));
564 Year := Year_Number'Value (D (1 .. 4));
565 Month := Month_Number'Value (D (6 .. 7));
566 Day := Day_Number'Value (D (9 .. 10));
574 procedure Extract_Time
576 Hour : out Hour_Number;
577 Minute : out Minute_Number;
578 Second : out Second_Number;
579 Check_Space : Boolean := False) is
582 if Check_Space and then D (Index - 1) /= ' ' then
583 raise Constraint_Error;
586 if D (Index + 2) /= ':' or else D (Index + 5) /= ':' then
587 raise Constraint_Error;
590 Hour := Hour_Number'Value (D (Index .. Index + 1));
591 Minute := Minute_Number'Value (D (Index + 3 .. Index + 4));
592 Second := Second_Number'Value (D (Index + 6 .. Index + 7));
595 -- Start of processing for Value
598 Split (Clock, Year, Month, Day, Hour, Minute, Second, Sub_Second);
604 and then D_Length /= 10
605 and then D_Length /= 17
606 and then D_Length /= 19
608 raise Constraint_Error;
611 -- After the correct length has been determined, it is safe to create
612 -- a local string copy in order to avoid String'First N arithmetic.
614 D (1 .. D_Length) := Date;
624 Extract_Time (1, Hour, Minute, Second);
626 Extract_Date (Year, Month, Day, True);
636 elsif D_Length = 10 then
637 Extract_Date (Year, Month, Day);
646 elsif D_Length = 17 then
647 Extract_Date (Year, Month, Day, True);
648 Extract_Time (10, Hour, Minute, Second, True);
652 -- yyyy*mm*dd hh:mm:ss
655 Extract_Date (Year, Month, Day);
656 Extract_Time (12, Hour, Minute, Second, True);
662 or else not Month'Valid
663 or else not Day'Valid
664 or else not Hour'Valid
665 or else not Minute'Valid
666 or else not Second'Valid
668 raise Constraint_Error;
671 return Time_Of (Year, Month, Day, Hour, Minute, Second, Sub_Second);
679 (Date : Ada.Calendar.Time;
680 Picture : Picture_String)
683 Ada.Text_IO.Put (Image (Date, Picture));
686 end GNAT.Calendar.Time_IO;