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: 19 March 2014, 12:33 AM EDT
Posts: 44
Thanks: 7
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: 17 April 2014, 9:14 AM EDT
Location: India
Posts: 1,796
Thanks: 23
Thanked 516 Times in 495 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 12: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: 19 March 2014, 12:33 AM EDT
Posts: 44
Thanks: 7
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: 17 April 2014, 9:14 AM EDT
Location: India
Posts: 1,796
Thanks: 23
Thanked 516 Times in 495 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 07:50 AM
How to get time duration between two human readable time stamp in Unix? ford99 Shell Programming and Scripting 3 07-19-2011 09:52 AM
Calculate time stamp difference bharath.phatak Shell Programming and Scripting 4 07-12-2011 08:15 AM
regarding time stamp aishsimplesweet Shell Programming and Scripting 3 01-28-2011 09:58 AM
Calculate the Time stamp difference firestar Shell Programming and Scripting 2 09-08-2010 04:18 AM



All times are GMT -4. The time now is 05:23 PM.