Doomsday rule: Difference between revisions
m
syntax highlighting fixup automation
(Added AppleScript.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 64:
{{trans|Python}}
<
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)))</
{{out}}
Line 100:
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<
# 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</
{{out}}
<pre>
Line 160:
=={{header|ALGOL W}}==
<
% 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.</
{{out}}
<pre>
Line 217:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
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
}</
{{out}}
<pre> weekday 1800 1 6
Line 247:
=={{header|AppleScript}}==
<
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()</
{{output}}
<
1875-03-29 --> Monday
1915-12-07 --> Tuesday
Line 283:
2043-05-14 --> Thursday
2077-02-12 --> Friday
2101-04-02 --> Saturday"</
=={{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.
<
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</
{{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.
<
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)))
$)</
{{out}}
<pre>January 6, 1800 was on a Monday.
Line 364:
=={{header|C}}==
<
#include <stdint.h>
#include <stdbool.h>
Line 416:
return 0;
}</
{{out}}
<pre>January 6, 1800 was on a Monday.
Line 427:
=={{header|C++}}==
<
#include <cstdint>
Line 476:
return 0;
}</
{{out}}
<pre>January 6, 1800 was on a Monday
Line 487:
=={{header|CLU}}==
<
return(year//4=0 & (year//100=0 | year//400=0))
end leap_year
Line 533:
stream$putl(po, weekday(d))
end
end start_up</
{{out}}
<pre>1 June 1800 was on a Sunday
Line 544:
=={{header|Cowgol}}==
<
record Date is
Line 596:
print_date(&dates[i]);
i := i + 1;
end loop;</
{{out}}
<pre>1/6/1800: Monday
Line 608:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
generalizations kernel math math.order math.vectors sequences ;
Line 640:
2077 2 12
2101 4 2
[ <date> test ] 3 7 mnapply</
{{out}}
<pre>
Line 653:
=={{header|FOCAL}}==
<
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"</
{{out}}
<pre>M= 1 D= 6 Y= 1800 DAY=MONDAY
Line 701:
=={{header|FreeBASIC}}==
<
{ 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 )</
{{out}}
<pre>Monday
Line 747:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 794:
fmt.Printf("%s -> %s\n", date, days[dow])
}
}</
{{out}}
Line 809:
=={{header|Haskell}}==
<
data Date = Date {year :: Int, month :: Int, day :: Int}
Line 852:
main :: IO ()
main = putStr $ unlines $ map dateAndDay dates</
{{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
<
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</
Also note that adding 7 is like adding 0 in modulo 7 arithmetic.
Line 874:
Thus:
<
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'
}}</
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).
<
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'
}}</
These two implementations produce equivalent results.
Line 895:
Task examples:
<
Monday
get_weekday 1875 3 29
Line 908:
Friday
get_weekday 2101 4 2
Saturday</
=={{header|Java}}==
<
public static void main(String[] args) {
final Date[] dates = {
Line 966:
return weekdays[(doom + day - anchor + 7) % 7];
}
}</
{{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">
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))</
{{out}}
<pre>
Line 1,035:
=={{header|Julia}}==
<
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))
</
<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.
<
const
Line 1,110:
echo &"For {date}, expected {dt.weekday}, found {wday}."
else:
echo date, " → ", wday</
{{out}}
Line 1,123:
=={{header|Perl}}==
{{trans|Raku}}
<
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";
}</
{{out}}
<pre>
Line 1,156:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,214:
=={{header|PL/M}}==
<
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</
{{out}}
<pre>1/6/1800: MONDAY
Line 1,306:
=={{header|Python}}==
<
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)))</
{{out}}
<pre>January 06, 1800 was a Monday
Line 1,346:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
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 '';
}</
{{out}}
<pre>Conway - 1800-01-06 is a: Monday
Line 1,395:
=={{header|REXX}}==
<
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)</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,425:
=={{header|Rust}}==
<
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);
}</
{{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.
<
var days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"]
Line 1,520:
var d = Date.parse(date, Date.isoDate)
System.print("%(date) -> %(d.weekDay)")
}</
{{out}}
Line 1,546:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
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"</
{{out}}
<pre>
|