SUMMARY: show date from x number of days ago

From: Thompson , Troy <tthompson_at_tmisolutions.com>
Date: Fri, 24 Mar 2000 08:37:34 -0500

Thank-you everyone for the numerous answers to my question, which was:

        I am looking for a way to add/subtract days from the current date
for display, without changing the actual date/time.

        In Oracle I can do "sysdate - 3" to display the date from 3 days
ago.
         
        I am looking for a way to display the date - x number of days.

        Eg. today is Thu Mar 23 I would like to a way to display Mon Mar 20
(today - 3 days) or Fri Mar 17 (today - 6 days) etc.

        Is there a command or script that will do this?


Special thanx go to

Jim Fitzmaurice (jpfitz_at_fnal.gov)
Dan (dsr_at_mail.lns.cornell.edu)
Michael Huff (mhuff_at_ox.com)
Richard Westlake (r.westlake_at_mail.cryst.bbk.ac.uk)
Dennis Breeden (D.Breeden_at_wcom.com)
Jerome M Berkman (jerry_at_uclink4.berkeley.edu)
Thomas Hoffman (hofmannt_at_post.ch)
Marie-Claude Vialatte (mc.vialatte_at_cust.univ-bpclermont.fr)

They were all very helpful answers, but for myself Jerome M Berkman's perl
script was exactly what I was looking for.

Here is a summary of what people sent me.



----------------------------------------------------------------------------
------------------
Jim Fitzmaurice (jpfitz_at_fnal.gov) wrote:
        I don't know of an easy way to do this on Tru64 4.0e. Quite a few
years ago
I wrote a ksh script that could add or subtract any number of days from the
current date. But that was 3 jobs and 9 years ago and on the CX-UX flavor of
UNIX. It was complex taking into account leap years and such, but it is
possible, if you don't get a better way from someone else.

        Too bad your not on a LINUX system, on LINUX the command date -d '3
days
ago' will display just what you want, and it's just that simple. (And some
people still insist that Open Source won't work.)

        I wonder if the LINUX date command would compile on Tru64. Probably
not,
unless you downloaded gcc to your machine and compiled it under that, then
it might work. But I suppose your looking for something simpler. Heck
writing a script would probably be easier, but not as interesting.

Jim Fitzmaurice

----------------------------------------------------------------------------
------------------
Dan (dsr_at_mail.lns.cornell.edu) wrote

The date command from the GNU sh-utils package (available from
ftp://ftp.gnu.org) will do this:

dsr_lnscu4% /usr/local/bin/date --date="-3 days"
Mon Mar 20 14:50:59 EST 2000
dsr_lnscu4% /usr/local/bin/date --date="-6 days"
Fri Mar 17 14:51:02 EST 2000

-dan

----------------------------------------------------------------------------
------------------
Michael Huff (mhuff_at_ox.com) wrote:

Here is a trick that I've used:

#!/bin/ksh

localtime=$(date +%H)
utc=$(date -u +%H)

offset=$(($utc - $localtime))
today=$(TZ="EST${offset}EDT${offset}" date +%Y%m%d)

offset=$(($offset + 24))
yesterday=$(TZ="EST+${offset}EDT+${offset}" date +%Y%m%d)

offset=$((48 - $offset))
tomorrow=$(TZ="EST-${offset}EDT-${offset}" date +%Y%m%d)

echo "YESTERDAY=$yesterday"
echo "TODAY=$today"
echo "TOMORROW=$tomorrow"

----------------------------------------------------------------------------
------------------
Richard Westlake (r.westlake_at_mail.cryst.bbk.ac.uk) wrote:

This web page may help
    http://www.cryst.bbk.ac.uk/~richards/tricks/dates_sh.html

Richard Westlake

----------------------------------------------------------------------------
------------------
Dennis Breeden (D.Breeden_at_wcom.com) wrote

Below is a script to extract yesterday date, below in one for seven days
ago. I understand GNUdate support the function you want though.
YESTERDAY



#!/bin/ksh
###############################################
# Extract day of month
###############################################
a=`date +"%e"`
###############################################
# Extract abbreviated month text
###############################################
b=`date +"%b"`
###############################################
# Find out if Feb is 28 or 29 daze
# (Leap year complience)
###############################################
#feb=28
#cal 2 `date +%Y`|tail +3|grep 29 && feb=29
febd=`date +%Y`
feba=`cal 2 $febd | tail -2 | head -1 | wc -m`
febb=`expr $feba - 2`
febc=`expr $feba - 1`
feb=`cal 2 $febd | tail -2 | head -1 | cut -c$febb,$febc`
###############################################
# Determine value of adjust value if
# if it the first day of the month
###############################################
case $b in
Jan) c=31;;
Feb) c=$feb;;
Mar) c=31;;
Apr) c=30;;
May) c=31;;
Jun) c=30;;
Jul) c=31;;
Aug) c=31;;
Sep) c=30;;
Oct) c=31;;
Nov) c=30;;
Dec) c=31;;
esac
############################################
# Adjust month and day if necessary if
# it is the first day of the month
############################################
if
[ $a -le 1 ]
then
monthnum=`date +"%m"`
f=`expr $monthnum - 1`
# d=`date +"%d"`
# i=`expr 1 - $d`
case $f in
1) c=31;;
2) c=$feb;;
3) c=31;;
4) c=30;;
5) c=31;;
6) c=30;;
7) c=31;;
8) c=31;;
9) c=30;;
10) c=31;;
11) c=30;;
12) c=31;;
esac
# e=`expr $c - $i`
e=$c
else
d=`date +"%d"`
i=`expr $d - 1`
if
[ $i -le 9 ]
then
e="0"$i
else
e=$i
fi
f=`date +"%m"`
fi
echo "$f$e"




LASTWEEK



#!/bin/ksh
###############################################
# Extract day of month
###############################################
a=`date +"%e"`
###############################################
# Extract abbreviated month text
###############################################
b=`date +"%b"`
###############################################
# Find out if Feb is 28 or 29 daze
###############################################
#feb=28
#cal 2 `date +%Y`|tail +3|grep 29 && feb=29
febd=`date +%Y`
feba=`cal 2 $febd | tail -2 | head -1 | wc -m`
febb=`expr $feba - 2`
febc=`expr $feba - 1`
feb=`cal 2 $febd | tail -2 | head -1 | cut -c$febb,$febc`
###############################################
# Determine value of adjust value if
# if it the first day of the month
###############################################
case $b in
Jan) c=31;;
Feb) c=$feb;;
Mar) c=31;;
Apr) c=30;;
May) c=31;;
Jun) c=30;;
Jul) c=31;;
Aug) c=31;;
Sep) c=30;;
Oct) c=31;;
Nov) c=30;;
Dec) c=31;;
esac
############################################
# Adjust month and day if necessary if
# it is the first day of the month
############################################
if
[ $a -le 7 ]
then
monthnum=`date +"%m"`
f=`expr $monthnum - 1`
d=`date +"%d"`
i=`expr 7 - $d`
case $f in
1) c=31;;
2) c=$feb;;
3) c=31;;
4) c=30;;
5) c=31;;
6) c=30;;
7) c=31;;
8) c=31;;
9) c=30;;
10) c=31;;
11) c=30;;
12) c=31;;
esac
e=`expr $c - $i`
else
d=`date +"%d"`
i=`expr $d - 7`
if
[ $i -le 9 ]
then
e="0"$i
else
e=$i
fi
f=`date +"%m"`
fi
echo "$f$e"

----------------------------------------------------------------------------
------------------
Jerome M Berkman (jerry_at_uclink4.berkeley.edu) wrote:

Here is a short perl script which if stored in "prev" and with
proper modes, will do it.

        - Jerry Berkman, UC Berkeley

#! /usr/bin/perl -w

# show previous date

if( $#ARGV != 0 ) {
        print "Usage: $0 n\nWhere \"n\" is the number of days previous\n";
        exit 1;
}

require "ctime.pl";
print ctime( time - $ARGV[0]*24*3600 );

----------------------------------------------------------------------------
------------------
Thomas Hoffman (hofmannt_at_post.ch) wrote:

I did it with perl.

usage:
        t.pl days

Examples:
        #> t.pl 8
        Sam Apr 01
        #> t.pl -1
        Thu Mar 23
        #
        #> t.pl -6
        Sam Mar 18

Please forgive me if the names of the days and month are not correct. My
english is not so good.

Regards Thomas



#!/bin/perl
# --------------------------------------------------------------
# Filename: t.pl
# Creation Date: 19.01.2000
# Author: T.Hofmann
#
# Abstract: The argument is added to the time
#
# Parameters none
# --------------------------------------------------------------
# Variables definitions

$numberdays = $ARGV[0]*24*3600;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time() +
$numberdays);
#$year += 1900;
#$mon += 1;
$stringwday = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sam')[$wday];
$stringmon = ('Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Oct',
'Nov', 'Dec')[$mon];
$date=sprintf "%s %s %02d",$stringwday,$stringmon,$mday;
print $date;
print "\n";


----------------------------------------------------------------------------
------------------
Marie-Claude Vialatte (mc.vialatte_at_cust.univ-bpclermont.fr) wrote:

Look at GNU date in sh-utils.
You will find it in many anonymous FTP sites.



Troy Thompson
TMI Communications
(613) 742-0010 ext 4406
tthompson_at_tmisolutions.com
www.tmisolutions.com


> -----Original Message-----
> From: Thompson , Troy
> Sent: Thursday, March 23, 2000 1:00 PM
> To: 'tru64-unix-managers_at_ornl.gov'
> Subject: show date from x number of days ago
>
> Hello All,
>
> I am running DU 4.0e, but this is more of a general unix question. Sorry
> if this doesn't belong on this list.
>
> I am looking for a way to add/subtract days from the current date for
> display, without changing the actual date/time.
>
> In Oracle I can do "sysdate - 3" to display the date from 3 days ago.
>
> I am looking for a way to display the date - x number of days.
>
> Eg. today is Thu Mar 23 I would like to a way to display Mon Mar 20 (today
> - 3 days) or Fri Mar 17 (today - 6 days) etc.
>
> Is there a command or script that will do this?
>
> Thanks in advance.
>
>
> Troy Thompson
> TMI Communications
> (613) 742-0010 ext 4406
> tthompson_at_tmisolutions.com
> www.tmisolutions.com
>
>
Received on Fri Mar 24 2000 - 13:38:28 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:40 NZDT