Day of the week: Difference between revisions

No edit summary
Line 977:
Between 2008 and 2121, Christmas day falls on a Sunday on the following years:
2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
</pre>
===Comparison of Some Common Day-of-Week Algorithms===
The following program exercises some common &quot;Day-0f-Week&quot; algorithms to confirm they all arrive at the same result.
<lang NetRexx>/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
days = 'Monday Tuesday Wednesday Thursday Friday Saturday Sunday'
yearRanges = [int 2008, 2121]
 
searchday = ''
searchday['index'] = days.wordpos('Sunday')
searchday[0] = 0
 
algorithmName = ['Java Calendar', 'Zeller[1]', 'Zeller[2]', 'Sakamoto', 'Gauss', 'Keith', 'Babwani']
 
loop alg = 0 to algorithmName.length - 1
sd = searchday[0] + 1
searchday[0] = sd
searchday['agorithm', sd] = algorithmName[alg]
loop year = yearRanges[0] to yearRanges[1]
select case alg
when 0 then dayIndex = getDaynumJavaLibrary(year, 12, 25)
when 1 then dayIndex = getDaynumZellersCongruenceMethod1(year, 12, 25)
when 2 then dayIndex = getDaynumZellersCongruenceMethod2(year, 12, 25)
when 3 then dayIndex = getDaynumSakamoto(year, 12, 25)
when 4 then dayIndex = getDaynumGauss(year, 12, 25)
when 5 then dayIndex = getDaynumKeith(year, 12, 25)
when 6 then dayIndex = getDaynumBabwani(year, 12, 25)
otherwise nop
end
if dayIndex = searchday['index'] then
searchday[sd] = searchday[sd] year
end year
end alg
 
-- display results
say 'Between' yearRanges[0] 'and' yearRanges[1]', Christmas day falls on a Sunday in the following years:'
loop r_ = 1 to searchday[0]
searchday[r_] = searchday[r_].strip.changestr(' ', ',')
say searchday['agorithm', r_].right(20)':' searchday[r_]
end r_
 
return
 
-- -----------------------------------------------------------------------------
method getDaynumJavaLibrary(Year = int, Month = int, Day = int, iso = Rexx 'Y') public static binary returns int
-- The day-of-week is an integer value where 1 is Sunday, 2 is Monday, ..., and 7 is Saturday
-- For an ISO week date Day-of-Week d (1 = Monday to 7 = Sunday), use d = ((h - 1 + 6) mod 7) + 1
 
cal = Calendar
jmNumber = [ -
Calendar.JANUARY, Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL -
, Calendar.MAY, Calendar.JUNE, Calendar.JULY, Calendar.AUGUST -
, Calendar.SEPTEMBER, Calendar.OCTOBER, Calendar.NOVEMBER, Calendar.DECEMBER -
]
 
mon = jmNumber[Month - 1]
cal = GregorianCalendar(Year, mon, Day)
h = cal.get(Calendar.DAY_OF_WEEK)
 
if 'YES'.abbrev(iso.upper, 1) then w = ((h - 1 + 6) // 7) + 1
else w = h
 
return w
 
-- -----------------------------------------------------------------------------
method getDaynumZellersCongruenceMethod1(Year = int, Month = int, Day = int, iso = Rexx 'Y') public static returns int
-- DayNum results in an integer in the range 0-6 where 0 represents Monday etc.
-- For an ISO week date add 1
 
if Month = 1 | Month = 2 then do
Month = Month + 12
Year = Year - 1
end
 
MonthFactor = 2 * Month + 3 * (Month + 1) % 5
YearFactor = Year + Year % 4 - Year % 100 + Year % 400
DayNum = (Day + MonthFactor + YearFactor) // 7
 
if 'YES'.abbrev(iso.upper, 1) then d = DayNum + 1
else d = DayNum
 
return d
 
-- -----------------------------------------------------------------------------
method getDaynumZellersCongruenceMethod2(Year = int, Month = int, Day = int, iso = Rexx 'Y') public static binary returns int
-- h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ...)
-- For an ISO week date Day-of-Week d (1 = Monday to 7 = Sunday), use d = ((h + 5) mod 7) + 1
 
if Month < 3 then do
Month = Month + 12
Year = Year - 1
end
q = Day
m = Month
Y = Year
 
h = (q + ((m + 1) * 26 % 10) + Y + (Y % 4) + 6 * (Y % 100) + (Y % 400)) // 7
 
if 'YES'.abbrev(iso.upper, 1) then d = ((h + 5) // 7) + 1
else d = h
 
return d
 
-- -----------------------------------------------------------------------------
method getDaynumSakamoto(y = int, m = int, d = int, iso = Rexx 'Y') public static binary returns int
-- h is the day of the week (0 = Sunday, 1 = Monday, 2 = Tuesday...)
-- For an ISO week date Day-of-Week d (1 = Monday to 7 = Sunday), use d = ((h + 6) mod 7) + 1
 
t = [int 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
y = y - (m < 3)
h = (y + y % 4 - y % 100 + y % 400 + t[m - 1] + d) // 7
 
if 'YES'.abbrev(iso.upper, 1) then d = ((h + 6) // 7) + 1
else d = h
 
return d
 
-- -----------------------------------------------------------------------------
method getDaynumGauss(Year = int, Month = int, Day = int, iso = Rexx 'Y') public static binary returns int
-- W is week day (0 = Sunday, ..., 6 = Saturday)
-- For an ISO week date Day-of-Week d (1 = Monday to 7 = Sunday), use d = ((h + 6) mod 7) + 1
 
Year = Year - (Month < 3)
k = double Day
C = double Year % 100
Y = double Year // 100
m = double ((Month + 9) // 12) + 1
 
W = modulo(int (k + Math.floor(2.6 * m - 0.2) + y + Math.floor(y / 4) + Math.floor(c / 4) - 2 * c), 7)
 
if 'YES'.abbrev(iso.upper, 1) then h = ((W + 6) // 7) + 1
else h = W
 
return h
 
-- -----------------------------------------------------------------------------
method getDaynumKeith(y = int, m = int, d = int, iso = Rexx 'Y') public constant binary returns int
-- W is week day (0 = Sunday, ..., 6 = Saturday)
-- For an ISO week date Day-of-Week d (1 = Monday to 7 = Sunday), use d = ((h + 6) mod 7) + 1
 
if m < 3 then do
d = d + y
y = y - 1
end
else do
d = d + y - 2
end
 
h = (23 * m % 9 + d + 4 + y % 4 - y % 100 + y % 400) // 7
 
if 'YES'.abbrev(iso.upper, 1) then W = ((h + 6) // 7) + 1
else W = h
 
return W
 
-- -----------------------------------------------------------------------------
method getDaynumBabwani(Year = int, Month = int, Day = int, iso = Rexx 'Y') public constant binary returns int
-- return dow = Day of week: 0 = Saturday, 1 = Sunday, ... 6 = Friday
-- For an ISO week date Day-of-Week W (1 = Monday to 7 = Sunday), use W = ((dow + 5) mod 7) + 1
 
y = Year
m = Month
d = Day
 
dow = int -- dow stands for day of week
dowfg = double
fmonth = int
leap = int
 
if ((y // 100 == 0) && (y // 400 \= 0)) then -- leap function 1 for leap & 0 for non-leap
leap = 0
else if (y // 4 == 0) then
leap = 1
else
leap = 0
 
fmonth = 3 + (2 - leap) * ((m + 2) % (2 * m)) + (5 * m + m % 9) % 2 -- f(m) formula
fmonth = fmonth // 7 -- f(m) is brought in range of 0 to 6
 
century = y % 100
lastdigits = y // 100
 
dowfg = 1.25 * lastdigits + fmonth + d - 2 * (century // 4) -- function of weekday for Gregorian
dow = int dowfg // 7 -- remainder on division by 7
 
if 'YES'.abbrev(iso.upper, 1) then W = ((dow + 5) // 7) + 1
else W = dow
 
return W
 
-- -----------------------------------------------------------------------------
method modulo(N = int, D = int) inheritable static binary returns int
return (D + (N // D)) // D
</lang>
;Output
<pre>
Between 2008 and 2121, Christmas day falls on a Sunday in the following years:
Java Calendar: 2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
Zeller[1]: 2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
Zeller[2]: 2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
Sakamoto: 2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
Gauss: 2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
Keith: 2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
Babwani: 2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118
</pre>
 
Anonymous user