Day of the week

Day of the week
You are encouraged to solve this task according to the task description, using any language you may know.

A company decides that whenever Xmas falls on a Sunday they will give their workers all extra paid holidays so that, together with any public holidays, workers will not have to work the following week (between the 25th of December and the first of January).

In what years between 2008 and 2121 will the 25th of December be a Sunday?

Using any standard date handling libraries of your programming language;
compare the dates calculated with the output of other languages to discover any anomalies in the handling of dates which may be due to, for example, overflow in types used to represent dates/times similar to y2k type problems.

set ChristmasSundays to{}set Christmas to(current date)setmonthof Christmas toDecemberset day of Christmas to25repeatwith year from2008to2121set year of Christmas to yearifweekdayof Christmas isSundaythensetendof ChristmasSundays to yearendrepeatChristmasSundays

Or, using a higher-order filter function, and a
more functional style of composition:

Dec 25, 2011 is a Sunday.
Dec 25, 2016 is a Sunday.
Dec 25, 2022 is a Sunday.
Dec 25, 2033 is a Sunday.
Dec 25, 2039 is a Sunday.
Dec 25, 2044 is a Sunday.
Dec 25, 2050 is a Sunday.
Dec 25, 2061 is a Sunday.
Dec 25, 2067 is a Sunday.
Dec 25, 2072 is a Sunday.
Dec 25, 2078 is a Sunday.
Dec 25, 2089 is a Sunday.
Dec 25, 2095 is a Sunday.
Dec 25, 2101 is a Sunday.
Dec 25, 2107 is a Sunday.
Dec 25, 2112 is a Sunday.
Dec 25, 2118 is a Sunday.

25 December 2011 is sunday
25 December 2016 is sunday
25 December 2022 is sunday
25 December 2033 is sunday
25 December 2039 is sunday
25 December 2044 is sunday
25 December 2050 is sunday
25 December 2061 is sunday
25 December 2067 is sunday
25 December 2072 is sunday
25 December 2078 is sunday
25 December 2089 is sunday
25 December 2095 is sunday
25 December 2101 is sunday
25 December 2107 is sunday
25 December 2112 is sunday
25 December 2118 is sunday

25 December 2011 is Sunday
25 December 2016 is Sunday
25 December 2022 is Sunday
25 December 2033 is Sunday
25 December 2039 is Sunday
25 December 2044 is Sunday
25 December 2050 is Sunday
25 December 2061 is Sunday
25 December 2067 is Sunday
25 December 2072 is Sunday
25 December 2078 is Sunday
25 December 2089 is Sunday
25 December 2095 is Sunday
25 December 2101 is Sunday
25 December 2107 is Sunday
25 December 2112 is Sunday
25 December 2118 is Sunday

'In what years between 2008 and 2121 will the 25th of December be a Sunday?DIM date ASINTEGER, dayname ASSTRINGFORDIM year =2008TO2121 date = year *10000+1225 dayname = dateconv(date,"dddd")IF dayname ="Sunday"THENPRINT"Christmas Day is on a Sunday in ", yearENDIFNEXTPAUSE

25 December 2011 is Sunday
25 December 2016 is Sunday
25 December 2022 is Sunday
25 December 2033 is Sunday
25 December 2039 is Sunday
25 December 2044 is Sunday
25 December 2050 is Sunday
25 December 2061 is Sunday
25 December 2067 is Sunday
25 December 2072 is Sunday
25 December 2078 is Sunday
25 December 2089 is Sunday
25 December 2095 is Sunday
25 December 2101 is Sunday
25 December 2107 is Sunday
25 December 2112 is Sunday
25 December 2118 is Sunday

main =mapM_putStrLn["25 December "++show year ++" is Sunday"| year <-[2008..2121], isXmasSunday year]

Output:

25 December 2011 is Sunday
25 December 2016 is Sunday
25 December 2022 is Sunday
25 December 2033 is Sunday
25 December 2039 is Sunday
25 December 2044 is Sunday
25 December 2050 is Sunday
25 December 2061 is Sunday
25 December 2067 is Sunday
25 December 2072 is Sunday
25 December 2078 is Sunday
25 December 2089 is Sunday
25 December 2095 is Sunday
25 December 2101 is Sunday
25 December 2107 is Sunday
25 December 2112 is Sunday
25 December 2118 is Sunday

This code uses the Dates module, which is being incorporated into Julian's standard library with the current development version (0.4). I've used isdefined to make this code good for the current stable version (0.3) as well as for future releases. If Dates is not installed on your instance of Julian try Pkg.add("Dates") from the REPL.

12/25/2011 is a Sunday
12/25/2016 is a Sunday
12/25/2022 is a Sunday
12/25/2033 is a Sunday
12/25/2039 is a Sunday
12/25/2044 is a Sunday
12/25/2050 is a Sunday
12/25/2061 is a Sunday
12/25/2067 is a Sunday
12/25/2072 is a Sunday
12/25/2078 is a Sunday
12/25/2089 is a Sunday
12/25/2095 is a Sunday
12/25/2101 is a Sunday
12/25/2107 is a Sunday
12/25/2112 is a Sunday
12/25/2118 is a Sunday

function xmasSunday startDate endDate convert the long date to dateitems put it into xmasDay put 12 into item 2 of xmasDay put 25 into item 3 of xmasDay repeat with i = startDate to endDate put i into item 1 of xmasDay convert xmasDay to dateItems if item 7 of xmasDay is 1 then put i & comma after xmasYear end repeat if the last char of xmasYear is comma then delete the last char of xmasYear return xmasYearend xmasSunday

DOWHOLIDAY ;In what years between 2008 and 2121 will December 25 be a Sunday? ;Uses the VA's public domain routine %DTC (Part of the Kernel) named here DIDTC NEW BDT,EDT,CHECK,CHKFOR,LIST,I,X,Y ;BDT - the beginning year to check ;EDT - the end year to check ;BDT and EDT are year offsets from the epoch date 1/1/1700 ;CHECK - the month and day to look at ;CHKFOR - what day of the week to look for ;LIST - list of years in which the condition is true ;I - the year currently being checked ;X - the date in an "internal" format, for input to DOW^DIDTC ;Y - the output from DOW^DIDTC SET BDT=308,EDT=421,CHECK="1225",CHKFOR=0,LIST="" FOR I=BDT:1:EDT SET X=I_CHECK D DOW^DIDTC SET:(Y=0) LIST=$SELECT($LENGTH(LIST):LIST_", ",1:"")_(I+1700) IF $LENGTH(LIST)=0 WRITE !,"There are no years that have Christmas on a Sunday in the given range." IF $LENGTH(LIST) WRITE !,"The following years have Christmas on a Sunday: ",LIST KILL BDT,EDT,CHECK,CHKFOR,LIST,I,X,Y QUIT

tryfor i =2008to2121do(* I'm lazy so we'll just borrow the current time instead of having to set all the fields explicitly *)let mytime ={(localtime (time ()))with tm_year = i -1900; tm_mon =11; tm_mday =25}intrylet _, mytime = mktime mytime inif mytime.tm_wday =0thenPrintf.printf "25 December %d is Sunday\n" iwith e ->Printf.printf "%d is the last year we can specify\n"(i-1);raise edonewith _ ->()

Output:

of a run on a 32 bit machine

25 December 2011 is Sunday
25 December 2016 is Sunday
25 December 2022 is Sunday
25 December 2033 is Sunday
2037 is the last year we can specify

let()=let years = list_make_seq 20082121inlet years =List.filter (fun year -> Date.day_of_week (Date.make year 1225)= Date.Sun) years inprint_endline"December 25 is a Sunday in:";List.iter (Printf.printf "%d\n") years

Christmas Day 2011 is a Sunday
Christmas Day 2016 is a Sunday
Christmas Day 2022 is a Sunday
Christmas Day 2033 is a Sunday
Christmas Day 2039 is a Sunday
Christmas Day 2044 is a Sunday
Christmas Day 2050 is a Sunday
Christmas Day 2061 is a Sunday
Christmas Day 2067 is a Sunday
Christmas Day 2072 is a Sunday
Christmas Day 2078 is a Sunday
Christmas Day 2089 is a Sunday
Christmas Day 2095 is a Sunday
Christmas Day 2101 is a Sunday
Christmas Day 2107 is a Sunday
Christmas Day 2112 is a Sunday
Christmas Day 2118 is a Sunday

say'December 25th,' y "falls on a Sunday."end/*y*//*stick a fork in it, we're all done. */

output when using the default input:

December 25th, 2011 falls on a Sunday.
December 25th, 2016 falls on a Sunday.
December 25th, 2022 falls on a Sunday.
December 25th, 2033 falls on a Sunday.
December 25th, 2039 falls on a Sunday.
December 25th, 2044 falls on a Sunday.
December 25th, 2050 falls on a Sunday.
December 25th, 2061 falls on a Sunday.
December 25th, 2067 falls on a Sunday.
December 25th, 2072 falls on a Sunday.
December 25th, 2078 falls on a Sunday.
December 25th, 2089 falls on a Sunday.
December 25th, 2095 falls on a Sunday.
December 25th, 2101 falls on a Sunday.
December 25th, 2107 falls on a Sunday.
December 25th, 2112 falls on a Sunday.
December 25th, 2118 falls on a Sunday.

December 25th, 2011 falls on a Sunday.
December 25th, 2016 falls on a Sunday.
December 25th, 2022 falls on a Sunday.
December 25th, 2033 falls on a Sunday.
December 25th, 2039 falls on a Sunday.
December 25th, 2044 falls on a Sunday.
December 25th, 2050 falls on a Sunday.
December 25th, 2061 falls on a Sunday.
December 25th, 2067 falls on a Sunday.
December 25th, 2072 falls on a Sunday.
December 25th, 2078 falls on a Sunday.
December 25th, 2089 falls on a Sunday.
December 25th, 2095 falls on a Sunday.
December 25th, 2101 falls on a Sunday.
December 25th, 2107 falls on a Sunday.
December 25th, 2112 falls on a Sunday.
December 25th, 2118 falls on a Sunday.

for year = 2008 to 2121 if val(date$("12-25-";year)) mod 7 = 5 then print "For ";year;"xmas is Sunday"next year

For 2011 xmas is Sunday
For 2016 xmas is Sunday
For 2022 xmas is Sunday
For 2033 xmas is Sunday
For 2039 xmas is Sunday
For 2044 xmas is Sunday
For 2050 xmas is Sunday
For 2061 xmas is Sunday
For 2067 xmas is Sunday
For 2072 xmas is Sunday
For 2078 xmas is Sunday
For 2089 xmas is Sunday
For 2095 xmas is Sunday
For 2101 xmas is Sunday
For 2107 xmas is Sunday
For 2112 xmas is Sunday
For 2118 xmas is Sunday

(define(day-of-week year month day)(if(< month 3)(begin(set! month (+ month 12))(set! year (- year 1))))(+1(remainder(+5 day (quotient(*(+1 month)13)5) year (quotient year 4)(*(quotient year 100)6)(quotient year 400))7)))

The library time.s7i defines
the function dayOfWeek,
which returns 1 for monday, 2 for tuesday, and so on up to 7 for sunday.

$ include "seed7_05.s7i"; include "time.s7i";

const proc: main is func local var integer: year is 0; begin for year range 2008 to 2122 do if dayOfWeek(date(year, 12, 25)) = 7 then writeln("Christmas comes on a sunday in " <& year); end if; end for; end func;

Output:

Christmas comes on a sunday in 2011
Christmas comes on a sunday in 2016
Christmas comes on a sunday in 2022
Christmas comes on a sunday in 2033
Christmas comes on a sunday in 2039
Christmas comes on a sunday in 2044
Christmas comes on a sunday in 2050
Christmas comes on a sunday in 2061
Christmas comes on a sunday in 2067
Christmas comes on a sunday in 2072
Christmas comes on a sunday in 2078
Christmas comes on a sunday in 2089
Christmas comes on a sunday in 2095
Christmas comes on a sunday in 2101
Christmas comes on a sunday in 2107
Christmas comes on a sunday in 2112
Christmas comes on a sunday in 2118

xmas 2011 is a sunday
xmas 2016 is a sunday
xmas 2022 is a sunday
xmas 2033 is a sunday
xmas 2039 is a sunday
xmas 2044 is a sunday
xmas 2050 is a sunday
xmas 2061 is a sunday
xmas 2067 is a sunday
xmas 2072 is a sunday
xmas 2078 is a sunday
xmas 2089 is a sunday
xmas 2095 is a sunday
xmas 2101 is a sunday
xmas 2107 is a sunday
xmas 2112 is a sunday
xmas 2118 is a sunday

Unix commands may use time_t to count seconds since the epoch. For systems with 32-bit time, the counter overflows during 19 January 2038. These scripts continue to 2121 and may need a system with 64-bit time, to prevent the overflow.

This format always puts Sunday in columns 1 and 2. The solution uses tail to delete the first 2 lines (month, year, names of days), cut to extract Sunday's columns, and grep to check if "25" appears in those columns.

If the system has 32-bit time, this script will malfunction for years >= 2038; it will print no year from 2038 to 2121 (unless today is Sunday, then it prints every year from 2038 to 2121). This happens because strftime -r '%F' $year-12-25 yields -1 for an out-of-range date, and strftime '%A' -1 yields name of today.

A standard library, stt, provides basic date manipulation functions,
and is imported in this example. Unix era times denominated in seconds since
1969 (excluding leap seconds) are represented as natural numbers with
unlimited precision. Results are valid for the arbitrarily distant
future assuming the Gregorian calendar remains in effect.

The algorithm relies on the string_to_time function converting a date
expressed as a character string to seconds without needing a weekday field in
the input, and the time_to_string function outputting the corresponding
date with the weekday included. The output is then filtered for Sundays.

The original routine in the library only worked correctly between the years
1980 and 2099. It was upgraded with this new routine that handles all dates in
the Gregorian calendar, from 1583 onward. It's based on Zeller's Congruence.