Doomsday rule: Difference between revisions
Content added Content deleted
m (sp) |
|||
Line 612: | Line 612: | ||
April 2, 2101 will be on a Saturday |
April 2, 2101 will be on a Saturday |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
|||
We use the “times” standard module to parse the dates, check for leap years and compare results with those obtained with the doomsday rule (useful only to trap bugs in our implementation). |
|||
We use also the “WeekDay” type of this module which is an enumeration starting with “dMon” for Monday and ending with “dSun” for Sunday. |
|||
<lang Nim>import strformat, times |
|||
const |
|||
NormDoom = [1: 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] |
|||
LeapDoom = [1: 4, 1, 7, 2, 4, 6, 4, 1, 5, 3, 7, 5] |
|||
proc weekday(year, month, day: int): WeekDay = |
|||
let doom = (2 + 5 * (year mod 4) + 4 * (year mod 100) + 6 * (year mod 400)) mod 7 |
|||
let anchor = if year.isLeapYear: LeapDoom[month] else: NormDoom[month] |
|||
let wd = (doom + day - anchor + 7) mod 7 |
|||
result = if wd == 0: dSun else: WeekDay(wd - 1) |
|||
const Dates = ["1800-01-06", "1875-03-29", "1915-12-07", |
|||
"1970-12-23", "2043-05-14", "2077-02-12", "2101-04-02"] |
|||
for date in Dates: |
|||
let dt = date.parse("yyyy-MM-dd") |
|||
let wday = weekday(dt.year, ord(dt.month), dt.monthday) |
|||
if wday != dt.weekday: |
|||
echo &"For {date}, expected {dt.weekday}, found {wday}." |
|||
else: |
|||
echo date, " → ", wday</lang> |
|||
{{out}} |
|||
<pre>1800-01-06 → Monday |
|||
1875-03-29 → Monday |
|||
1915-12-07 → Tuesday |
|||
1970-12-23 → Wednesday |
|||
2043-05-14 → Thursday |
|||
2077-02-12 → Friday |
|||
2101-04-02 → Saturday</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |