1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . C A L E N D A R . F O R M A T T I N G --
9 -- Copyright (C) 2006-2009, Free Software Foundation, Inc. --
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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada.Calendar; use Ada.Calendar;
33 with Ada.Calendar.Time_Zones; use Ada.Calendar.Time_Zones;
35 package body Ada.Calendar.Formatting is
37 --------------------------
38 -- Implementation Notes --
39 --------------------------
41 -- All operations in this package are target and time representation
42 -- independent, thus only one source file is needed for multiple targets.
44 procedure Check_Char (S : String; C : Character; Index : Integer);
45 -- Subsidiary to the two versions of Value. Determine whether the
46 -- input string S has character C at position Index. Raise
47 -- Constraint_Error if there is a mismatch.
49 procedure Check_Digit (S : String; Index : Integer);
50 -- Subsidiary to the two versions of Value. Determine whether the
51 -- character of string S at position Index is a digit. This catches
52 -- invalid input such as 1983-*1-j3 u5:n7:k9 which should be
53 -- 1983-01-03 05:07:09. Raise Constraint_Error if there is a mismatch.
59 procedure Check_Char (S : String; C : Character; Index : Integer) is
61 if S (Index) /= C then
62 raise Constraint_Error;
70 procedure Check_Digit (S : String; Index : Integer) is
72 if S (Index) not in '0' .. '9' then
73 raise Constraint_Error;
83 Time_Zone : Time_Zones.Time_Offset := 0) return Day_Number
94 pragma Unreferenced (Y, Mo, H, Mi);
97 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
105 function Day_Of_Week (Date : Time) return Day_Name is
107 return Day_Name'Val (Formatting_Operations.Day_Of_Week (Date));
116 Time_Zone : Time_Zones.Time_Offset := 0) return Hour_Number
124 Ss : Second_Duration;
127 pragma Unreferenced (Y, Mo, D, Mi);
130 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
139 (Elapsed_Time : Duration;
140 Include_Time_Fraction : Boolean := False) return String
143 Minute : Minute_Number;
144 Second : Second_Number;
145 Sub_Second : Duration;
151 Result : String := "-00:00:00.00";
154 Split (abs (Elapsed_Time), Hour, Minute, Second, Sub_Second);
156 -- Determine the two slice bounds for the result string depending on
157 -- whether the input is negative and whether fractions are requested.
159 if Elapsed_Time < 0.0 then
165 if Include_Time_Fraction then
171 -- Prevent rounding when converting to natural
173 Sub_Second := Sub_Second * 100.0;
175 if Sub_Second > 0.0 then
176 Sub_Second := Sub_Second - 0.5;
179 SS_Nat := Natural (Sub_Second);
182 Hour_Str : constant String := Hour_Number'Image (Hour);
183 Minute_Str : constant String := Minute_Number'Image (Minute);
184 Second_Str : constant String := Second_Number'Image (Second);
185 SS_Str : constant String := Natural'Image (SS_Nat);
188 -- Hour processing, positions 2 and 3
191 Result (3) := Hour_Str (2);
193 Result (2) := Hour_Str (2);
194 Result (3) := Hour_Str (3);
197 -- Minute processing, positions 5 and 6
200 Result (6) := Minute_Str (2);
202 Result (5) := Minute_Str (2);
203 Result (6) := Minute_Str (3);
206 -- Second processing, positions 8 and 9
209 Result (9) := Second_Str (2);
211 Result (8) := Second_Str (2);
212 Result (9) := Second_Str (3);
215 -- Optional sub second processing, positions 11 and 12
217 if Include_Time_Fraction then
219 Result (12) := SS_Str (2);
221 Result (11) := SS_Str (2);
222 Result (12) := SS_Str (3);
226 return Result (Low .. High);
236 Include_Time_Fraction : Boolean := False;
237 Time_Zone : Time_Zones.Time_Offset := 0) return String
240 Month : Month_Number;
243 Minute : Minute_Number;
244 Second : Second_Number;
245 Sub_Second : Duration;
247 Leap_Second : Boolean;
249 Result : String := "0000-00-00 00:00:00.00";
252 Split (Date, Year, Month, Day,
253 Hour, Minute, Second, Sub_Second, Leap_Second, Time_Zone);
255 -- Prevent rounding when converting to natural
257 Sub_Second := Sub_Second * 100.0;
259 if Sub_Second > 0.0 then
260 Sub_Second := Sub_Second - 0.5;
263 SS_Nat := Natural (Sub_Second);
266 Year_Str : constant String := Year_Number'Image (Year);
267 Month_Str : constant String := Month_Number'Image (Month);
268 Day_Str : constant String := Day_Number'Image (Day);
269 Hour_Str : constant String := Hour_Number'Image (Hour);
270 Minute_Str : constant String := Minute_Number'Image (Minute);
271 Second_Str : constant String := Second_Number'Image (Second);
272 SS_Str : constant String := Natural'Image (SS_Nat);
275 -- Year processing, positions 1, 2, 3 and 4
277 Result (1) := Year_Str (2);
278 Result (2) := Year_Str (3);
279 Result (3) := Year_Str (4);
280 Result (4) := Year_Str (5);
282 -- Month processing, positions 6 and 7
285 Result (7) := Month_Str (2);
287 Result (6) := Month_Str (2);
288 Result (7) := Month_Str (3);
291 -- Day processing, positions 9 and 10
294 Result (10) := Day_Str (2);
296 Result (9) := Day_Str (2);
297 Result (10) := Day_Str (3);
300 -- Hour processing, positions 12 and 13
303 Result (13) := Hour_Str (2);
305 Result (12) := Hour_Str (2);
306 Result (13) := Hour_Str (3);
309 -- Minute processing, positions 15 and 16
312 Result (16) := Minute_Str (2);
314 Result (15) := Minute_Str (2);
315 Result (16) := Minute_Str (3);
318 -- Second processing, positions 18 and 19
321 Result (19) := Second_Str (2);
323 Result (18) := Second_Str (2);
324 Result (19) := Second_Str (3);
327 -- Optional sub second processing, positions 21 and 22
329 if Include_Time_Fraction then
331 Result (22) := SS_Str (2);
333 Result (21) := SS_Str (2);
334 Result (22) := SS_Str (3);
339 return Result (1 .. 19);
350 Time_Zone : Time_Zones.Time_Offset := 0) return Minute_Number
358 Ss : Second_Duration;
361 pragma Unreferenced (Y, Mo, D, H);
364 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
374 Time_Zone : Time_Zones.Time_Offset := 0) return Month_Number
382 Ss : Second_Duration;
385 pragma Unreferenced (Y, D, H, Mi);
388 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
396 function Second (Date : Time) return Second_Number is
403 Ss : Second_Duration;
406 pragma Unreferenced (Y, Mo, D, H, Mi);
409 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le);
419 Minute : Minute_Number;
420 Second : Second_Number := 0;
421 Sub_Second : Second_Duration := 0.0) return Day_Duration is
427 or else not Minute'Valid
428 or else not Second'Valid
429 or else not Sub_Second'Valid
431 raise Constraint_Error;
434 return Day_Duration (Hour * 3_600) +
435 Day_Duration (Minute * 60) +
436 Day_Duration (Second) +
445 (Seconds : Day_Duration;
446 Hour : out Hour_Number;
447 Minute : out Minute_Number;
448 Second : out Second_Number;
449 Sub_Second : out Second_Duration)
456 if not Seconds'Valid then
457 raise Constraint_Error;
460 if Seconds = 0.0 then
463 Secs := Natural (Seconds - 0.5);
466 Sub_Second := Second_Duration (Seconds - Day_Duration (Secs));
467 Hour := Hour_Number (Secs / 3_600);
468 Secs := Secs mod 3_600;
469 Minute := Minute_Number (Secs / 60);
470 Second := Second_Number (Secs mod 60);
475 or else not Minute'Valid
476 or else not Second'Valid
477 or else not Sub_Second'Valid
489 Year : out Year_Number;
490 Month : out Month_Number;
491 Day : out Day_Number;
492 Seconds : out Day_Duration;
493 Leap_Second : out Boolean;
494 Time_Zone : Time_Zones.Time_Offset := 0)
500 Tz : constant Long_Integer := Long_Integer (Time_Zone);
503 Formatting_Operations.Split
513 Leap_Sec => Leap_Second,
520 or else not Month'Valid
521 or else not Day'Valid
522 or else not Seconds'Valid
534 Year : out Year_Number;
535 Month : out Month_Number;
536 Day : out Day_Number;
537 Hour : out Hour_Number;
538 Minute : out Minute_Number;
539 Second : out Second_Number;
540 Sub_Second : out Second_Duration;
541 Time_Zone : Time_Zones.Time_Offset := 0)
545 Tz : constant Long_Integer := Long_Integer (Time_Zone);
548 Formatting_Operations.Split
557 Sub_Sec => Sub_Second,
565 or else not Month'Valid
566 or else not Day'Valid
567 or else not Hour'Valid
568 or else not Minute'Valid
569 or else not Second'Valid
570 or else not Sub_Second'Valid
582 Year : out Year_Number;
583 Month : out Month_Number;
584 Day : out Day_Number;
585 Hour : out Hour_Number;
586 Minute : out Minute_Number;
587 Second : out Second_Number;
588 Sub_Second : out Second_Duration;
589 Leap_Second : out Boolean;
590 Time_Zone : Time_Zones.Time_Offset := 0)
593 Tz : constant Long_Integer := Long_Integer (Time_Zone);
596 Formatting_Operations.Split
605 Sub_Sec => Sub_Second,
606 Leap_Sec => Leap_Second,
613 or else not Month'Valid
614 or else not Day'Valid
615 or else not Hour'Valid
616 or else not Minute'Valid
617 or else not Second'Valid
618 or else not Sub_Second'Valid
628 function Sub_Second (Date : Time) return Second_Duration is
635 Ss : Second_Duration;
638 pragma Unreferenced (Y, Mo, D, H, Mi);
641 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le);
651 Month : Month_Number;
653 Seconds : Day_Duration := 0.0;
654 Leap_Second : Boolean := False;
655 Time_Zone : Time_Zones.Time_Offset := 0) return Time
657 Adj_Year : Year_Number := Year;
658 Adj_Month : Month_Number := Month;
659 Adj_Day : Day_Number := Day;
661 H : constant Integer := 1;
662 M : constant Integer := 1;
663 Se : constant Integer := 1;
664 Ss : constant Duration := 0.1;
665 Tz : constant Long_Integer := Long_Integer (Time_Zone);
671 or else not Month'Valid
672 or else not Day'Valid
673 or else not Seconds'Valid
674 or else not Time_Zone'Valid
676 raise Constraint_Error;
679 -- A Seconds value of 86_400 denotes a new day. This case requires an
680 -- adjustment to the input values.
682 if Seconds = 86_400.0 then
683 if Day < Days_In_Month (Month)
684 or else (Is_Leap (Year)
692 Adj_Month := Month + 1;
695 Adj_Year := Year + 1;
701 Formatting_Operations.Time_Of
710 Leap_Sec => Leap_Second,
711 Use_Day_Secs => True,
722 Month : Month_Number;
725 Minute : Minute_Number;
726 Second : Second_Number;
727 Sub_Second : Second_Duration := 0.0;
728 Leap_Second : Boolean := False;
729 Time_Zone : Time_Zones.Time_Offset := 0) return Time
731 Dd : constant Day_Duration := Day_Duration'First;
732 Tz : constant Long_Integer := Long_Integer (Time_Zone);
738 or else not Month'Valid
739 or else not Day'Valid
740 or else not Hour'Valid
741 or else not Minute'Valid
742 or else not Second'Valid
743 or else not Sub_Second'Valid
744 or else not Time_Zone'Valid
746 raise Constraint_Error;
750 Formatting_Operations.Time_Of
758 Sub_Sec => Sub_Second,
759 Leap_Sec => Leap_Second,
760 Use_Day_Secs => False,
771 Time_Zone : Time_Zones.Time_Offset := 0) return Time
773 D : String (1 .. 22);
775 Month : Month_Number;
778 Minute : Minute_Number;
779 Second : Second_Number;
780 Sub_Second : Second_Duration := 0.0;
785 if not Time_Zone'Valid then
786 raise Constraint_Error;
792 and then Date'Length /= 22
794 raise Constraint_Error;
797 -- After the correct length has been determined, it is safe to
798 -- copy the Date in order to avoid Date'First + N indexing.
800 D (1 .. Date'Length) := Date;
804 Check_Char (D, '-', 5);
805 Check_Char (D, '-', 8);
806 Check_Char (D, ' ', 11);
807 Check_Char (D, ':', 14);
808 Check_Char (D, ':', 17);
810 if Date'Length = 22 then
811 Check_Char (D, '.', 20);
814 -- Leading zero checks
822 if Date'Length = 22 then
828 Year := Year_Number (Year_Number'Value (D (1 .. 4)));
829 Month := Month_Number (Month_Number'Value (D (6 .. 7)));
830 Day := Day_Number (Day_Number'Value (D (9 .. 10)));
831 Hour := Hour_Number (Hour_Number'Value (D (12 .. 13)));
832 Minute := Minute_Number (Minute_Number'Value (D (15 .. 16)));
833 Second := Second_Number (Second_Number'Value (D (18 .. 19)));
837 if Date'Length = 22 then
838 Sub_Second := Second_Duration (Second_Duration'Value (D (20 .. 22)));
844 or else not Month'Valid
845 or else not Day'Valid
846 or else not Hour'Valid
847 or else not Minute'Valid
848 or else not Second'Valid
849 or else not Sub_Second'Valid
851 raise Constraint_Error;
854 return Time_Of (Year, Month, Day,
855 Hour, Minute, Second, Sub_Second, False, Time_Zone);
858 when others => raise Constraint_Error;
865 function Value (Elapsed_Time : String) return Duration is
866 D : String (1 .. 11);
868 Minute : Minute_Number;
869 Second : Second_Number;
870 Sub_Second : Second_Duration := 0.0;
875 if Elapsed_Time'Length /= 8
876 and then Elapsed_Time'Length /= 11
878 raise Constraint_Error;
881 -- After the correct length has been determined, it is safe to
882 -- copy the Elapsed_Time in order to avoid Date'First + N indexing.
884 D (1 .. Elapsed_Time'Length) := Elapsed_Time;
888 Check_Char (D, ':', 3);
889 Check_Char (D, ':', 6);
891 if Elapsed_Time'Length = 11 then
892 Check_Char (D, '.', 9);
895 -- Leading zero checks
901 if Elapsed_Time'Length = 11 then
907 Hour := Hour_Number (Hour_Number'Value (D (1 .. 2)));
908 Minute := Minute_Number (Minute_Number'Value (D (4 .. 5)));
909 Second := Second_Number (Second_Number'Value (D (7 .. 8)));
913 if Elapsed_Time'Length = 11 then
914 Sub_Second := Second_Duration (Second_Duration'Value (D (9 .. 11)));
920 or else not Minute'Valid
921 or else not Second'Valid
922 or else not Sub_Second'Valid
924 raise Constraint_Error;
927 return Seconds_Of (Hour, Minute, Second, Sub_Second);
930 when others => raise Constraint_Error;
939 Time_Zone : Time_Zones.Time_Offset := 0) return Year_Number
947 Ss : Second_Duration;
950 pragma Unreferenced (Mo, D, H, Mi);
953 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
957 end Ada.Calendar.Formatting;