Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Date and time calculations in UNIX

Posted on 2013-10-25
8
Medium Priority
?
986 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 85

Expert Comment

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

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 35

Expert Comment

by:Duncan Roe
ID: 39603405
You can flag an error if the output value is negative, if you like
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 85

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 35

Accepted Solution

by:
Duncan Roe earned 1500 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

916 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