OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [ada/] [a-calend.adb] - Blame information for rev 706

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 706 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT RUN-TIME COMPONENTS                         --
4
--                                                                          --
5
--                         A D A . C A L E N D A R                          --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
10
--                                                                          --
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.                                     --
17
--                                                                          --
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.               --
21
--                                                                          --
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/>.                                          --
26
--                                                                          --
27
-- GNAT was originally developed  by the GNAT team at  New York University. --
28
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29
--                                                                          --
30
------------------------------------------------------------------------------
31
 
32
with Ada.Unchecked_Conversion;
33
 
34
with Interfaces.C;
35
 
36
with System.OS_Primitives;
37
 
38
package body Ada.Calendar is
39
 
40
   --------------------------
41
   -- Implementation Notes --
42
   --------------------------
43
 
44
   --  In complex algorithms, some variables of type Ada.Calendar.Time carry
45
   --  suffix _S or _N to denote units of seconds or nanoseconds.
46
   --
47
   --  Because time is measured in different units and from different origins
48
   --  on various targets, a system independent model is incorporated into
49
   --  Ada.Calendar. The idea behind the design is to encapsulate all target
50
   --  dependent machinery in a single package, thus providing a uniform
51
   --  interface to all existing and any potential children.
52
 
53
   --     package Ada.Calendar
54
   --        procedure Split (5 parameters) -------+
55
   --                                              | Call from local routine
56
   --     private                                  |
57
   --        package Formatting_Operations         |
58
   --           procedure Split (11 parameters) <--+
59
   --        end Formatting_Operations             |
60
   --     end Ada.Calendar                         |
61
   --                                              |
62
   --     package Ada.Calendar.Formatting          | Call from child routine
63
   --        procedure Split (9 or 10 parameters) -+
64
   --     end Ada.Calendar.Formatting
65
 
66
   --  The behaviour of the interfacing routines is controlled via various
67
   --  flags. All new Ada 2005 types from children of Ada.Calendar are
68
   --  emulated by a similar type. For instance, type Day_Number is replaced
69
   --  by Integer in various routines. One ramification of this model is that
70
   --  the caller site must perform validity checks on returned results.
71
   --  The end result of this model is the lack of target specific files per
72
   --  child of Ada.Calendar (a-calfor, a-calfor-vms, a-calfor-vxwors, etc).
73
 
74
   -----------------------
75
   -- Local Subprograms --
76
   -----------------------
77
 
78
   procedure Check_Within_Time_Bounds (T : Time_Rep);
79
   --  Ensure that a time representation value falls withing the bounds of Ada
80
   --  time. Leap seconds support is taken into account.
81
 
82
   procedure Cumulative_Leap_Seconds
83
     (Start_Date    : Time_Rep;
84
      End_Date      : Time_Rep;
85
      Elapsed_Leaps : out Natural;
86
      Next_Leap     : out Time_Rep);
87
   --  Elapsed_Leaps is the sum of the leap seconds that have occurred on or
88
   --  after Start_Date and before (strictly before) End_Date. Next_Leap_Sec
89
   --  represents the next leap second occurrence on or after End_Date. If
90
   --  there are no leaps seconds after End_Date, End_Of_Time is returned.
91
   --  End_Of_Time can be used as End_Date to count all the leap seconds that
92
   --  have occurred on or after Start_Date.
93
   --
94
   --  Note: Any sub seconds of Start_Date and End_Date are discarded before
95
   --  the calculations are done. For instance: if 113 seconds is a leap
96
   --  second (it isn't) and 113.5 is input as an End_Date, the leap second
97
   --  at 113 will not be counted in Leaps_Between, but it will be returned
98
   --  as Next_Leap_Sec. Thus, if the caller wants to know if the End_Date is
99
   --  a leap second, the comparison should be:
100
   --
101
   --     End_Date >= Next_Leap_Sec;
102
   --
103
   --  After_Last_Leap is designed so that this comparison works without
104
   --  having to first check if Next_Leap_Sec is a valid leap second.
105
 
106
   function Duration_To_Time_Rep is
107
     new Ada.Unchecked_Conversion (Duration, Time_Rep);
108
   --  Convert a duration value into a time representation value
109
 
110
   function Time_Rep_To_Duration is
111
     new Ada.Unchecked_Conversion (Time_Rep, Duration);
112
   --  Convert a time representation value into a duration value
113
 
114
   function UTC_Time_Offset
115
     (Date        : Time;
116
      Is_Historic : Boolean) return Long_Integer;
117
   --  This routine acts as an Ada wrapper around __gnat_localtime_tzoff which
118
   --  in turn utilizes various OS-dependent mechanisms to calculate the time
119
   --  zone offset of a date. Formal parameter Date represents an arbitrary
120
   --  time stamp, either in the past, now, or in the future. If the flag
121
   --  Is_Historic is set, this routine would try to calculate to the best of
122
   --  the OS's abilities the time zone offset that was or will be in effect
123
   --  on Date. If the flag is set to False, the routine returns the current
124
   --  time zone with Date effectively set to Clock.
125
   --
126
   --  NOTE: Targets which support localtime_r will aways return a historic
127
   --  time zone even if flag Is_Historic is set to False because this is how
128
   --  localtime_r operates.
129
 
130
   -----------------
131
   -- Local Types --
132
   -----------------
133
 
134
   --  An integer time duration. The type is used whenever a positive elapsed
135
   --  duration is needed, for instance when splitting a time value. Here is
136
   --  how Time_Rep and Time_Dur are related:
137
 
138
   --            'First  Ada_Low                  Ada_High  'Last
139
   --  Time_Rep: +-------+------------------------+---------+
140
   --  Time_Dur:         +------------------------+---------+
141
   --                    0                                  'Last
142
 
143
   type Time_Dur is range 0 .. 2 ** 63 - 1;
144
 
145
   --------------------------
146
   -- Leap seconds control --
147
   --------------------------
148
 
149
   Flag : Integer;
150
   pragma Import (C, Flag, "__gl_leap_seconds_support");
151
   --  This imported value is used to determine whether the compilation had
152
   --  binder flag "-y" present which enables leap seconds. A value of zero
153
   --  signifies no leap seconds support while a value of one enables support.
154
 
155
   Leap_Support : constant Boolean := (Flag = 1);
156
   --  Flag to controls the usage of leap seconds in all Ada.Calendar routines
157
 
158
   Leap_Seconds_Count : constant Natural := 25;
159
 
160
   ---------------------
161
   -- Local Constants --
162
   ---------------------
163
 
164
   Ada_Min_Year          : constant Year_Number := Year_Number'First;
165
   Secs_In_Four_Years    : constant := (3 * 365 + 366) * Secs_In_Day;
166
   Secs_In_Non_Leap_Year : constant := 365 * Secs_In_Day;
167
   Nanos_In_Four_Years   : constant := Secs_In_Four_Years * Nano;
168
 
169
   --  Lower and upper bound of Ada time. The zero (0) value of type Time is
170
   --  positioned at year 2150. Note that the lower and upper bound account
171
   --  for the non-leap centennial years.
172
 
173
   Ada_Low  : constant Time_Rep := -(61 * 366 + 188 * 365) * Nanos_In_Day;
174
   Ada_High : constant Time_Rep :=  (60 * 366 + 190 * 365) * Nanos_In_Day;
175
 
176
   --  Even though the upper bound of time is 2399-12-31 23:59:59.999999999
177
   --  UTC, it must be increased to include all leap seconds.
178
 
179
   Ada_High_And_Leaps : constant Time_Rep :=
180
                          Ada_High + Time_Rep (Leap_Seconds_Count) * Nano;
181
 
182
   --  Two constants used in the calculations of elapsed leap seconds.
183
   --  End_Of_Time is later than Ada_High in time zone -28. Start_Of_Time
184
   --  is earlier than Ada_Low in time zone +28.
185
 
186
   End_Of_Time   : constant Time_Rep :=
187
                     Ada_High + Time_Rep (3) * Nanos_In_Day;
188
   Start_Of_Time : constant Time_Rep :=
189
                     Ada_Low - Time_Rep (3) * Nanos_In_Day;
190
 
191
   --  The Unix lower time bound expressed as nanoseconds since the start of
192
   --  Ada time in UTC.
193
 
194
   Unix_Min : constant Time_Rep :=
195
                Ada_Low + Time_Rep (17 * 366 + 52 * 365) * Nanos_In_Day;
196
 
197
   --  The Unix upper time bound expressed as nanoseconds since the start of
198
   --  Ada time in UTC.
199
 
200
   Unix_Max : constant Time_Rep :=
201
                Ada_Low + Time_Rep (34 * 366 + 102 * 365) * Nanos_In_Day +
202
                          Time_Rep (Leap_Seconds_Count) * Nano;
203
 
204
   Epoch_Offset : constant Time_Rep := (136 * 365 + 44 * 366) * Nanos_In_Day;
205
   --  The difference between 2150-1-1 UTC and 1970-1-1 UTC expressed in
206
   --  nanoseconds. Note that year 2100 is non-leap.
207
 
208
   Cumulative_Days_Before_Month :
209
     constant array (Month_Number) of Natural :=
210
       (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
211
 
212
   --  The following table contains the hard time values of all existing leap
213
   --  seconds. The values are produced by the utility program xleaps.adb. This
214
   --  must be updated when additional leap second times are defined.
215
 
216
   Leap_Second_Times : constant array (1 .. Leap_Seconds_Count) of Time_Rep :=
217
     (-5601484800000000000,
218
      -5585587199000000000,
219
      -5554051198000000000,
220
      -5522515197000000000,
221
      -5490979196000000000,
222
      -5459356795000000000,
223
      -5427820794000000000,
224
      -5396284793000000000,
225
      -5364748792000000000,
226
      -5317487991000000000,
227
      -5285951990000000000,
228
      -5254415989000000000,
229
      -5191257588000000000,
230
      -5112287987000000000,
231
      -5049129586000000000,
232
      -5017593585000000000,
233
      -4970332784000000000,
234
      -4938796783000000000,
235
      -4907260782000000000,
236
      -4859827181000000000,
237
      -4812566380000000000,
238
      -4765132779000000000,
239
      -4544207978000000000,
240
      -4449513577000000000,
241
      -4339180776000000000);
242
 
243
   ---------
244
   -- "+" --
245
   ---------
246
 
247
   function "+" (Left : Time; Right : Duration) return Time is
248
      pragma Unsuppress (Overflow_Check);
249
      Left_N : constant Time_Rep := Time_Rep (Left);
250
   begin
251
      return Time (Left_N + Duration_To_Time_Rep (Right));
252
   exception
253
      when Constraint_Error =>
254
         raise Time_Error;
255
   end "+";
256
 
257
   function "+" (Left : Duration; Right : Time) return Time is
258
   begin
259
      return Right + Left;
260
   end "+";
261
 
262
   ---------
263
   -- "-" --
264
   ---------
265
 
266
   function "-" (Left : Time; Right : Duration) return Time is
267
      pragma Unsuppress (Overflow_Check);
268
      Left_N : constant Time_Rep := Time_Rep (Left);
269
   begin
270
      return Time (Left_N - Duration_To_Time_Rep (Right));
271
   exception
272
      when Constraint_Error =>
273
         raise Time_Error;
274
   end "-";
275
 
276
   function "-" (Left : Time; Right : Time) return Duration is
277
      pragma Unsuppress (Overflow_Check);
278
 
279
      Dur_Low  : constant Time_Rep := Duration_To_Time_Rep (Duration'First);
280
      Dur_High : constant Time_Rep := Duration_To_Time_Rep (Duration'Last);
281
      --  The bounds of type Duration expressed as time representations
282
 
283
      Res_N : Time_Rep;
284
 
285
   begin
286
      Res_N := Time_Rep (Left) - Time_Rep (Right);
287
 
288
      --  Due to the extended range of Ada time, "-" is capable of producing
289
      --  results which may exceed the range of Duration. In order to prevent
290
      --  the generation of bogus values by the Unchecked_Conversion, we apply
291
      --  the following check.
292
 
293
      if Res_N < Dur_Low or else Res_N > Dur_High then
294
         raise Time_Error;
295
      end if;
296
 
297
      return Time_Rep_To_Duration (Res_N);
298
 
299
   exception
300
      when Constraint_Error =>
301
         raise Time_Error;
302
   end "-";
303
 
304
   ---------
305
   -- "<" --
306
   ---------
307
 
308
   function "<" (Left, Right : Time) return Boolean is
309
   begin
310
      return Time_Rep (Left) < Time_Rep (Right);
311
   end "<";
312
 
313
   ----------
314
   -- "<=" --
315
   ----------
316
 
317
   function "<=" (Left, Right : Time) return Boolean is
318
   begin
319
      return Time_Rep (Left) <= Time_Rep (Right);
320
   end "<=";
321
 
322
   ---------
323
   -- ">" --
324
   ---------
325
 
326
   function ">" (Left, Right : Time) return Boolean is
327
   begin
328
      return Time_Rep (Left) > Time_Rep (Right);
329
   end ">";
330
 
331
   ----------
332
   -- ">=" --
333
   ----------
334
 
335
   function ">=" (Left, Right : Time) return Boolean is
336
   begin
337
      return Time_Rep (Left) >= Time_Rep (Right);
338
   end ">=";
339
 
340
   ------------------------------
341
   -- Check_Within_Time_Bounds --
342
   ------------------------------
343
 
344
   procedure Check_Within_Time_Bounds (T : Time_Rep) is
345
   begin
346
      if Leap_Support then
347
         if T < Ada_Low or else T > Ada_High_And_Leaps then
348
            raise Time_Error;
349
         end if;
350
      else
351
         if T < Ada_Low or else T > Ada_High then
352
            raise Time_Error;
353
         end if;
354
      end if;
355
   end Check_Within_Time_Bounds;
356
 
357
   -----------
358
   -- Clock --
359
   -----------
360
 
361
   function Clock return Time is
362
      Elapsed_Leaps : Natural;
363
      Next_Leap_N   : Time_Rep;
364
 
365
      --  The system clock returns the time in UTC since the Unix Epoch of
366
      --  1970-01-01 00:00:00.0. We perform an origin shift to the Ada Epoch
367
      --  by adding the number of nanoseconds between the two origins.
368
 
369
      Res_N : Time_Rep :=
370
                Duration_To_Time_Rep (System.OS_Primitives.Clock) + Unix_Min;
371
 
372
   begin
373
      --  If the target supports leap seconds, determine the number of leap
374
      --  seconds elapsed until this moment.
375
 
376
      if Leap_Support then
377
         Cumulative_Leap_Seconds
378
           (Start_Of_Time, Res_N, Elapsed_Leaps, Next_Leap_N);
379
 
380
         --  The system clock may fall exactly on a leap second
381
 
382
         if Res_N >= Next_Leap_N then
383
            Elapsed_Leaps := Elapsed_Leaps + 1;
384
         end if;
385
 
386
      --  The target does not support leap seconds
387
 
388
      else
389
         Elapsed_Leaps := 0;
390
      end if;
391
 
392
      Res_N := Res_N + Time_Rep (Elapsed_Leaps) * Nano;
393
 
394
      return Time (Res_N);
395
   end Clock;
396
 
397
   -----------------------------
398
   -- Cumulative_Leap_Seconds --
399
   -----------------------------
400
 
401
   procedure Cumulative_Leap_Seconds
402
     (Start_Date    : Time_Rep;
403
      End_Date      : Time_Rep;
404
      Elapsed_Leaps : out Natural;
405
      Next_Leap     : out Time_Rep)
406
   is
407
      End_Index   : Positive;
408
      End_T       : Time_Rep := End_Date;
409
      Start_Index : Positive;
410
      Start_T     : Time_Rep := Start_Date;
411
 
412
   begin
413
      --  Both input dates must be normalized to UTC
414
 
415
      pragma Assert (Leap_Support and then End_Date >= Start_Date);
416
 
417
      Next_Leap := End_Of_Time;
418
 
419
      --  Make sure that the end date does not exceed the upper bound
420
      --  of Ada time.
421
 
422
      if End_Date > Ada_High then
423
         End_T := Ada_High;
424
      end if;
425
 
426
      --  Remove the sub seconds from both dates
427
 
428
      Start_T := Start_T - (Start_T mod Nano);
429
      End_T   := End_T   - (End_T   mod Nano);
430
 
431
      --  Some trivial cases:
432
      --                     Leap 1 . . . Leap N
433
      --  ---+========+------+############+-------+========+-----
434
      --     Start_T  End_T                       Start_T  End_T
435
 
436
      if End_T < Leap_Second_Times (1) then
437
         Elapsed_Leaps := 0;
438
         Next_Leap     := Leap_Second_Times (1);
439
         return;
440
 
441
      elsif Start_T > Leap_Second_Times (Leap_Seconds_Count) then
442
         Elapsed_Leaps := 0;
443
         Next_Leap     := End_Of_Time;
444
         return;
445
      end if;
446
 
447
      --  Perform the calculations only if the start date is within the leap
448
      --  second occurrences table.
449
 
450
      if Start_T <= Leap_Second_Times (Leap_Seconds_Count) then
451
 
452
         --    1    2                  N - 1   N
453
         --  +----+----+--  . . .  --+-------+---+
454
         --  | T1 | T2 |             | N - 1 | N |
455
         --  +----+----+--  . . .  --+-------+---+
456
         --         ^                   ^
457
         --         | Start_Index       | End_Index
458
         --         +-------------------+
459
         --             Leaps_Between
460
 
461
         --  The idea behind the algorithm is to iterate and find two
462
         --  closest dates which are after Start_T and End_T. Their
463
         --  corresponding index difference denotes the number of leap
464
         --  seconds elapsed.
465
 
466
         Start_Index := 1;
467
         loop
468
            exit when Leap_Second_Times (Start_Index) >= Start_T;
469
            Start_Index := Start_Index + 1;
470
         end loop;
471
 
472
         End_Index := Start_Index;
473
         loop
474
            exit when End_Index > Leap_Seconds_Count
475
              or else Leap_Second_Times (End_Index) >= End_T;
476
            End_Index := End_Index + 1;
477
         end loop;
478
 
479
         if End_Index <= Leap_Seconds_Count then
480
            Next_Leap := Leap_Second_Times (End_Index);
481
         end if;
482
 
483
         Elapsed_Leaps := End_Index - Start_Index;
484
 
485
      else
486
         Elapsed_Leaps := 0;
487
      end if;
488
   end Cumulative_Leap_Seconds;
489
 
490
   ---------
491
   -- Day --
492
   ---------
493
 
494
   function Day (Date : Time) return Day_Number is
495
      D : Day_Number;
496
      Y : Year_Number;
497
      M : Month_Number;
498
      S : Day_Duration;
499
      pragma Unreferenced (Y, M, S);
500
   begin
501
      Split (Date, Y, M, D, S);
502
      return D;
503
   end Day;
504
 
505
   -------------
506
   -- Is_Leap --
507
   -------------
508
 
509
   function Is_Leap (Year : Year_Number) return Boolean is
510
   begin
511
      --  Leap centennial years
512
 
513
      if Year mod 400 = 0 then
514
         return True;
515
 
516
      --  Non-leap centennial years
517
 
518
      elsif Year mod 100 = 0 then
519
         return False;
520
 
521
      --  Regular years
522
 
523
      else
524
         return Year mod 4 = 0;
525
      end if;
526
   end Is_Leap;
527
 
528
   -----------
529
   -- Month --
530
   -----------
531
 
532
   function Month (Date : Time) return Month_Number is
533
      Y : Year_Number;
534
      M : Month_Number;
535
      D : Day_Number;
536
      S : Day_Duration;
537
      pragma Unreferenced (Y, D, S);
538
   begin
539
      Split (Date, Y, M, D, S);
540
      return M;
541
   end Month;
542
 
543
   -------------
544
   -- Seconds --
545
   -------------
546
 
547
   function Seconds (Date : Time) return Day_Duration is
548
      Y : Year_Number;
549
      M : Month_Number;
550
      D : Day_Number;
551
      S : Day_Duration;
552
      pragma Unreferenced (Y, M, D);
553
   begin
554
      Split (Date, Y, M, D, S);
555
      return S;
556
   end Seconds;
557
 
558
   -----------
559
   -- Split --
560
   -----------
561
 
562
   procedure Split
563
     (Date    : Time;
564
      Year    : out Year_Number;
565
      Month   : out Month_Number;
566
      Day     : out Day_Number;
567
      Seconds : out Day_Duration)
568
   is
569
      H  : Integer;
570
      M  : Integer;
571
      Se : Integer;
572
      Ss : Duration;
573
      Le : Boolean;
574
 
575
      pragma Unreferenced (H, M, Se, Ss, Le);
576
 
577
   begin
578
      --  Even though the input time zone is UTC (0), the flag Is_Ada_05 will
579
      --  ensure that Split picks up the local time zone.
580
 
581
      Formatting_Operations.Split
582
        (Date      => Date,
583
         Year      => Year,
584
         Month     => Month,
585
         Day       => Day,
586
         Day_Secs  => Seconds,
587
         Hour      => H,
588
         Minute    => M,
589
         Second    => Se,
590
         Sub_Sec   => Ss,
591
         Leap_Sec  => Le,
592
         Is_Ada_05 => False,
593
         Time_Zone => 0);
594
 
595
      --  Validity checks
596
 
597
      if not Year'Valid    or else
598
         not Month'Valid   or else
599
         not Day'Valid     or else
600
         not Seconds'Valid
601
      then
602
         raise Time_Error;
603
      end if;
604
   end Split;
605
 
606
   -------------
607
   -- Time_Of --
608
   -------------
609
 
610
   function Time_Of
611
     (Year    : Year_Number;
612
      Month   : Month_Number;
613
      Day     : Day_Number;
614
      Seconds : Day_Duration := 0.0) return Time
615
   is
616
      --  The values in the following constants are irrelevant, they are just
617
      --  placeholders; the choice of constructing a Day_Duration value is
618
      --  controlled by the Use_Day_Secs flag.
619
 
620
      H  : constant Integer := 1;
621
      M  : constant Integer := 1;
622
      Se : constant Integer := 1;
623
      Ss : constant Duration := 0.1;
624
 
625
   begin
626
      --  Validity checks
627
 
628
      if not Year'Valid    or else
629
         not Month'Valid   or else
630
         not Day'Valid     or else
631
         not Seconds'Valid
632
      then
633
         raise Time_Error;
634
      end if;
635
 
636
      --  Even though the input time zone is UTC (0), the flag Is_Ada_05 will
637
      --  ensure that Split picks up the local time zone.
638
 
639
      return
640
        Formatting_Operations.Time_Of
641
          (Year         => Year,
642
           Month        => Month,
643
           Day          => Day,
644
           Day_Secs     => Seconds,
645
           Hour         => H,
646
           Minute       => M,
647
           Second       => Se,
648
           Sub_Sec      => Ss,
649
           Leap_Sec     => False,
650
           Use_Day_Secs => True,
651
           Is_Ada_05    => False,
652
           Time_Zone    => 0);
653
   end Time_Of;
654
 
655
   ---------------------
656
   -- UTC_Time_Offset --
657
   ---------------------
658
 
659
   function UTC_Time_Offset
660
     (Date        : Time;
661
      Is_Historic : Boolean) return Long_Integer
662
   is
663
      --  The following constants denote February 28 during non-leap centennial
664
      --  years, the units are nanoseconds.
665
 
666
      T_2100_2_28 : constant Time_Rep := Ada_Low +
667
                      (Time_Rep (49 * 366 + 150 * 365 + 59) * Secs_In_Day +
668
                       Time_Rep (Leap_Seconds_Count)) * Nano;
669
 
670
      T_2200_2_28 : constant Time_Rep := Ada_Low +
671
                      (Time_Rep (73 * 366 + 226 * 365 + 59) * Secs_In_Day +
672
                       Time_Rep (Leap_Seconds_Count)) * Nano;
673
 
674
      T_2300_2_28 : constant Time_Rep := Ada_Low +
675
                      (Time_Rep (97 * 366 + 302 * 365 + 59) * Secs_In_Day +
676
                       Time_Rep (Leap_Seconds_Count)) * Nano;
677
 
678
      --  56 years (14 leap years + 42 non-leap years) in nanoseconds:
679
 
680
      Nanos_In_56_Years : constant := (14 * 366 + 42 * 365) * Nanos_In_Day;
681
 
682
      type int_Pointer  is access all Interfaces.C.int;
683
      type long_Pointer is access all Interfaces.C.long;
684
 
685
      type time_t is
686
        range -(2 ** (Standard'Address_Size - Integer'(1))) ..
687
              +(2 ** (Standard'Address_Size - Integer'(1)) - 1);
688
      type time_t_Pointer is access all time_t;
689
 
690
      procedure localtime_tzoff
691
        (timer       : time_t_Pointer;
692
         is_historic : int_Pointer;
693
         off         : long_Pointer);
694
      pragma Import (C, localtime_tzoff, "__gnat_localtime_tzoff");
695
      --  This routine is a interfacing wrapper around the library function
696
      --  __gnat_localtime_tzoff. Parameter 'timer' represents a Unix-based
697
      --  time equivalent of the input date. If flag 'is_historic' is set, this
698
      --  routine would try to calculate to the best of the OS's abilities the
699
      --  time zone offset that was or will be in effect on 'timer'. If the
700
      --  flag is set to False, the routine returns the current time zone
701
      --  regardless of what 'timer' designates. Parameter 'off' captures the
702
      --  UTC offset of 'timer'.
703
 
704
      Adj_Cent : Integer;
705
      Date_N   : Time_Rep;
706
      Flag     : aliased Interfaces.C.int;
707
      Offset   : aliased Interfaces.C.long;
708
      Secs_T   : aliased time_t;
709
 
710
   --  Start of processing for UTC_Time_Offset
711
 
712
   begin
713
      Date_N := Time_Rep (Date);
714
 
715
      --  Dates which are 56 years apart fall on the same day, day light saving
716
      --  and so on. Non-leap centennial years violate this rule by one day and
717
      --  as a consequence, special adjustment is needed.
718
 
719
      Adj_Cent :=
720
        (if    Date_N <= T_2100_2_28 then 0
721
         elsif Date_N <= T_2200_2_28 then 1
722
         elsif Date_N <= T_2300_2_28 then 2
723
         else                             3);
724
 
725
      if Adj_Cent > 0 then
726
         Date_N := Date_N - Time_Rep (Adj_Cent) * Nanos_In_Day;
727
      end if;
728
 
729
      --  Shift the date within bounds of Unix time
730
 
731
      while Date_N < Unix_Min loop
732
         Date_N := Date_N + Nanos_In_56_Years;
733
      end loop;
734
 
735
      while Date_N >= Unix_Max loop
736
         Date_N := Date_N - Nanos_In_56_Years;
737
      end loop;
738
 
739
      --  Perform a shift in origins from Ada to Unix
740
 
741
      Date_N := Date_N - Unix_Min;
742
 
743
      --  Convert the date into seconds
744
 
745
      Secs_T := time_t (Date_N / Nano);
746
 
747
      --  Determine whether to treat the input date as historical or not
748
 
749
      Flag := (if Is_Historic then 1 else 0);
750
 
751
      localtime_tzoff
752
        (Secs_T'Unchecked_Access,
753
         Flag'Unchecked_Access,
754
         Offset'Unchecked_Access);
755
 
756
      return Long_Integer (Offset);
757
   end UTC_Time_Offset;
758
 
759
   ----------
760
   -- Year --
761
   ----------
762
 
763
   function Year (Date : Time) return Year_Number is
764
      Y : Year_Number;
765
      M : Month_Number;
766
      D : Day_Number;
767
      S : Day_Duration;
768
      pragma Unreferenced (M, D, S);
769
   begin
770
      Split (Date, Y, M, D, S);
771
      return Y;
772
   end Year;
773
 
774
   --  The following packages assume that Time is a signed 64 bit integer
775
   --  type, the units are nanoseconds and the origin is the start of Ada
776
   --  time (1901-01-01 00:00:00.0 UTC).
777
 
778
   ---------------------------
779
   -- Arithmetic_Operations --
780
   ---------------------------
781
 
782
   package body Arithmetic_Operations is
783
 
784
      ---------
785
      -- Add --
786
      ---------
787
 
788
      function Add (Date : Time; Days : Long_Integer) return Time is
789
         pragma Unsuppress (Overflow_Check);
790
         Date_N : constant Time_Rep := Time_Rep (Date);
791
      begin
792
         return Time (Date_N + Time_Rep (Days) * Nanos_In_Day);
793
      exception
794
         when Constraint_Error =>
795
            raise Time_Error;
796
      end Add;
797
 
798
      ----------------
799
      -- Difference --
800
      ----------------
801
 
802
      procedure Difference
803
        (Left         : Time;
804
         Right        : Time;
805
         Days         : out Long_Integer;
806
         Seconds      : out Duration;
807
         Leap_Seconds : out Integer)
808
      is
809
         Res_Dur       : Time_Dur;
810
         Earlier       : Time_Rep;
811
         Elapsed_Leaps : Natural;
812
         Later         : Time_Rep;
813
         Negate        : Boolean := False;
814
         Next_Leap_N   : Time_Rep;
815
         Sub_Secs      : Duration;
816
         Sub_Secs_Diff : Time_Rep;
817
 
818
      begin
819
         --  Both input time values are assumed to be in UTC
820
 
821
         if Left >= Right then
822
            Later   := Time_Rep (Left);
823
            Earlier := Time_Rep (Right);
824
         else
825
            Later   := Time_Rep (Right);
826
            Earlier := Time_Rep (Left);
827
            Negate  := True;
828
         end if;
829
 
830
         --  If the target supports leap seconds, process them
831
 
832
         if Leap_Support then
833
            Cumulative_Leap_Seconds
834
              (Earlier, Later, Elapsed_Leaps, Next_Leap_N);
835
 
836
            if Later >= Next_Leap_N then
837
               Elapsed_Leaps := Elapsed_Leaps + 1;
838
            end if;
839
 
840
         --  The target does not support leap seconds
841
 
842
         else
843
            Elapsed_Leaps := 0;
844
         end if;
845
 
846
         --  Sub seconds processing. We add the resulting difference to one
847
         --  of the input dates in order to account for any potential rounding
848
         --  of the difference in the next step.
849
 
850
         Sub_Secs_Diff := Later mod Nano - Earlier mod Nano;
851
         Earlier       := Earlier + Sub_Secs_Diff;
852
         Sub_Secs      := Duration (Sub_Secs_Diff) / Nano_F;
853
 
854
         --  Difference processing. This operation should be able to calculate
855
         --  the difference between opposite values which are close to the end
856
         --  and start of Ada time. To accommodate the large range, we convert
857
         --  to seconds. This action may potentially round the two values and
858
         --  either add or drop a second. We compensate for this issue in the
859
         --  previous step.
860
 
861
         Res_Dur :=
862
           Time_Dur (Later / Nano - Earlier / Nano) - Time_Dur (Elapsed_Leaps);
863
 
864
         Days         := Long_Integer (Res_Dur / Secs_In_Day);
865
         Seconds      := Duration (Res_Dur mod Secs_In_Day) + Sub_Secs;
866
         Leap_Seconds := Integer (Elapsed_Leaps);
867
 
868
         if Negate then
869
            Days    := -Days;
870
            Seconds := -Seconds;
871
 
872
            if Leap_Seconds /= 0 then
873
               Leap_Seconds := -Leap_Seconds;
874
            end if;
875
         end if;
876
      end Difference;
877
 
878
      --------------
879
      -- Subtract --
880
      --------------
881
 
882
      function Subtract (Date : Time; Days : Long_Integer) return Time is
883
         pragma Unsuppress (Overflow_Check);
884
         Date_N : constant Time_Rep := Time_Rep (Date);
885
      begin
886
         return Time (Date_N - Time_Rep (Days) * Nanos_In_Day);
887
      exception
888
         when Constraint_Error =>
889
            raise Time_Error;
890
      end Subtract;
891
 
892
   end Arithmetic_Operations;
893
 
894
   ---------------------------
895
   -- Conversion_Operations --
896
   ---------------------------
897
 
898
   package body Conversion_Operations is
899
 
900
      -----------------
901
      -- To_Ada_Time --
902
      -----------------
903
 
904
      function To_Ada_Time (Unix_Time : Long_Integer) return Time is
905
         pragma Unsuppress (Overflow_Check);
906
         Unix_Rep : constant Time_Rep := Time_Rep (Unix_Time) * Nano;
907
      begin
908
         return Time (Unix_Rep - Epoch_Offset);
909
      exception
910
         when Constraint_Error =>
911
            raise Time_Error;
912
      end To_Ada_Time;
913
 
914
      -----------------
915
      -- To_Ada_Time --
916
      -----------------
917
 
918
      function To_Ada_Time
919
        (tm_year  : Integer;
920
         tm_mon   : Integer;
921
         tm_day   : Integer;
922
         tm_hour  : Integer;
923
         tm_min   : Integer;
924
         tm_sec   : Integer;
925
         tm_isdst : Integer) return Time
926
      is
927
         pragma Unsuppress (Overflow_Check);
928
         Year   : Year_Number;
929
         Month  : Month_Number;
930
         Day    : Day_Number;
931
         Second : Integer;
932
         Leap   : Boolean;
933
         Result : Time_Rep;
934
 
935
      begin
936
         --  Input processing
937
 
938
         Year  := Year_Number (1900 + tm_year);
939
         Month := Month_Number (1 + tm_mon);
940
         Day   := Day_Number (tm_day);
941
 
942
         --  Step 1: Validity checks of input values
943
 
944
         if not Year'Valid or else not Month'Valid or else not Day'Valid
945
           or else tm_hour  not in 0 .. 24
946
           or else tm_min   not in 0 .. 59
947
           or else tm_sec   not in 0 .. 60
948
           or else tm_isdst not in -1 .. 1
949
         then
950
            raise Time_Error;
951
         end if;
952
 
953
         --  Step 2: Potential leap second
954
 
955
         if tm_sec = 60 then
956
            Leap   := True;
957
            Second := 59;
958
         else
959
            Leap   := False;
960
            Second := tm_sec;
961
         end if;
962
 
963
         --  Step 3: Calculate the time value
964
 
965
         Result :=
966
           Time_Rep
967
             (Formatting_Operations.Time_Of
968
               (Year         => Year,
969
                Month        => Month,
970
                Day          => Day,
971
                Day_Secs     => 0.0,      --  Time is given in h:m:s
972
                Hour         => tm_hour,
973
                Minute       => tm_min,
974
                Second       => Second,
975
                Sub_Sec      => 0.0,      --  No precise sub second given
976
                Leap_Sec     => Leap,
977
                Use_Day_Secs => False,    --  Time is given in h:m:s
978
                Is_Ada_05    => True,     --  Force usage of explicit time zone
979
                Time_Zone    => 0));      --  Place the value in UTC
980
 
981
         --  Step 4: Daylight Savings Time
982
 
983
         if tm_isdst = 1 then
984
            Result := Result + Time_Rep (3_600) * Nano;
985
         end if;
986
 
987
         return Time (Result);
988
 
989
      exception
990
         when Constraint_Error =>
991
            raise Time_Error;
992
      end To_Ada_Time;
993
 
994
      -----------------
995
      -- To_Duration --
996
      -----------------
997
 
998
      function To_Duration
999
        (tv_sec  : Long_Integer;
1000
         tv_nsec : Long_Integer) return Duration
1001
      is
1002
         pragma Unsuppress (Overflow_Check);
1003
      begin
1004
         return Duration (tv_sec) + Duration (tv_nsec) / Nano_F;
1005
      end To_Duration;
1006
 
1007
      ------------------------
1008
      -- To_Struct_Timespec --
1009
      ------------------------
1010
 
1011
      procedure To_Struct_Timespec
1012
        (D       : Duration;
1013
         tv_sec  : out Long_Integer;
1014
         tv_nsec : out Long_Integer)
1015
      is
1016
         pragma Unsuppress (Overflow_Check);
1017
         Secs      : Duration;
1018
         Nano_Secs : Duration;
1019
 
1020
      begin
1021
         --  Seconds extraction, avoid potential rounding errors
1022
 
1023
         Secs   := D - 0.5;
1024
         tv_sec := Long_Integer (Secs);
1025
 
1026
         --  Nanoseconds extraction
1027
 
1028
         Nano_Secs := D - Duration (tv_sec);
1029
         tv_nsec := Long_Integer (Nano_Secs * Nano);
1030
      end To_Struct_Timespec;
1031
 
1032
      ------------------
1033
      -- To_Struct_Tm --
1034
      ------------------
1035
 
1036
      procedure To_Struct_Tm
1037
        (T       : Time;
1038
         tm_year : out Integer;
1039
         tm_mon  : out Integer;
1040
         tm_day  : out Integer;
1041
         tm_hour : out Integer;
1042
         tm_min  : out Integer;
1043
         tm_sec  : out Integer)
1044
      is
1045
         pragma Unsuppress (Overflow_Check);
1046
         Year      : Year_Number;
1047
         Month     : Month_Number;
1048
         Second    : Integer;
1049
         Day_Secs  : Day_Duration;
1050
         Sub_Sec   : Duration;
1051
         Leap_Sec  : Boolean;
1052
 
1053
      begin
1054
         --  Step 1: Split the input time
1055
 
1056
         Formatting_Operations.Split
1057
           (T, Year, Month, tm_day, Day_Secs,
1058
            tm_hour, tm_min, Second, Sub_Sec, Leap_Sec, True, 0);
1059
 
1060
         --  Step 2: Correct the year and month
1061
 
1062
         tm_year := Year - 1900;
1063
         tm_mon  := Month - 1;
1064
 
1065
         --  Step 3: Handle leap second occurrences
1066
 
1067
         tm_sec := (if Leap_Sec then 60 else Second);
1068
      end To_Struct_Tm;
1069
 
1070
      ------------------
1071
      -- To_Unix_Time --
1072
      ------------------
1073
 
1074
      function To_Unix_Time (Ada_Time : Time) return Long_Integer is
1075
         pragma Unsuppress (Overflow_Check);
1076
         Ada_Rep : constant Time_Rep := Time_Rep (Ada_Time);
1077
      begin
1078
         return Long_Integer ((Ada_Rep + Epoch_Offset) / Nano);
1079
      exception
1080
         when Constraint_Error =>
1081
            raise Time_Error;
1082
      end To_Unix_Time;
1083
   end Conversion_Operations;
1084
 
1085
   ----------------------
1086
   -- Delay_Operations --
1087
   ----------------------
1088
 
1089
   package body Delay_Operations is
1090
 
1091
      -----------------
1092
      -- To_Duration --
1093
      -----------------
1094
 
1095
      function To_Duration (Date : Time) return Duration is
1096
         pragma Unsuppress (Overflow_Check);
1097
 
1098
         Safe_Ada_High : constant Time_Rep := Ada_High - Epoch_Offset;
1099
         --  This value represents a "safe" end of time. In order to perform a
1100
         --  proper conversion to Unix duration, we will have to shift origins
1101
         --  at one point. For very distant dates, this means an overflow check
1102
         --  failure. To prevent this, the function returns the "safe" end of
1103
         --  time (roughly 2219) which is still distant enough.
1104
 
1105
         Elapsed_Leaps : Natural;
1106
         Next_Leap_N   : Time_Rep;
1107
         Res_N         : Time_Rep;
1108
 
1109
      begin
1110
         Res_N := Time_Rep (Date);
1111
 
1112
         --  Step 1: If the target supports leap seconds, remove any leap
1113
         --  seconds elapsed up to the input date.
1114
 
1115
         if Leap_Support then
1116
            Cumulative_Leap_Seconds
1117
              (Start_Of_Time, Res_N, Elapsed_Leaps, Next_Leap_N);
1118
 
1119
            --  The input time value may fall on a leap second occurrence
1120
 
1121
            if Res_N >= Next_Leap_N then
1122
               Elapsed_Leaps := Elapsed_Leaps + 1;
1123
            end if;
1124
 
1125
         --  The target does not support leap seconds
1126
 
1127
         else
1128
            Elapsed_Leaps := 0;
1129
         end if;
1130
 
1131
         Res_N := Res_N - Time_Rep (Elapsed_Leaps) * Nano;
1132
 
1133
         --  Step 2: Perform a shift in origins to obtain a Unix equivalent of
1134
         --  the input. Guard against very large delay values such as the end
1135
         --  of time since the computation will overflow.
1136
 
1137
         Res_N := (if Res_N > Safe_Ada_High then Safe_Ada_High
1138
                                            else Res_N + Epoch_Offset);
1139
 
1140
         return Time_Rep_To_Duration (Res_N);
1141
      end To_Duration;
1142
 
1143
   end Delay_Operations;
1144
 
1145
   ---------------------------
1146
   -- Formatting_Operations --
1147
   ---------------------------
1148
 
1149
   package body Formatting_Operations is
1150
 
1151
      -----------------
1152
      -- Day_Of_Week --
1153
      -----------------
1154
 
1155
      function Day_Of_Week (Date : Time) return Integer is
1156
         Date_N    : constant Time_Rep := Time_Rep (Date);
1157
         Time_Zone : constant Long_Integer := UTC_Time_Offset (Date, True);
1158
         Ada_Low_N : Time_Rep;
1159
         Day_Count : Long_Integer;
1160
         Day_Dur   : Time_Dur;
1161
         High_N    : Time_Rep;
1162
         Low_N     : Time_Rep;
1163
 
1164
      begin
1165
         --  As declared, the Ada Epoch is set in UTC. For this calculation to
1166
         --  work properly, both the Epoch and the input date must be in the
1167
         --  same time zone. The following places the Epoch in the input date's
1168
         --  time zone.
1169
 
1170
         Ada_Low_N := Ada_Low - Time_Rep (Time_Zone) * Nano;
1171
 
1172
         if Date_N > Ada_Low_N then
1173
            High_N := Date_N;
1174
            Low_N  := Ada_Low_N;
1175
         else
1176
            High_N := Ada_Low_N;
1177
            Low_N  := Date_N;
1178
         end if;
1179
 
1180
         --  Determine the elapsed seconds since the start of Ada time
1181
 
1182
         Day_Dur := Time_Dur (High_N / Nano - Low_N / Nano);
1183
 
1184
         --  Count the number of days since the start of Ada time. 1901-01-01
1185
         --  GMT was a Tuesday.
1186
 
1187
         Day_Count := Long_Integer (Day_Dur / Secs_In_Day) + 1;
1188
 
1189
         return Integer (Day_Count mod 7);
1190
      end Day_Of_Week;
1191
 
1192
      -----------
1193
      -- Split --
1194
      -----------
1195
 
1196
      procedure Split
1197
        (Date      : Time;
1198
         Year      : out Year_Number;
1199
         Month     : out Month_Number;
1200
         Day       : out Day_Number;
1201
         Day_Secs  : out Day_Duration;
1202
         Hour      : out Integer;
1203
         Minute    : out Integer;
1204
         Second    : out Integer;
1205
         Sub_Sec   : out Duration;
1206
         Leap_Sec  : out Boolean;
1207
         Is_Ada_05 : Boolean;
1208
         Time_Zone : Long_Integer)
1209
      is
1210
         --  The following constants represent the number of nanoseconds
1211
         --  elapsed since the start of Ada time to and including the non
1212
         --  leap centennial years.
1213
 
1214
         Year_2101 : constant Time_Rep := Ada_Low +
1215
                       Time_Rep (49 * 366 + 151 * 365) * Nanos_In_Day;
1216
         Year_2201 : constant Time_Rep := Ada_Low +
1217
                       Time_Rep (73 * 366 + 227 * 365) * Nanos_In_Day;
1218
         Year_2301 : constant Time_Rep := Ada_Low +
1219
                       Time_Rep (97 * 366 + 303 * 365) * Nanos_In_Day;
1220
 
1221
         Date_Dur       : Time_Dur;
1222
         Date_N         : Time_Rep;
1223
         Day_Seconds    : Natural;
1224
         Elapsed_Leaps  : Natural;
1225
         Four_Year_Segs : Natural;
1226
         Hour_Seconds   : Natural;
1227
         Is_Leap_Year   : Boolean;
1228
         Next_Leap_N    : Time_Rep;
1229
         Rem_Years      : Natural;
1230
         Sub_Sec_N      : Time_Rep;
1231
         Year_Day       : Natural;
1232
 
1233
      begin
1234
         Date_N := Time_Rep (Date);
1235
 
1236
         --  Step 1: Leap seconds processing in UTC
1237
 
1238
         if Leap_Support then
1239
            Cumulative_Leap_Seconds
1240
              (Start_Of_Time, Date_N, Elapsed_Leaps, Next_Leap_N);
1241
 
1242
            Leap_Sec := Date_N >= Next_Leap_N;
1243
 
1244
            if Leap_Sec then
1245
               Elapsed_Leaps := Elapsed_Leaps + 1;
1246
            end if;
1247
 
1248
         --  The target does not support leap seconds
1249
 
1250
         else
1251
            Elapsed_Leaps := 0;
1252
            Leap_Sec      := False;
1253
         end if;
1254
 
1255
         Date_N := Date_N - Time_Rep (Elapsed_Leaps) * Nano;
1256
 
1257
         --  Step 2: Time zone processing. This action converts the input date
1258
         --  from GMT to the requested time zone. Applies from Ada 2005 on.
1259
 
1260
         if Is_Ada_05 then
1261
            if Time_Zone /= 0 then
1262
               Date_N := Date_N + Time_Rep (Time_Zone) * 60 * Nano;
1263
            end if;
1264
 
1265
         --  Ada 83 and 95
1266
 
1267
         else
1268
            declare
1269
               Off : constant Long_Integer :=
1270
                       UTC_Time_Offset (Time (Date_N), False);
1271
 
1272
            begin
1273
               Date_N := Date_N + Time_Rep (Off) * Nano;
1274
            end;
1275
         end if;
1276
 
1277
         --  Step 3: Non-leap centennial year adjustment in local time zone
1278
 
1279
         --  In order for all divisions to work properly and to avoid more
1280
         --  complicated arithmetic, we add fake February 29s to dates which
1281
         --  occur after a non-leap centennial year.
1282
 
1283
         if Date_N >= Year_2301 then
1284
            Date_N := Date_N + Time_Rep (3) * Nanos_In_Day;
1285
 
1286
         elsif Date_N >= Year_2201 then
1287
            Date_N := Date_N + Time_Rep (2) * Nanos_In_Day;
1288
 
1289
         elsif Date_N >= Year_2101 then
1290
            Date_N := Date_N + Time_Rep (1) * Nanos_In_Day;
1291
         end if;
1292
 
1293
         --  Step 4: Sub second processing in local time zone
1294
 
1295
         Sub_Sec_N := Date_N mod Nano;
1296
         Sub_Sec   := Duration (Sub_Sec_N) / Nano_F;
1297
         Date_N    := Date_N - Sub_Sec_N;
1298
 
1299
         --  Convert Date_N into a time duration value, changing the units
1300
         --  to seconds.
1301
 
1302
         Date_Dur := Time_Dur (Date_N / Nano - Ada_Low / Nano);
1303
 
1304
         --  Step 5: Year processing in local time zone. Determine the number
1305
         --  of four year segments since the start of Ada time and the input
1306
         --  date.
1307
 
1308
         Four_Year_Segs := Natural (Date_Dur / Secs_In_Four_Years);
1309
 
1310
         if Four_Year_Segs > 0 then
1311
            Date_Dur := Date_Dur - Time_Dur (Four_Year_Segs) *
1312
                                   Secs_In_Four_Years;
1313
         end if;
1314
 
1315
         --  Calculate the remaining non-leap years
1316
 
1317
         Rem_Years := Natural (Date_Dur / Secs_In_Non_Leap_Year);
1318
 
1319
         if Rem_Years > 3 then
1320
            Rem_Years := 3;
1321
         end if;
1322
 
1323
         Date_Dur := Date_Dur - Time_Dur (Rem_Years) * Secs_In_Non_Leap_Year;
1324
 
1325
         Year := Ada_Min_Year + Natural (4 * Four_Year_Segs + Rem_Years);
1326
         Is_Leap_Year := Is_Leap (Year);
1327
 
1328
         --  Step 6: Month and day processing in local time zone
1329
 
1330
         Year_Day := Natural (Date_Dur / Secs_In_Day) + 1;
1331
 
1332
         Month := 1;
1333
 
1334
         --  Processing for months after January
1335
 
1336
         if Year_Day > 31 then
1337
            Month    := 2;
1338
            Year_Day := Year_Day - 31;
1339
 
1340
            --  Processing for a new month or a leap February
1341
 
1342
            if Year_Day > 28
1343
              and then (not Is_Leap_Year or else Year_Day > 29)
1344
            then
1345
               Month    := 3;
1346
               Year_Day := Year_Day - 28;
1347
 
1348
               if Is_Leap_Year then
1349
                  Year_Day := Year_Day - 1;
1350
               end if;
1351
 
1352
               --  Remaining months
1353
 
1354
               while Year_Day > Days_In_Month (Month) loop
1355
                  Year_Day := Year_Day - Days_In_Month (Month);
1356
                  Month    := Month + 1;
1357
               end loop;
1358
            end if;
1359
         end if;
1360
 
1361
         --  Step 7: Hour, minute, second and sub second processing in local
1362
         --  time zone.
1363
 
1364
         Day          := Day_Number (Year_Day);
1365
         Day_Seconds  := Integer (Date_Dur mod Secs_In_Day);
1366
         Day_Secs     := Duration (Day_Seconds) + Sub_Sec;
1367
         Hour         := Day_Seconds / 3_600;
1368
         Hour_Seconds := Day_Seconds mod 3_600;
1369
         Minute       := Hour_Seconds / 60;
1370
         Second       := Hour_Seconds mod 60;
1371
      end Split;
1372
 
1373
      -------------
1374
      -- Time_Of --
1375
      -------------
1376
 
1377
      function Time_Of
1378
        (Year         : Year_Number;
1379
         Month        : Month_Number;
1380
         Day          : Day_Number;
1381
         Day_Secs     : Day_Duration;
1382
         Hour         : Integer;
1383
         Minute       : Integer;
1384
         Second       : Integer;
1385
         Sub_Sec      : Duration;
1386
         Leap_Sec     : Boolean := False;
1387
         Use_Day_Secs : Boolean := False;
1388
         Is_Ada_05    : Boolean := False;
1389
         Time_Zone    : Long_Integer := 0) return Time
1390
      is
1391
         Count         : Integer;
1392
         Elapsed_Leaps : Natural;
1393
         Next_Leap_N   : Time_Rep;
1394
         Res_N         : Time_Rep;
1395
         Rounded_Res_N : Time_Rep;
1396
 
1397
      begin
1398
         --  Step 1: Check whether the day, month and year form a valid date
1399
 
1400
         if Day > Days_In_Month (Month)
1401
           and then (Day /= 29 or else Month /= 2 or else not Is_Leap (Year))
1402
         then
1403
            raise Time_Error;
1404
         end if;
1405
 
1406
         --  Start accumulating nanoseconds from the low bound of Ada time
1407
 
1408
         Res_N := Ada_Low;
1409
 
1410
         --  Step 2: Year processing and centennial year adjustment. Determine
1411
         --  the number of four year segments since the start of Ada time and
1412
         --  the input date.
1413
 
1414
         Count := (Year - Year_Number'First) / 4;
1415
 
1416
         for Four_Year_Segments in 1 .. Count loop
1417
            Res_N := Res_N + Nanos_In_Four_Years;
1418
         end loop;
1419
 
1420
         --  Note that non-leap centennial years are automatically considered
1421
         --  leap in the operation above. An adjustment of several days is
1422
         --  required to compensate for this.
1423
 
1424
         if Year > 2300 then
1425
            Res_N := Res_N - Time_Rep (3) * Nanos_In_Day;
1426
 
1427
         elsif Year > 2200 then
1428
            Res_N := Res_N - Time_Rep (2) * Nanos_In_Day;
1429
 
1430
         elsif Year > 2100 then
1431
            Res_N := Res_N - Time_Rep (1) * Nanos_In_Day;
1432
         end if;
1433
 
1434
         --  Add the remaining non-leap years
1435
 
1436
         Count := (Year - Year_Number'First) mod 4;
1437
         Res_N := Res_N + Time_Rep (Count) * Secs_In_Non_Leap_Year * Nano;
1438
 
1439
         --  Step 3: Day of month processing. Determine the number of days
1440
         --  since the start of the current year. Do not add the current
1441
         --  day since it has not elapsed yet.
1442
 
1443
         Count := Cumulative_Days_Before_Month (Month) + Day - 1;
1444
 
1445
         --  The input year is leap and we have passed February
1446
 
1447
         if Is_Leap (Year)
1448
           and then Month > 2
1449
         then
1450
            Count := Count + 1;
1451
         end if;
1452
 
1453
         Res_N := Res_N + Time_Rep (Count) * Nanos_In_Day;
1454
 
1455
         --  Step 4: Hour, minute, second and sub second processing
1456
 
1457
         if Use_Day_Secs then
1458
            Res_N := Res_N + Duration_To_Time_Rep (Day_Secs);
1459
 
1460
         else
1461
            Res_N :=
1462
              Res_N + Time_Rep (Hour * 3_600 + Minute * 60 + Second) * Nano;
1463
 
1464
            if Sub_Sec = 1.0 then
1465
               Res_N := Res_N + Time_Rep (1) * Nano;
1466
            else
1467
               Res_N := Res_N + Duration_To_Time_Rep (Sub_Sec);
1468
            end if;
1469
         end if;
1470
 
1471
         --  At this point, the generated time value should be withing the
1472
         --  bounds of Ada time.
1473
 
1474
         Check_Within_Time_Bounds (Res_N);
1475
 
1476
         --  Step 4: Time zone processing. At this point we have built an
1477
         --  arbitrary time value which is not related to any time zone.
1478
         --  For simplicity, the time value is normalized to GMT, producing
1479
         --  a uniform representation which can be treated by arithmetic
1480
         --  operations for instance without any additional corrections.
1481
 
1482
         if Is_Ada_05 then
1483
            if Time_Zone /= 0 then
1484
               Res_N := Res_N - Time_Rep (Time_Zone) * 60 * Nano;
1485
            end if;
1486
 
1487
         --  Ada 83 and 95
1488
 
1489
         else
1490
            declare
1491
               Current_Off   : constant Long_Integer :=
1492
                                 UTC_Time_Offset (Time (Res_N), False);
1493
               Current_Res_N : constant Time_Rep :=
1494
                                 Res_N - Time_Rep (Current_Off) * Nano;
1495
               Off           : constant Long_Integer :=
1496
                                 UTC_Time_Offset (Time (Current_Res_N), False);
1497
 
1498
            begin
1499
               Res_N := Res_N - Time_Rep (Off) * Nano;
1500
            end;
1501
         end if;
1502
 
1503
         --  Step 5: Leap seconds processing in GMT
1504
 
1505
         if Leap_Support then
1506
            Cumulative_Leap_Seconds
1507
              (Start_Of_Time, Res_N, Elapsed_Leaps, Next_Leap_N);
1508
 
1509
            Res_N := Res_N + Time_Rep (Elapsed_Leaps) * Nano;
1510
 
1511
            --  An Ada 2005 caller requesting an explicit leap second or an
1512
            --  Ada 95 caller accounting for an invisible leap second.
1513
 
1514
            if Leap_Sec or else Res_N >= Next_Leap_N then
1515
               Res_N := Res_N + Time_Rep (1) * Nano;
1516
            end if;
1517
 
1518
            --  Leap second validity check
1519
 
1520
            Rounded_Res_N := Res_N - (Res_N mod Nano);
1521
 
1522
            if Is_Ada_05
1523
              and then Leap_Sec
1524
              and then Rounded_Res_N /= Next_Leap_N
1525
            then
1526
               raise Time_Error;
1527
            end if;
1528
         end if;
1529
 
1530
         return Time (Res_N);
1531
      end Time_Of;
1532
 
1533
   end Formatting_Operations;
1534
 
1535
   ---------------------------
1536
   -- Time_Zones_Operations --
1537
   ---------------------------
1538
 
1539
   package body Time_Zones_Operations is
1540
 
1541
      ---------------------
1542
      -- UTC_Time_Offset --
1543
      ---------------------
1544
 
1545
      function UTC_Time_Offset (Date : Time) return Long_Integer is
1546
      begin
1547
         return UTC_Time_Offset (Date, True);
1548
      end UTC_Time_Offset;
1549
 
1550
   end Time_Zones_Operations;
1551
 
1552
--  Start of elaboration code for Ada.Calendar
1553
 
1554
begin
1555
   System.OS_Primitives.Initialize;
1556
 
1557
end Ada.Calendar;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.