PR target/35485
[official-gcc.git] / gcc / ada / g-calend.adb
blob9d3c33d3d35947d684a94f5d6a363e53a408da7a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . C A L E N D A R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2008, AdaCore --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 package body GNAT.Calendar is
36 use Ada.Calendar;
37 use Interfaces;
39 -----------------
40 -- Day_In_Year --
41 -----------------
43 function Day_In_Year (Date : Time) return Day_In_Year_Number is
44 Year : Year_Number;
45 Month : Month_Number;
46 Day : Day_Number;
47 Day_Secs : Day_Duration;
48 pragma Unreferenced (Day_Secs);
49 begin
50 Split (Date, Year, Month, Day, Day_Secs);
51 return Julian_Day (Year, Month, Day) - Julian_Day (Year, 1, 1) + 1;
52 end Day_In_Year;
54 -----------------
55 -- Day_Of_Week --
56 -----------------
58 function Day_Of_Week (Date : Time) return Day_Name is
59 Year : Year_Number;
60 Month : Month_Number;
61 Day : Day_Number;
62 Day_Secs : Day_Duration;
63 pragma Unreferenced (Day_Secs);
64 begin
65 Split (Date, Year, Month, Day, Day_Secs);
66 return Day_Name'Val ((Julian_Day (Year, Month, Day)) mod 7);
67 end Day_Of_Week;
69 ----------
70 -- Hour --
71 ----------
73 function Hour (Date : Time) return Hour_Number is
74 Year : Year_Number;
75 Month : Month_Number;
76 Day : Day_Number;
77 Hour : Hour_Number;
78 Minute : Minute_Number;
79 Second : Second_Number;
80 Sub_Second : Second_Duration;
81 pragma Unreferenced (Year, Month, Day, Minute, Second, Sub_Second);
82 begin
83 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
84 return Hour;
85 end Hour;
87 ----------------
88 -- Julian_Day --
89 ----------------
91 -- Julian_Day is used to by Day_Of_Week and Day_In_Year. Note that this
92 -- implementation is not expensive.
94 function Julian_Day
95 (Year : Year_Number;
96 Month : Month_Number;
97 Day : Day_Number) return Integer
99 Internal_Year : Integer;
100 Internal_Month : Integer;
101 Internal_Day : Integer;
102 Julian_Date : Integer;
103 C : Integer;
104 Ya : Integer;
106 begin
107 Internal_Year := Integer (Year);
108 Internal_Month := Integer (Month);
109 Internal_Day := Integer (Day);
111 if Internal_Month > 2 then
112 Internal_Month := Internal_Month - 3;
113 else
114 Internal_Month := Internal_Month + 9;
115 Internal_Year := Internal_Year - 1;
116 end if;
118 C := Internal_Year / 100;
119 Ya := Internal_Year - (100 * C);
121 Julian_Date := (146_097 * C) / 4 +
122 (1_461 * Ya) / 4 +
123 (153 * Internal_Month + 2) / 5 +
124 Internal_Day + 1_721_119;
126 return Julian_Date;
127 end Julian_Day;
129 ------------
130 -- Minute --
131 ------------
133 function Minute (Date : Time) return Minute_Number is
134 Year : Year_Number;
135 Month : Month_Number;
136 Day : Day_Number;
137 Hour : Hour_Number;
138 Minute : Minute_Number;
139 Second : Second_Number;
140 Sub_Second : Second_Duration;
141 pragma Unreferenced (Year, Month, Day, Hour, Second, Sub_Second);
142 begin
143 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
144 return Minute;
145 end Minute;
147 ------------
148 -- Second --
149 ------------
151 function Second (Date : Time) return Second_Number is
152 Year : Year_Number;
153 Month : Month_Number;
154 Day : Day_Number;
155 Hour : Hour_Number;
156 Minute : Minute_Number;
157 Second : Second_Number;
158 Sub_Second : Second_Duration;
159 pragma Unreferenced (Year, Month, Day, Hour, Minute, Sub_Second);
160 begin
161 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
162 return Second;
163 end Second;
165 -----------
166 -- Split --
167 -----------
169 procedure Split
170 (Date : Time;
171 Year : out Year_Number;
172 Month : out Month_Number;
173 Day : out Day_Number;
174 Hour : out Hour_Number;
175 Minute : out Minute_Number;
176 Second : out Second_Number;
177 Sub_Second : out Second_Duration)
179 Day_Secs : Day_Duration;
180 Secs : Natural;
182 begin
183 Split (Date, Year, Month, Day, Day_Secs);
185 if Day_Secs = 0.0 then
186 Secs := 0;
187 else
188 Secs := Natural (Day_Secs - 0.5);
189 end if;
191 Sub_Second := Second_Duration (Day_Secs - Day_Duration (Secs));
192 Hour := Hour_Number (Secs / 3_600);
193 Secs := Secs mod 3_600;
194 Minute := Minute_Number (Secs / 60);
195 Second := Second_Number (Secs mod 60);
196 end Split;
198 ----------------
199 -- Sub_Second --
200 ----------------
202 function Sub_Second (Date : Time) return Second_Duration is
203 Year : Year_Number;
204 Month : Month_Number;
205 Day : Day_Number;
206 Hour : Hour_Number;
207 Minute : Minute_Number;
208 Second : Second_Number;
209 Sub_Second : Second_Duration;
210 pragma Unreferenced (Year, Month, Day, Hour, Minute, Second);
211 begin
212 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
213 return Sub_Second;
214 end Sub_Second;
216 -------------
217 -- Time_Of --
218 -------------
220 function Time_Of
221 (Year : Year_Number;
222 Month : Month_Number;
223 Day : Day_Number;
224 Hour : Hour_Number;
225 Minute : Minute_Number;
226 Second : Second_Number;
227 Sub_Second : Second_Duration := 0.0) return Time
230 Day_Secs : constant Day_Duration :=
231 Day_Duration (Hour * 3_600) +
232 Day_Duration (Minute * 60) +
233 Day_Duration (Second) +
234 Sub_Second;
235 begin
236 return Time_Of (Year, Month, Day, Day_Secs);
237 end Time_Of;
239 -----------------
240 -- To_Duration --
241 -----------------
243 function To_Duration (T : not null access timeval) return Duration is
245 procedure timeval_to_duration
246 (T : not null access timeval;
247 sec : not null access C.long;
248 usec : not null access C.long);
249 pragma Import (C, timeval_to_duration, "__gnat_timeval_to_duration");
251 Micro : constant := 10**6;
252 sec : aliased C.long;
253 usec : aliased C.long;
255 begin
256 timeval_to_duration (T, sec'Access, usec'Access);
257 return Duration (sec) + Duration (usec) / Micro;
258 end To_Duration;
260 ----------------
261 -- To_Timeval --
262 ----------------
264 function To_Timeval (D : Duration) return timeval is
266 procedure duration_to_timeval
267 (Sec : C.long;
268 Usec : C.long;
269 T : not null access timeval);
270 pragma Import (C, duration_to_timeval, "__gnat_duration_to_timeval");
272 Micro : constant := 10**6;
273 Result : aliased timeval;
274 sec : C.long;
275 usec : C.long;
277 begin
278 if D = 0.0 then
279 sec := 0;
280 usec := 0;
281 else
282 sec := C.long (D - 0.5);
283 usec := C.long ((D - Duration (sec)) * Micro - 0.5);
284 end if;
286 duration_to_timeval (sec, usec, Result'Access);
288 return Result;
289 end To_Timeval;
291 ------------------
292 -- Week_In_Year --
293 ------------------
295 function Week_In_Year (Date : Time) return Week_In_Year_Number is
296 Year : Year_Number;
297 Month : Month_Number;
298 Day : Day_Number;
299 Hour : Hour_Number;
300 Minute : Minute_Number;
301 Second : Second_Number;
302 Sub_Second : Second_Duration;
303 Jan_1 : Day_Name;
304 Shift : Week_In_Year_Number;
305 Start_Week : Week_In_Year_Number;
307 pragma Unreferenced (Hour, Minute, Second, Sub_Second);
309 function Is_Leap (Year : Year_Number) return Boolean;
310 -- Return True if Year denotes a leap year. Leap centennial years are
311 -- properly handled.
313 function Jan_1_Day_Of_Week
314 (Jan_1 : Day_Name;
315 Year : Year_Number;
316 Last_Year : Boolean := False;
317 Next_Year : Boolean := False) return Day_Name;
318 -- Given the weekday of January 1 in Year, determine the weekday on
319 -- which January 1 fell last year or will fall next year as set by
320 -- the two flags. This routine does not call Time_Of or Split.
322 function Last_Year_Has_53_Weeks
323 (Jan_1 : Day_Name;
324 Year : Year_Number) return Boolean;
325 -- Given the weekday of January 1 in Year, determine whether last year
326 -- has 53 weeks. A False value implies that the year has 52 weeks.
328 -------------
329 -- Is_Leap --
330 -------------
332 function Is_Leap (Year : Year_Number) return Boolean is
333 begin
334 if Year mod 400 = 0 then
335 return True;
336 elsif Year mod 100 = 0 then
337 return False;
338 else
339 return Year mod 4 = 0;
340 end if;
341 end Is_Leap;
343 -----------------------
344 -- Jan_1_Day_Of_Week --
345 -----------------------
347 function Jan_1_Day_Of_Week
348 (Jan_1 : Day_Name;
349 Year : Year_Number;
350 Last_Year : Boolean := False;
351 Next_Year : Boolean := False) return Day_Name
353 Shift : Integer := 0;
355 begin
356 if Last_Year then
357 if Is_Leap (Year - 1) then
358 Shift := -2;
359 else
360 Shift := -1;
361 end if;
363 elsif Next_Year then
364 if Is_Leap (Year) then
365 Shift := 2;
366 else
367 Shift := 1;
368 end if;
369 end if;
371 return Day_Name'Val ((Day_Name'Pos (Jan_1) + Shift) mod 7);
372 end Jan_1_Day_Of_Week;
374 ----------------------------
375 -- Last_Year_Has_53_Weeks --
376 ----------------------------
378 function Last_Year_Has_53_Weeks
379 (Jan_1 : Day_Name;
380 Year : Year_Number) return Boolean
382 Last_Jan_1 : constant Day_Name :=
383 Jan_1_Day_Of_Week (Jan_1, Year, Last_Year => True);
384 begin
385 -- These two cases are illustrated in the table below
387 return
388 Last_Jan_1 = Thursday
389 or else
390 (Last_Jan_1 = Wednesday
391 and then Is_Leap (Year - 1));
392 end Last_Year_Has_53_Weeks;
394 -- Start of processing for Week_In_Year
396 begin
397 Split (Date, Year, Month, Day, Hour, Minute, Second, Sub_Second);
399 -- According to ISO 8601, the first week of year Y is the week that
400 -- contains the first Thursday in year Y. The following table contains
401 -- all possible combinations of years and weekdays along with examples.
403 -- +-------+------+-------+---------+
404 -- | Jan 1 | Leap | Weeks | Example |
405 -- +-------+------+-------+---------+
406 -- | Mon | No | 52 | 2007 |
407 -- +-------+------+-------+---------+
408 -- | Mon | Yes | 52 | 1996 |
409 -- +-------+------+-------+---------+
410 -- | Tue | No | 52 | 2002 |
411 -- +-------+------+-------+---------+
412 -- | Tue | Yes | 52 | 1980 |
413 -- +-------+------+-------+---------+
414 -- | Wed | No | 52 | 2003 |
415 -- +-------+------#########---------+
416 -- | Wed | Yes # 53 # 1992 |
417 -- +-------+------#-------#---------+
418 -- | Thu | No # 53 # 1998 |
419 -- +-------+------#-------#---------+
420 -- | Thu | Yes # 53 # 2004 |
421 -- +-------+------#########---------+
422 -- | Fri | No | 52 | 1999 |
423 -- +-------+------+-------+---------+
424 -- | Fri | Yes | 52 | 1988 |
425 -- +-------+------+-------+---------+
426 -- | Sat | No | 52 | 1994 |
427 -- +-------+------+-------+---------+
428 -- | Sat | Yes | 52 | 1972 |
429 -- +-------+------+-------+---------+
430 -- | Sun | No | 52 | 1995 |
431 -- +-------+------+-------+---------+
432 -- | Sun | Yes | 52 | 1956 |
433 -- +-------+------+-------+---------+
435 -- A small optimization, the input date is January 1. Note that this
436 -- is a key day since it determines the number of weeks and is used
437 -- when special casing the first week of January and the last week of
438 -- December.
440 if Day = 1
441 and then Month = 1
442 then
443 Jan_1 := Day_Of_Week (Date);
444 else
445 Jan_1 := Day_Of_Week (Time_Of (Year, 1, 1, 0.0));
446 end if;
448 if Month = 1 then
450 -- Special case 1: January 1, 2 and 3. These three days may belong
451 -- to last year's last week which can be week number 52 or 53.
453 -- +-----+-----+-----+=====+-----+-----+-----+
454 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
455 -- +-----+-----+-----+-----+-----+-----+-----+
456 -- | 26 | 27 | 28 # 29 # 30 | 31 | 1 |
457 -- +-----+-----+-----+-----+-----+-----+-----+
458 -- | 27 | 28 | 29 # 30 # 31 | 1 | 2 |
459 -- +-----+-----+-----+-----+-----+-----+-----+
460 -- | 28 | 29 | 30 # 31 # 1 | 2 | 3 |
461 -- +-----+-----+-----+=====+-----+-----+-----+
463 if (Day = 1 and then Jan_1 in Friday .. Sunday)
464 or else
465 (Day = 2 and then Jan_1 in Friday .. Saturday)
466 or else
467 (Day = 3 and then Jan_1 = Friday)
468 then
469 if Last_Year_Has_53_Weeks (Jan_1, Year) then
470 return 53;
471 else
472 return 52;
473 end if;
475 -- Special case 2: January 1, 2, 3, 4, 5 and 6 of the first week. In
476 -- this scenario January 1 does not fall on a Monday.
478 -- +-----+-----+-----+=====+-----+-----+-----+
479 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
480 -- +-----+-----+-----+-----+-----+-----+-----+
481 -- | 29 | 30 | 31 # 1 # 2 | 3 | 4 |
482 -- +-----+-----+-----+-----+-----+-----+-----+
483 -- | 30 | 31 | 1 # 2 # 3 | 4 | 5 |
484 -- +-----+-----+-----+-----+-----+-----+-----+
485 -- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
486 -- +-----+-----+-----+-----+-----+-----+-----+
488 elsif (Day <= 4 and then Jan_1 in Tuesday .. Thursday)
489 or else
490 (Day = 5 and then Jan_1 in Tuesday .. Wednesday)
491 or else
492 (Day = 6 and then Jan_1 = Tuesday)
493 then
494 return 1;
495 end if;
497 -- Special case 3: December 29, 30 and 31. These days may belong to
498 -- next year's first week.
500 -- +-----+-----+-----+=====+-----+-----+-----+
501 -- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
502 -- +-----+-----+-----+-----+-----+-----+-----+
503 -- | 29 | 30 | 31 # 1 # 2 | 3 | 4 |
504 -- +-----+-----+-----+-----+-----+-----+-----+
505 -- | 30 | 31 | 1 # 2 # 3 | 4 | 5 |
506 -- +-----+-----+-----+-----+-----+-----+-----+
507 -- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
508 -- +-----+-----+-----+=====+-----+-----+-----+
510 elsif Month = 12
511 and then Day > 28
512 then
513 declare
514 Next_Jan_1 : constant Day_Name :=
515 Jan_1_Day_Of_Week (Jan_1, Year, Next_Year => True);
516 begin
517 if (Day = 29 and then Next_Jan_1 = Thursday)
518 or else
519 (Day = 30 and then Next_Jan_1 in Wednesday .. Thursday)
520 or else
521 (Day = 31 and then Next_Jan_1 in Tuesday .. Thursday)
522 then
523 return 1;
524 end if;
525 end;
526 end if;
528 -- Determine the week from which to start counting. If January 1 does
529 -- not belong to the first week of the input year, then the next week
530 -- is the first week.
532 if Jan_1 in Friday .. Sunday then
533 Start_Week := 1;
534 else
535 Start_Week := 2;
536 end if;
538 -- At this point all special combinations have been accounted for and
539 -- the proper start week has been found. Since January 1 may not fall
540 -- on a Monday, shift 7 - Day_Name'Pos (Jan_1). This action ensures an
541 -- origin which falls on Monday.
543 Shift := 7 - Day_Name'Pos (Jan_1);
544 return Start_Week + (Day_In_Year (Date) - Shift - 1) / 7;
545 end Week_In_Year;
547 end GNAT.Calendar;