Day of the week: Difference between revisions

Content deleted Content added
Ulrie (talk | contribs)
No edit summary
m Fixed lang tags.
Line 7: Line 7:


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
<lang ada>
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
Line 18: Line 17:
end if;
end if;
end loop;
end loop;
end Yuletide;
end Yuletide;</lang>
</lang>
Sample output:
Sample output:
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">
Line 99: Line 97:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol68>main:(
<pre>
main:(
# example from: http://www.xs4all.nl/~jmvdveer/algol.html - GPL #
# example from: http://www.xs4all.nl/~jmvdveer/algol.html - GPL #
INT sun=0 # , mon=1, tue=2, wed=3, thu=4, fri=5, sat=6 #;
INT sun=0 # , mon=1, tue=2, wed=3, thu=4, fri=5, sat=6 #;
Line 121: Line 118:
OD;
OD;
new line(stand out)
new line(stand out)
)</lang>
)
</pre>
Output:
Output:
<lang algol68>December 25th is a Sunday in: 2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</lang>
<pre>
December 25th is a Sunday in: 2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118
</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Line 181: Line 175:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <boost/date_time/gregorian/gregorian.hpp>
<lang C++>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>
#include <iostream>


Line 199: Line 192:
std::cout << "\n" ;
std::cout << "\n" ;
return 0 ;
return 0 ;
}</lang>
}
</lang>
This produces the following output:
This produces the following output:
<pre>
<pre>
Line 300: Line 292:
=={{header|D}}==
=={{header|D}}==
I'm sure this can be done better. If anyone wants to take the time, please improve it.
I'm sure this can be done better. If anyone wants to take the time, please improve it.
<lang d>
<lang d>import std.date;
import std.date;
import std.stdio;
import std.stdio;
void main() {
void main() {
Line 307: Line 298:
if (0 == WeekDay(parse("December 25, "~std.string.toString(year))))
if (0 == WeekDay(parse("December 25, "~std.string.toString(year))))
writefln("Christmas comes on a sunday in %d",year);
writefln("Christmas comes on a sunday in %d",year);
}</lang>
}
</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Forth has only TIME&DATE, which does not give day of week. Many public Forth Julian date calculators had year-2100 problems, but this algorithm works well.
Forth has only TIME&DATE, which does not give day of week. Many public Forth Julian date calculators had year-2100 problems, but this algorithm works well.
\ Zeller's Congruence
<lang forth>\ Zeller's Congruence
: zeller ( m -- days since March 1 )
: zeller ( m -- days since March 1 )
9 + 12 mod 1- 26 10 */ 3 + ;
9 + 12 mod 1- 26 10 */ 3 + ;

: weekday ( d m y -- 0..6 ) \ Monday..Sunday
: weekday ( d m y -- 0..6 ) \ Monday..Sunday
over 3 < if 1- then
over 3 < if 1- then
dup 4 /
dup 4 /
over 100 / -
over 100 / -
over 400 / + +
over 400 / + +
swap zeller + +
swap zeller + +
1+ 7 mod ;
1+ 7 mod ;

: yuletide
: yuletide
." December 25 is Sunday in "
." December 25 is Sunday in "
2122 2008 do
2122 2008 do
25 12 i weekday
25 12 i weekday
6 = if i . then
6 = if i . then
loop cr ;
loop cr ;


cr yuletide
cr yuletide
December 25 is Sunday in 2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118
December 25 is Sunday in 2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 438: Line 428:


=={{header|J}}==
=={{header|J}}==
load 'dates' NB. provides verb 'weekday'
<lang j> load 'dates' NB. provides verb 'weekday'
SunDec25=: #~ 0&=@:weekday@:(|:@,: ,. 12 25$~#,2:)
SunDec25=: #~ 0&=@:weekday@:(|:@,: ,. 12 25$~#,2:)
SunDec25 2008 + i.114
SunDec25 2008 + i.114
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 508: Line 498:


=={{header|M4}}==
=={{header|M4}}==
<lang M4>
<lang M4>divert(-1)
divert(-1)


define(`for',
define(`for',
Line 527: Line 516:


for(`yr',2008,2121,
for(`yr',2008,2121,
`ifelse(eval(julianxmas(yr)%7==6),1,`yr ')')
`ifelse(eval(julianxmas(yr)%7==6),1,`yr ')')</lang>
</lang>


Output:
Output:
Line 537: Line 525:


=={{header|Mathematica}}==
=={{header|Mathematica}}==
<lang Mathematica>Reap[If[DateString[{#,12,25},"DayName"]=="Sunday",Sow[#]]&/@Range[2008,2121]][[2,1]]</lang>
<lang Mathematica>
Reap[If[DateString[{#,12,25},"DayName"]=="Sunday",Sow[#]]&/@Range[2008,2121]][[2,1]]
</lang>
gives back:
gives back:
<lang Mathematica>{2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118}</lang>
<lang Mathematica>
{2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118}
</lang>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 760: Line 744:
}
}
}
}
?>
?></lang>
</lang>


Output:
Output:
Line 817: Line 800:


=={{header|R}}==
=={{header|R}}==
<lang R>
<lang R>years <- 2008:2121
years <- 2008:2121
xmas <- as.POSIXlt(paste(years, '/12/25', sep=""))
xmas <- as.POSIXlt(paste(years, '/12/25', sep=""))
years[xmas$wday==0]
years[xmas$wday==0]</lang>
</lang>
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118
2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118


Line 935: Line 916:
=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|bash}}
{{works with|bash}}
<pre>#! /bin/bash
<lang bash>#! /bin/bash


for i in `seq 2008 2121`
for i in `seq 2008 2121`
Line 942: Line 923:
done
done


exit 0
exit 0</lang>
</pre>


The first lines of output (from a 32bit GNU/Linux system, date version 6.9) are
The first lines of output (from a 32bit GNU/Linux system, date version 6.9) are


<lang bash>Sun Dec 25 00:00:00 CET 2011
<pre>
Sun Dec 25 00:00:00 CET 2011
Sun Dec 25 00:00:00 CET 2016
Sun Dec 25 00:00:00 CET 2016
Sun Dec 25 00:00:00 CET 2022
Sun Dec 25 00:00:00 CET 2022
Sun Dec 25 00:00:00 CET 2033
Sun Dec 25 00:00:00 CET 2033
date: invalid date `2038-12-25'
date: invalid date `2038-12-25'</lang>
</pre>


I.e., starting from year 2038, the <tt>date</tt> command (which uses the glibc library, at least on GNU systems), is not able to recognise the date as a valid one!
I.e., starting from year 2038, the <tt>date</tt> command (which uses the glibc library, at least on GNU systems), is not able to recognise the date as a valid one!
Line 959: Line 937:
===Different machine/OS version (64 bit)===
===Different machine/OS version (64 bit)===
This is the same command run on RedHat Linux.
This is the same command run on RedHat Linux.
<pre>bash-3.00$ date --version
<lang bash>bash-3.00$ date --version
date (coreutils) 5.2.1
date (coreutils) 5.2.1
Written by David MacKenzie.
Written by David MacKenzie.
Line 986: Line 964:
Sun Dec 25 00:00:00 GMT 2112
Sun Dec 25 00:00:00 GMT 2112
Sun Dec 25 00:00:00 GMT 2118
Sun Dec 25 00:00:00 GMT 2118
bash-3.00$</pre>
bash-3.00$</lang>


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==
Thanks to UNIX Shell implementation
Thanks to UNIX Shell implementation
seq 2008 2121 | xargs -IYEAR -n 1 date +%c -d 'Dec 25 YEAR' | grep Sun
<lang bash>seq 2008 2121 | xargs -IYEAR -n 1 date +%c -d 'Dec 25 YEAR' | grep Sun</lang>


(Output same as UNIX Shell)
(Output same as UNIX Shell)


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
<lang vedit>
<lang vedit>Buf_Switch(Buf_Free)
Buf_Switch(Buf_Free)
for (#3 = 2008; #3 < 2122; #3++) {
for (#3 = 2008; #3 < 2122; #3++) {
Reg_Set(10, "12/25/")
Reg_Set(10, "12/25/")
Line 1,003: Line 980:
Num_Ins(#3, NOCR)
Num_Ins(#3, NOCR)
}
}
}</lang>
}
</lang>


Output:
Output: