Date and time calculations in UNIX

Posted on 2013-10-25
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).
Question by:babyb00mer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
LVL 84

Expert Comment

ID: 39602292
perl -MDate::Manip -le 'print +(DateCalc("now",shift.":00")=~/^0:.*:(\d+:\d+):\d+$/)[0]||"illegal"'
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)))

Open in new window

Post back if you'd like an explanation, or take a look at the output from entering man date
LVL 34

Expert Comment

by:Duncan Roe
ID: 39603405
You can flag an error if the output value is negative, if you like
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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!


What would the Perl example look like using the same value(s) from the shell example?
LVL 84

Expert Comment

ID: 39607071
perl -MDate::Manip -le 'print +(DateCalc("now",shift.":00")=~/^0:.*:(\d+:\d+):\d+$/)[0]||"illegal"'  15: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.

Author Comment

ID: 39628886

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]
LVL 34

Accepted Solution

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

Author Comment

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/ 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.

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
can i read my emails on lamp ftp 4 70
trouble on installing syslog-ng on CentOS 7 7 155
leap year shell script 10 51
Linux Server mapping drive using SSH key 9 52
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

734 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