Linux, Unix, /etc

Danger Will Robinson! You are now entering a condescending Unix user zone!
Sponsored links (requires javascript):

Scripts: Dates and Times

cal: a modified version of cal(1)

I got this from that remarkable book, Unix Power Tools.

 
#!/bin/sh
#if user didn't give arguments, put >< around today's date

case $# in
    0) eval `date +d=%d`
     case $d in
	 01) d=1;;
	 02) d=2;;
	 03) d=3;;
	 04) d=4;;
	 05) d=5;;
	 06) d=6;;
	 07) d=7;;
	 08) d=8;;
	 09) d=9;;
     esac
    /usr/bin/cal |
    sed -e 's/^/ /' -e "s/^$d />$d</" -e "s/ $d$/>$d</" -e "s/ $d />$d</"
    ;;
    *) case $# in
	0)	set `date`; m=$2; y=$6 ;;	# no args: use today
	1)	m=$1; set `date`; y=$6 ;;	# 1 arg: use this year
	*)	m=$1; y=$2 ;;			# 2 args: month and year
    esac
    case $m in
	jan*|Jan*)	m=1 ;;
	feb*|Feb*)	m=2 ;;
	mar*|Mar*)	m=3 ;;
	apr*|Apr*)	m=4 ;;
	may*|May*)	m=5 ;;
	jun*|Jun*)	m=6 ;;
	jul*|Jul*)	m=7 ;;
	aug*|Aug*)	m=8 ;;
	sep*|Sep*)	m=9 ;;
	oct*|Oct*)	m=10 ;;
	nov*|Nov*)	m=11 ;;
	dec*|Dec*)	m=12 ;;
	[1-9]|10|11|12)	;;		# numeric month
	*)		y=$m; m="" ;;	# plain year
    esac
    /usr/bin/cal $m $y
    ;;
esac


qtr: the calendar for this quarter

cal $(date +'%b %Y' —date '1 month ago')
cal
cal $(date +'%b %Y' —date '1 month')


easter: calculate the date of Easter

An implementation in awk of the algorithm for calculating the date of Easter, as given in Don Knuth's "Fundamental Algorithms". Not very useful, but hey, I like awk!

 
#!/usr/bin/awk -f
# cf Knuth, p. 156
{
# 1. Golden number
g = ($1 % 19) + 1;

# 2. Century
c = int($1 / 100) + 1;

# 3. Corrections
# the number of years in which the leap year was dropped in order
# to keep step with the sun e.g. 1900
x = int((3 * c) / 4) - 12;
# synchronise Easter with the moon's orbit
z = int(((8 * c) + 5) / 25) - 5;

# 4. Find Sunday
d = int((5 * $1) / 4) - x - 10;

# 5. Epact (specifies when a full moon occurs)
e = (((11 * g) + 20 + z - x)) % 30;
if ((e == 25 && g > 11) || (e == 24)) {
    e = e + 1;
}

# 6. Find full moon.  Easter is supposedly the "first Sunday
# following the first full moon which occurs on or after 21st
# March".  Actually, peturbations in the moon's orbit make this
# not strictly true, but we are concerned here with the "calendar
# moon" rather than the actual moon.  The Nth of March is a 
# calendar full moon.
n = 44 - e;
if (n < 21) {
    n = n + 30;
}

# 7. Advance to Sunday
n = n + 7 - ((d + n) % 7);

# 8. Get month
if (n > 31) {
    n = n - 31;
    printf "%d April\n", n;
}
else {
    printf "%d March\n", n;
}
}


jdate: today's date in English

A convenient way on inserting the date in my favourite format in any document: e.g. in vi, typing "!!jdate", or the ex equivalent ":r!jdate", inserts "Sunday 21st October 2001".

 
#!/bin/sh
set `date +'%A %d %B %Y'`
day=$2
case $day in
    01) day=1;;
    02) day=2;;
    03) day=3;;
    04) day=4;;
    05) day=5;;
    06) day=6;;
    07) day=7;;
    08) day=8;;
    09) day=9;;
esac
case $day in
    1?) day=$day"th";;
    *1) day=$day"st";;
    *2) day=$day"nd";;
    *3) day=$day"rd";;
    *) day=$day"th";;
esac
echo $1 $day $3 $4


gdate: today's date in German

And this just does the same in German.

 
#!/bin/sh
set `date +'%A %d %B %Y'`
day=$2
case $day in
    01) day=1;;
    02) day=2;;
    03) day=3;;
    04) day=4;;
    05) day=5;;
    06) day=6;;
    07) day=7;;
    08) day=8;;
    09) day=9;;
esac
#case $day in
#    1?) day=$day"th";;
#    *1) day=$day"st";;
#    *2) day=$day"nd";;
#    *3) day=$day"rd";;
#    *) day=$day"th";;
#esac
day=$day"."
dayname=$1
case $dayname in
    Monday)	dayname=Montag;;
    Tuesday)	dayname=Dienstag;;
    Wednesday)	dayname=Mittwoch;;
    Thursday)	dayname=Donnerstag;;
    Friday)	dayname=Freitag;;
    Saturday)	dayname=Samstag;;
    Sunday)	dayname=Sontag;;
esac
echo $dayname $day $3 $4


month: view pcal calendar in text format for this month

pcal is a very useful program. It is handy to review the data without printing out the calender sometimes. This is where this little script comes in.

 
#!/bin/sh
#planner
set `date '+%m %Y'`
month=$1
year=$2
2>&1 pcal -c $month $year | less


year: view pcal calendar in text format for whole year or from the current month

 
#!/bin/sh
#year
case $1 in
"-h")
    echo "year: usage year [-f]"
    echo "Use -f for full year (default from current month)"
;;
"-f")
    2>&1 pcal -c -w | less
;;
*)
    set `date '+%m %Y'`
    month=$1
    year=$2
    left=`expr 12 - $month`
    2>&1 pcal -c -w $month $year $left | less
;;
esac


whattime: the time in various time zones

 
#!/bin/sh
for i in Europe/Dublin Europe/Berlin US/Eastern US/Central US/Mountain US/Pacific
do
    export TZ=$i
    case $1 in
	    "") echo "`date`	$i";;
	    *) echo "`date`	$i" | grep $1;;
    esac
done


weekday: does a given date fall on a weekday or a weekend

#!/bin/sh
day=$1
month=$2
year=$3
case $year in 
"") year=`date +%Y`;;
esac
case $month in
"") month=`date +%m`;;
esac
case $day in
"") 1>&2 echo "must give at least a day"; exit 2;;
esac
case $month in
Jan) month=01;;
Feb) month=02;;
Mar) month=03;;
Apr) month=04;;
May) month=05;;
Jun) month=06;;
Jul) month=07;;
Aug) month=08;;
Sep) month=09;;
Oct) month=10;;
Nov) month=11;;
Dec) month=12;;
esac
dayname=`date -d $month/$day/$year +%a`
case $dayname in
	Sat|Sun) echo "$day of $month is $dayname, not a weekday"; exit 0;;
	*) echo "$day of $month is $dayname, a weekday"; exit 1;;
esac


get-last-friday.awk

Trivial, but handy if that's when you get paid.

#!/bin/sh
/usr/bin/cal $1 $2 | 
awk '{ lasta = a; a = $6; if (a == "") a=lasta } END { print a }'


dateseries: usage: dateseries number_of_days

Starting from today, prints the dates of days up to number_of_days in the future, the date printed by jdate.

 
#!/bin/sh

progname=`basename $0`
case $# in
    0) 1>&2 echo "$progname: usage: dateseries number_of_days"; exit 1;;
esac
limit=$1

cvtday()
{
case $day in
    01) day=1;;
    02) day=2;;
    03) day=3;;
    04) day=4;;
    05) day=5;;
    06) day=6;;
    07) day=7;;
    08) day=8;;
    09) day=9;;
esac
case $day in
    1?) day=$day"th";;
    *1) day=$day"st";;
    *2) day=$day"nd";;
    *3) day=$day"rd";;
    *) day=$day"th";;
esac
}

for i in `series $limit`
do
    set `date +'%A %d %B %Y' —date "$i day"`
    day=$2
    cvtday
    today="$1 $day $3 $4"
    echo $today
done


cuckoo-clock

A silly little thing, run from crontab every hour to make the system chime like a cuckoo clock. Requires a suitable .wav file, obviously.

#!/bin/sh
#cuckoo-clock:
hour=$(expr `date +%H` % 12)
i=1
while :
do
    if [ $i -gt $hour ]
    then
	exit 0
    fi
    wavplay $HOME/sounds/cuckoo.wav
    i=$(expr $i + 1)
done



[back to Scripts index] [back to Linux, Unix, etc] [Main Site] [Weblog]



Contents licensed under the GPL