Date: Mon, 27 Jun 2005 21:47:50 -0500 Archive-name: unix-faq/shell/sh Posting-Frequency: monthly Version: $Id: cus-faq-appendix.html,v 1.2 2005/05/21 19:15:57 jhalpin Exp $ Maintainer: Joe Halpin Please read the introduction in the first section of this document. This section assumes you have read that introduction. ====================================================================== Appendix A: Examples Web sites: ------------------------------------------- Heiner Steven's Shelldorado site has quite a few example scripts, tutorials, and links to other such places. http://www.shelldorado.com/ Arbitrary Date Arithmetic ------------------------------------------- From: Tapani Tarvainen (tt@it.jyu.fi) Subject: Re: yesterday's date under the shell View: Complete Thread (8 articles) Original Format Newsgroups: comp.unix.shell Date: 2002-02-12 07:45:05 PST "Jean-No?l"writes: > To determine the yesterday date i do it so: > TZ=PST+24 date +%d > it work well but my question is: > does this work on all systems and all shells > or should i do it otherwise ??? No, it does not work on all systems at all times. In some it will work practically always, on others never, on most it works sometimes and sometimes not. I would recommend against it. Unfortunately there is no short and sweet portable solution. If you have or can install Gnu date it will do it cleanly, otherwise you can find a number of solutions posted in this group in the past. For a general solution you could try the following, which should work with POSIXy shells (I've only tested it with HP's which is essentially ksh88, though): #! /usr/bin/sh # Date calculations using POSIX shell # Tapani Tarvainen July 1998, February 2001 (POSIXified) # This code is in the public domain. # Julian Day Number from calendar date date2julian() # day month year { day=$1; month=$2; year=$3 tmpmonth=$((12 * year + month - 3)) tmpyear=$((tmpmonth / 12)) print $(( (734 * tmpmonth + 15) / 24 - 2 * tmpyear + \ tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 )) } # Calendar date from Julian Day Number julian2date() # julianday { tmpday=$(($1 - 1721119)) centuries=$(( (4 * tmpday - 1) / 146097)) tmpday=$((tmpday + centuries - centuries/4)) year=$(( (4 * tmpday - 1) / 1461)) tmpday=$((tmpday - (1461 * year) / 4)) month=$(( (10 * tmpday - 5) / 306)) day=$((tmpday - (306 * month + 5) / 10)) month=$((month + 2)) year=$((year + month/12)) month=$((month % 12 + 1)) print $day $month $year } # Day of week, Monday=1...Sunday=7 dow() # day month year { print $(( $(date2julian $1 $2 $3) % 7 + 1)) } ##################### The End ######################## Those allow rather arbitrary date computations. For example, yesterday's date can be computed like this: julian2date $(( $(date2julian $(date +"%d %m %Y") ) - 1 )) -- Tapani Tarvainen ====================================================================== Appendix B: References