Day of the week: Difference between revisions
Updated task description and Java, UNIX Shell needs code and output update |
→{{header|UNIX Shell}}: It is correct in that it shows a limitation |
||
Line 235: | Line 235: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{incorrect|UNIX Shell}} |
<!-- {{incorrect|UNIX Shell}} It is correct in that it shows a limitation --> |
||
{{works with|bash}} |
{{works with|bash}} |
||
Revision as of 17:04, 12 December 2008
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 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).
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 problems.
Ada
<ada> with Ada.Calendar.Formatting; use Ada.Calendar.Formatting; with Ada.Text_IO; use Ada.Text_IO;
procedure Yuletide is begin
for Year in 2008..2122 loop if Day_Of_Week (Time_Of (Year, 12, 25)) = Sunday then Put_Line (Image (Time_Of (Year, 12, 25))); end if; end loop;
end Yuletide; </ada> Sample output:
2011-12-25 00:00:00 2016-12-25 00:00:00 2022-12-25 00:00:00 2033-12-25 00:00:00 2039-12-25 00:00:00 2044-12-25 00:00:00 2050-12-25 00:00:00 2061-12-25 00:00:00 2067-12-25 00:00:00 2072-12-25 00:00:00 2078-12-25 00:00:00 2089-12-25 00:00:00 2095-12-25 00:00:00 2101-12-25 00:00:00 2107-12-25 00:00:00 2112-12-25 00:00:00 2118-12-25 00:00:00
C
<c>#include <stdio.h>
- include <time.h>
- include <string.h>
int main() {
struct tm mytime; int i; time_t m; for(i=2008; i<=2121; i++) { memset(&mytime, 0, sizeof(struct tm)); mytime.tm_mday = 25; mytime.tm_mon = 11; mytime.tm_year = i-1900; m = mktime(&mytime); if ( m < 0 ) { printf("%d is the last year we can specify\n", i-1); break; } if ( mytime.tm_wday == 0 ) { printf("25 December %d is Sunday\n", i); } }
}</c>
The output of a run on a 32 bit machine is
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
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<=2121;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 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
Objective-C
It should works also with Cocoa and OpenStep in general, but I can't test these.
<objc>#import <Foundation/Foundation.h>
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSUInteger i; for(i=2008; i<2121; i++) { NSCalendarDate *d = [[NSCalendarDate alloc] initWithYear: i month: 12 day: 25 hour: 0 minute: 0 second:0 timeZone: [NSTimeZone timeZoneWithAbbreviation:@"CET"] ]; if ( [d dayOfWeek] == 0 ) { printf("25 Dec %d is Sunday\n", i); } [d release]; } [pool release]; return 0;
}</objc>
Output:
25 Dec 2011 is Sunday 25 Dec 2016 is Sunday 25 Dec 2022 is Sunday 25 Dec 2033 is Sunday 25 Dec 2039 is Sunday 25 Dec 2044 is Sunday 25 Dec 2050 is Sunday 25 Dec 2061 is Sunday 25 Dec 2067 is Sunday 25 Dec 2072 is Sunday 25 Dec 2078 is Sunday 25 Dec 2089 is Sunday 25 Dec 2095 is Sunday 25 Dec 2101 is Sunday 25 Dec 2107 is Sunday 25 Dec 2112 is Sunday 25 Dec 2118 is Sunday
Perl
<perl>#! /usr/bin/perl -w
use Time::Local; use strict;
for(my $i=2008; $i < 2121; $i++) {
my $time = timelocal(0,0,0,25,11,$i); my ($s,$m,$h,$md,$mon,$y,$wd,$yd,$is) = localtime($time); if ( $wd eq 0 ) { print "25 Dec $i is Sunday\n"; }
}
exit 0;</perl>
Output:
25 Dec 2011 is Sunday 25 Dec 2016 is Sunday 25 Dec 2022 is Sunday 25 Dec 2033 is Sunday Day too big - 25195 > 24855 Sec too small - 25195 < 78352 Sec too big - 25195 > 15247 Cannot handle date (0, 0, 0, 25, 11, 2038) at ./ydate.pl line 8
I suppose there's a CPAN package that handles dates without relying on hardware/implementation dependent sizes (the Perl code basically seems to use the same functions used by C code)
Python
<python>import datetime
def yuletide():
sunday = 6 days = (day.strftime('%d %b %Y') for day in (datetime.date(year, 12, 25) for year in range(2008,2122)) if day.weekday() == sunday) print '\n'.join(days)
yuletide()</python>Output:
25 Dec 2011 25 Dec 2016 25 Dec 2022 25 Dec 2033 25 Dec 2039 25 Dec 2044 25 Dec 2050 25 Dec 2061 25 Dec 2067 25 Dec 2072 25 Dec 2078 25 Dec 2089 25 Dec 2095 25 Dec 2101 25 Dec 2107 25 Dec 2112 25 Dec 2118
UNIX Shell
#! /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$