Day of the week: Difference between revisions

UNIX Shell and UnixPipes: date -d seems to be a GNU extension. Add alternate solution with cal.
m (→‎{{header|UNIX Shell}}: Remove dependence on seq, which is not available everywhere bash is)
(UNIX Shell and UnixPipes: date -d seems to be a GNU extension. Add alternate solution with cal.)
Line 1,636:
 
=={{header|UNIX Shell}}==
===With GNU date===
This solution uses date -d, which seems to be a [[GNU]] extension, so it only works with those systems.
 
{{works with|bash}}
<lang bash>#! /bin/bash
Line 1,656 ⟶ 1,659:
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!
 
===''Different machine/OS version (64 bit)===:''
This is the same command run on RedHat Linux.
<lang bash>bash-3.00$ date --version
Line 1,686 ⟶ 1,689:
Sun Dec 25 00:00:00 GMT 2118
bash-3.00$</lang>
 
===With Unix cal===
The <code>cal</code> command is a tradition since Version 6 AT&T UNIX. This solution assumes that <code>cal</code> will always output a calendar in this format.
 
<pre>$ cal 12 2011
December 2011
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
</pre>
 
This format always puts Sunday in columns 1 and 2, so this solution can check if "25" appears in those columns. (Also, the year is not a problem for Sunday. Checking if 20 December 2011 is a Thursday? The year "2011" on the first line would have broken the check.) If your <code>cal</code> uses a different format, then you would need to edit the script.
 
{{works with|OpenBSD|4.8}}
<lang sh>y=2008
while test $y -lt 2122; do
cal 12 $y | cut -c1-2 | grep -Fq 25 && echo 25 Dec $y
y=`expr $y + 1`
done</lang>
 
OpenBSD <code>cal</code> accepted all years from 1 to 9999, so 2008 to 2122 was well within range. The output was identical to the C# program.
 
=={{header|UnixPipes}}==
This solution uses date -d, which seems to be a [[GNU]] extension, so it only works with those systems.
 
Thanks to UNIX Shell implementation
<lang bash>seq 2008 2121 | xargs -IYEAR -n 1 date +%c -d 'Dec 25 YEAR' | grep Sun</lang>
Anonymous user