Link to home
Start Free TrialLog in
Avatar of Edgar Cole
Edgar ColeFlag for United States of America

asked on

Date and time calculations in UNIX

I need to be able to calculate the difference in seconds between the current time and a future point in time. The subtrahend will be derived from the current time, and the minuend will be specified as the hour and minute, like this: HH:MM. The arguments are assumed to be 24-hour (military) time. A minuend predating the current time will be considered illegal. I'm working primarily with the Korn shell, but I'm willing to consider solutions from any of the other interpreters (e.g., Perl).
Avatar of ozo
ozo
Flag of United States of America image

perl -MDate::Manip -le 'print +(DateCalc("now",shift.":00")=~/^0:.*:(\d+:\d+):\d+$/)[0]||"illegal"'
The date command is brilliant for doing date arithmetic. You can convert any date it recognizes to epoch (seconds since 1970-01-01.00:00:00), do your arithmetic, then convert back to format and fields of your choice. Works from any shell (that can do arithmetic).

Do you really want to reject a lower HH:MM than time now? I would have expected you'd want to consider it as being a time tomorrow, at least if the resulting interval is less than, say, 12 hours. (Perl is not a shell by the way: out of Perl, Python and Tcl/Tk only Tcl/Tk provides a shell. But I digress).

In this example, I set shell variables HH & MM to represent 1500 hrs. Then I get how many seconds until then (it's now a bit after 14:10). I'm using bash, but did try it with ksh - worked fine
14:10:49$ HH=15
14:13:05$ MM=00
14:13:08$ echo $(($(date -d $HH:$MM +%s) - $(date +%s)))
2805
14:13:15$ 

Open in new window

Post back if you'd like an explanation, or take a look at the output from entering man date
You can flag an error if the output value is negative, if you like
Avatar of Edgar Cole

ASKER

Good stuff guys! I was a little concerned when half a day went by without a response! :-) And who was that lecturing me about the difference between Perl and shells?! Why you I oughta... ;-) Just kidding! Your thoroughness is appreciated!

from duncan_roe...

Do you really want to reject a lower HH:MM than time now? I would have expected you'd want to consider it as being a time tomorrow...
Definitely worth considering!

ozo...

What would the Perl example look like using the same value(s) from the shell example?
perl -MDate::Manip -le 'print +(DateCalc("now",shift.":00")=~/^0:.*:(\d+:\d+):\d+$/)[0]||"illegal"'  15:00
or
HH=15
MM=00
perl -MDate::Manip -le 'print +(DateCalc("now",shift.":00")=~/^0:.*:(\d+:\d+):\d+$/)[0]||"illegal"'  $HH:$MM
But if you want HH and MM separate, we can simplify the script a little.
duncan_roe..,

Where are you running the date command (i.e., Solaris, HP-UX, Linux, etc.)? On my AIX system, it's failing as follows:

$ HH=06
$ MM=00
$ echo $(($(date -d $HH:$MM +%s) - $(date +%s)))
date: illegal option -- d
Usage: date [-u] [+Field Descriptors]
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using the perl command I'm getting messages resembling the following:

# perl -MDate::Manip -le 'print +(DateCalc("now",shift.":00")=~/^0:.*:(\d+:\d+):\d+$/)[0]||"illegal"' 15:15
Can't locate Date/Manip.pm in @INC (@INC contains: /usr/opt/perl5/lib/5.8.8/aix-thread-multi /usr/opt/perl5/lib/5.8.8 /usr/opt/perl5
/lib/site_perl/5.8.8/aix-thread-multi /usr/opt/perl5/lib/site_perl/5.8.8 /usr/opt/perl5/lib/site_perl .).
BEGIN failed--compilation aborted.

Open in new window

I get similar errors from various environments - including SuSE Linux.