Solved

Date and time calculations in UNIX

Posted on 2013-10-25
8
923 Views
Last Modified: 2013-11-21
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).
0
Comment
Question by:babyb00mer
  • 3
  • 3
  • 2
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39602292
perl -MDate::Manip -le 'print +(DateCalc("now",shift.":00")=~/^0:.*:(\d+:\d+):\d+$/)[0]||"illegal"'
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39603404
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
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39603405
You can flag an error if the output value is negative, if you like
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:babyb00mer
ID: 39606313
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?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39607071
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.
0
 

Author Comment

by:babyb00mer
ID: 39628886
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]
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 500 total points
ID: 39629587
You need to install GNU date. You should be able to do that on AIX. Perhaps they already have it as gdate
0
 

Author Comment

by:babyb00mer
ID: 39639660
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question