1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . C A L E N D A R --
9 -- Copyright (C) 1999-2017, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Interfaces
.C
.Extensions
;
34 package body GNAT
.Calendar
is
42 function Day_In_Year
(Date
: Time
) return Day_In_Year_Number
is
46 Day_Secs
: Day_Duration
;
47 pragma Unreferenced
(Day_Secs
);
49 Split
(Date
, Year
, Month
, Day
, Day_Secs
);
50 return Julian_Day
(Year
, Month
, Day
) - Julian_Day
(Year
, 1, 1) + 1;
57 function Day_Of_Week
(Date
: Time
) return Day_Name
is
61 Day_Secs
: Day_Duration
;
62 pragma Unreferenced
(Day_Secs
);
64 Split
(Date
, Year
, Month
, Day
, Day_Secs
);
65 return Day_Name
'Val ((Julian_Day
(Year
, Month
, Day
)) mod 7);
72 function Hour
(Date
: Time
) return Hour_Number
is
77 Minute
: Minute_Number
;
78 Second
: Second_Number
;
79 Sub_Second
: Second_Duration
;
80 pragma Unreferenced
(Year
, Month
, Day
, Minute
, Second
, Sub_Second
);
82 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
90 -- Julian_Day is used to by Day_Of_Week and Day_In_Year. Note that this
91 -- implementation is not expensive.
96 Day
: Day_Number
) return Integer
98 Internal_Year
: Integer;
99 Internal_Month
: Integer;
100 Internal_Day
: Integer;
101 Julian_Date
: Integer;
106 Internal_Year
:= Integer (Year
);
107 Internal_Month
:= Integer (Month
);
108 Internal_Day
:= Integer (Day
);
110 if Internal_Month
> 2 then
111 Internal_Month
:= Internal_Month
- 3;
113 Internal_Month
:= Internal_Month
+ 9;
114 Internal_Year
:= Internal_Year
- 1;
117 C
:= Internal_Year
/ 100;
118 Ya
:= Internal_Year
- (100 * C
);
120 Julian_Date
:= (146_097
* C
) / 4 +
122 (153 * Internal_Month
+ 2) / 5 +
123 Internal_Day
+ 1_721_119
;
132 function Minute
(Date
: Time
) return Minute_Number
is
134 Month
: Month_Number
;
137 Minute
: Minute_Number
;
138 Second
: Second_Number
;
139 Sub_Second
: Second_Duration
;
140 pragma Unreferenced
(Year
, Month
, Day
, Hour
, Second
, Sub_Second
);
142 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
150 function Second
(Date
: Time
) return Second_Number
is
152 Month
: Month_Number
;
155 Minute
: Minute_Number
;
156 Second
: Second_Number
;
157 Sub_Second
: Second_Duration
;
158 pragma Unreferenced
(Year
, Month
, Day
, Hour
, Minute
, Sub_Second
);
160 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
170 Year
: out Year_Number
;
171 Month
: out Month_Number
;
172 Day
: out Day_Number
;
173 Hour
: out Hour_Number
;
174 Minute
: out Minute_Number
;
175 Second
: out Second_Number
;
176 Sub_Second
: out Second_Duration
)
178 Day_Secs
: Day_Duration
;
182 Split
(Date
, Year
, Month
, Day
, Day_Secs
);
184 Secs
:= (if Day_Secs
= 0.0 then 0 else Natural (Day_Secs
- 0.5));
185 Sub_Second
:= Second_Duration
(Day_Secs
- Day_Duration
(Secs
));
186 Hour
:= Hour_Number
(Secs
/ 3_600
);
187 Secs
:= Secs
mod 3_600
;
188 Minute
:= Minute_Number
(Secs
/ 60);
189 Second
:= Second_Number
(Secs
mod 60);
192 ---------------------
193 -- Split_At_Locale --
194 ---------------------
196 procedure Split_At_Locale
198 Year
: out Year_Number
;
199 Month
: out Month_Number
;
200 Day
: out Day_Number
;
201 Hour
: out Hour_Number
;
202 Minute
: out Minute_Number
;
203 Second
: out Second_Number
;
204 Sub_Second
: out Second_Duration
)
206 procedure Ada_Calendar_Split
208 Year
: out Year_Number
;
209 Month
: out Month_Number
;
210 Day
: out Day_Number
;
211 Day_Secs
: out Day_Duration
;
213 Minute
: out Integer;
214 Second
: out Integer;
215 Sub_Sec
: out Duration;
216 Leap_Sec
: out Boolean;
218 Is_Historic
: Boolean;
219 Time_Zone
: Long_Integer);
220 pragma Import
(Ada
, Ada_Calendar_Split
, "__gnat_split");
225 pragma Unreferenced
(Ds
, Le
);
228 -- Even though the input time zone is UTC (0), the flag Use_TZ will
229 -- ensure that Split picks up the local time zone.
240 Sub_Sec
=> Sub_Second
,
243 Is_Historic
=> False,
251 function Sub_Second
(Date
: Time
) return Second_Duration
is
253 Month
: Month_Number
;
256 Minute
: Minute_Number
;
257 Second
: Second_Number
;
258 Sub_Second
: Second_Duration
;
259 pragma Unreferenced
(Year
, Month
, Day
, Hour
, Minute
, Second
);
261 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
271 Month
: Month_Number
;
274 Minute
: Minute_Number
;
275 Second
: Second_Number
;
276 Sub_Second
: Second_Duration
:= 0.0) return Time
278 Day_Secs
: constant Day_Duration
:=
279 Day_Duration
(Hour
* 3_600
) +
280 Day_Duration
(Minute
* 60) +
281 Day_Duration
(Second
) +
284 return Time_Of
(Year
, Month
, Day
, Day_Secs
);
287 -----------------------
288 -- Time_Of_At_Locale --
289 -----------------------
291 function Time_Of_At_Locale
293 Month
: Month_Number
;
296 Minute
: Minute_Number
;
297 Second
: Second_Number
;
298 Sub_Second
: Second_Duration
:= 0.0) return Time
300 function Ada_Calendar_Time_Of
302 Month
: Month_Number
;
304 Day_Secs
: Day_Duration
;
310 Use_Day_Secs
: Boolean;
312 Is_Historic
: Boolean;
313 Time_Zone
: Long_Integer) return Time
;
314 pragma Import
(Ada
, Ada_Calendar_Time_Of
, "__gnat_time_of");
317 -- Even though the input time zone is UTC (0), the flag Use_TZ will
318 -- ensure that Split picks up the local time zone.
329 Sub_Sec
=> Sub_Second
,
331 Use_Day_Secs
=> False,
333 Is_Historic
=> False,
335 end Time_Of_At_Locale
;
341 function To_Duration
(T
: not null access timeval
) return Duration is
343 procedure timeval_to_duration
344 (T
: not null access timeval
;
345 sec
: not null access C
.Extensions
.long_long
;
346 usec
: not null access C
.long
);
347 pragma Import
(C
, timeval_to_duration
, "__gnat_timeval_to_duration");
349 Micro
: constant := 10**6;
350 sec
: aliased C
.Extensions
.long_long
;
351 usec
: aliased C
.long
;
354 timeval_to_duration
(T
, sec
'Access, usec
'Access);
355 return Duration (sec
) + Duration (usec
) / Micro
;
362 function To_Timeval
(D
: Duration) return timeval
is
364 procedure duration_to_timeval
365 (Sec
: C
.Extensions
.long_long
;
367 T
: not null access timeval
);
368 pragma Import
(C
, duration_to_timeval
, "__gnat_duration_to_timeval");
370 Micro
: constant := 10**6;
371 Result
: aliased timeval
;
372 sec
: C
.Extensions
.long_long
;
380 sec
:= C
.Extensions
.long_long
(D
- 0.5);
381 usec
:= C
.long
((D
- Duration (sec
)) * Micro
- 0.5);
384 duration_to_timeval
(sec
, usec
, Result
'Access);
393 function Week_In_Year
(Date
: Time
) return Week_In_Year_Number
is
395 Week
: Week_In_Year_Number
;
396 pragma Unreferenced
(Year
);
398 Year_Week_In_Year
(Date
, Year
, Week
);
402 -----------------------
403 -- Year_Week_In_Year --
404 -----------------------
406 procedure Year_Week_In_Year
408 Year
: out Year_Number
;
409 Week
: out Week_In_Year_Number
)
411 Month
: Month_Number
;
414 Minute
: Minute_Number
;
415 Second
: Second_Number
;
416 Sub_Second
: Second_Duration
;
418 Shift
: Week_In_Year_Number
;
419 Start_Week
: Week_In_Year_Number
;
421 pragma Unreferenced
(Hour
, Minute
, Second
, Sub_Second
);
423 function Is_Leap
(Year
: Year_Number
) return Boolean;
424 -- Return True if Year denotes a leap year. Leap centennial years are
427 function Jan_1_Day_Of_Week
430 Last_Year
: Boolean := False;
431 Next_Year
: Boolean := False) return Day_Name
;
432 -- Given the weekday of January 1 in Year, determine the weekday on
433 -- which January 1 fell last year or will fall next year as set by
434 -- the two flags. This routine does not call Time_Of or Split.
436 function Last_Year_Has_53_Weeks
438 Year
: Year_Number
) return Boolean;
439 -- Given the weekday of January 1 in Year, determine whether last year
440 -- has 53 weeks. A False value implies that the year has 52 weeks.
446 function Is_Leap
(Year
: Year_Number
) return Boolean is
448 if Year
mod 400 = 0 then
450 elsif Year
mod 100 = 0 then
453 return Year
mod 4 = 0;
457 -----------------------
458 -- Jan_1_Day_Of_Week --
459 -----------------------
461 function Jan_1_Day_Of_Week
464 Last_Year
: Boolean := False;
465 Next_Year
: Boolean := False) return Day_Name
467 Shift
: Integer := 0;
471 Shift
:= (if Is_Leap
(Year
- 1) then -2 else -1);
473 Shift
:= (if Is_Leap
(Year
) then 2 else 1);
476 return Day_Name
'Val ((Day_Name
'Pos (Jan_1
) + Shift
) mod 7);
477 end Jan_1_Day_Of_Week
;
479 ----------------------------
480 -- Last_Year_Has_53_Weeks --
481 ----------------------------
483 function Last_Year_Has_53_Weeks
485 Year
: Year_Number
) return Boolean
487 Last_Jan_1
: constant Day_Name
:=
488 Jan_1_Day_Of_Week
(Jan_1
, Year
, Last_Year
=> True);
491 -- These two cases are illustrated in the table below
494 Last_Jan_1
= Thursday
495 or else (Last_Jan_1
= Wednesday
and then Is_Leap
(Year
- 1));
496 end Last_Year_Has_53_Weeks
;
498 -- Start of processing for Week_In_Year
501 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
503 -- According to ISO 8601, the first week of year Y is the week that
504 -- contains the first Thursday in year Y. The following table contains
505 -- all possible combinations of years and weekdays along with examples.
507 -- +-------+------+-------+---------+
508 -- | Jan 1 | Leap | Weeks | Example |
509 -- +-------+------+-------+---------+
510 -- | Mon | No | 52 | 2007 |
511 -- +-------+------+-------+---------+
512 -- | Mon | Yes | 52 | 1996 |
513 -- +-------+------+-------+---------+
514 -- | Tue | No | 52 | 2002 |
515 -- +-------+------+-------+---------+
516 -- | Tue | Yes | 52 | 1980 |
517 -- +-------+------+-------+---------+
518 -- | Wed | No | 52 | 2003 |
519 -- +-------+------#########---------+
520 -- | Wed | Yes # 53 # 1992 |
521 -- +-------+------#-------#---------+
522 -- | Thu | No # 53 # 1998 |
523 -- +-------+------#-------#---------+
524 -- | Thu | Yes # 53 # 2004 |
525 -- +-------+------#########---------+
526 -- | Fri | No | 52 | 1999 |
527 -- +-------+------+-------+---------+
528 -- | Fri | Yes | 52 | 1988 |
529 -- +-------+------+-------+---------+
530 -- | Sat | No | 52 | 1994 |
531 -- +-------+------+-------+---------+
532 -- | Sat | Yes | 52 | 1972 |
533 -- +-------+------+-------+---------+
534 -- | Sun | No | 52 | 1995 |
535 -- +-------+------+-------+---------+
536 -- | Sun | Yes | 52 | 1956 |
537 -- +-------+------+-------+---------+
539 -- A small optimization, the input date is January 1. Note that this
540 -- is a key day since it determines the number of weeks and is used
541 -- when special casing the first week of January and the last week of
544 Jan_1
:= Day_Of_Week
(if Day
= 1 and then Month
= 1
546 else (Time_Of
(Year
, 1, 1, 0.0)));
548 -- Special cases for January
552 -- Special case 1: January 1, 2 and 3. These three days may belong
553 -- to last year's last week which can be week number 52 or 53.
555 -- +-----+-----+-----+=====+-----+-----+-----+
556 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
557 -- +-----+-----+-----+-----+-----+-----+-----+
558 -- | 26 | 27 | 28 # 29 # 30 | 31 | 1 |
559 -- +-----+-----+-----+-----+-----+-----+-----+
560 -- | 27 | 28 | 29 # 30 # 31 | 1 | 2 |
561 -- +-----+-----+-----+-----+-----+-----+-----+
562 -- | 28 | 29 | 30 # 31 # 1 | 2 | 3 |
563 -- +-----+-----+-----+=====+-----+-----+-----+
565 if (Day
= 1 and then Jan_1
in Friday
.. Sunday
)
567 (Day
= 2 and then Jan_1
in Friday
.. Saturday
)
569 (Day
= 3 and then Jan_1
= Friday
)
571 Week
:= (if Last_Year_Has_53_Weeks
(Jan_1
, Year
) then 53 else 52);
573 -- January 1, 2 and 3 belong to the previous year
578 -- Special case 2: January 1, 2, 3, 4, 5, 6 and 7 of the first week
580 -- +-----+-----+-----+=====+-----+-----+-----+
581 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
582 -- +-----+-----+-----+-----+-----+-----+-----+
583 -- | 29 | 30 | 31 # 1 # 2 | 3 | 4 |
584 -- +-----+-----+-----+-----+-----+-----+-----+
585 -- | 30 | 31 | 1 # 2 # 3 | 4 | 5 |
586 -- +-----+-----+-----+-----+-----+-----+-----+
587 -- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
588 -- +-----+-----+-----+-----+-----+-----+-----+
589 -- | 1 | 2 | 3 # 4 # 5 | 6 | 7 |
590 -- +-----+-----+-----+=====+-----+-----+-----+
592 elsif (Day
<= 4 and then Jan_1
in Monday
.. Thursday
)
594 (Day
= 5 and then Jan_1
in Monday
.. Wednesday
)
596 (Day
= 6 and then Jan_1
in Monday
.. Tuesday
)
598 (Day
= 7 and then Jan_1
= Monday
)
604 -- Month other than 1
606 -- Special case 3: December 29, 30 and 31. These days may belong to
607 -- next year's first week.
609 -- +-----+-----+-----+=====+-----+-----+-----+
610 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
611 -- +-----+-----+-----+-----+-----+-----+-----+
612 -- | 29 | 30 | 31 # 1 # 2 | 3 | 4 |
613 -- +-----+-----+-----+-----+-----+-----+-----+
614 -- | 30 | 31 | 1 # 2 # 3 | 4 | 5 |
615 -- +-----+-----+-----+-----+-----+-----+-----+
616 -- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
617 -- +-----+-----+-----+=====+-----+-----+-----+
619 elsif Month
= 12 and then Day
> 28 then
621 Next_Jan_1
: constant Day_Name
:=
622 Jan_1_Day_Of_Week
(Jan_1
, Year
, Next_Year
=> True);
624 if (Day
= 29 and then Next_Jan_1
= Thursday
)
626 (Day
= 30 and then Next_Jan_1
in Wednesday
.. Thursday
)
628 (Day
= 31 and then Next_Jan_1
in Tuesday
.. Thursday
)
637 -- Determine the week from which to start counting. If January 1 does
638 -- not belong to the first week of the input year, then the next week
639 -- is the first week.
641 Start_Week
:= (if Jan_1
in Friday
.. Sunday
then 1 else 2);
643 -- At this point all special combinations have been accounted for and
644 -- the proper start week has been found. Since January 1 may not fall
645 -- on a Monday, shift 7 - Day_Name'Pos (Jan_1). This action ensures an
646 -- origin which falls on Monday.
648 Shift
:= 7 - Day_Name
'Pos (Jan_1
);
649 Week
:= Start_Week
+ (Day_In_Year
(Date
) - Shift
- 1) / 7;
650 end Year_Week_In_Year
;