Day of the week

Revision as of 15:01, 12 December 2008 by rosettacode>ShinTakezou (→‎{{header|UNIX Shell}}: should we instead say the version of the underlying glibc/libc?)

A company decides that whenever Xmas falls on a Sunday that 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).

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

In what years between 2008 and 2099 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 problems.

Java

<java>import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar;

public class Yuletide{ public static void main(String[] args) { for(int i = 2008;i<=2099;i++){ GregorianCalendar cal = new GregorianCalendar(i, Calendar.DECEMBER, 25); if((cal.get(Calendar.DAY_OF_WEEK))==Calendar.SUNDAY){ System.out.println(new Date(cal.getTimeInMillis())); } } } }</java> Output:

Sun Dec 25 00:00:00 CST 2011
Sun Dec 25 00:00:00 CST 2016
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

UNIX Shell

Works with: bash
#! /bin/bash

for((i=2009; i <= 2099; i++))
do
 date -d "$i-12-25" |grep Sun
done

exit 0

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

Sun Dec 25 00:00:00 CET 2011
Sun Dec 25 00:00:00 CET 2016
Sun Dec 25 00:00:00 CET 2022
Sun Dec 25 00:00:00 CET 2033
date: invalid date `2038-12-25'

I.e., starting from year 2038, the date command (which uses the glibc library, at least on GNU systems), is not able to recognise the date as a valid one!

Different machine/OS version #1

This is the same command run on RedHat Linux.

bash-3.00$ date --version
date (coreutils) 5.2.1
Written by David MacKenzie.

Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
bash-3.00$ uname -a
Linux brslln01 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
bash-3.00$ for((i=2009; i <= 2099; i++)); do  date -d "$i-12-25" |egrep Sun; done
Sun Dec 25 00:00:00 GMT 2011
Sun Dec 25 00:00:00 GMT 2016
Sun Dec 25 00:00:00 GMT 2022
Sun Dec 25 00:00:00 GMT 2033
Sun Dec 25 00:00:00 GMT 2039
Sun Dec 25 00:00:00 GMT 2044
Sun Dec 25 00:00:00 GMT 2050
Sun Dec 25 00:00:00 GMT 2061
Sun Dec 25 00:00:00 GMT 2067
Sun Dec 25 00:00:00 GMT 2072
Sun Dec 25 00:00:00 GMT 2078
Sun Dec 25 00:00:00 GMT 2089
Sun Dec 25 00:00:00 GMT 2095
bash-3.00$