a-calend-vms.adb 38 KB
Newer Older
kenner's avatar
kenner committed
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                        GNAT RUN-TIME COMPONENTS                          --
--                                                                          --
--                         A D A . C A L E N D A R                          --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
kenner's avatar
kenner committed
10 11 12
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- 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- --
kenner's avatar
kenner committed
14 15
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 17 18 19 20 21 22 23 24 25
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
kenner's avatar
kenner committed
26 27
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
28
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
kenner's avatar
kenner committed
29 30 31
--                                                                          --
------------------------------------------------------------------------------

32
--  This is the Alpha/VMS version
kenner's avatar
kenner committed
33

34 35
with Ada.Unchecked_Conversion;

36 37 38
with System.Aux_DEC;       use System.Aux_DEC;
with System.OS_Primitives; use System.OS_Primitives;

kenner's avatar
kenner committed
39 40
package body Ada.Calendar is

41 42 43
   --------------------------
   -- Implementation Notes --
   --------------------------
kenner's avatar
kenner committed
44

45 46
   --  Variables of type Ada.Calendar.Time have suffix _S or _M to denote
   --  units of seconds or milis.
kenner's avatar
kenner committed
47

48 49
   --  Because time is measured in different units and from different origins
   --  on various targets, a system independent model is incorporated into
50
   --  Ada.Calendar. The idea behind the design is to encapsulate all target
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
   --  dependent machinery in a single package, thus providing a uniform
   --  interface to all existing and any potential children.

   --     package Ada.Calendar
   --        procedure Split (5 parameters) -------+
   --                                              | Call from local routine
   --     private                                  |
   --        package Formatting_Operations         |
   --           procedure Split (11 parameters) <--+
   --        end Formatting_Operations             |
   --     end Ada.Calendar                         |
   --                                              |
   --     package Ada.Calendar.Formatting          | Call from child routine
   --        procedure Split (9 or 10 parameters) -+
   --     end Ada.Calendar.Formatting

   --  The behaviour of the interfacing routines is controlled via various
   --  flags. All new Ada 2005 types from children of Ada.Calendar are
   --  emulated by a similar type. For instance, type Day_Number is replaced
   --  by Integer in various routines. One ramification of this model is that
   --  the caller site must perform validity checks on returned results.
   --  The end result of this model is the lack of target specific files per
   --  child of Ada.Calendar (a-calfor, a-calfor-vms, a-calfor-vxwors, etc).

75 76 77
   -----------------------
   -- Local Subprograms --
   -----------------------
kenner's avatar
kenner committed
78

79
   procedure Check_Within_Time_Bounds (T : OS_Time);
80 81
   --  Ensure that a time representation value falls withing the bounds of Ada
   --  time. Leap seconds support is taken into account.
82 83

   procedure Cumulative_Leap_Seconds
84 85
     (Start_Date    : OS_Time;
      End_Date      : OS_Time;
86
      Elapsed_Leaps : out Natural;
87
      Next_Leap_Sec : out OS_Time);
88
   --  Elapsed_Leaps is the sum of the leap seconds that have occurred on or
89
   --  after Start_Date and before (strictly before) End_Date. Next_Leap_Sec
90
   --  represents the next leap second occurrence on or after End_Date. If
91 92
   --  there are no leaps seconds after End_Date, End_Of_Time is returned.
   --  End_Of_Time can be used as End_Date to count all the leap seconds that
93
   --  have occurred on or after Start_Date.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
   --
   --  Note: Any sub seconds of Start_Date and End_Date are discarded before
   --  the calculations are done. For instance: if 113 seconds is a leap
   --  second (it isn't) and 113.5 is input as an End_Date, the leap second
   --  at 113 will not be counted in Leaps_Between, but it will be returned
   --  as Next_Leap_Sec. Thus, if the caller wants to know if the End_Date is
   --  a leap second, the comparison should be:
   --
   --     End_Date >= Next_Leap_Sec;
   --
   --  After_Last_Leap is designed so that this comparison works without
   --  having to first check if Next_Leap_Sec is a valid leap second.

   function To_Duration (T : Time) return Duration;
   function To_Relative_Time (D : Duration) return Time;
   --  It is important to note that duration's fractional part denotes nano
   --  seconds while the units of Time are 100 nanoseconds. If a regular
   --  Unchecked_Conversion was employed, the resulting values would be off
   --  by 100.
kenner's avatar
kenner committed
113

114 115 116
   --------------------------
   -- Leap seconds control --
   --------------------------
kenner's avatar
kenner committed
117

118 119 120 121 122 123 124 125 126 127
   Flag : Integer;
   pragma Import (C, Flag, "__gl_leap_seconds_support");
   --  This imported value is used to determine whether the compilation had
   --  binder flag "-y" present which enables leap seconds. A value of zero
   --  signifies no leap seconds support while a value of one enables the
   --  support.

   Leap_Support : constant Boolean := Flag = 1;
   --  The above flag controls the usage of leap seconds in all Ada.Calendar
   --  routines.
128

129
   Leap_Seconds_Count : constant Natural := 25;
130

131 132 133 134
   ---------------------
   -- Local Constants --
   ---------------------

135 136
   --  The range of Ada time expressed as milis since the VMS Epoch

137 138
   Ada_Low  : constant OS_Time :=  (10 * 366 +  32 * 365 + 45) * Milis_In_Day;
   Ada_High : constant OS_Time := (131 * 366 + 410 * 365 + 45) * Milis_In_Day;
139 140 141 142

   --  Even though the upper bound of time is 2399-12-31 23:59:59.9999999
   --  UTC, it must be increased to include all leap seconds.

143 144
   Ada_High_And_Leaps : constant OS_Time :=
                          Ada_High + OS_Time (Leap_Seconds_Count) * Mili;
145 146 147 148 149

   --  Two constants used in the calculations of elapsed leap seconds.
   --  End_Of_Time is later than Ada_High in time zone -28. Start_Of_Time
   --  is earlier than Ada_Low in time zone +28.

150 151
   End_Of_Time   : constant OS_Time := Ada_High + OS_Time (3) * Milis_In_Day;
   Start_Of_Time : constant OS_Time := Ada_Low  - OS_Time (3) * Milis_In_Day;
kenner's avatar
kenner committed
152

153 154 155
   --  The following table contains the hard time values of all existing leap
   --  seconds. The values are produced by the utility program xleaps.adb.

156
   Leap_Second_Times : constant array (1 .. Leap_Seconds_Count) of OS_Time :=
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
     (35855136000000000,
      36014112010000000,
      36329472020000000,
      36644832030000000,
      36960192040000000,
      37276416050000000,
      37591776060000000,
      37907136070000000,
      38222496080000000,
      38695104090000000,
      39010464100000000,
      39325824110000000,
      39957408120000000,
      40747104130000000,
      41378688140000000,
      41694048150000000,
      42166656160000000,
      42482016170000000,
      42797376180000000,
      43271712190000000,
      43744320200000000,
      44218656210000000,
179
      46427904220000000,
180 181
      47374848230000000,
      48478176240000000);
kenner's avatar
kenner committed
182 183 184 185 186 187 188 189

   ---------
   -- "+" --
   ---------

   function "+" (Left : Time; Right : Duration) return Time is
      pragma Unsuppress (Overflow_Check);
   begin
190
      return Left + To_Relative_Time (Right);
kenner's avatar
kenner committed
191 192 193 194 195 196 197 198
   exception
      when Constraint_Error =>
         raise Time_Error;
   end "+";

   function "+" (Left : Duration; Right : Time) return Time is
      pragma Unsuppress (Overflow_Check);
   begin
199
      return Right + Left;
kenner's avatar
kenner committed
200 201 202 203 204 205 206 207 208
   exception
      when Constraint_Error =>
         raise Time_Error;
   end "+";

   ---------
   -- "-" --
   ---------

209
   function "-" (Left : Time; Right : Duration) return Time is
kenner's avatar
kenner committed
210 211
      pragma Unsuppress (Overflow_Check);
   begin
212
      return Left - To_Relative_Time (Right);
kenner's avatar
kenner committed
213 214 215 216 217 218 219
   exception
      when Constraint_Error =>
         raise Time_Error;
   end "-";

   function "-" (Left : Time; Right : Time) return Duration is
      pragma Unsuppress (Overflow_Check);
220

221 222
      --  The bound of type Duration expressed as time

223 224 225 226
      Dur_High : constant OS_Time :=
                   OS_Time (To_Relative_Time (Duration'Last));
      Dur_Low  : constant OS_Time :=
                   OS_Time (To_Relative_Time (Duration'First));
227

228
      Res_M : OS_Time;
229

kenner's avatar
kenner committed
230
   begin
231
      Res_M := OS_Time (Left) - OS_Time (Right);
232

233 234 235 236
      --  Due to the extended range of Ada time, "-" is capable of producing
      --  results which may exceed the range of Duration. In order to prevent
      --  the generation of bogus values by the Unchecked_Conversion, we apply
      --  the following check.
237 238 239

      if Res_M < Dur_Low
        or else Res_M >= Dur_High
240 241
      then
         raise Time_Error;
242 243 244 245

      --  Normal case, result fits

      else
246
         return To_Duration (Time (Res_M));
247 248
      end if;

kenner's avatar
kenner committed
249 250 251 252 253 254 255 256 257 258 259
   exception
      when Constraint_Error =>
         raise Time_Error;
   end "-";

   ---------
   -- "<" --
   ---------

   function "<" (Left, Right : Time) return Boolean is
   begin
260
      return OS_Time (Left) < OS_Time (Right);
kenner's avatar
kenner committed
261 262 263 264 265 266 267 268
   end "<";

   ----------
   -- "<=" --
   ----------

   function "<=" (Left, Right : Time) return Boolean is
   begin
269
      return OS_Time (Left) <= OS_Time (Right);
kenner's avatar
kenner committed
270 271 272 273 274 275 276 277
   end "<=";

   ---------
   -- ">" --
   ---------

   function ">" (Left, Right : Time) return Boolean is
   begin
278
      return OS_Time (Left) > OS_Time (Right);
kenner's avatar
kenner committed
279 280 281 282 283 284 285 286
   end ">";

   ----------
   -- ">=" --
   ----------

   function ">=" (Left, Right : Time) return Boolean is
   begin
287
      return OS_Time (Left) >= OS_Time (Right);
kenner's avatar
kenner committed
288 289
   end ">=";

290 291 292
   ------------------------------
   -- Check_Within_Time_Bounds --
   ------------------------------
293

294
   procedure Check_Within_Time_Bounds (T : OS_Time) is
295
   begin
296 297 298 299 300 301 302 303 304 305
      if Leap_Support then
         if T < Ada_Low or else T > Ada_High_And_Leaps then
            raise Time_Error;
         end if;
      else
         if T < Ada_Low or else T > Ada_High then
            raise Time_Error;
         end if;
      end if;
   end Check_Within_Time_Bounds;
306

kenner's avatar
kenner committed
307 308 309 310 311
   -----------
   -- Clock --
   -----------

   function Clock return Time is
312
      Elapsed_Leaps : Natural;
313 314
      Next_Leap_M   : OS_Time;
      Res_M         : constant OS_Time := OS_Clock;
315

kenner's avatar
kenner committed
316
   begin
317 318 319 320 321
      --  Note that on other targets a soft-link is used to get a different
      --  clock depending whether tasking is used or not. On VMS this isn't
      --  needed since all clock calls end up using SYS$GETTIM, so call the
      --  OS_Primitives version for efficiency.

322 323 324 325 326 327 328 329
      --  If the target supports leap seconds, determine the number of leap
      --  seconds elapsed until this moment.

      if Leap_Support then
         Cumulative_Leap_Seconds
           (Start_Of_Time, Res_M, Elapsed_Leaps, Next_Leap_M);

         --  The system clock may fall exactly on a leap second
330

331 332 333
         if Res_M >= Next_Leap_M then
            Elapsed_Leaps := Elapsed_Leaps + 1;
         end if;
334

335
      --  The target does not support leap seconds
336 337

      else
338
         Elapsed_Leaps := 0;
339
      end if;
340

341
      return Time (Res_M + OS_Time (Elapsed_Leaps) * Mili);
kenner's avatar
kenner committed
342 343
   end Clock;

344 345 346 347 348
   -----------------------------
   -- Cumulative_Leap_Seconds --
   -----------------------------

   procedure Cumulative_Leap_Seconds
349 350
     (Start_Date    : OS_Time;
      End_Date      : OS_Time;
351
      Elapsed_Leaps : out Natural;
352
      Next_Leap_Sec : out OS_Time)
353 354
   is
      End_Index   : Positive;
355
      End_T       : OS_Time := End_Date;
356
      Start_Index : Positive;
357
      Start_T     : OS_Time := Start_Date;
358 359

   begin
360
      pragma Assert (Leap_Support and then End_Date >= Start_Date);
361

362
      Next_Leap_Sec := End_Of_Time;
363

364
      --  Make sure that the end date does not exceed the upper bound
365 366 367 368 369 370 371 372 373 374 375
      --  of Ada time.

      if End_Date > Ada_High then
         End_T := Ada_High;
      end if;

      --  Remove the sub seconds from both dates

      Start_T := Start_T - (Start_T mod Mili);
      End_T   := End_T   - (End_T   mod Mili);

376 377 378 379
      --  Some trivial cases:
      --                     Leap 1 . . . Leap N
      --  ---+========+------+############+-------+========+-----
      --     Start_T  End_T                       Start_T  End_T
380 381 382 383 384 385

      if End_T < Leap_Second_Times (1) then
         Elapsed_Leaps := 0;
         Next_Leap_Sec := Leap_Second_Times (1);
         return;

386
      elsif Start_T > Leap_Second_Times (Leap_Seconds_Count) then
387
         Elapsed_Leaps := 0;
388
         Next_Leap_Sec := End_Of_Time;
389 390 391 392
         return;
      end if;

      --  Perform the calculations only if the start date is within the leap
393
      --  second occurrences table.
394

395
      if Start_T <= Leap_Second_Times (Leap_Seconds_Count) then
396 397 398 399 400 401 402 403 404 405 406

         --    1    2                  N - 1   N
         --  +----+----+--  . . .  --+-------+---+
         --  | T1 | T2 |             | N - 1 | N |
         --  +----+----+--  . . .  --+-------+---+
         --         ^                   ^
         --         | Start_Index       | End_Index
         --         +-------------------+
         --             Leaps_Between

         --  The idea behind the algorithm is to iterate and find two closest
407 408
         --  dates which are after Start_T and End_T. Their corresponding
         --  index difference denotes the number of leap seconds elapsed.
409 410 411 412 413 414 415 416 417

         Start_Index := 1;
         loop
            exit when Leap_Second_Times (Start_Index) >= Start_T;
            Start_Index := Start_Index + 1;
         end loop;

         End_Index := Start_Index;
         loop
418
            exit when End_Index > Leap_Seconds_Count
419 420 421 422
              or else Leap_Second_Times (End_Index) >= End_T;
            End_Index := End_Index + 1;
         end loop;

423
         if End_Index <= Leap_Seconds_Count then
424 425 426 427 428 429 430 431 432 433
            Next_Leap_Sec := Leap_Second_Times (End_Index);
         end if;

         Elapsed_Leaps := End_Index - Start_Index;

      else
         Elapsed_Leaps := 0;
      end if;
   end Cumulative_Leap_Seconds;

kenner's avatar
kenner committed
434 435 436 437 438
   ---------
   -- Day --
   ---------

   function Day (Date : Time) return Day_Number is
439 440 441 442
      Y : Year_Number;
      M : Month_Number;
      D : Day_Number;
      S : Day_Duration;
443
      pragma Unreferenced (Y, M, S);
kenner's avatar
kenner committed
444
   begin
445 446
      Split (Date, Y, M, D, S);
      return D;
kenner's avatar
kenner committed
447 448
   end Day;

449 450 451 452 453 454
   -------------
   -- Is_Leap --
   -------------

   function Is_Leap (Year : Year_Number) return Boolean is
   begin
455
      --  Leap centennial years
456 457 458 459

      if Year mod 400 = 0 then
         return True;

460
      --  Non-leap centennial years
461 462 463 464 465 466 467 468 469 470 471

      elsif Year mod 100 = 0 then
         return False;

      --  Regular years

      else
         return Year mod 4 = 0;
      end if;
   end Is_Leap;

kenner's avatar
kenner committed
472 473 474 475 476
   -----------
   -- Month --
   -----------

   function Month (Date : Time) return Month_Number is
477 478 479 480
      Y : Year_Number;
      M : Month_Number;
      D : Day_Number;
      S : Day_Duration;
481
      pragma Unreferenced (Y, D, S);
kenner's avatar
kenner committed
482
   begin
483 484
      Split (Date, Y, M, D, S);
      return M;
kenner's avatar
kenner committed
485 486 487 488 489 490 491
   end Month;

   -------------
   -- Seconds --
   -------------

   function Seconds (Date : Time) return Day_Duration is
492 493 494 495
      Y : Year_Number;
      M : Month_Number;
      D : Day_Number;
      S : Day_Duration;
496
      pragma Unreferenced (Y, M, D);
kenner's avatar
kenner committed
497
   begin
498 499
      Split (Date, Y, M, D, S);
      return S;
kenner's avatar
kenner committed
500 501 502 503 504 505 506 507 508 509 510 511 512
   end Seconds;

   -----------
   -- Split --
   -----------

   procedure Split
     (Date    : Time;
      Year    : out Year_Number;
      Month   : out Month_Number;
      Day     : out Day_Number;
      Seconds : out Day_Duration)
   is
513 514 515 516 517
      H  : Integer;
      M  : Integer;
      Se : Integer;
      Ss : Duration;
      Le : Boolean;
518

kenner's avatar
kenner committed
519
   begin
520 521 522
      --  Use UTC as the local time zone on VMS, the status of flag Is_Ada_05
      --  is irrelevant in this case.

523
      Formatting_Operations.Split
524 525 526 527 528 529 530 531 532 533 534 535
        (Date      => Date,
         Year      => Year,
         Month     => Month,
         Day       => Day,
         Day_Secs  => Seconds,
         Hour      => H,
         Minute    => M,
         Second    => Se,
         Sub_Sec   => Ss,
         Leap_Sec  => Le,
         Is_Ada_05 => False,
         Time_Zone => 0);
kenner's avatar
kenner committed
536

537 538 539 540 541 542
      --  Validity checks

      if not Year'Valid
        or else not Month'Valid
        or else not Day'Valid
        or else not Seconds'Valid
kenner's avatar
kenner committed
543 544 545 546 547 548 549 550 551 552 553 554 555
      then
         raise Time_Error;
      end if;
   end Split;

   -------------
   -- Time_Of --
   -------------

   function Time_Of
     (Year    : Year_Number;
      Month   : Month_Number;
      Day     : Day_Number;
556
      Seconds : Day_Duration := 0.0) return Time
kenner's avatar
kenner committed
557
   is
558 559 560
      --  The values in the following constants are irrelevant, they are just
      --  placeholders; the choice of constructing a Day_Duration value is
      --  controlled by the Use_Day_Secs flag.
kenner's avatar
kenner committed
561

562 563 564 565
      H  : constant Integer := 1;
      M  : constant Integer := 1;
      Se : constant Integer := 1;
      Ss : constant Duration := 0.1;
566

kenner's avatar
kenner committed
567
   begin
568 569 570
      if not Year'Valid
        or else not Month'Valid
        or else not Day'Valid
kenner's avatar
kenner committed
571 572
        or else not Seconds'Valid
      then
573
         raise Time_Error;
kenner's avatar
kenner committed
574 575
      end if;

576 577 578
      --  Use UTC as the local time zone on VMS, the status of flag Is_Ada_05
      --  is irrelevant in this case.

579 580
      return
        Formatting_Operations.Time_Of
581 582 583 584 585 586 587 588
          (Year         => Year,
           Month        => Month,
           Day          => Day,
           Day_Secs     => Seconds,
           Hour         => H,
           Minute       => M,
           Second       => Se,
           Sub_Sec      => Ss,
589 590
           Leap_Sec     => False,
           Use_Day_Secs => True,
591
           Is_Ada_05    => False,
592 593
           Time_Zone    => 0);
   end Time_Of;
kenner's avatar
kenner committed
594

595 596 597
   -----------------
   -- To_Duration --
   -----------------
kenner's avatar
kenner committed
598

599 600 601 602 603 604
   function To_Duration (T : Time) return Duration is
      function Time_To_Duration is
        new Ada.Unchecked_Conversion (Time, Duration);
   begin
      return Time_To_Duration (T * 100);
   end To_Duration;
kenner's avatar
kenner committed
605

606 607 608
   ----------------------
   -- To_Relative_Time --
   ----------------------
kenner's avatar
kenner committed
609

610 611 612 613 614 615
   function To_Relative_Time (D : Duration) return Time is
      function Duration_To_Time is
        new Ada.Unchecked_Conversion (Duration, Time);
   begin
      return Duration_To_Time (D / 100.0);
   end To_Relative_Time;
kenner's avatar
kenner committed
616 617 618 619 620 621

   ----------
   -- Year --
   ----------

   function Year (Date : Time) return Year_Number is
622 623 624 625
      Y : Year_Number;
      M : Month_Number;
      D : Day_Number;
      S : Day_Duration;
626
      pragma Unreferenced (M, D, S);
kenner's avatar
kenner committed
627
   begin
628 629
      Split (Date, Y, M, D, S);
      return Y;
kenner's avatar
kenner committed
630 631
   end Year;

632 633
   --  The following packages assume that Time is a Long_Integer, the units
   --  are 100 nanoseconds and the starting point in the VMS Epoch.
634

635 636 637
   ---------------------------
   -- Arithmetic_Operations --
   ---------------------------
638

639
   package body Arithmetic_Operations is
640

641 642 643
      ---------
      -- Add --
      ---------
644

645
      function Add (Date : Time; Days : Long_Integer) return Time is
646
         pragma Unsuppress (Overflow_Check);
647
         Date_M : constant OS_Time := OS_Time (Date);
648
      begin
649
         return Time (Date_M + OS_Time (Days) * Milis_In_Day);
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
      exception
         when Constraint_Error =>
            raise Time_Error;
      end Add;

      ----------------
      -- Difference --
      ----------------

      procedure Difference
        (Left         : Time;
         Right        : Time;
         Days         : out Long_Integer;
         Seconds      : out Duration;
         Leap_Seconds : out Integer)
665
      is
666 667 668
         Diff_M        : OS_Time;
         Diff_S        : OS_Time;
         Earlier       : OS_Time;
669
         Elapsed_Leaps : Natural;
670
         Later         : OS_Time;
671
         Negate        : Boolean := False;
672
         Next_Leap     : OS_Time;
673 674
         Sub_Seconds   : Duration;

675
      begin
676 677 678 679
         --  This classification is necessary in order to avoid a Time_Error
         --  being raised by the arithmetic operators in Ada.Calendar.

         if Left >= Right then
680 681
            Later   := OS_Time (Left);
            Earlier := OS_Time (Right);
682
         else
683 684
            Later   := OS_Time (Right);
            Earlier := OS_Time (Left);
685 686 687
            Negate  := True;
         end if;

688
         --  If the target supports leap seconds, process them
689

690 691 692
         if Leap_Support then
            Cumulative_Leap_Seconds
              (Earlier, Later, Elapsed_Leaps, Next_Leap);
693

694 695 696 697 698 699 700 701
            if Later >= Next_Leap then
               Elapsed_Leaps := Elapsed_Leaps + 1;
            end if;

         --  The target does not support leap seconds

         else
            Elapsed_Leaps := 0;
702 703
         end if;

704
         Diff_M := Later - Earlier - OS_Time (Elapsed_Leaps) * Mili;
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

         --  Sub second processing

         Sub_Seconds := Duration (Diff_M mod Mili) / Mili_F;

         --  Convert to seconds. Note that his action eliminates the sub
         --  seconds automatically.

         Diff_S := Diff_M / Mili;

         Days := Long_Integer (Diff_S / Secs_In_Day);
         Seconds := Duration (Diff_S mod Secs_In_Day) + Sub_Seconds;
         Leap_Seconds := Integer (Elapsed_Leaps);

         if Negate then
720 721 722 723 724 725
            Days    := -Days;
            Seconds := -Seconds;

            if Leap_Seconds /= 0 then
               Leap_Seconds := -Leap_Seconds;
            end if;
726 727 728 729 730 731 732 733
         end if;
      end Difference;

      --------------
      -- Subtract --
      --------------

      function Subtract (Date : Time; Days : Long_Integer) return Time is
734
         pragma Unsuppress (Overflow_Check);
735
         Date_M : constant OS_Time := OS_Time (Date);
736
      begin
737
         return Time (Date_M - OS_Time (Days) * Milis_In_Day);
738 739 740 741 742 743
      exception
         when Constraint_Error =>
            raise Time_Error;
      end Subtract;
   end Arithmetic_Operations;

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
   ---------------------------
   -- Conversion_Operations --
   ---------------------------

   package body Conversion_Operations is

      Epoch_Offset : constant OS_Time := 35067168000000000;
      --  The difference between 1970-1-1 UTC and 1858-11-17 UTC expressed in
      --  100 nanoseconds.

      -----------------
      -- To_Ada_Time --
      -----------------

      function To_Ada_Time (Unix_Time : Long_Integer) return Time is
         pragma Unsuppress (Overflow_Check);
         Unix_Rep : constant OS_Time := OS_Time (Unix_Time) * Mili;
      begin
         return Time (Unix_Rep + Epoch_Offset);
      exception
         when Constraint_Error =>
            raise Time_Error;
      end To_Ada_Time;

      -----------------
      -- To_Ada_Time --
      -----------------

      function To_Ada_Time
        (tm_year  : Integer;
         tm_mon   : Integer;
         tm_day   : Integer;
         tm_hour  : Integer;
         tm_min   : Integer;
         tm_sec   : Integer;
         tm_isdst : Integer) return Time
      is
         pragma Unsuppress (Overflow_Check);

         Year_Shift  : constant Integer := 1900;
         Month_Shift : constant Integer := 1;

         Year   : Year_Number;
         Month  : Month_Number;
         Day    : Day_Number;
         Second : Integer;
         Leap   : Boolean;
         Result : OS_Time;

      begin
         --  Input processing

         Year  := Year_Number (Year_Shift + tm_year);
         Month := Month_Number (Month_Shift + tm_mon);
         Day   := Day_Number (tm_day);

         --  Step 1: Validity checks of input values

         if not Year'Valid
           or else not Month'Valid
           or else not Day'Valid
           or else tm_hour not in 0 .. 24
           or else tm_min not in 0 .. 59
           or else tm_sec not in 0 .. 60
           or else tm_isdst not in -1 .. 1
         then
            raise Time_Error;
         end if;

         --  Step 2: Potential leap second

         if tm_sec = 60 then
            Leap   := True;
            Second := 59;
         else
            Leap   := False;
            Second := tm_sec;
         end if;

         --  Step 3: Calculate the time value

         Result :=
           OS_Time
             (Formatting_Operations.Time_Of
               (Year         => Year,
                Month        => Month,
                Day          => Day,
                Day_Secs     => 0.0,      --  Time is given in h:m:s
                Hour         => tm_hour,
                Minute       => tm_min,
                Second       => Second,
                Sub_Sec      => 0.0,      --  No precise sub second given
                Leap_Sec     => Leap,
                Use_Day_Secs => False,    --  Time is given in h:m:s
                Is_Ada_05    => True,     --  Force usage of explicit time zone
                Time_Zone    => 0));      --  Place the value in UTC
         --  Step 4: Daylight Savings Time

         if tm_isdst = 1 then
            Result := Result + OS_Time (3_600) * Mili;
         end if;

         return Time (Result);
      exception
         when Constraint_Error =>
            raise Time_Error;
      end To_Ada_Time;

      -----------------
      -- To_Duration --
      -----------------

      function To_Duration
        (tv_sec  : Long_Integer;
         tv_nsec : Long_Integer) return Duration
      is
         pragma Unsuppress (Overflow_Check);
      begin
         return Duration (tv_sec) + Duration (tv_nsec) / Mili_F;
      end To_Duration;

      ------------------------
      -- To_Struct_Timespec --
      ------------------------

      procedure To_Struct_Timespec
        (D       : Duration;
         tv_sec  : out Long_Integer;
         tv_nsec : out Long_Integer)
      is
         pragma Unsuppress (Overflow_Check);
         Secs      : Duration;
         Nano_Secs : Duration;

      begin
         --  Seconds extraction, avoid potential rounding errors

         Secs   := D - 0.5;
         tv_sec := Long_Integer (Secs);

         --  100 Nanoseconds extraction

         Nano_Secs := D - Duration (tv_sec);
         tv_nsec := Long_Integer (Nano_Secs * Mili);
      end To_Struct_Timespec;

      ------------------
      -- To_Struct_Tm --
      ------------------

      procedure To_Struct_Tm
        (T       : Time;
         tm_year : out Integer;
         tm_mon  : out Integer;
         tm_day  : out Integer;
         tm_hour : out Integer;
         tm_min  : out Integer;
         tm_sec  : out Integer)
      is
         pragma Unsuppress (Overflow_Check);
         Year      : Year_Number;
         Month     : Month_Number;
         Second    : Integer;
         Day_Secs  : Day_Duration;
         Sub_Sec   : Duration;
         Leap_Sec  : Boolean;

      begin
         --  Step 1: Split the input time

         Formatting_Operations.Split
           (T, Year, Month, tm_day, Day_Secs,
            tm_hour, tm_min, Second, Sub_Sec, Leap_Sec, True, 0);

         --  Step 2: Correct the year and month

         tm_year := Year - 1900;
         tm_mon  := Month - 1;

rwild's avatar
rwild committed
923
         --  Step 3: Handle leap second occurrences
924

925
         tm_sec := (if Leap_Sec then 60 else Second);
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
      end To_Struct_Tm;

      ------------------
      -- To_Unix_Time --
      ------------------

      function To_Unix_Time (Ada_Time : Time) return Long_Integer is
         pragma Unsuppress (Overflow_Check);
         Ada_OS_Time : constant OS_Time := OS_Time (Ada_Time);
      begin
         return Long_Integer ((Ada_OS_Time - Epoch_Offset) / Mili);
      exception
         when Constraint_Error =>
            raise Time_Error;
      end To_Unix_Time;
   end Conversion_Operations;

943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
   ---------------------------
   -- Formatting_Operations --
   ---------------------------

   package body Formatting_Operations is

      -----------------
      -- Day_Of_Week --
      -----------------

      function Day_Of_Week (Date : Time) return Integer is
         Y : Year_Number;
         M : Month_Number;
         D : Day_Number;
         S : Day_Duration;

         Day_Count     : Long_Integer;
         Midday_Date_S : Time;

      begin
         Split (Date, Y, M, D, S);

         --  Build a time value in the middle of the same day and convert the
         --  time value to seconds.

         Midday_Date_S := Time_Of (Y, M, D, 43_200.0) / Mili;

         --  Count the number of days since the start of VMS time. 1858-11-17
         --  was a Wednesday.

         Day_Count := Long_Integer (Midday_Date_S / Secs_In_Day) + 2;

         return Integer (Day_Count mod 7);
      end Day_Of_Week;

      -----------
      -- Split --
      -----------

      procedure Split
983 984 985 986 987 988 989 990 991 992 993 994
        (Date      : Time;
         Year      : out Year_Number;
         Month     : out Month_Number;
         Day       : out Day_Number;
         Day_Secs  : out Day_Duration;
         Hour      : out Integer;
         Minute    : out Integer;
         Second    : out Integer;
         Sub_Sec   : out Duration;
         Leap_Sec  : out Boolean;
         Is_Ada_05 : Boolean;
         Time_Zone : Long_Integer)
995
      is
996 997 998 999
         --  The flag Is_Ada_05 is present for interfacing purposes

         pragma Unreferenced (Is_Ada_05);

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
         procedure Numtim
           (Status : out Unsigned_Longword;
            Timbuf : out Unsigned_Word_Array;
            Timadr : Time);

         pragma Interface (External, Numtim);

         pragma Import_Valued_Procedure
           (Numtim, "SYS$NUMTIM",
           (Unsigned_Longword, Unsigned_Word_Array, Time),
           (Value, Reference, Reference));

         Status : Unsigned_Longword;
         Timbuf : Unsigned_Word_Array (1 .. 7);

         Ada_Min_Year : constant := 1901;
         Ada_Max_Year : constant := 2399;

1018
         Date_M        : OS_Time;
1019
         Elapsed_Leaps : Natural;
1020
         Next_Leap_M   : OS_Time;
1021 1022

      begin
1023
         Date_M := OS_Time (Date);
1024 1025 1026

         --  Step 1: Leap seconds processing

1027 1028
         if Leap_Support then
            Cumulative_Leap_Seconds
1029
              (Start_Of_Time, Date_M, Elapsed_Leaps, Next_Leap_M);
1030 1031 1032 1033 1034 1035

            Leap_Sec := Date_M >= Next_Leap_M;

            if Leap_Sec then
               Elapsed_Leaps := Elapsed_Leaps + 1;
            end if;
1036

1037
         --  The target does not support leap seconds
1038

1039 1040 1041
         else
            Elapsed_Leaps := 0;
            Leap_Sec      := False;
1042 1043
         end if;

1044
         Date_M := Date_M - OS_Time (Elapsed_Leaps) * Mili;
1045

1046 1047 1048
         --  Step 2: Time zone processing

         if Time_Zone /= 0 then
1049
            Date_M := Date_M + OS_Time (Time_Zone) * 60 * Mili;
1050 1051 1052 1053 1054
         end if;

         --  After the leap seconds and time zone have been accounted for,
         --  the date should be within the bounds of Ada time.

1055 1056
         if Date_M < Ada_Low
           or else Date_M > Ada_High
1057 1058 1059 1060 1061 1062
         then
            raise Time_Error;
         end if;

         --  Step 3: Sub second processing

1063
         Sub_Sec := Duration (Date_M mod Mili) / Mili_F;
1064 1065 1066

         --  Drop the sub seconds

1067
         Date_M := Date_M - (Date_M mod Mili);
1068 1069 1070

         --  Step 4: VMS system call

1071
         Numtim (Status, Timbuf, Time (Date_M));
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

         if Status mod 2 /= 1
           or else Timbuf (1) not in Ada_Min_Year .. Ada_Max_Year
         then
            raise Time_Error;
         end if;

         --  Step 5: Time components processing

         Year   := Year_Number (Timbuf (1));
         Month  := Month_Number (Timbuf (2));
         Day    := Day_Number (Timbuf (3));
         Hour   := Integer (Timbuf (4));
         Minute := Integer (Timbuf (5));
         Second := Integer (Timbuf (6));
1087

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
         Day_Secs := Day_Duration (Hour   * 3_600) +
                     Day_Duration (Minute *    60) +
                     Day_Duration (Second)         +
                                   Sub_Sec;
      end Split;

      -------------
      -- Time_Of --
      -------------

      function Time_Of
        (Year         : Year_Number;
         Month        : Month_Number;
         Day          : Day_Number;
         Day_Secs     : Day_Duration;
         Hour         : Integer;
         Minute       : Integer;
         Second       : Integer;
         Sub_Sec      : Duration;
1107 1108 1109 1110
         Leap_Sec     : Boolean := False;
         Use_Day_Secs : Boolean := False;
         Is_Ada_05    : Boolean := False;
         Time_Zone    : Long_Integer := 0) return Time
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
      is
         procedure Cvt_Vectim
           (Status         : out Unsigned_Longword;
            Input_Time     : Unsigned_Word_Array;
            Resultant_Time : out Time);

         pragma Interface (External, Cvt_Vectim);

         pragma Import_Valued_Procedure
           (Cvt_Vectim, "LIB$CVT_VECTIM",
           (Unsigned_Longword, Unsigned_Word_Array, Time),
           (Value, Reference, Reference));

         Status : Unsigned_Longword;
         Timbuf : Unsigned_Word_Array (1 .. 7);

1127 1128 1129 1130 1131 1132 1133
         Y  : Year_Number  := Year;
         Mo : Month_Number := Month;
         D  : Day_Number   := Day;
         H  : Integer      := Hour;
         Mi : Integer      := Minute;
         Se : Integer      := Second;
         Su : Duration     := Sub_Sec;
1134

1135 1136
         Elapsed_Leaps : Natural;
         Int_Day_Secs  : Integer;
1137 1138 1139 1140
         Next_Leap_M   : OS_Time;
         Res           : Time;
         Res_M         : OS_Time;
         Rounded_Res_M : OS_Time;
1141 1142

      begin
1143 1144 1145 1146 1147 1148 1149
         --  No validity checks are performed on the input values since it is
         --  assumed that the called has already performed them.

         --  Step 1: Hour, minute, second and sub second processing

         if Use_Day_Secs then

1150
            --  A day seconds value of 86_400 designates a new day
1151 1152

            if Day_Secs = 86_400.0 then
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
               declare
                  Adj_Year  : Year_Number := Year;
                  Adj_Month : Month_Number := Month;
                  Adj_Day   : Day_Number   := Day;

               begin
                  if Day < Days_In_Month (Month)
                    or else (Month = 2
                               and then Is_Leap (Year))
                  then
                     Adj_Day := Day + 1;

                  --  The day adjustment moves the date to a new month

                  else
                     Adj_Day := 1;

                     if Month < 12 then
                        Adj_Month := Month + 1;

                     --  The month adjustment moves the date to a new year

                     else
                        Adj_Month := 1;
                        Adj_Year  := Year + 1;
                     end if;
                  end if;

                  Y  := Adj_Year;
                  Mo := Adj_Month;
                  D  := Adj_Day;
                  H  := 0;
                  Mi := 0;
                  Se := 0;
                  Su := 0.0;
               end;

            --  Normal case (not exactly one day)
1191 1192 1193 1194

            else
               --  Sub second extraction

1195 1196 1197 1198
               Int_Day_Secs :=
                 (if Day_Secs > 0.0
                  then Integer (Day_Secs - 0.5)
                  else Integer (Day_Secs));
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

               H  := Int_Day_Secs / 3_600;
               Mi := (Int_Day_Secs / 60) mod 60;
               Se := Int_Day_Secs mod 60;
               Su := Day_Secs - Duration (Int_Day_Secs);
            end if;
         end if;

         --  Step 2: System call to VMS

1209 1210 1211
         Timbuf (1) := Unsigned_Word (Y);
         Timbuf (2) := Unsigned_Word (Mo);
         Timbuf (3) := Unsigned_Word (D);
1212 1213 1214 1215 1216
         Timbuf (4) := Unsigned_Word (H);
         Timbuf (5) := Unsigned_Word (Mi);
         Timbuf (6) := Unsigned_Word (Se);
         Timbuf (7) := 0;

1217
         Cvt_Vectim (Status, Timbuf, Res);
1218 1219 1220 1221 1222

         if Status mod 2 /= 1 then
            raise Time_Error;
         end if;

1223
         --  Step 3: Sub second adjustment
1224

1225
         Res_M := OS_Time (Res) + OS_Time (Su * Mili_F);
1226

1227
         --  Step 4: Bounds check
1228

1229
         Check_Within_Time_Bounds (Res_M);
1230 1231 1232 1233

         --  Step 5: Time zone processing

         if Time_Zone /= 0 then
1234
            Res_M := Res_M - OS_Time (Time_Zone) * 60 * Mili;
1235 1236 1237
         end if;

         --  Step 6: Leap seconds processing
1238

1239 1240 1241
         if Leap_Support then
            Cumulative_Leap_Seconds
              (Start_Of_Time, Res_M, Elapsed_Leaps, Next_Leap_M);
1242

1243
            Res_M := Res_M + OS_Time (Elapsed_Leaps) * Mili;
1244

1245 1246
            --  An Ada 2005 caller requesting an explicit leap second or an
            --  Ada 95 caller accounting for an invisible leap second.
1247

1248 1249 1250
            if Leap_Sec
              or else Res_M >= Next_Leap_M
            then
1251
               Res_M := Res_M + OS_Time (1) * Mili;
1252
            end if;
1253

1254
            --  Leap second validity check
1255

1256
            Rounded_Res_M := Res_M - (Res_M mod Mili);
1257

1258 1259 1260 1261 1262 1263
            if Is_Ada_05
              and then Leap_Sec
              and then Rounded_Res_M /= Next_Leap_M
            then
               raise Time_Error;
            end if;
1264 1265
         end if;

1266
         return Time (Res_M);
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
      end Time_Of;
   end Formatting_Operations;

   ---------------------------
   -- Time_Zones_Operations --
   ---------------------------

   package body Time_Zones_Operations is

      ---------------------
      -- UTC_Time_Offset --
      ---------------------

      function UTC_Time_Offset (Date : Time) return Long_Integer is
         --  Formal parameter Date is here for interfacing, but is never
         --  actually used.

         pragma Unreferenced (Date);

         function get_gmtoff return Long_Integer;
         pragma Import (C, get_gmtoff, "get_gmtoff");

      begin
         --  VMS is not capable of determining the time zone in some past or
         --  future point in time denoted by Date, thus the current time zone
         --  is retrieved.

         return get_gmtoff;
      end UTC_Time_Offset;
   end Time_Zones_Operations;
kenner's avatar
kenner committed
1297
end Ada.Calendar;