Subject: Re: How to get arbitrary time in shell script?
To: None <netbsd-users@netbsd.org>
From: David Laight <David.Laight@btinternet.com>
List: netbsd-users
Date: 11/29/2001 10:03:12
If you know where you are:
% TZ=GMT-72 date
will give the date 72 hours ahead...

Solaris has no limit on the number of hours shift your timezone has,
one was introduced in UnixWare 7.
Cygwin won't let me move more than 167 hours (1 week)
Dunno about netBSD - I'd have to reboot this system twice...
Changing TZ is excellent for making it look as though you generated
a printout several days earlier.  Unfortunately you cannot specify
an offset relative to the current one, only to GMT (UTC).

> > > So, how can i get a date from three days ago?
> > > date(1) doesn't have an option like 
> > > 
> > >    date '+ 3 days ago'
> > 
> >  date -r `expr \`date +%s\` - 259200`
> > # where 259200 is 60 seconds * 60 minutes * 24 hours * 3 days

Well this is entirely non-portable - probably why you hadn't heard of it!
The 'unix spec' (I've just checked the opengroup web site) only has:
    date [-u] +format
    date [-u] mmddhhmm[[cc]]yy]
%s isn't a valid format.

The FSF 'date' used by cygwin has '-r, --reference=FILE: display the last
    modification time of FILE'
    (but does know about the %s format)
> > 
> 
> Maybe it even looks a little bit nicer this way:
> 
>        date -r `expr $(date +%s) - 259200`
One day someone will try hard to depracate '`...`' but they haven't
suceeded yet.  The '$(...)' form is only understood by some shells,
in particular the 'standard' bourne shell (the one scripts should be
written for) doesn't support it.

If you are going to do it, do it properly...

        date -r $(expr $(date +%s) - 3 '*' 24 '*' 60 '*' 60)

    David