SUMMARY: date manipulation in shell scripts

From: Young, Jim x3125 (JimY@sequus.com)
Date: Wed Jun 25 1997 - 16:41:32 CDT


Thanks to:
   assis @ npd.uel.br (Marcos Assis Silva)
   Stuart_Lawson@ivax.com
   Adrian Konig (akonig@spectraweb.ch)
   gustavo@cpqd.br (Gustavo Chaves)
   RAVKRISH.IN.ORACLE.COM.ofcmail@in.oracle.com (K. Ravi)
   Steffen Kluge <kluge@fujitsu.com.au>
   garry.robbins@labatt.com
   jeffw@smoe.org
The question was:
================================================
> Managers,
> I've had a hard time locating information on simple manipulation of
> dates, such as finding the number of days between two dates. Are you
> aware of some simple utilities, UNIX commands, or samples of shell
> script code that I could use?
================================================
Some replies:
------------------------------------------------------------------------
-------------------------
FROM Jeff:
Your best bet is to do your date calcs in terms of 'unix time'
which is the number of seconds since Jan 1 1970.

Once you convert both dates to the number of seconds since Jan 1
1970, it's easy to subtract the 2 and then figure out how many
days it is.

You can either do it in perl, or get gnu date, which has the
ability to work in seconds.

See the man pages for time(2) and ctime(3C).

Here's a snippet of perl that prints the date 7 days from now:

$start_time = time;
chop($today_date = &ctime($start_time));
chop($nextweek_date = &ctime($start_time + 604800));
print "today is $today_date and a week from today is $nextweek_date.\n";
------------------------------------------------------------------------
-------------------------
FROM Steffen:
Downloading GNU date is the half way of getting there. It gives you
a formatted output (like /usr/bin/date) for ANY date you specify.
I'm not having it here at the moment but I think it also does delta
calculations for you. Have a look
------------------------------------------------------------------------
-------------------------
FROM Ravi:
Hi,
 Some time back, I saw a similar thread on the newsgroup
comp.unix.solaris.
You can search the newsgroup archive at
http://www.dejanews.com/forms/dnq.html. (The subject was something like
'Yesterday's date from shell' and the date was around end of
Feb/beginning of
March. These details can hrlp you to narrow down the search)
   [ Yes, there is good info there ... Jim]
I also got a C program to do the job, on the same list. It's somewhat
easily
doable in Perl also. Let me know if you're interested. I can send the C

program and some Perl code.
------------------------------------------------------------------------
-------------------------
FROM Marcos:
        Hi. To get around these same difficulties I ended up
building two
utilities of my own breeding. One of them, "mktime" gets a date in
plain
`date` format and yields the corresponding number of seconds since
blah,
blah. The othe one, "cftime", does the other way around, ie, it takes
the a
number representing the number os seconds since ... and spits out a
date
string in plain `date` format. With them, it's a piece of cake to
"normalize"
dates, do aritmethics (sp?) on them, and display those "normalized"
dates
when needed. To complement them I compiled and installed the GNU
version of
"date". It accepts a -d switch thru which it can operate on any date
and not
just the system one. They would be available upon request (source) if
you are
interested. Regards ...
------------------------------------------------------------------------
-------------------------

James P. Young, as of 30 June, 1997 at James_Young@gilead.com

 



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:58 CDT