Day of the week: Difference between revisions
Added MiniScript
(→{{header|GW-BASIC}}: Used features of GW-BASIC to simplify expressions. Added output.) |
(Added MiniScript) |
||
(33 intermediate revisions by 15 users not shown) | |||
Line 527:
5 IF NOT FN D7( FN RD(Y) - 6) THEN PRINT Y,
6 NEXT Y</syntaxhighlight>
==={{header|ASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="basic">
REM Day of the week
Month = 12
Day = 25
FOR Year = 2007 TO 2122
GOSUB CalcDayOfWeek:
IF DayOfWeek = 0 THEN
PRINT Year;
ENDIF
NEXT Year
PRINT
END
CalcDayOfWeek:
REM Sunday = 0, Saturday = 6
IF Month < 3 THEN
Year = Year - 1
Month = Month + 12
ENDIF
DayOfWeek = Year
YearDiv = Year / 4
DayOfWeek = DayOfWeek + YearDiv
YearDiv = Year / 100
DayOfWeek = DayOfWeek - YearDiv
YearDiv = Year / 400
DayOfWeek = DayOfWeek + YearDiv
DayPlus = 153 * Month
DayPlus = DayPlus + 8
DayPlus = DayPlus / 5
DayOfWeek = DayOfWeek + Day
DayOfWeek = DayOfWeek + DayPlus
DayOfWeek = DayOfWeek MOD 7
RETURN
</syntaxhighlight>
{{out}}
<pre>
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118
</pre>
==={{header|Atari BASIC}}===
{{trans|Commodore BASIC}}
Line 582 ⟶ 623:
2112
2118</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">for yr = 2008 to 2121
if wd(12, 25, yr) = 0 then print "Dec 25 "; yr
next
end
function wd(m, d, y)
if m < 3 then # if m = 1 or m = 2 then
m += 12
y -= 1
end if
return (y + (y \ 4) - (y \ 100) + (y \ 400) + d + ((153 * m + 8) \ 5)) % 7
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|BBC BASIC}}===
Line 592 ⟶ 649:
ENDIF
NEXT</syntaxhighlight>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|Applesoft BASIC}}
<syntaxhighlight lang="qbasic">10 CLS : REM 10 HOME for Applesoft BASIC
20 DEF fnd7(n) = n - 7 * INT (n / 7)
30 DEF fnrd(y) = 365 * y + INT (y / 4) - INT (y / 100) + INT (y / 400)
40 PRINT "YEARS WITH CHRISTMAS ON A SUNDAY" CHR$(13)
50 FOR y = 2008 TO 2121
60 IF NOT fn d7(fn rd(y)-6) THEN PRINT y,
70 NEXT y</syntaxhighlight>
{{out}}
<pre>YEARS WITH CHRISTMAS ON A SUNDAY
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118 </pre>
==={{header|Commodore BASIC}}===
Line 822 ⟶ 893:
==={{header|GW-BASIC}}===
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">10 REM Day of the week
20 DEFINT D, M, Y-Z
Line 871 ⟶ 943:
==={{header|Minimal BASIC}}===
{{works with|IS-BASIC}}
<syntaxhighlight lang="gwbasic">10 REM Find years with Sunday Christmas
20 LET F = 2008
30 LET T = 2121
Line 885 ⟶ 957:
120 NEXT Y
130 PRINT
140 END</syntaxhighlight>
==={{header|MSX Basic}}===
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">10 REM Find years with Sunday Christmas
11 CLS
20 LET F = 2008
30 LET T = 2121
40 PRINT "Sunday Christmases"; F; "-"; T
50 PRINT
60 FOR Y = F TO T
70 LET E = Y * 365 + INT(Y/4) - INT(Y/100) + INT(Y/400)
80 LET X = E - 6
90 LET D = X - 7 * INT(X/7)
100 IF D <> 0 THEN 120
110 PRINT Y; " ";
120 NEXT Y
130 PRINT
140 END</syntaxhighlight>
==={{header|Palo Alto Tiny BASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="basic">10 REM DAY OF THE WEEK
20 LET M=12,D=25
30 FOR Y=2007 TO 2122
40 GOSUB 200
50 IF Z=0 PRINT Y," ",
60 NEXT Y
70 PRINT
80 STOP
170 REM CALCULATE DAY OF WEEK Z GIVEN
180 REM YEAR Y, MONTH M, AND DAY D
190 REM SUNDAY = 0, SATURDAY = 6
200 IF M<3 LET Y=Y-1,M=M+12
210 LET Z=Y+Y/4-Y/100+Y/400
220 LET Z=Z+D+(153*M+8)/5
230 LET Z=Z-(Z/7)*7
240 RETURN</syntaxhighlight>
{{out}}
<pre> 2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</pre>
==={{header|PureBasic}}===
Line 895 ⟶ 1,007:
EndIf
Next</syntaxhighlight>
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FOR yr = 2008 TO 2121
IF wd(12, 25, yr) = 0 THEN PRINT "Dec 25 "; yr
NEXT yr
END
FUNCTION wd (m, d, y)
IF m < 3 THEN
LET m = m + 12
LET y = y - 1
END IF
wd = ((y + INT(y / 4) - INT(y / 100) + INT(y / 400) + d + INT((153 * m + 8) / 5)) MOD 7)
END FUNCTION</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|QL SuperBASIC}}===
Line 941 ⟶ 1,071:
2112
2118</pre>
==={{header|Quite BASIC}}===
The [[#MSX Basic|MSX Basic]] solution works without any changes.
==={{header|Run BASIC}}===
Line 1,085 ⟶ 1,218:
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">10 REM Day of the week
40 LET D = 25
50
60 LET Y =
70 GOSUB 200
80 IF Z =
90 GOTO 50
{{out}}
<pre>2011
Line 1,122 ⟶ 1,255:
2112
2118</pre>
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION wd (m, d, y)
IF m < 3 THEN
LET m = m + 12
LET y = y - 1
END IF
LET wd = REMAINDER ((y + INT(y / 4) - INT(y / 100) + INT(y / 400) + d + INT((153 * m + 8) / 5)), 7)
END FUNCTION
FOR yr = 2008 TO 2121
IF wd(12, 25, yr) = 0 THEN PRINT "Dec 25 "; yr
NEXT yr
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|VBA}}===
Line 1,167 ⟶ 1,316:
2118
</pre>
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">PROGRAM "progname"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
DECLARE FUNCTION wd (m, d, y)
FUNCTION Entry ()
FOR yr = 2008 TO 2121
IF wd(12, 25, yr) = 0 THEN PRINT "Dec 25 "; yr
NEXT yr
END FUNCTION
FUNCTION wd (m, d, y)
IF m < 3 THEN
m = m + 12
DEC y
END IF
RETURN ((y + INT(y / 4) - INT(y / 100) + INT(y / 400) + d + INT((153 * m + 8) / 5)) MOD 7)
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|Yabasic}}===
Line 1,469 ⟶ 1,644:
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <
#include <ranges>
#include <iostream>
int main(
std::cout << "Yuletide holidays must be allowed in the following years:\n";
for (int year : std::views::iota(2008, 2121)
| std::views::filter([](auto year) {
if (std::chrono::weekday{
std::chrono::year{year}/std::chrono::December/25}
== std::chrono::Sunday) {
return false;
})) {
std::cout << year << '\n';
}
}</syntaxhighlight>
{{out}}
Line 1,950 ⟶ 2,125:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func dayOfTheWeek year month day
# Based on Conway's doomsday algorithm
# 1. Calculate the doomsday for the century
Line 2,014 ⟶ 2,189:
NthDay += day
# 6. Finally, calculate the day of the week
.
for i = 2008 to 2121
print "Christmas in " & i & " is on Sunday"
.
Line 2,505 ⟶ 2,679:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Day_of_the_week}}
'''Solution'''
[[File:Fōrmulæ - Day of the week 01.png]]
[[File:Fōrmulæ - Day of the week 02.png]]
=={{header|Frink}}==
Line 2,787 ⟶ 2,963:
=={{header|Java}}==
<syntaxhighlight lang="java">
import static java.util.Calendar.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Line 2,793 ⟶ 2,971:
public class Yuletide{
public static void main(String[] args) {
Calendar calendar;
int count = 1;
for (int year = 2008; year <= 2121; year++) {
calendar = new GregorianCalendar(year, DECEMBER, 25);
if (calendar.get(DAY_OF_WEEK) == SUNDAY) {
if (count != 1)
System.out.print(", ");
System.out.printf("%d", calendar.get(YEAR));
count++;
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118
</pre>
=={{header|JavaScript}}==
Line 3,012 ⟶ 3,181:
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118
</syntaxhighlight>
=={{header|Koka}}==
<syntaxhighlight lang="koka">
import std/time/date
import std/time/calendar
import std/time/instant
import std/time/utc
fun main()
for(2008, 2121) fn(year)
val i = instant(year, 12, 25, cal=cal-gregorian)
val dow = (i.days+6)%7 // plus 6 since 2000-01-01 epoch was a Saturday
match dow.weekday
Sun -> println(year.show)
_ -> ()
</syntaxhighlight>
{{out}}
<pre>
2011
2016
2022
2033
2039
2044
2050
2061
2067
2072
2078
2089
2095
2101
2107
2112
2118
</pre>
=={{header|Kotlin}}==
Line 3,049 ⟶ 3,255:
2118
</pre>
=={{header|Lambdatalk}}==
{{trans|Javascript}}
<syntaxhighlight lang="javascript">
{xmasOnSunday 2008 2121}
->
2011
2016
2022
2033
2039
2044
2050
2061
2067
2072
2078
2089
2095
2101
2107
2112
2118
{script
LAMBDATALK.DICT["xmasOnSunday"] = function() {
var args = arguments[0].trim().split(" "),
days = [];
for (var year = args[0]; year <= args[1]; year++) {
var xmas = new Date(year, 11, 25)
if ( xmas.getDay() === 0 )
days.push(year)
}
return days.join("\n")
};
}
</syntaxhighlight>
=={{header|Lasso}}==
Line 3,354 ⟶ 3,602:
lambda([y], weekday(y, 12, 25) = 'sunday));
/* [2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118] */</syntaxhighlight>
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">import "dateTime"
print "Years between 2008 and 2121 when 25th December falls on Sunday:"
years = []
for year in range(2008, 2121)
date = year + "-12-25"
if dateTime.weekday(date) == 0 then years.push year
end for
print years.join(", ")</syntaxhighlight>
{{out}}
<pre>Years between 2008 and 2121 when 25th December falls on Sunday:
2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118
</pre>
=={{header|МК-61/52}}==
Line 4,220 ⟶ 4,484:
"2118"
})
</pre>
=={{header|PL/0}}==
{{trans|GW-BASIC}}
<syntaxhighlight lang="pascal">
var year, month, day, dayofweek;
procedure calcdayofweek;
begin
if month < 3 then
begin
year := year - 1;
month := month + 12
end;
dayofweek := year + year / 4 - year / 100 + year / 400;
dayofweek := dayofweek + day + (153 * month + 8) / 5;
dayofweek := dayofweek - (dayofweek / 7) * 7
end;
begin
month := 12; day := 25;
year := 2007;
while year <= 2122 do
begin
call calcdayofweek;
if dayofweek = 0 then ! year;
year := year + 1
end
end.
</syntaxhighlight>
{{out}}
<pre>
2011
2016
2022
2033
2039
2044
2050
2061
2067
2072
2078
2089
2095
2101
2107
2112
2118
</pre>
Line 4,294 ⟶ 4,607:
2112
2118
</pre>
=={{header|PL/M}}==
{{Trans|ALGOL W}}which is{{Trans|Fortran}}
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* FIND YEARS WHERE CHRISTMAS DAY FALLS ON A SUNDAY */
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
/* TASK */
/* RETURNS THE DAY OF THE WEEK CORRESPONDING To D/M/Y */
DAY$OF$WEEK: PROCEDURE( D, M, Y )BYTE;
DECLARE ( D, M, Y ) ADDRESS;
DECLARE ( J, K, MM, YY ) ADDRESS;
MM = M;
YY = Y;
IF MM <= 2 THEN DO;
MM = MM + 12;
YY = YY - 1;
END;
J = YY / 100;
K = YY MOD 100;
RETURN ( D + ( ( MM + 1 ) * 26 ) / 10 + K + K / 4 + J / 4 + 5 * J )
MOD 7;
END DAY$OF$WEEK ;
DECLARE ( YEAR, MONTH, DAY, COUNT ) ADDRESS;
CALL PR$STRING( .'25TH OF DECEMBER IS A SUNDAY IN$' );CALL PR$NL;
COUNT = 0;
DO YEAR = 2008 TO 2121;
DAY = DAY$OF$WEEK( 25, 12, YEAR );
IF DAY = 1 THEN DO;
CALL PR$CHAR( ' ' );CALL PR$NUMBER( YEAR );
IF ( COUNT := COUNT + 1 ) MOD 10= 0 THEN CALL PR$NL;
END;
END;
EOF
</syntaxhighlight>
{{out}}
<pre>
25TH OF DECEMBER IS A SUNDAY IN
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072
2078 2089 2095 2101 2107 2112 2118
</pre>
Line 4,787 ⟶ 5,162:
=={{header|RPL}}==
Early RPL
{{works with|
≪ '''IF''' OVER 2 ≤ '''THEN''' 1 - SWAP 12 + SWAP '''END'''
100 MOD LAST /
DUP 4 /
SWAP 1 + 13 * 5 /
7 MOD 5 + 7 MOD 1 +
≫ '<span style="color:blue">WKDAY</span>' STO
In 1990, RPL gained some basic functions for calculating the date, but nothing for directly obtaining the day of the week.
{{works with|HP|48}}
≪ { "MON" TUE" "WED" "THU" "FRI" "SAT" "SUN" }
SWAP 0 TSTR 1 3 SUB POS
≫ '<span style="color:blue">WKDAY</span>' STO <span style="color:grey">@ ( dd.mmyyyy → 1..7 )</span>
≪ { } 2008 2121 '''FOR''' year
'''IF''' 25 12 year <span style="color:blue">WKDAY</span> 7 == '''THEN''' year + '''END NEXT'''
≫ EVAL
{{out}}
<pre>
Line 4,855 ⟶ 5,231:
25 Dec 2118
</pre>
=={{header|Rust}}==
Line 5,151 ⟶ 5,525:
25-Dec-2118</pre>
=={{header|
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "yuletide" );
pragma annotate( description, "A company decides that whenever Xmas falls on a Sunday they will give their" );
pragma annotate( description, "workers all extra paid holidays so that, together with any public holidays," );
pragma annotate( description, "workers will not have to work the following week (between the 25th of" );
pragma annotate( description, "December and the first of January)." );
pragma annotate( description, "");
pragma annotate( description, "In what years between 2008 and 2121 will the 25th of December be a Sunday?" );
pragma annotate( description, "");
pragma annotate( description, "Using any standard date handling libraries of your programming language;" );
pragma annotate( description, "compare the dates calculated with the output of other languages to discover" );
pragma annotate( description, "any anomalies in the handling of dates which may be due to, for example," );
pragma annotate( description, "overflow in types used to represent dates/times similar to y2k type" );
pragma annotate( description, "problems. ");
pragma annotate( see_also, "http://rosettacode.org/wiki/Day_of_the_week" );
pragma annotate( author, "Ken O. Burtch ");
pragma license( unrestricted );
pragma restriction( no_external_commands );
procedure yuletide is
begin
for Year in 2008..2121 loop
if calendar.day_of_week ( calendar.time_of (Year, 12, 25, 0)) = 1 then
put_line( "Christmas " & strings.image( Year ) & " is on a Sunday" );
end if;
end loop;
end yuletide;</syntaxhighlight>
=={{header|SQL}}==
===Oracle===
SQL has good support for date functions; care must be taken with NLS settings (globalization support), in the code below the date format language is passed in as an argument to the relevant function. (Or, see a variation that does not depend on language settings, after the output shown below.)
Line 5,191 ⟶ 5,596:
Alternatively, the WHERE clause can be written in a way that avoids the complication of language settings. The (overloaded) TRUNC function, as applied to dates, takes a second argument indicating "to what" we must truncate. One option is 'iw' for "ISO week"; this truncates to the most recent Monday (the beginning of the ISO standard week, which is Monday through Sunday by definition). Like so (replace in the query above):
<syntaxhighlight lang="sql">where dt - trunc(dt, 'iw') = 6</syntaxhighlight>
===SQLite3===
<syntaxhighlight lang="sql">WITH RECURSIVE cte AS (
SELECT DATE('2008-12-25', '+'||(12*0)||' months') as dt, 1 AS level
UNION ALL
SELECT DATE('2008-12-25', '+'||(12*level)||' months') as dt, c.level + 1
FROM cte c
WHERE c.level <= 2121 - 2008 + 1
)
SELECT strftime('%Y', dt)
FROM cte
where strftime('%w', dt) = '0';
</syntaxhighlight>
===PostgreSQL===
<syntaxhighlight lang="sql"> WITH RECURSIVE cte AS (
SELECT date '2008-12-25' + interval '12 month' * 0 as dt, 1 AS level
UNION ALL
SELECT date '2008-12-25' + interval '12 month' * level as dt, c.level + 1
FROM cte c
WHERE c.level <= 2121 - 2008 + 1
)
SELECT dt
FROM cte
where to_char(dt, 'Dy') = 'Sun';
</syntaxhighlight>
=={{header|Standard ML}}==
Line 5,302 ⟶ 5,734:
var year=2008
let formatter=
formatter.dateFormat = "yyyy-MM-dd"
let gregorian:NSCalendar! = NSCalendar(calendarIdentifier:
while (year<2122){
var date:NSDate!=formatter.
var components=gregorian.components(
var dayOfWeek:NSInteger=components.weekday!
if(dayOfWeek==1){
}
year+
}
</syntaxhighlight>
{{out}}
<pre>2011
Line 5,648 ⟶ 6,081:
=={{header|Wren}}==
{{libheader|Wren-date}}
<syntaxhighlight lang="
System.print("Years between 2008 and 2121 when 25th December falls on Sunday:")
|