-- --
-- B o d y --
-- --
--- Copyright (C) 1999-2007, AdaCore --
+-- Copyright (C) 1999-2009, AdaCore --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
begin
Split (Date, Year, Month, Day, Day_Secs);
- if Day_Secs = 0.0 then
- Secs := 0;
- else
- Secs := Natural (Day_Secs - 0.5);
- end if;
-
+ Secs := (if Day_Secs = 0.0 then 0 else Natural (Day_Secs - 0.5));
Sub_Second := Second_Duration (Day_Secs - Day_Duration (Secs));
Hour := Hour_Number (Secs / 3_600);
Secs := Secs mod 3_600;
------------------
function Week_In_Year (Date : Time) return Week_In_Year_Number is
- Year : Year_Number;
+ Year : Year_Number;
+ Week : Week_In_Year_Number;
+ pragma Unreferenced (Year);
+ begin
+ Year_Week_In_Year (Date, Year, Week);
+ return Week;
+ end Week_In_Year;
+
+ -----------------------
+ -- Year_Week_In_Year --
+ -----------------------
+
+ procedure Year_Week_In_Year
+ (Date : Time;
+ Year : out Year_Number;
+ Week : out Week_In_Year_Number)
+ is
Month : Month_Number;
Day : Day_Number;
Hour : Hour_Number;
pragma Unreferenced (Hour, Minute, Second, Sub_Second);
function Is_Leap (Year : Year_Number) return Boolean;
- -- Return True if Year denotes a leap year. Leap centential years are
+ -- Return True if Year denotes a leap year. Leap centennial years are
-- properly handled.
function Jan_1_Day_Of_Week
begin
if Last_Year then
- if Is_Leap (Year - 1) then
- Shift := -2;
- else
- Shift := -1;
- end if;
-
+ Shift := (if Is_Leap (Year - 1) then -2 else -1);
elsif Next_Year then
- if Is_Leap (Year) then
- Shift := 2;
- else
- Shift := 1;
- end if;
+ Shift := (if Is_Leap (Year) then 2 else 1);
end if;
return Day_Name'Val ((Day_Name'Pos (Jan_1) + Shift) mod 7);
is
Last_Jan_1 : constant Day_Name :=
Jan_1_Day_Of_Week (Jan_1, Year, Last_Year => True);
+
begin
-- These two cases are illustrated in the table below
return
Last_Jan_1 = Thursday
- or else
- (Last_Jan_1 = Wednesday
- and then Is_Leap (Year - 1));
+ or else (Last_Jan_1 = Wednesday and then Is_Leap (Year - 1));
end Last_Year_Has_53_Weeks;
-- Start of processing for Week_In_Year
-- when special casing the first week of January and the last week of
-- December.
- if Day = 1
- and then Month = 1
- then
- Jan_1 := Day_Of_Week (Date);
- else
- Jan_1 := Day_Of_Week (Time_Of (Year, 1, 1, 0.0));
- end if;
+ Jan_1 := Day_Of_Week (if Day = 1 and then Month = 1
+ then Date
+ else (Time_Of (Year, 1, 1, 0.0)));
+
+ -- Special cases for January
if Month = 1 then
-- +-----+-----+-----+=====+-----+-----+-----+
if (Day = 1 and then Jan_1 in Friday .. Sunday)
- or else
+ or else
(Day = 2 and then Jan_1 in Friday .. Saturday)
- or else
+ or else
(Day = 3 and then Jan_1 = Friday)
then
- if Last_Year_Has_53_Weeks (Jan_1, Year) then
- return 53;
- else
- return 52;
- end if;
+ Week := (if Last_Year_Has_53_Weeks (Jan_1, Year) then 53 else 52);
+
+ -- January 1, 2 and 3 belong to the previous year
- -- Special case 2: January 1, 2, 3, 4, 5 and 6 of the first week. In
- -- this scenario January 1 does not fall on a Monday.
+ Year := Year - 1;
+ return;
+
+ -- Special case 2: January 1, 2, 3, 4, 5, 6 and 7 of the first week
-- +-----+-----+-----+=====+-----+-----+-----+
-- | Mon | Tue | Wed # Thu # Fri | Sat | Sun |
-- +-----+-----+-----+-----+-----+-----+-----+
-- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
-- +-----+-----+-----+-----+-----+-----+-----+
+ -- | 1 | 2 | 3 # 4 # 5 | 6 | 7 |
+ -- +-----+-----+-----+=====+-----+-----+-----+
- elsif (Day <= 4 and then Jan_1 in Tuesday .. Thursday)
- or else
- (Day = 5 and then Jan_1 in Tuesday .. Wednesday)
- or else
- (Day = 6 and then Jan_1 = Tuesday)
+ elsif (Day <= 4 and then Jan_1 in Monday .. Thursday)
+ or else
+ (Day = 5 and then Jan_1 in Monday .. Wednesday)
+ or else
+ (Day = 6 and then Jan_1 in Monday .. Tuesday)
+ or else
+ (Day = 7 and then Jan_1 = Monday)
then
- return 1;
+ Week := 1;
+ return;
end if;
+ -- Month other than 1
+
-- Special case 3: December 29, 30 and 31. These days may belong to
-- next year's first week.
-- | 31 | 1 | 2 # 3 # 4 | 5 | 6 |
-- +-----+-----+-----+=====+-----+-----+-----+
- elsif Month = 12
- and then Day > 28
- then
+ elsif Month = 12 and then Day > 28 then
declare
Next_Jan_1 : constant Day_Name :=
Jan_1_Day_Of_Week (Jan_1, Year, Next_Year => True);
begin
if (Day = 29 and then Next_Jan_1 = Thursday)
- or else
+ or else
(Day = 30 and then Next_Jan_1 in Wednesday .. Thursday)
- or else
+ or else
(Day = 31 and then Next_Jan_1 in Tuesday .. Thursday)
then
- return 1;
+ Year := Year + 1;
+ Week := 1;
+ return;
end if;
end;
end if;
-- not belong to the first week of the input year, then the next week
-- is the first week.
- if Jan_1 in Friday .. Sunday then
- Start_Week := 1;
- else
- Start_Week := 2;
- end if;
+ Start_Week := (if Jan_1 in Friday .. Sunday then 1 else 2);
-- At this point all special combinations have been accounted for and
-- the proper start week has been found. Since January 1 may not fall
-- origin which falls on Monday.
Shift := 7 - Day_Name'Pos (Jan_1);
- return Start_Week + (Day_In_Year (Date) - Shift - 1) / 7;
- end Week_In_Year;
+ Week := Start_Week + (Day_In_Year (Date) - Shift - 1) / 7;
+ end Year_Week_In_Year;
end GNAT.Calendar;