-- The above flag controls the usage of leap seconds in all Ada.Calendar
-- routines.
- Leap_Seconds_Count : constant Natural := 23;
+ Leap_Seconds_Count : constant Natural := 24;
---------------------
-- Local Constants --
Ada_Min_Year : constant Year_Number := Year_Number'First;
Secs_In_Four_Years : constant := (3 * 365 + 366) * Secs_In_Day;
Secs_In_Non_Leap_Year : constant := 365 * Secs_In_Day;
+ Nanos_In_Four_Years : constant := Secs_In_Four_Years * Nano;
-- Lower and upper bound of Ada time. The zero (0) value of type Time is
-- positioned at year 2150. Note that the lower and upper bound account
-4859827181000000000,
-4812566380000000000,
-4765132779000000000,
- -4544207978000000000);
+ -4544207978000000000,
+ -4449513577000000000);
---------
-- "+" --
-- Step 3: Handle leap second occurrences
- if Leap_Sec then
- tm_sec := 60;
- else
- tm_sec := Second;
- end if;
+ tm_sec := (if Leap_Sec then 60 else Second);
end To_Struct_Tm;
------------------
-- the input. Guard against very large delay values such as the end
-- of time since the computation will overflow.
- if Res_N > Safe_Ada_High then
- Res_N := Safe_Ada_High;
- else
- Res_N := Res_N + Epoch_Offset;
- end if;
+ Res_N := (if Res_N > Safe_Ada_High then Safe_Ada_High
+ else Res_N + Epoch_Offset);
return Time_Rep_To_Duration (Res_N);
end To_Duration;
-----------------
function Day_Of_Week (Date : Time) return Integer is
- Y : Year_Number;
- Mo : Month_Number;
- D : Day_Number;
- Ds : Day_Duration;
- H : Integer;
- Mi : Integer;
- Se : Integer;
- Su : Duration;
- Le : Boolean;
-
- pragma Unreferenced (Ds, H, Mi, Se, Su, Le);
+ Date_N : constant Time_Rep := Time_Rep (Date);
+ Time_Zone : constant Long_Integer :=
+ Time_Zones_Operations.UTC_Time_Offset (Date);
+ Ada_Low_N : Time_Rep;
Day_Count : Long_Integer;
- Res_Dur : Time_Dur;
- Res_N : Time_Rep;
+ Day_Dur : Time_Dur;
+ High_N : Time_Rep;
+ Low_N : Time_Rep;
begin
- Formatting_Operations.Split
- (Date => Date,
- Year => Y,
- Month => Mo,
- Day => D,
- Day_Secs => Ds,
- Hour => H,
- Minute => Mi,
- Second => Se,
- Sub_Sec => Su,
- Leap_Sec => Le,
- Is_Ada_05 => True,
- Time_Zone => 0);
-
- -- Build a time value in the middle of the same day
-
- Res_N :=
- Time_Rep
- (Formatting_Operations.Time_Of
- (Year => Y,
- Month => Mo,
- Day => D,
- Day_Secs => 0.0,
- Hour => 12,
- Minute => 0,
- Second => 0,
- Sub_Sec => 0.0,
- Leap_Sec => False,
- Use_Day_Secs => False,
- Is_Ada_05 => True,
- Time_Zone => 0));
+ -- As declared, the Ada Epoch is set in UTC. For this calculation to
+ -- work properly, both the Epoch and the input date must be in the
+ -- same time zone. The following places the Epoch in the input date's
+ -- time zone.
+
+ Ada_Low_N := Ada_Low - Time_Rep (Time_Zone) * Nano;
+
+ if Date_N > Ada_Low_N then
+ High_N := Date_N;
+ Low_N := Ada_Low_N;
+ else
+ High_N := Ada_Low_N;
+ Low_N := Date_N;
+ end if;
-- Determine the elapsed seconds since the start of Ada time
- Res_Dur := Time_Dur (Res_N / Nano - Ada_Low / Nano);
+ Day_Dur := Time_Dur (High_N / Nano - Low_N / Nano);
- -- Count the number of days since the start of Ada time. 1901-1-1
+ -- Count the number of days since the start of Ada time. 1901-01-01
-- GMT was a Tuesday.
- Day_Count := Long_Integer (Res_Dur / Secs_In_Day) + 1;
+ Day_Count := Long_Integer (Day_Dur / Secs_In_Day) + 1;
return Integer (Day_Count mod 7);
end Day_Of_Week;
-- the input date.
Count := (Year - Year_Number'First) / 4;
- Res_N := Res_N + Time_Rep (Count) * Secs_In_Four_Years * Nano;
+ for Four_Year_Segments in 1 .. Count loop
+ Res_N := Res_N + Nanos_In_Four_Years;
+ end loop;
-- Note that non-leap centennial years are automatically considered
-- leap in the operation above. An adjustment of several days is
Res_N := Res_N + Duration_To_Time_Rep (Day_Secs);
else
- Res_N := Res_N +
- Time_Rep (Hour * 3_600 + Minute * 60 + Second) * Nano;
+ Res_N :=
+ Res_N + Time_Rep (Hour * 3_600 + Minute * 60 + Second) * Nano;
if Sub_Sec = 1.0 then
Res_N := Res_N + Time_Rep (1) * Nano;
Nanos_In_56_Years : constant := (14 * 366 + 42 * 365) * Nanos_In_Day;
- -- Base C types. There is no point dragging in Interfaces.C just for
- -- these four types.
-
- type char_Pointer is access Character;
- subtype int is Integer;
subtype long is Long_Integer;
type long_Pointer is access all long;
- -- The Ada equivalent of struct tm and type time_t
-
- type tm is record
- tm_sec : int; -- seconds after the minute (0 .. 60)
- tm_min : int; -- minutes after the hour (0 .. 59)
- tm_hour : int; -- hours since midnight (0 .. 24)
- tm_mday : int; -- day of the month (1 .. 31)
- tm_mon : int; -- months since January (0 .. 11)
- tm_year : int; -- years since 1900
- tm_wday : int; -- days since Sunday (0 .. 6)
- tm_yday : int; -- days since January 1 (0 .. 365)
- tm_isdst : int; -- Daylight Savings Time flag (-1 .. 1)
- tm_gmtoff : long; -- offset from UTC in seconds
- tm_zone : char_Pointer; -- timezone abbreviation
- end record;
-
- type tm_Pointer is access all tm;
-
- subtype time_t is long;
+ type time_t is
+ range -(2 ** (Standard'Address_Size - Integer'(1))) ..
+ +(2 ** (Standard'Address_Size - Integer'(1)) - 1);
type time_t_Pointer is access all time_t;
procedure localtime_tzoff
- (C : time_t_Pointer;
- res : tm_Pointer;
- off : long_Pointer);
+ (timer : time_t_Pointer;
+ off : long_Pointer);
pragma Import (C, localtime_tzoff, "__gnat_localtime_tzoff");
-- This is a lightweight wrapper around the system library function
-- localtime_r. Parameter 'off' captures the UTC offset which is either
---------------------
function UTC_Time_Offset (Date : Time) return Long_Integer is
- Adj_Cent : Integer := 0;
+ Adj_Cent : Integer;
Date_N : Time_Rep;
Offset : aliased long;
Secs_T : aliased time_t;
- Secs_TM : aliased tm;
begin
Date_N := Time_Rep (Date);
-- saving and so on. Non-leap centennial years violate this rule by
-- one day and as a consequence, special adjustment is needed.
- if Date_N > T_2100_2_28 then
- if Date_N > T_2200_2_28 then
- if Date_N > T_2300_2_28 then
- Adj_Cent := 3;
- else
- Adj_Cent := 2;
- end if;
-
- else
- Adj_Cent := 1;
- end if;
- end if;
+ Adj_Cent :=
+ (if Date_N <= T_2100_2_28 then 0
+ elsif Date_N <= T_2200_2_28 then 1
+ elsif Date_N <= T_2300_2_28 then 2
+ else 3);
if Adj_Cent > 0 then
Date_N := Date_N - Time_Rep (Adj_Cent) * Nanos_In_Day;
localtime_tzoff
(Secs_T'Unchecked_Access,
- Secs_TM'Unchecked_Access,
Offset'Unchecked_Access);
return Offset;