Doomsday rule: Difference between revisions

m
syntax highlighting fixup automation
(Added AppleScript.)
m (syntax highlighting fixup automation)
Line 64:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F isleap(year)
R year % 4 == 0 & (year % 100 != 0 | year % 400 == 0)
 
Line 85:
L(year, month, day) [(1800, 1, 6), (1875, 3, 29), (1915, 12, 7),
(1970, 12, 23), (2043, 5, 14), (2077, 2, 12), (2101, 4, 2)]
print(‘#.-#02-#02 -> #.’.format(year, month, day, weekday(year, month, day)))</langsyntaxhighlight>
 
{{out}}
Line 100:
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<langsyntaxhighlight lang="algol68">BEGIN # find the day of the week of dates using John Conway's Doomsday rule #
# returns the day of the week (Sunday = 0, Monday = 1,...) for the date #
# specified by ccyy, mm and dd #
Line 146:
test dow( 2022, 6, 19 )
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 160:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % find the day of the week of dates using John Conway's Doomsday rule %
% returns the day of the week (Sunday = 0, Monday = 1,...) for the date %
% specified by ccyy, mm and dd %
Line 202:
testDow( 2022, 6, 19 )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 217:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">weekday←{⎕IO←1
days←'Sunday' 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday'
leap←4 7∊⍨2⊥0=4 100 400∘|
Line 229:
anchor←m⊃(1+leap y)⊃nd ld
(1+7|7+doom+d-anchor)⊃days
}</langsyntaxhighlight>
{{out}}
<pre> weekday 1800 1 6
Line 247:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on dayOfWeek(yyyymmdd)
tell yyyymmdd to set {y, m, d} to {word 1 as integer, word 2 as integer, word 3 as integer}
set doomsdayForYear to (y + y div 4 - y div 100 + y div 400 + 2) -- (mod 7 further down anyway)
Line 274:
end task
 
task()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"1800-01-06 --> Monday
1875-03-29 --> Monday
1915-12-07 --> Tuesday
Line 283:
2043-05-14 --> Thursday
2077-02-12 --> Friday
2101-04-02 --> Saturday"</langsyntaxhighlight>
 
=={{header|BASIC}}==
Note that 1/6/1800 is actually a Monday, not a Sunday. As far as I can tell this is actually the correct day.
 
<langsyntaxhighlight BASIClang="basic">10 DIM D$(7): FOR I=1 TO 7: READ D$(I): NEXT I
20 DIM D(12,1): FOR I=0 TO 1: FOR J=1 TO 12: READ D(J,I): NEXT J,I
30 READ Y: IF Y=0 THEN END ELSE READ M,D
Line 302:
130 DATA 4,1,7,4,2,6,4,1,5,3,7,5
140 DATA 1800,1,6, 1875,3,29, 1915,12,7, 1970,12,23
150 DATA 2043,5,14, 2077,2,12, 2101,4,2, 0</langsyntaxhighlight>
{{out}}
<pre> 1/ 6/1800: Monday
Line 315:
Note that 1/6/1800 is actually a Monday, not a Sunday. As far as I can tell this is actually the correct day.
 
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let dayname(n) =
Line 351:
writef("February 12, 2077 will be on a %S.*N", dayname(weekday(2077, 2, 12)))
writef("April 2, 2101 will be on a %S.*N", dayname(weekday(2101, 4, 2)))
$)</langsyntaxhighlight>
{{out}}
<pre>January 6, 1800 was on a Monday.
Line 364:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
Line 416:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>January 6, 1800 was on a Monday.
Line 427:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
 
Line 476:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>January 6, 1800 was on a Monday
Line 487:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">leap_year = proc (year: int) returns (bool)
return(year//4=0 & (year//100=0 | year//400=0))
end leap_year
Line 533:
stream$putl(po, weekday(d))
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>1 June 1800 was on a Sunday
Line 544:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
record Date is
Line 596:
print_date(&dates[i]);
i := i + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre>1/6/1800: Monday
Line 608:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: accessors calendar calendar.english formatting
generalizations kernel math math.order math.vectors sequences ;
 
Line 640:
2077 2 12
2101 4 2
[ <date> test ] 3 7 mnapply</langsyntaxhighlight>
{{out}}
<pre>
Line 653:
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 S X(1)=3;S X(2)=7;S X(3)=7;S X(4)=4;S X(5)=2;S X(6)=6
01.15 S X(7)=4;S X(8)=1;S X(9)=5;S X(10)=3;S X(11)=7;S X(12)=5
01.20 S Y=1800;S M= 1;S D= 6;D 2;D 3
Line 690:
04.50 I (E-2)4.6;T "TUESDAY";R
04.60 I (E-1)4.7;T "MONDAY";R
04.70 T "SUNDAY"</langsyntaxhighlight>
{{out}}
<pre>M= 1 D= 6 Y= 1800 DAY=MONDAY
Line 701:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim shared as ubyte fdoom(0 to 1, 1 to 12) = {_
{ 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 }, _
{ 4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 } } 'the first doomsday in
Line 735:
print get_day( 2043, 05, 14 )
print get_day( 2077, 02, 12 )
print get_day( 2101, 04, 02 )</langsyntaxhighlight>
{{out}}
<pre>Monday
Line 747:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 794:
fmt.Printf("%s -> %s\n", date, days[dow])
}
}</langsyntaxhighlight>
 
{{out}}
Line 809:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf
 
data Date = Date {year :: Int, month :: Int, day :: Int}
Line 852:
 
main :: IO ()
main = putStr $ unlines $ map dateAndDay dates</langsyntaxhighlight>
{{out}}
<pre>1800-01-06: Monday
Line 865:
 
To find the doomsday for a month, find the number of days for each month in that year, and compute the running sum from right to left. Then add 1 to each of those numbers, find the mod 7 remainder and add 1 again
<langsyntaxhighlight Jlang="j"> 1+7|1++/\.31 28 31 30 31 30 31 31 30 31 30 31
3 7 7 4 2 6 4 1 5 3 7 5
1+7|1++/\.31 29 31 30 31 30 31 31 30 31 30 31
4 1 7 4 2 6 4 1 5 3 7 5</langsyntaxhighlight>
 
Also note that adding 7 is like adding 0 in modulo 7 arithmetic.
Line 874:
Thus:
 
<langsyntaxhighlight Jlang="j">get_weekday=: {{ 'Y M D'=. y
Y0=. todayno Y,1 1
Y1=. todayno 1+Y,0 0
Line 880:
dday=. 7|2 5 4 6+/ .*1,4 100 400|/Y
'day',~;(7|D+dday-aday){;:'Sun Mon Tues Wednes Thurs Fri Satur'
}}</langsyntaxhighlight>
 
That said, it's more concise to look up these anchor days. We can use the nonleap year sequence and adjust january and february's values for leap years by adding 1 or 2 (under 1 offset 7 modulo arithmetic).
 
<langsyntaxhighlight Jlang="j">get_weekday=: {{ 'Y M D'=. y
leap=. 0~:/ .=4 100 400|/Y
aday=. 1+7|(M*leap*3>M)+M{_ 2 6 6 3 1 5 3 0 4 2 6 4
dday=. 7|2 5 4 6+/ .*1,4 100 400|/Y
'day',~;(7|D+dday-aday){;:'Sun Mon Tues Wednes Thurs Fri Satur'
}}</langsyntaxhighlight>
 
These two implementations produce equivalent results.
Line 895:
Task examples:
 
<langsyntaxhighlight Jlang="j"> get_weekday 1800 1 6
Monday
get_weekday 1875 3 29
Line 908:
Friday
get_weekday 2101 4 2
Saturday</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">class Doom {
public static void main(String[] args) {
final Date[] dates = {
Line 966:
return weekdays[(doom + day - anchor + 7) % 7];
}
}</langsyntaxhighlight>
{{out}}
<pre>01/06/1800: Monday
Line 982:
 
Note that the assertions as defined here are only checked if the environment variable JQ_ASSERT is set.
<syntaxhighlight lang="jq">
<lang jq>
def weekdaynames: ["Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
 
Line 1,021:
("February 12, 2077 will be on a "+ get_weekday(2077; 2; 12)),
("April 2, 2101 will be on a " + get_weekday(2101; 4; 2)),
("April 2, 21011 will be on a " + get_weekday(21011; 4; 2))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,035:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">module DoomsdayRule
export get_weekday
 
Line 1,074:
println("February 12, 2077 will be on a ", get_weekday(2077, 2, 12))
println("April 2, 2101 will be on a ", get_weekday(2101, 4, 2))
</langsyntaxhighlight>{{out}}
<pre>
January 6, 1800 was on a Monday
Line 1,089:
We use also the “WeekDay” type of this module which is an enumeration starting with “dMon” for Monday and ending with “dSun” for Sunday.
 
<langsyntaxhighlight Nimlang="nim">import strformat, times
 
const
Line 1,110:
echo &"For {date}, expected {dt.weekday}, found {wday}."
else:
echo date, " → ", wday</langsyntaxhighlight>
 
{{out}}
Line 1,123:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl"># 20210602 Perl programming solution
 
use strict;
Line 1,143:
for (qw( 1800-01-06 1875-03-29 1915-12-07 1970-12-23 2043-05-14 2077-02-12 2101-04-02 )) {
print $_, " is a : ", dow $_, "\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,156:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,194:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n%-30s (%d = %d ? %s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ds</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bis</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bid</span><span style="color: #0000FF;">,</span><span style="color: #000000;">drd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ok</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,214:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,295:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>1/6/1800: MONDAY
Line 1,306:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from datetime import date
from calendar import isleap
 
Line 1,334:
for d in dates:
tense = "was" if d < date.today() else "is" if d == date.today() else "will be"
print("{} {} a {}".format(d.strftime("%B %d, %Y"), tense, weekday(d)))</langsyntaxhighlight>
{{out}}
<pre>January 06, 1800 was a Monday
Line 1,346:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>my @dow = < Sunday Monday Tuesday Wednesday Thursday Friday Saturday >;
 
my %doomsday = False => [3,7,7,4,2,6,4,1,5,3,7,5], True => [4,1,7,4,2,6,4,1,5,3,7,5];
Line 1,370:
say "Builtin - $_ is a: ", @dow[Date.new($_).day-of-week];
say '';
}</langsyntaxhighlight>
{{out}}
<pre>Conway - 1800-01-06 is a: Monday
Line 1,395:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds the day─of─week for a specified date using Conway's Doomsday rule. */
parse arg $ /*obtain optional arguments from the CL*/
if $='' | $="," then $= , /*Not specified? Then use the default.*/
Line 1,412:
/*──────────────────────────────────────────────────────────────────────────────────────*/
doomsday: parse arg ?; return (2 + 5 * (?//4) + 4 * (?//100) + 6 * (?//400) ) // 7
leapyear: arg #; ly= #//4==0; if ly==0 then return 0; return ((#//100\==0) | #//400==0)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,425:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn day_of_week(year: u32, month: u32, day: u32) -> u32 {
const LEAPYEAR_FIRSTDOOMSDAYS: [u32; 12] = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5];
const NONLEAPYEAR_FIRSTDOOMSDAYS: [u32; 12] = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5];
Line 1,467:
print_day_of_week(2077, 2, 12);
print_day_of_week(2101, 4, 2);
}</langsyntaxhighlight>
 
{{out}}
Line 1,483:
{{libheader|Wren-date}}
We only use the above module to check the dates of the week given by Conway's method. The latter are worked out from scratch.
<langsyntaxhighlight lang="ecmascript">import "/date" for Date
 
var days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"]
Line 1,520:
var d = Date.parse(date, Date.isoDate)
System.print("%(date) -> %(d.weekDay)")
}</langsyntaxhighlight>
 
{{out}}
Line 1,546:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">dim fdoom(1, 12)
for x = 0 to arraysize(fdoom(),1)
for y = 1 to arraysize(fdoom(),2)
Line 1,589:
data 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5
data 4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5
data "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"</langsyntaxhighlight>
{{out}}
<pre>
10,343

edits