Time stamp Difference | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Time stamp Difference

Shell Programming and Scripting


Tags
time stamp

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-11-2013
netdbaind netdbaind is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 31 October 2014, 5:19 AM EDT
Posts: 59
Thanks: 10
Thanked 0 Times in 0 Posts
Time stamp Difference

I have a log file which wrote time stamp like this


Code:
2013-02-11 00:46:40.389037 
 
         2013-02-12 11:46:40.197045


can any one help me to get the time stamp difference of these two line in seconds.
Sponsored Links
    #2  
Old 02-11-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 28 November 2014, 3:27 AM EST
Location: India
Posts: 1,834
Thanks: 25
Thanked 526 Times in 505 Posts

Code:
to=$(date -d "2013-02-12 11:46:40.197045" +%s)
from=$(date -d "2013-02-11 00:46:40.389037" +%s)
echo $(( $to - $from ))


Last edited by balajesuri; 02-12-2013 at 01:05 AM..
The Following User Says Thank You to balajesuri For This Useful Post:
netdbaind (02-12-2013)
Sponsored Links
    #3  
Old 02-11-2013
franzpizzo's Avatar
franzpizzo franzpizzo is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 6 February 2014, 9:56 AM EST
Posts: 68
Thanks: 0
Thanked 12 Times in 12 Posts
Hi, for calculare the difference between two date you must convert them in epoch with a code like this and then operate the difference

Code:
HOUR=12
MIN=41
SEC=11
DAY=01
MONTH=01
YEAR=2012
set -A MONTHS 0 0 31 59 90 120 151 181 212 243 273 304 334 365
echo "b=0;if(${MONTH}>2) if (${YEAR}%4==0) b=1; ${SEC}+${MIN}*60+${HOUR}*3600+(${MONTHS[${MONTH}]} + ${DAY} + b-1)*86400+(${YEAR}-1970)*31536000+((${YEAR}-1969)/4)*86400" | bc

    #4  
Old 02-12-2013
netdbaind netdbaind is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 31 October 2014, 5:19 AM EDT
Posts: 59
Thanks: 10
Thanked 0 Times in 0 Posts
thanks balajesuri ur code worked!

can you plz explain ur code.
Sponsored Links
    #5  
Old 02-12-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 28 November 2014, 3:27 AM EST
Location: India
Posts: 1,834
Thanks: 25
Thanked 526 Times in 505 Posts

Code:
# Calculate the time in seconds from epoch and store the value in "to". Check man date to see what switch -d and format %s do.
to=$(date -d "2013-02-12 11:46:40.197045" +%s)
from=$(date -d "2013-02-11 00:46:40.389037" +%s)

# Calculate the difference.
echo $(( $to - $from ))

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
System time and Cron time stamp not matching karghum Solaris 4 04-25-2012 08:50 AM
How to get time duration between two human readable time stamp in Unix? ford99 Shell Programming and Scripting 3 07-19-2011 10:52 AM
Calculate time stamp difference bharath.phatak Shell Programming and Scripting 4 07-12-2011 09:15 AM
regarding time stamp aishsimplesweet Shell Programming and Scripting 3 01-28-2011 10:58 AM
Calculate the Time stamp difference firestar Shell Programming and Scripting 2 09-08-2010 05:18 AM



All times are GMT -4. The time now is 06:53 AM.