1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . C A L E N D A R --
9 -- Copyright (C) 1999-2023, 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
;
48 Split
(Date
, Year
, Month
, Day
, Day_Secs
);
49 return Julian_Day
(Year
, Month
, Day
) - Julian_Day
(Year
, 1, 1) + 1;
56 function Day_Of_Week
(Date
: Time
) return Day_Name
is
60 Day_Secs
: Day_Duration
;
62 Split
(Date
, Year
, Month
, Day
, Day_Secs
);
63 return Day_Name
'Val ((Julian_Day
(Year
, Month
, Day
)) mod 7);
70 function Hour
(Date
: Time
) return Hour_Number
is
75 Minute
: Minute_Number
;
76 Second
: Second_Number
;
77 Sub_Second
: Second_Duration
;
79 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
87 -- Julian_Day is used to by Day_Of_Week and Day_In_Year. Note that this
88 -- implementation is not expensive.
93 Day
: Day_Number
) return Integer
95 Internal_Year
: Integer;
96 Internal_Month
: Integer;
97 Internal_Day
: Integer;
98 Julian_Date
: Integer;
103 Internal_Year
:= Integer (Year
);
104 Internal_Month
:= Integer (Month
);
105 Internal_Day
:= Integer (Day
);
107 if Internal_Month
> 2 then
108 Internal_Month
:= Internal_Month
- 3;
110 Internal_Month
:= Internal_Month
+ 9;
111 Internal_Year
:= Internal_Year
- 1;
114 C
:= Internal_Year
/ 100;
115 Ya
:= Internal_Year
- (100 * C
);
117 Julian_Date
:= (146_097
* C
) / 4 +
119 (153 * Internal_Month
+ 2) / 5 +
120 Internal_Day
+ 1_721_119
;
129 function Minute
(Date
: Time
) return Minute_Number
is
131 Month
: Month_Number
;
134 Minute
: Minute_Number
;
135 Second
: Second_Number
;
136 Sub_Second
: Second_Duration
;
138 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
146 function Second
(Date
: Time
) return Second_Number
is
148 Month
: Month_Number
;
151 Minute
: Minute_Number
;
152 Second
: Second_Number
;
153 Sub_Second
: Second_Duration
;
155 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
165 Year
: out Year_Number
;
166 Month
: out Month_Number
;
167 Day
: out Day_Number
;
168 Hour
: out Hour_Number
;
169 Minute
: out Minute_Number
;
170 Second
: out Second_Number
;
171 Sub_Second
: out Second_Duration
)
173 Day_Secs
: Day_Duration
;
177 Split
(Date
, Year
, Month
, Day
, Day_Secs
);
179 Secs
:= (if Day_Secs
= 0.0 then 0 else Natural (Day_Secs
- 0.5));
180 Sub_Second
:= Second_Duration
(Day_Secs
- Day_Duration
(Secs
));
181 Hour
:= Hour_Number
(Secs
/ 3_600
);
182 Secs
:= Secs
mod 3_600
;
183 Minute
:= Minute_Number
(Secs
/ 60);
184 Second
:= Second_Number
(Secs
mod 60);
187 ---------------------
188 -- Split_At_Locale --
189 ---------------------
191 procedure Split_At_Locale
193 Year
: out Year_Number
;
194 Month
: out Month_Number
;
195 Day
: out Day_Number
;
196 Hour
: out Hour_Number
;
197 Minute
: out Minute_Number
;
198 Second
: out Second_Number
;
199 Sub_Second
: out Second_Duration
)
201 procedure Ada_Calendar_Split
203 Year
: out Year_Number
;
204 Month
: out Month_Number
;
205 Day
: out Day_Number
;
206 Day_Secs
: out Day_Duration
;
208 Minute
: out Integer;
209 Second
: out Integer;
210 Sub_Sec
: out Duration;
211 Leap_Sec
: out Boolean;
213 Is_Historic
: Boolean;
214 Time_Zone
: Long_Integer);
215 pragma Import
(Ada
, Ada_Calendar_Split
, "__gnat_split");
221 -- Even though the input time zone is UTC (0), the flag Use_TZ will
222 -- ensure that Split picks up the local time zone. ???But Use_TZ is
223 -- False below, and anyway, Use_TZ has no effect if Time_Zone is 0.
234 Sub_Sec
=> Sub_Second
,
237 Is_Historic
=> False,
245 function Sub_Second
(Date
: Time
) return Second_Duration
is
247 Month
: Month_Number
;
250 Minute
: Minute_Number
;
251 Second
: Second_Number
;
252 Sub_Second
: Second_Duration
;
254 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
264 Month
: Month_Number
;
267 Minute
: Minute_Number
;
268 Second
: Second_Number
;
269 Sub_Second
: Second_Duration
:= 0.0) return Time
271 Day_Secs
: constant Day_Duration
:=
272 Day_Duration
(Hour
* 3_600
) +
273 Day_Duration
(Minute
* 60) +
274 Day_Duration
(Second
) +
277 return Time_Of
(Year
, Month
, Day
, Day_Secs
);
280 -----------------------
281 -- Time_Of_At_Locale --
282 -----------------------
284 function Time_Of_At_Locale
286 Month
: Month_Number
;
289 Minute
: Minute_Number
;
290 Second
: Second_Number
;
291 Sub_Second
: Second_Duration
:= 0.0) return Time
293 function Ada_Calendar_Time_Of
295 Month
: Month_Number
;
297 Day_Secs
: Day_Duration
;
303 Use_Day_Secs
: Boolean;
305 Is_Historic
: Boolean;
306 Time_Zone
: Long_Integer) return Time
;
307 pragma Import
(Ada
, Ada_Calendar_Time_Of
, "__gnat_time_of");
310 -- Even though the input time zone is UTC (0), the flag Use_TZ will
311 -- ensure that Split picks up the local time zone. ???But there is no
312 -- call to Split here.
323 Sub_Sec
=> Sub_Second
,
325 Use_Day_Secs
=> False,
327 Is_Historic
=> False,
329 end Time_Of_At_Locale
;
335 function To_Duration
(T
: not null access timeval
) return Duration is
337 procedure timeval_to_duration
338 (T
: not null access timeval
;
339 sec
: not null access C
.Extensions
.long_long
;
340 usec
: not null access C
.long
);
341 pragma Import
(C
, timeval_to_duration
, "__gnat_timeval_to_duration");
343 Micro
: constant := 10**6;
344 sec
: aliased C
.Extensions
.long_long
;
345 usec
: aliased C
.long
;
348 timeval_to_duration
(T
, sec
'Access, usec
'Access);
349 pragma Annotate
(CodePeer
, Modified
, sec
);
350 pragma Annotate
(CodePeer
, Modified
, usec
);
352 return Duration (sec
) + Duration (usec
) / Micro
;
359 function To_Timeval
(D
: Duration) return timeval
is
361 procedure duration_to_timeval
362 (Sec
: C
.Extensions
.long_long
;
364 T
: not null access timeval
);
365 pragma Import
(C
, duration_to_timeval
, "__gnat_duration_to_timeval");
367 Micro
: constant := 10**6;
368 Result
: aliased timeval
;
369 sec
: C
.Extensions
.long_long
;
377 sec
:= C
.Extensions
.long_long
(D
- 0.5);
378 usec
:= C
.long
((D
- Duration (sec
)) * Micro
- 0.5);
381 duration_to_timeval
(sec
, usec
, Result
'Access);
390 function Week_In_Year
(Date
: Time
) return Week_In_Year_Number
is
392 Week
: Week_In_Year_Number
;
394 Year_Week_In_Year
(Date
, Year
, Week
);
398 -----------------------
399 -- Year_Week_In_Year --
400 -----------------------
402 procedure Year_Week_In_Year
404 Year
: out Year_Number
;
405 Week
: out Week_In_Year_Number
)
407 Month
: Month_Number
;
410 Minute
: Minute_Number
;
411 Second
: Second_Number
;
412 Sub_Second
: Second_Duration
;
414 Shift
: Week_In_Year_Number
;
415 Start_Week
: Week_In_Year_Number
;
417 function Is_Leap
(Year
: Year_Number
) return Boolean;
418 -- Return True if Year denotes a leap year. Leap centennial years are
421 function Jan_1_Day_Of_Week
424 Last_Year
: Boolean := False;
425 Next_Year
: Boolean := False) return Day_Name
;
426 -- Given the weekday of January 1 in Year, determine the weekday on
427 -- which January 1 fell last year or will fall next year as set by
428 -- the two flags. This routine does not call Time_Of or Split.
430 function Last_Year_Has_53_Weeks
432 Year
: Year_Number
) return Boolean;
433 -- Given the weekday of January 1 in Year, determine whether last year
434 -- has 53 weeks. A False value implies that the year has 52 weeks.
440 function Is_Leap
(Year
: Year_Number
) return Boolean is
442 if Year
mod 400 = 0 then
444 elsif Year
mod 100 = 0 then
447 return Year
mod 4 = 0;
451 -----------------------
452 -- Jan_1_Day_Of_Week --
453 -----------------------
455 function Jan_1_Day_Of_Week
458 Last_Year
: Boolean := False;
459 Next_Year
: Boolean := False) return Day_Name
461 Shift
: Integer := 0;
465 Shift
:= (if Is_Leap
(Year
- 1) then -2 else -1);
467 Shift
:= (if Is_Leap
(Year
) then 2 else 1);
470 return Day_Name
'Val ((Day_Name
'Pos (Jan_1
) + Shift
) mod 7);
471 end Jan_1_Day_Of_Week
;
473 ----------------------------
474 -- Last_Year_Has_53_Weeks --
475 ----------------------------
477 function Last_Year_Has_53_Weeks
479 Year
: Year_Number
) return Boolean
481 Last_Jan_1
: constant Day_Name
:=
482 Jan_1_Day_Of_Week
(Jan_1
, Year
, Last_Year
=> True);
485 -- These two cases are illustrated in the table below
488 Last_Jan_1
= Thursday
489 or else (Last_Jan_1
= Wednesday
and then Is_Leap
(Year
- 1));
490 end Last_Year_Has_53_Weeks
;
492 -- Start of processing for Week_In_Year
495 Split
(Date
, Year
, Month
, Day
, Hour
, Minute
, Second
, Sub_Second
);
497 -- According to ISO 8601, the first week of year Y is the week that
498 -- contains the first Thursday in year Y. The following table contains
499 -- all possible combinations of years and weekdays along with examples.
501 -- +-------+------+-------+---------+
502 -- | Jan 1 | Leap | Weeks | Example |
503 -- +-------+------+-------+---------+
504 -- | Mon | No | 52 | 2007 |
505 -- +-------+------+-------+---------+
506 -- | Mon | Yes | 52 | 1996 |
507 -- +-------+------+-------+---------+
508 -- | Tue | No | 52 | 2002 |
509 -- +-------+------+-------+---------+
510 -- | Tue | Yes | 52 | 1980 |
511 -- +-------+------+-------+---------+
512 -- | Wed | No | 52 | 2003 |
513 -- +-------+------#########---------+
514 -- | Wed | Yes # 53 # 1992 |
515 -- +-------+------#-------#---------+
516 -- | Thu | No # 53 # 1998 |
517 -- +-------+------#-------#---------+
518 -- | Thu | Yes # 53 # 2004 |
519 -- +-------+------#########---------+
520 -- | Fri | No | 52 | 1999 |
521 -- +-------+------+-------+---------+
522 -- | Fri | Yes | 52 | 1988 |
523 -- +-------+------+-------+---------+
524 -- | Sat | No | 52 | 1994 |
525 -- +-------+------+-------+---------+
526 -- | Sat | Yes | 52 | 1972 |
527 -- +-------+------+-------+---------+
528 -- | Sun | No | 52 | 1995 |
529 -- +-------+------+-------+---------+
530 -- | Sun | Yes | 52 | 1956 |
531 -- +-------+------+-------+---------+
533 -- A small optimization, the input date is January 1. Note that this
534 -- is a key day since it determines the number of weeks and is used
535 -- when special casing the first week of January and the last week of
538 Jan_1
:= Day_Of_Week
(if Day
= 1 and then Month
= 1
540 else (Time_Of
(Year
, 1, 1, 0.0)));
542 -- Special cases for January
546 -- Special case 1: January 1, 2 and 3. These three days may belong
547 -- to last year's last week which can be week number 52 or 53.
549 -- +-----+-----+-----+=====+-----+-----+-----+
550 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
551 -- +-----+-----+-----+-----+-----+-----+-----+
552 -- | 26 | 27 | 28 # 29 # 30 | 31 | 1 |
553 -- +-----+-----+-----+-----+-----+-----+-----+
554 -- | 27 | 28 | 29 # 30 # 31 | 1 | 2 |
555 -- +-----+-----+-----+-----+-----+-----+-----+
556 -- | 28 | 29 | 30 # 31 # 1 | 2 | 3 |
557 -- +-----+-----+-----+=====+-----+-----+-----+
559 if (Day
= 1 and then Jan_1
in Friday
.. Sunday
)
561 (Day
= 2 and then Jan_1
in Friday
.. Saturday
)
563 (Day
= 3 and then Jan_1
= Friday
)
565 Week
:= (if Last_Year_Has_53_Weeks
(Jan_1
, Year
) then 53 else 52);
567 -- January 1, 2 and 3 belong to the previous year
572 -- Special case 2: January 1, 2, 3, 4, 5, 6 and 7 of the first week
574 -- +-----+-----+-----+=====+-----+-----+-----+
575 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
576 -- +-----+-----+-----+-----+-----+-----+-----+
577 -- | 29 | 30 | 31 # 1 # 2 | 3 | 4 |
578 -- +-----+-----+-----+-----+-----+-----+-----+
579 -- | 30 | 31 | 1 # 2 # 3 | 4 | 5 |
580 -- +-----+-----+-----+-----+-----+-----+-----+
581 -- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
582 -- +-----+-----+-----+-----+-----+-----+-----+
583 -- | 1 | 2 | 3 # 4 # 5 | 6 | 7 |
584 -- +-----+-----+-----+=====+-----+-----+-----+
586 elsif (Day
<= 4 and then Jan_1
in Monday
.. Thursday
)
588 (Day
= 5 and then Jan_1
in Monday
.. Wednesday
)
590 (Day
= 6 and then Jan_1
in Monday
.. Tuesday
)
592 (Day
= 7 and then Jan_1
= Monday
)
598 -- Month other than 1
600 -- Special case 3: December 29, 30 and 31. These days may belong to
601 -- next year's first week.
603 -- +-----+-----+-----+=====+-----+-----+-----+
604 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
605 -- +-----+-----+-----+-----+-----+-----+-----+
606 -- | 29 | 30 | 31 # 1 # 2 | 3 | 4 |
607 -- +-----+-----+-----+-----+-----+-----+-----+
608 -- | 30 | 31 | 1 # 2 # 3 | 4 | 5 |
609 -- +-----+-----+-----+-----+-----+-----+-----+
610 -- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
611 -- +-----+-----+-----+=====+-----+-----+-----+
613 elsif Month
= 12 and then Day
> 28 then
615 Next_Jan_1
: constant Day_Name
:=
616 Jan_1_Day_Of_Week
(Jan_1
, Year
, Next_Year
=> True);
618 if (Day
= 29 and then Next_Jan_1
= Thursday
)
620 (Day
= 30 and then Next_Jan_1
in Wednesday
.. Thursday
)
622 (Day
= 31 and then Next_Jan_1
in Tuesday
.. Thursday
)
631 -- Determine the week from which to start counting. If January 1 does
632 -- not belong to the first week of the input year, then the next week
633 -- is the first week.
635 Start_Week
:= (if Jan_1
in Friday
.. Sunday
then 1 else 2);
637 -- At this point all special combinations have been accounted for and
638 -- the proper start week has been found. Since January 1 may not fall
639 -- on a Monday, shift 7 - Day_Name'Pos (Jan_1). This action ensures an
640 -- origin which falls on Monday.
642 Shift
:= 7 - Day_Name
'Pos (Jan_1
);
643 Week
:= Start_Week
+ (Day_In_Year
(Date
) - Shift
- 1) / 7;
644 end Year_Week_In_Year
;