Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date and time calculations in UNIX

Posted on 2013-10-25
8
Medium Priority
?
971 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
[X]
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
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 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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

721 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