Day of the week: Difference between revisions

Line 583:
 
=={{header|Forth}}==
{{incorrect|Forth|While the program will actually solve the task, the weekday function is wrong and won't work for january or february (the "year-1 month+12" trick is not correctly implemented).}}
 
Forth has only TIME&DATE, which does not give day of week. Many public Forth Julian date calculators had year-2100 problems, but this algorithm works well.
<lang forth>\ Zeller's Congruence
\ Zeller's Congruence
: zeller ( m -- days since March 1 )
: weekday 9( +d 12m mody 1-- wd) \ 26 10 */ 31 +mon..7 ;sun
over 3 < if 1- swap 12 + swap then
 
1+ 7100 /mod ;
: weekday ( d m y -- 0..6 ) \ Monday..Sunday
overdup 34 </ ifswap 1-2* then-
swap dup 4 / + +
overswap 1001+ 13 5 */ -+ +
( in zeller 0=sat, so -2 to 0= mon, then mod, then 1+ for 1=mon)
over 400 / + +
swap2- zeller7 +mod 1+ ;
1+ 7 mod ;
 
: yuletide
." December 25 is Sunday in "
2122 2008 do
25 12 i weekday
67 = if i . then
loop cr ;
</lang>
 
<lang forth>
cr yuletide
December 25 is Sunday in 2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</lang>
ok
</lang>
 
cr yuletide
December 25 is Sunday in 2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</lang>
 
To show year-2100 problems with SwiftForth's provided Modified Julian Day support: