Day of the week: Difference between revisions

Added MiniScript
(→‎{{header|ASIC}}: Added a solution.)
(Added MiniScript)
 
(30 intermediate revisions by 15 users not shown)
Line 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 633 ⟶ 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 913 ⟶ 943:
 
==={{header|Minimal BASIC}}===
{{works with|IS-BASIC}}
<syntaxhighlight lang="gwbasic">
<syntaxhighlight lang="gwbasic">10 REM Find years with Sunday Christmas
20 LET F = 2008
30 LET T = 2121
Line 927 ⟶ 957:
120 NEXT Y
130 PRINT
140 END</syntaxhighlight>
 
</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 937 ⟶ 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 983 ⟶ 1,071:
2112
2118</pre>
 
==={{header|Quite BASIC}}===
The [[#MSX Basic|MSX Basic]] solution works without any changes.
 
==={{header|Run BASIC}}===
Line 1,164 ⟶ 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,209 ⟶ 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,511 ⟶ 1,644:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <boost/date_time/gregorian/gregorian.hppchrono>
#include <ranges>
#include <iostream>
 
int main( ) {
std::cout << "Yuletide holidays must be allowed in the following years:\n";
using namespace boost::gregorian ;
for (int year : std::views::iota(2008, 2121)
 
| std::views::filter([](auto year) {
std::cout
if (std::chrono::weekday{
<< "Yuletide holidays must be allowed in the following years:\n" ;
std::chrono::year{year}/std::chrono::December/25}
for ( int i = 2008 ; i < 2121 ; i++ ) {
== std::chrono::Sunday) {
greg_year gy = i ;
date d ( gy, Dec , 25 ) return true;
if ( d.day_of_week( ) == Sunday ) { }
return false;
std::cout << i << std::endl ;
})) {
std::cout << year << '\n';
}
}
std::cout << "\n" ;
return 0 ;
}</syntaxhighlight>
{{out}}
Line 1,992 ⟶ 2,125:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func dayOfTheWeek year month day . result .
# Based on Conway's doomsday algorithm
# 1. Calculate the doomsday for the century
Line 2,056 ⟶ 2,189:
NthDay += day
# 6. Finally, calculate the day of the week
result =return (januaryOne + NthDay - 1) mod 7
.
for i = 2008 to 2121
callif dayOfTheWeek i 12 25 result= 0
if result = 0
print "Christmas in " & i & " is on Sunday"
.
Line 2,547 ⟶ 2,679:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Day_of_the_week}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Day of the week 01.png]]
In '''[https://formulae.org/?example=Day_of_the_week this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Day of the week 02.png]]
 
=={{header|Frink}}==
Line 2,829 ⟶ 2,963:
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.util.Calendar;
import static java.util.Calendar.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Line 2,835 ⟶ 2,971:
public class Yuletide{
public static void main(String[] args) {
Calendar calendar;
for(int i = 2008;i<=2121;i++){
int count = 1;
Calendar cal = new GregorianCalendar(i, Calendar.DECEMBER,
for (int year = 2008; year <= 2121; year++) {
25);
calendar = new GregorianCalendar(year, DECEMBER, 25);
if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
if (calendar.get(DAY_OF_WEEK) == SUNDAY) {
System.out.println(cal.getTime());
if (count != 1)
}
System.out.print(", ");
}
System.out.printf("%d", calendar.get(YEAR));
count++;
}
}
}
}
}</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
<pre>Sun Dec 25 00:00:00 CST 2011
2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118
Sun Dec 25 00:00:00 CST 2016
</pre>
Sun Dec 25 00:00:00 CST 2022
Sun Dec 25 00:00:00 CST 2033
Sun Dec 25 00:00:00 CST 2039
Sun Dec 25 00:00:00 CST 2044
Sun Dec 25 00:00:00 CST 2050
Sun Dec 25 00:00:00 CST 2061
Sun Dec 25 00:00:00 CST 2067
Sun Dec 25 00:00:00 CST 2072
Sun Dec 25 00:00:00 CST 2078
Sun Dec 25 00:00:00 CST 2089
Sun Dec 25 00:00:00 CST 2095
Sun Dec 25 00:00:00 CST 2101
Sun Dec 25 00:00:00 CST 2107
Sun Dec 25 00:00:00 CST 2112
Sun Dec 25 00:00:00 CST 2118</pre>
 
=={{header|JavaScript}}==
Line 3,054 ⟶ 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,091 ⟶ 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,396 ⟶ 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,262 ⟶ 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,336 ⟶ 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,829 ⟶ 5,162:
 
=={{header|RPL}}==
Early RPL doesversions do not have any date library, so a specific instruction implementsimplement Zeller's congruence with a stack-oriented algorithm.
{{works with|Halcyon CalcHP|4.2.728}}
'''IF''' OVER 2 ≤ '''THEN''' 1 - SWAP 12 + SWAP '''END'''
100 MOD LAST / IP FLOOR
DUP 4 / IPFLOOR SWAP DUP + - SWAP DUP 4 / IPFLOOR + +
SWAP 1 + 13 * 5 / IPFLOOR + +
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.
'WKDAY' STO
{{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'''
2008 2121 FOR year
≫ EVAL
IF 25 12 year WKDAY 7 == THEN year + END
NEXT
EVAL
{{out}}
<pre>
Line 4,897 ⟶ 5,231:
25 Dec 2118
</pre>
 
(Note: The Time class could not handle dates beyond 2038 prior to Ruby 1.9.2.[https://www.ruby-lang.org/en/news/2010/08/18/ruby-1-9.2-released/])
 
=={{header|Rust}}==
Line 5,193 ⟶ 5,525:
25-Dec-2118</pre>
 
=={{header|SQLSparForte}}==
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,233 ⟶ 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,344 ⟶ 5,734:
 
var year=2008
let formatter=NSDateFormatterDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
 
let gregorian:NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorianNSCalendar.Identifier.gregorian)
while (year<2122){
var date:NSDate!=formatter.dateFromStringdate(from: String(year)+"-12-25") as NSDate?
var components=gregorian.components(NSCalendarUnitNSCalendar.CalendarUnitWeekdayUnit.weekday, fromDatefrom: date as Date)
var dayOfWeek:NSInteger=components.weekday!
if(dayOfWeek==1){
printlnprint(year)
}
year++=1
}
}</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>2011
Line 5,690 ⟶ 6,081:
=={{header|Wren}}==
{{libheader|Wren-date}}
<syntaxhighlight lang="ecmascriptwren">import "./date" for Date
 
System.print("Years between 2008 and 2121 when 25th December falls on Sunday:")
9,476

edits