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 - 0.5;
174 SS_Nat := Natural (Sub_Second);
177 Hour_Str : constant String := Hour_Number'Image (Hour);
178 Minute_Str : constant String := Minute_Number'Image (Minute);
179 Second_Str : constant String := Second_Number'Image (Second);
180 SS_Str : constant String := Natural'Image (SS_Nat);
183 -- Hour processing, positions 2 and 3
186 Result (3) := Hour_Str (2);
188 Result (2) := Hour_Str (2);
189 Result (3) := Hour_Str (3);
192 -- Minute processing, positions 5 and 6
195 Result (6) := Minute_Str (2);
197 Result (5) := Minute_Str (2);
198 Result (6) := Minute_Str (3);
201 -- Second processing, positions 8 and 9
204 Result (9) := Second_Str (2);
206 Result (8) := Second_Str (2);
207 Result (9) := Second_Str (3);
210 -- Optional sub second processing, positions 11 and 12
212 if Include_Time_Fraction then
214 Result (12) := SS_Str (2);
216 Result (11) := SS_Str (2);
217 Result (12) := SS_Str (3);
221 return Result (Low .. High);
231 Include_Time_Fraction : Boolean := False;
232 Time_Zone : Time_Zones.Time_Offset := 0) return String
235 Month : Month_Number;
238 Minute : Minute_Number;
239 Second : Second_Number;
240 Sub_Second : Duration;
242 Leap_Second : Boolean;
244 Result : String := "0000-00-00 00:00:00.00";
247 Split (Date, Year, Month, Day,
248 Hour, Minute, Second, Sub_Second, Leap_Second, Time_Zone);
250 -- Prevent rounding when converting to natural
252 Sub_Second := Sub_Second * 100.0 - 0.5;
253 SS_Nat := Natural (Sub_Second);
256 Year_Str : constant String := Year_Number'Image (Year);
257 Month_Str : constant String := Month_Number'Image (Month);
258 Day_Str : constant String := Day_Number'Image (Day);
259 Hour_Str : constant String := Hour_Number'Image (Hour);
260 Minute_Str : constant String := Minute_Number'Image (Minute);
261 Second_Str : constant String := Second_Number'Image (Second);
262 SS_Str : constant String := Natural'Image (SS_Nat);
265 -- Year processing, positions 1, 2, 3 and 4
267 Result (1) := Year_Str (2);
268 Result (2) := Year_Str (3);
269 Result (3) := Year_Str (4);
270 Result (4) := Year_Str (5);
272 -- Month processing, positions 6 and 7
275 Result (7) := Month_Str (2);
277 Result (6) := Month_Str (2);
278 Result (7) := Month_Str (3);
281 -- Day processing, positions 9 and 10
284 Result (10) := Day_Str (2);
286 Result (9) := Day_Str (2);
287 Result (10) := Day_Str (3);
290 -- Hour processing, positions 12 and 13
293 Result (13) := Hour_Str (2);
295 Result (12) := Hour_Str (2);
296 Result (13) := Hour_Str (3);
299 -- Minute processing, positions 15 and 16
302 Result (16) := Minute_Str (2);
304 Result (15) := Minute_Str (2);
305 Result (16) := Minute_Str (3);
308 -- Second processing, positions 18 and 19
311 Result (19) := Second_Str (2);
313 Result (18) := Second_Str (2);
314 Result (19) := Second_Str (3);
317 -- Optional sub second processing, positions 21 and 22
319 if Include_Time_Fraction then
321 Result (22) := SS_Str (2);
323 Result (21) := SS_Str (2);
324 Result (22) := SS_Str (3);
329 return Result (1 .. 19);
340 Time_Zone : Time_Zones.Time_Offset := 0) return Minute_Number
348 Ss : Second_Duration;
351 pragma Unreferenced (Y, Mo, D, H);
354 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
364 Time_Zone : Time_Zones.Time_Offset := 0) return Month_Number
372 Ss : Second_Duration;
375 pragma Unreferenced (Y, D, H, Mi);
378 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
386 function Second (Date : Time) return Second_Number is
393 Ss : Second_Duration;
396 pragma Unreferenced (Y, Mo, D, H, Mi);
399 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le);
409 Minute : Minute_Number;
410 Second : Second_Number := 0;
411 Sub_Second : Second_Duration := 0.0) return Day_Duration is
417 or else not Minute'Valid
418 or else not Second'Valid
419 or else not Sub_Second'Valid
421 raise Constraint_Error;
424 return Day_Duration (Hour * 3_600) +
425 Day_Duration (Minute * 60) +
426 Day_Duration (Second) +
435 (Seconds : Day_Duration;
436 Hour : out Hour_Number;
437 Minute : out Minute_Number;
438 Second : out Second_Number;
439 Sub_Second : out Second_Duration)
446 if not Seconds'Valid then
447 raise Constraint_Error;
450 if Seconds = 0.0 then
453 Secs := Natural (Seconds - 0.5);
456 Sub_Second := Second_Duration (Seconds - Day_Duration (Secs));
457 Hour := Hour_Number (Secs / 3_600);
458 Secs := Secs mod 3_600;
459 Minute := Minute_Number (Secs / 60);
460 Second := Second_Number (Secs mod 60);
465 or else not Minute'Valid
466 or else not Second'Valid
467 or else not Sub_Second'Valid
479 Year : out Year_Number;
480 Month : out Month_Number;
481 Day : out Day_Number;
482 Seconds : out Day_Duration;
483 Leap_Second : out Boolean;
484 Time_Zone : Time_Zones.Time_Offset := 0)
490 Tz : constant Long_Integer := Long_Integer (Time_Zone);
493 Formatting_Operations.Split
503 Leap_Sec => Leap_Second,
510 or else not Month'Valid
511 or else not Day'Valid
512 or else not Seconds'Valid
524 Year : out Year_Number;
525 Month : out Month_Number;
526 Day : out Day_Number;
527 Hour : out Hour_Number;
528 Minute : out Minute_Number;
529 Second : out Second_Number;
530 Sub_Second : out Second_Duration;
531 Time_Zone : Time_Zones.Time_Offset := 0)
535 Tz : constant Long_Integer := Long_Integer (Time_Zone);
538 Formatting_Operations.Split
547 Sub_Sec => Sub_Second,
555 or else not Month'Valid
556 or else not Day'Valid
557 or else not Hour'Valid
558 or else not Minute'Valid
559 or else not Second'Valid
560 or else not Sub_Second'Valid
572 Year : out Year_Number;
573 Month : out Month_Number;
574 Day : out Day_Number;
575 Hour : out Hour_Number;
576 Minute : out Minute_Number;
577 Second : out Second_Number;
578 Sub_Second : out Second_Duration;
579 Leap_Second : out Boolean;
580 Time_Zone : Time_Zones.Time_Offset := 0)
583 Tz : constant Long_Integer := Long_Integer (Time_Zone);
586 Formatting_Operations.Split
595 Sub_Sec => Sub_Second,
596 Leap_Sec => Leap_Second,
603 or else not Month'Valid
604 or else not Day'Valid
605 or else not Hour'Valid
606 or else not Minute'Valid
607 or else not Second'Valid
608 or else not Sub_Second'Valid
618 function Sub_Second (Date : Time) return Second_Duration is
625 Ss : Second_Duration;
628 pragma Unreferenced (Y, Mo, D, H, Mi);
631 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le);
641 Month : Month_Number;
643 Seconds : Day_Duration := 0.0;
644 Leap_Second : Boolean := False;
645 Time_Zone : Time_Zones.Time_Offset := 0) return Time
647 Adj_Year : Year_Number := Year;
648 Adj_Month : Month_Number := Month;
649 Adj_Day : Day_Number := Day;
651 H : constant Integer := 1;
652 M : constant Integer := 1;
653 Se : constant Integer := 1;
654 Ss : constant Duration := 0.1;
655 Tz : constant Long_Integer := Long_Integer (Time_Zone);
661 or else not Month'Valid
662 or else not Day'Valid
663 or else not Seconds'Valid
664 or else not Time_Zone'Valid
666 raise Constraint_Error;
669 -- A Seconds value of 86_400 denotes a new day. This case requires an
670 -- adjustment to the input values.
672 if Seconds = 86_400.0 then
673 if Day < Days_In_Month (Month)
674 or else (Is_Leap (Year)
682 Adj_Month := Month + 1;
685 Adj_Year := Year + 1;
691 Formatting_Operations.Time_Of
700 Leap_Sec => Leap_Second,
701 Use_Day_Secs => True,
712 Month : Month_Number;
715 Minute : Minute_Number;
716 Second : Second_Number;
717 Sub_Second : Second_Duration := 0.0;
718 Leap_Second : Boolean := False;
719 Time_Zone : Time_Zones.Time_Offset := 0) return Time
721 Dd : constant Day_Duration := Day_Duration'First;
722 Tz : constant Long_Integer := Long_Integer (Time_Zone);
728 or else not Month'Valid
729 or else not Day'Valid
730 or else not Hour'Valid
731 or else not Minute'Valid
732 or else not Second'Valid
733 or else not Sub_Second'Valid
734 or else not Time_Zone'Valid
736 raise Constraint_Error;
740 Formatting_Operations.Time_Of
748 Sub_Sec => Sub_Second,
749 Leap_Sec => Leap_Second,
750 Use_Day_Secs => False,
761 Time_Zone : Time_Zones.Time_Offset := 0) return Time
763 D : String (1 .. 22);
765 Month : Month_Number;
768 Minute : Minute_Number;
769 Second : Second_Number;
770 Sub_Second : Second_Duration := 0.0;
775 if not Time_Zone'Valid then
776 raise Constraint_Error;
782 and then Date'Length /= 22
784 raise Constraint_Error;
787 -- After the correct length has been determined, it is safe to
788 -- copy the Date in order to avoid Date'First + N indexing.
790 D (1 .. Date'Length) := Date;
794 Check_Char (D, '-', 5);
795 Check_Char (D, '-', 8);
796 Check_Char (D, ' ', 11);
797 Check_Char (D, ':', 14);
798 Check_Char (D, ':', 17);
800 if Date'Length = 22 then
801 Check_Char (D, '.', 20);
804 -- Leading zero checks
812 if Date'Length = 22 then
818 Year := Year_Number (Year_Number'Value (D (1 .. 4)));
819 Month := Month_Number (Month_Number'Value (D (6 .. 7)));
820 Day := Day_Number (Day_Number'Value (D (9 .. 10)));
821 Hour := Hour_Number (Hour_Number'Value (D (12 .. 13)));
822 Minute := Minute_Number (Minute_Number'Value (D (15 .. 16)));
823 Second := Second_Number (Second_Number'Value (D (18 .. 19)));
827 if Date'Length = 22 then
828 Sub_Second := Second_Duration (Second_Duration'Value (D (20 .. 22)));
834 or else not Month'Valid
835 or else not Day'Valid
836 or else not Hour'Valid
837 or else not Minute'Valid
838 or else not Second'Valid
839 or else not Sub_Second'Valid
841 raise Constraint_Error;
844 return Time_Of (Year, Month, Day,
845 Hour, Minute, Second, Sub_Second, False, Time_Zone);
848 when others => raise Constraint_Error;
855 function Value (Elapsed_Time : String) return Duration is
856 D : String (1 .. 11);
858 Minute : Minute_Number;
859 Second : Second_Number;
860 Sub_Second : Second_Duration := 0.0;
865 if Elapsed_Time'Length /= 8
866 and then Elapsed_Time'Length /= 11
868 raise Constraint_Error;
871 -- After the correct length has been determined, it is safe to
872 -- copy the Elapsed_Time in order to avoid Date'First + N indexing.
874 D (1 .. Elapsed_Time'Length) := Elapsed_Time;
878 Check_Char (D, ':', 3);
879 Check_Char (D, ':', 6);
881 if Elapsed_Time'Length = 11 then
882 Check_Char (D, '.', 9);
885 -- Leading zero checks
891 if Elapsed_Time'Length = 11 then
897 Hour := Hour_Number (Hour_Number'Value (D (1 .. 2)));
898 Minute := Minute_Number (Minute_Number'Value (D (4 .. 5)));
899 Second := Second_Number (Second_Number'Value (D (7 .. 8)));
903 if Elapsed_Time'Length = 11 then
904 Sub_Second := Second_Duration (Second_Duration'Value (D (9 .. 11)));
910 or else not Minute'Valid
911 or else not Second'Valid
912 or else not Sub_Second'Valid
914 raise Constraint_Error;
917 return Seconds_Of (Hour, Minute, Second, Sub_Second);
920 when others => raise Constraint_Error;
929 Time_Zone : Time_Zones.Time_Offset := 0) return Year_Number
937 Ss : Second_Duration;
940 pragma Unreferenced (Mo, D, H, Mi);
943 Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone);
947 end Ada.Calendar.Formatting;