Computing.Net > Forums > Unix > Calculation of time difference

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Calculation of time difference

Reply to Message Icon

Name: gatasandy
Date: August 4, 2006 at 04:07:36 Pacific
OS: Windows XP
CPU/Ram: 1 GB
Comment:

Hi,
Here is the problem:
How do I calculate the time difference in Unix ? For example time difference between 02 AUG 2006 6:20:30 pm and 04 AUG 2006 12:15:45 pm in intended units ie ; min. Any idea is welcome.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 4, 2006 at 11:03:38 Pacific
Reply:

This type of problem is best solved by determining the number of seconds from the epoch - Jan 1, 1970 GMT. The common Unix tools doesn't handle this problem very well (although GNU's gawk & date commands do). I'd use a scripting language like Perl:

#!/usr/local/bin/perl

use Time::Local;

# months start at 0
# years have 1900 subtracted:

###time=timelocal($sec, $min, $hours, $day, $mon, $year)
$time1=timelocal(30, 20, 18, , 2, 7, 106);
$time2=timelocal(45, 15, 12, , 4, 7, 106);

$time3=$time2-$time1;

print "$time3 difference in seconds\n";


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Calculation of time difference

Calculating time difference www.computing.net/answers/unix/calculating-time-difference/6988.html

Time difference calculation in csh www.computing.net/answers/unix/time-difference-calculation-in-csh/7131.html

calculate elapsed time www.computing.net/answers/unix/calculate-elapsed-time-/5425.html