1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . C A L E N D A R --
9 -- Copyright (C) 1992-2007, 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 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 -- This is the Alpha/VMS version
36 with System.Aux_DEC; use System.Aux_DEC;
38 with Ada.Unchecked_Conversion;
40 package body Ada.Calendar is
42 --------------------------
43 -- Implementation Notes --
44 --------------------------
46 -- Variables of type Ada.Calendar.Time have suffix _S or _M to denote
47 -- units of seconds or milis.
49 -- Because time is measured in different units and from different origins
50 -- on various targets, a system independent model is incorporated into
51 -- Ada.Calendar. The idea behing the design is to encapsulate all target
52 -- dependent machinery in a single package, thus providing a uniform
53 -- interface to all existing and any potential children.
55 -- package Ada.Calendar
56 -- procedure Split (5 parameters) -------+
57 -- | Call from local routine
59 -- package Formatting_Operations |
60 -- procedure Split (11 parameters) <--+
61 -- end Formatting_Operations |
64 -- package Ada.Calendar.Formatting | Call from child routine
65 -- procedure Split (9 or 10 parameters) -+
66 -- end Ada.Calendar.Formatting
68 -- The behaviour of the interfacing routines is controlled via various
69 -- flags. All new Ada 2005 types from children of Ada.Calendar are
70 -- emulated by a similar type. For instance, type Day_Number is replaced
71 -- by Integer in various routines. One ramification of this model is that
72 -- the caller site must perform validity checks on returned results.
73 -- The end result of this model is the lack of target specific files per
74 -- child of Ada.Calendar (a-calfor, a-calfor-vms, a-calfor-vxwors, etc).
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Check_Within_Time_Bounds (T : Time);
81 -- Ensure that a time representation value falls withing the bounds of Ada
82 -- time. Leap seconds support is taken into account.
84 procedure Cumulative_Leap_Seconds
87 Elapsed_Leaps : out Natural;
88 Next_Leap_Sec : out Time);
89 -- Elapsed_Leaps is the sum of the leap seconds that have occured on or
90 -- after Start_Date and before (strictly before) End_Date. Next_Leap_Sec
91 -- represents the next leap second occurence on or after End_Date. If
92 -- there are no leaps seconds after End_Date, End_Of_Time is returned.
93 -- End_Of_Time can be used as End_Date to count all the leap seconds that
94 -- have occured on or after Start_Date.
96 -- Note: Any sub seconds of Start_Date and End_Date are discarded before
97 -- the calculations are done. For instance: if 113 seconds is a leap
98 -- second (it isn't) and 113.5 is input as an End_Date, the leap second
99 -- at 113 will not be counted in Leaps_Between, but it will be returned
100 -- as Next_Leap_Sec. Thus, if the caller wants to know if the End_Date is
101 -- a leap second, the comparison should be:
103 -- End_Date >= Next_Leap_Sec;
105 -- After_Last_Leap is designed so that this comparison works without
106 -- having to first check if Next_Leap_Sec is a valid leap second.
108 function To_Duration (T : Time) return Duration;
109 function To_Relative_Time (D : Duration) return Time;
110 -- It is important to note that duration's fractional part denotes nano
111 -- seconds while the units of Time are 100 nanoseconds. If a regular
112 -- Unchecked_Conversion was employed, the resulting values would be off
115 --------------------------
116 -- Leap seconds control --
117 --------------------------
120 pragma Import (C, Flag, "__gl_leap_seconds_support");
121 -- This imported value is used to determine whether the compilation had
122 -- binder flag "-y" present which enables leap seconds. A value of zero
123 -- signifies no leap seconds support while a value of one enables the
126 Leap_Support : constant Boolean := Flag = 1;
127 -- The above flag controls the usage of leap seconds in all Ada.Calendar
130 Leap_Seconds_Count : constant Natural := 23;
132 ---------------------
133 -- Local Constants --
134 ---------------------
136 -- The range of Ada time expressed as milis since the VMS Epoch
138 Ada_Low : constant Time := (10 * 366 + 32 * 365 + 45) * Milis_In_Day;
139 Ada_High : constant Time := (131 * 366 + 410 * 365 + 45) * Milis_In_Day;
141 -- Even though the upper bound of time is 2399-12-31 23:59:59.9999999
142 -- UTC, it must be increased to include all leap seconds.
144 Ada_High_And_Leaps : constant Time :=
145 Ada_High + Time (Leap_Seconds_Count) * Mili;
147 -- Two constants used in the calculations of elapsed leap seconds.
148 -- End_Of_Time is later than Ada_High in time zone -28. Start_Of_Time
149 -- is earlier than Ada_Low in time zone +28.
151 End_Of_Time : constant Time := Ada_High + Time (3) * Milis_In_Day;
152 Start_Of_Time : constant Time := Ada_Low - Time (3) * Milis_In_Day;
154 -- The following table contains the hard time values of all existing leap
155 -- seconds. The values are produced by the utility program xleaps.adb.
157 Leap_Second_Times : constant array (1 .. Leap_Seconds_Count) of Time :=
186 function "+" (Left : Time; Right : Duration) return Time is
187 pragma Unsuppress (Overflow_Check);
189 return Left + To_Relative_Time (Right);
191 when Constraint_Error =>
195 function "+" (Left : Duration; Right : Time) return Time is
196 pragma Unsuppress (Overflow_Check);
200 when Constraint_Error =>
208 function "-" (Left : Time; Right : Duration) return Time is
209 pragma Unsuppress (Overflow_Check);
211 return Left - To_Relative_Time (Right);
213 when Constraint_Error =>
217 function "-" (Left : Time; Right : Time) return Duration is
218 pragma Unsuppress (Overflow_Check);
220 -- The bound of type Duration expressed as time
222 Dur_High : constant Time := To_Relative_Time (Duration'Last);
223 Dur_Low : constant Time := To_Relative_Time (Duration'First);
228 Res_M := Left - Right;
230 -- Due to the extended range of Ada time, "-" is capable of producing
231 -- results which may exceed the range of Duration. In order to prevent
232 -- the generation of bogus values by the Unchecked_Conversion, we apply
233 -- the following check.
236 or else Res_M >= Dur_High
240 -- Normal case, result fits
243 return To_Duration (Res_M);
247 when Constraint_Error =>
255 function "<" (Left, Right : Time) return Boolean is
257 return Long_Integer (Left) < Long_Integer (Right);
264 function "<=" (Left, Right : Time) return Boolean is
266 return Long_Integer (Left) <= Long_Integer (Right);
273 function ">" (Left, Right : Time) return Boolean is
275 return Long_Integer (Left) > Long_Integer (Right);
282 function ">=" (Left, Right : Time) return Boolean is
284 return Long_Integer (Left) >= Long_Integer (Right);
287 ------------------------------
288 -- Check_Within_Time_Bounds --
289 ------------------------------
291 procedure Check_Within_Time_Bounds (T : Time) is
294 if T < Ada_Low or else T > Ada_High_And_Leaps then
298 if T < Ada_Low or else T > Ada_High then
302 end Check_Within_Time_Bounds;
308 function Clock return Time is
309 Elapsed_Leaps : Natural;
311 Res_M : constant Time := Time (OSP.OS_Clock);
314 -- Note that on other targets a soft-link is used to get a different
315 -- clock depending whether tasking is used or not. On VMS this isn't
316 -- needed since all clock calls end up using SYS$GETTIM, so call the
317 -- OS_Primitives version for efficiency.
319 -- If the target supports leap seconds, determine the number of leap
320 -- seconds elapsed until this moment.
323 Cumulative_Leap_Seconds
324 (Start_Of_Time, Res_M, Elapsed_Leaps, Next_Leap_M);
326 -- The system clock may fall exactly on a leap second
328 if Res_M >= Next_Leap_M then
329 Elapsed_Leaps := Elapsed_Leaps + 1;
332 -- The target does not support leap seconds
338 return Res_M + Time (Elapsed_Leaps) * Mili;
341 -----------------------------
342 -- Cumulative_Leap_Seconds --
343 -----------------------------
345 procedure Cumulative_Leap_Seconds
348 Elapsed_Leaps : out Natural;
349 Next_Leap_Sec : out Time)
351 End_Index : Positive;
352 End_T : Time := End_Date;
353 Start_Index : Positive;
354 Start_T : Time := Start_Date;
357 pragma Assert (Leap_Support and then End_Date >= Start_Date);
359 Next_Leap_Sec := End_Of_Time;
361 -- Make sure that the end date does not excede the upper bound
364 if End_Date > Ada_High then
368 -- Remove the sub seconds from both dates
370 Start_T := Start_T - (Start_T mod Mili);
371 End_T := End_T - (End_T mod Mili);
373 -- Some trivial cases:
374 -- Leap 1 . . . Leap N
375 -- ---+========+------+############+-------+========+-----
376 -- Start_T End_T Start_T End_T
378 if End_T < Leap_Second_Times (1) then
380 Next_Leap_Sec := Leap_Second_Times (1);
383 elsif Start_T > Leap_Second_Times (Leap_Seconds_Count) then
385 Next_Leap_Sec := End_Of_Time;
389 -- Perform the calculations only if the start date is within the leap
390 -- second occurences table.
392 if Start_T <= Leap_Second_Times (Leap_Seconds_Count) then
395 -- +----+----+-- . . . --+-------+---+
396 -- | T1 | T2 | | N - 1 | N |
397 -- +----+----+-- . . . --+-------+---+
399 -- | Start_Index | End_Index
400 -- +-------------------+
403 -- The idea behind the algorithm is to iterate and find two closest
404 -- dates which are after Start_T and End_T. Their corresponding
405 -- index difference denotes the number of leap seconds elapsed.
409 exit when Leap_Second_Times (Start_Index) >= Start_T;
410 Start_Index := Start_Index + 1;
413 End_Index := Start_Index;
415 exit when End_Index > Leap_Seconds_Count
416 or else Leap_Second_Times (End_Index) >= End_T;
417 End_Index := End_Index + 1;
420 if End_Index <= Leap_Seconds_Count then
421 Next_Leap_Sec := Leap_Second_Times (End_Index);
424 Elapsed_Leaps := End_Index - Start_Index;
429 end Cumulative_Leap_Seconds;
435 function Day (Date : Time) return Day_Number is
441 Split (Date, Y, M, D, S);
449 function Is_Leap (Year : Year_Number) return Boolean is
451 -- Leap centenial years
453 if Year mod 400 = 0 then
456 -- Non-leap centenial years
458 elsif Year mod 100 = 0 then
464 return Year mod 4 = 0;
472 function Month (Date : Time) return Month_Number is
478 Split (Date, Y, M, D, S);
486 function Seconds (Date : Time) return Day_Duration is
492 Split (Date, Y, M, D, S);
502 Year : out Year_Number;
503 Month : out Month_Number;
504 Day : out Day_Number;
505 Seconds : out Day_Duration)
514 -- Use UTC as the local time zone on VMS, the status of flag Is_Ada_05
515 -- is irrelevant in this case.
517 Formatting_Operations.Split
534 or else not Month'Valid
535 or else not Day'Valid
536 or else not Seconds'Valid
548 Month : Month_Number;
550 Seconds : Day_Duration := 0.0) return Time
552 -- The values in the following constants are irrelevant, they are just
553 -- placeholders; the choice of constructing a Day_Duration value is
554 -- controlled by the Use_Day_Secs flag.
556 H : constant Integer := 1;
557 M : constant Integer := 1;
558 Se : constant Integer := 1;
559 Ss : constant Duration := 0.1;
563 or else not Month'Valid
564 or else not Day'Valid
565 or else not Seconds'Valid
570 -- Use UTC as the local time zone on VMS, the status of flag Is_Ada_05
571 -- is irrelevant in this case.
574 Formatting_Operations.Time_Of
584 Use_Day_Secs => True,
593 function To_Duration (T : Time) return Duration is
594 function Time_To_Duration is
595 new Ada.Unchecked_Conversion (Time, Duration);
597 return Time_To_Duration (T * 100);
600 ----------------------
601 -- To_Relative_Time --
602 ----------------------
604 function To_Relative_Time (D : Duration) return Time is
605 function Duration_To_Time is
606 new Ada.Unchecked_Conversion (Duration, Time);
608 return Duration_To_Time (D / 100.0);
609 end To_Relative_Time;
615 function Year (Date : Time) return Year_Number is
621 Split (Date, Y, M, D, S);
625 -- The following packages assume that Time is a Long_Integer, the units
626 -- are 100 nanoseconds and the starting point in the VMS Epoch.
628 ---------------------------
629 -- Arithmetic_Operations --
630 ---------------------------
632 package body Arithmetic_Operations is
638 function Add (Date : Time; Days : Long_Integer) return Time is
639 pragma Unsuppress (Overflow_Check);
641 return Date + Time (Days) * Milis_In_Day;
643 when Constraint_Error =>
654 Days : out Long_Integer;
655 Seconds : out Duration;
656 Leap_Seconds : out Integer)
658 Mili_F : constant Duration := 10_000_000.0;
663 Elapsed_Leaps : Natural;
665 Negate : Boolean := False;
667 Sub_Seconds : Duration;
670 -- This classification is necessary in order to avoid a Time_Error
671 -- being raised by the arithmetic operators in Ada.Calendar.
673 if Left >= Right then
682 -- If the target supports leap seconds, process them
685 Cumulative_Leap_Seconds
686 (Earlier, Later, Elapsed_Leaps, Next_Leap);
688 if Later >= Next_Leap then
689 Elapsed_Leaps := Elapsed_Leaps + 1;
692 -- The target does not support leap seconds
698 Diff_M := Later - Earlier - Time (Elapsed_Leaps) * Mili;
700 -- Sub second processing
702 Sub_Seconds := Duration (Diff_M mod Mili) / Mili_F;
704 -- Convert to seconds. Note that his action eliminates the sub
705 -- seconds automatically.
707 Diff_S := Diff_M / Mili;
709 Days := Long_Integer (Diff_S / Secs_In_Day);
710 Seconds := Duration (Diff_S mod Secs_In_Day) + Sub_Seconds;
711 Leap_Seconds := Integer (Elapsed_Leaps);
717 if Leap_Seconds /= 0 then
718 Leap_Seconds := -Leap_Seconds;
727 function Subtract (Date : Time; Days : Long_Integer) return Time is
728 pragma Unsuppress (Overflow_Check);
730 return Date - Time (Days) * Milis_In_Day;
732 when Constraint_Error =>
735 end Arithmetic_Operations;
737 ---------------------------
738 -- Formatting_Operations --
739 ---------------------------
741 package body Formatting_Operations is
747 function Day_Of_Week (Date : Time) return Integer is
753 Day_Count : Long_Integer;
754 Midday_Date_S : Time;
757 Split (Date, Y, M, D, S);
759 -- Build a time value in the middle of the same day and convert the
760 -- time value to seconds.
762 Midday_Date_S := Time_Of (Y, M, D, 43_200.0) / Mili;
764 -- Count the number of days since the start of VMS time. 1858-11-17
767 Day_Count := Long_Integer (Midday_Date_S / Secs_In_Day) + 2;
769 return Integer (Day_Count mod 7);
778 Year : out Year_Number;
779 Month : out Month_Number;
780 Day : out Day_Number;
781 Day_Secs : out Day_Duration;
783 Minute : out Integer;
784 Second : out Integer;
785 Sub_Sec : out Duration;
786 Leap_Sec : out Boolean;
788 Time_Zone : Long_Integer)
790 -- The flag Is_Ada_05 is present for interfacing purposes
792 pragma Unreferenced (Is_Ada_05);
795 (Status : out Unsigned_Longword;
796 Timbuf : out Unsigned_Word_Array;
799 pragma Interface (External, Numtim);
801 pragma Import_Valued_Procedure
802 (Numtim, "SYS$NUMTIM",
803 (Unsigned_Longword, Unsigned_Word_Array, Time),
804 (Value, Reference, Reference));
806 Status : Unsigned_Longword;
807 Timbuf : Unsigned_Word_Array (1 .. 7);
809 Ada_Min_Year : constant := 1901;
810 Ada_Max_Year : constant := 2399;
811 Mili_F : constant Duration := 10_000_000.0;
814 Elapsed_Leaps : Natural;
820 -- Step 1: Leap seconds processing
823 Cumulative_Leap_Seconds
824 (Start_Of_Time, Date, Elapsed_Leaps, Next_Leap_M);
826 Leap_Sec := Date_M >= Next_Leap_M;
829 Elapsed_Leaps := Elapsed_Leaps + 1;
832 -- The target does not support leap seconds
839 Date_M := Date_M - Time (Elapsed_Leaps) * Mili;
841 -- Step 2: Time zone processing
843 if Time_Zone /= 0 then
844 Date_M := Date_M + Time (Time_Zone) * 60 * Mili;
847 -- After the leap seconds and time zone have been accounted for,
848 -- the date should be within the bounds of Ada time.
851 or else Date_M > Ada_High
856 -- Step 3: Sub second processing
858 Sub_Sec := Duration (Date_M mod Mili) / Mili_F;
860 -- Drop the sub seconds
862 Date_M := Date_M - (Date_M mod Mili);
864 -- Step 4: VMS system call
866 Numtim (Status, Timbuf, Date_M);
869 or else Timbuf (1) not in Ada_Min_Year .. Ada_Max_Year
874 -- Step 5: Time components processing
876 Year := Year_Number (Timbuf (1));
877 Month := Month_Number (Timbuf (2));
878 Day := Day_Number (Timbuf (3));
879 Hour := Integer (Timbuf (4));
880 Minute := Integer (Timbuf (5));
881 Second := Integer (Timbuf (6));
883 Day_Secs := Day_Duration (Hour * 3_600) +
884 Day_Duration (Minute * 60) +
885 Day_Duration (Second) +
895 Month : Month_Number;
897 Day_Secs : Day_Duration;
903 Use_Day_Secs : Boolean;
905 Time_Zone : Long_Integer) return Time
908 (Status : out Unsigned_Longword;
909 Input_Time : Unsigned_Word_Array;
910 Resultant_Time : out Time);
912 pragma Interface (External, Cvt_Vectim);
914 pragma Import_Valued_Procedure
915 (Cvt_Vectim, "LIB$CVT_VECTIM",
916 (Unsigned_Longword, Unsigned_Word_Array, Time),
917 (Value, Reference, Reference));
919 Status : Unsigned_Longword;
920 Timbuf : Unsigned_Word_Array (1 .. 7);
922 Mili_F : constant := 10_000_000.0;
924 Y : Year_Number := Year;
925 Mo : Month_Number := Month;
926 D : Day_Number := Day;
928 Mi : Integer := Minute;
929 Se : Integer := Second;
930 Su : Duration := Sub_Sec;
932 Elapsed_Leaps : Natural;
933 Int_Day_Secs : Integer;
936 Rounded_Res_M : Time;
939 -- No validity checks are performed on the input values since it is
940 -- assumed that the called has already performed them.
942 -- Step 1: Hour, minute, second and sub second processing
946 -- A day seconds value of 86_400 designates a new day
948 if Day_Secs = 86_400.0 then
950 Adj_Year : Year_Number := Year;
951 Adj_Month : Month_Number := Month;
952 Adj_Day : Day_Number := Day;
955 if Day < Days_In_Month (Month)
957 and then Is_Leap (Year))
961 -- The day adjustment moves the date to a new month
967 Adj_Month := Month + 1;
969 -- The month adjustment moves the date to a new year
973 Adj_Year := Year + 1;
986 -- Normal case (not exactly one day)
989 -- Sub second extraction
991 if Day_Secs > 0.0 then
992 Int_Day_Secs := Integer (Day_Secs - 0.5);
994 Int_Day_Secs := Integer (Day_Secs);
997 H := Int_Day_Secs / 3_600;
998 Mi := (Int_Day_Secs / 60) mod 60;
999 Se := Int_Day_Secs mod 60;
1000 Su := Day_Secs - Duration (Int_Day_Secs);
1004 -- Step 2: System call to VMS
1006 Timbuf (1) := Unsigned_Word (Y);
1007 Timbuf (2) := Unsigned_Word (Mo);
1008 Timbuf (3) := Unsigned_Word (D);
1009 Timbuf (4) := Unsigned_Word (H);
1010 Timbuf (5) := Unsigned_Word (Mi);
1011 Timbuf (6) := Unsigned_Word (Se);
1014 Cvt_Vectim (Status, Timbuf, Res_M);
1016 if Status mod 2 /= 1 then
1020 -- Step 3: Sub second adjustment
1022 Res_M := Res_M + Time (Su * Mili_F);
1024 -- Step 4: Bounds check
1026 Check_Within_Time_Bounds (Res_M);
1028 -- Step 5: Time zone processing
1030 if Time_Zone /= 0 then
1031 Res_M := Res_M - Time (Time_Zone) * 60 * Mili;
1034 -- Step 6: Leap seconds processing
1036 if Leap_Support then
1037 Cumulative_Leap_Seconds
1038 (Start_Of_Time, Res_M, Elapsed_Leaps, Next_Leap_M);
1040 Res_M := Res_M + Time (Elapsed_Leaps) * Mili;
1042 -- An Ada 2005 caller requesting an explicit leap second or an
1043 -- Ada 95 caller accounting for an invisible leap second.
1046 or else Res_M >= Next_Leap_M
1048 Res_M := Res_M + Time (1) * Mili;
1051 -- Leap second validity check
1053 Rounded_Res_M := Res_M - (Res_M mod Mili);
1057 and then Rounded_Res_M /= Next_Leap_M
1065 end Formatting_Operations;
1067 ---------------------------
1068 -- Time_Zones_Operations --
1069 ---------------------------
1071 package body Time_Zones_Operations is
1073 ---------------------
1074 -- UTC_Time_Offset --
1075 ---------------------
1077 function UTC_Time_Offset (Date : Time) return Long_Integer is
1078 -- Formal parameter Date is here for interfacing, but is never
1081 pragma Unreferenced (Date);
1083 function get_gmtoff return Long_Integer;
1084 pragma Import (C, get_gmtoff, "get_gmtoff");
1087 -- VMS is not capable of determining the time zone in some past or
1088 -- future point in time denoted by Date, thus the current time zone
1092 end UTC_Time_Offset;
1093 end Time_Zones_Operations;