Find time difference


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find time difference
# 15  
Old 06-27-2012
You have been given all the requested information to start your work...
Never heard of splitting a "complex" task into more simpler ones? (Basics of Data Processing...) like dealing separately days calculation and what is left: time HH.MM.SS? and knowing if you have to subtract a day ...
...
(Even my young son knows how to solve time problems since I have been takling him with speed in km/h and m/s problems like the famous crossing of two trains leaving 2 different cities at different time ( and trains travelling at different speed)...)

Last edited by vbe; 06-27-2012 at 01:06 PM.. Reason: addendumabout the trains, typos
# 16  
Old 06-27-2012
Dear Vbe

I really dont like your way of conversation,its so offensive.
As stated before this is the first time for me to touch unix scripting ,i have never ever worked or even opened a file and this is my 3rd day learning .

So be patient on me and there is no use for you to be attacking .

I was not given the basic info as you see .

Being a Mod doesnt mean you can attack in such way, i request you to delete your post.

Regards,
# 17  
Old 06-27-2012
An open logic idea for calculating the time difference. If the date is always the same, then the date difference is immaterial:
The mathematics is very similar to normal numeric subtraction except that the "borrow 1" becomes "borrow 60" when dealing with minutes and seconds, and "borrow 24" when dealing with hours.
This script contains no proper code to deal with a time difference of more than 24 hours, but does allow the time range to overlap midnight.
Not thoroughtly tested. Designed to demonstrate the principle.

Code:
egrep -v "TIME|==" filename.txt | awk '{print $1,$2}' |  read timestamp_raw1 timestamp_raw2
timestamp1=$( echo "${timestamp_raw1}" | cut -d. -f1 )
timestamp2=$( echo "${timestamp_raw2}" | cut -d. -f1 )
#
hh1=$( echo "${timestamp1}"|cut -c9-10 )
mm1=$( echo "${timestamp1}"|cut -c11-12 )
ss1=$( echo "${timestamp1}"|cut -c13-14 )
#
hh2=$( echo "${timestamp2}"|cut -c9-10 )
mm2=$( echo "${timestamp2}"|cut -c11-12 )
ss2=$( echo "${timestamp2}"|cut -c13-14 )
#
if [ ${ss2} -lt ${ss1} ]
then
        ss2=$(( ${ss2} + 60 ))
        mm1=$(( ${mm1} + 1 ))
fi
ss3=$(( ${ss2} - ${ss1} ))
#
if [ ${mm2} -lt ${mm1} ]
then
        mm2=$(( ${mm2} + 60 ))
        hh1=$(( ${hh1} + 1 ))
fi
mm3=$(( ${mm2} - ${mm1} ))
#
if [ ${hh2} -lt ${hh1} ]
then
        hh2=$(( ${hh2} + 24 ))
        # Insert code to borrow 1 from the days if needed
fi
hh3=$(( ${hh2} - ${hh1} ))
#
#
echo "Difference: ${hh3}:${mm3}:${ss3}"


./scriptnname
Difference: 1:14:11

This User Gave Thanks to methyl For This Post:
# 18  
Old 06-28-2012
Thanks alot mthyl for your great support ,im trying it ...i have some problems but i will fix Smilie
I likd the fix of ss2 lt ss1 its really smart of you Smilie

I have an idea; i need you to correct me if i am wrong.

ill transfer h,m,s into seconds

so h1=h*60*60;m1=m*60*60 s=s1

so now i think i can subtract seconds to seconds then divide by 60 at the end .

also can put condition if o/p greater than 60 i could divide by another 60 to be in hours.

---------- Post updated at 06:49 AM ---------- Previous update was at 06:39 AM ----------

here is what i meant could any body correct me

Code:
echo $(date "${1:0:4}-${1:4:2}-${1:6:2}
                              ${1:8:2}:${1:10:2}:${1:12:2}" +%s)
}
difference=$(( `seconds2 $2` - `seconds1 $1` ))

im not sure about the
Code:
%s

it didnt work although i read it should work
# 19  
Old 06-28-2012
Quote:
h1=h*60*60;m1=m*60*60 s=s1
Surely that should be:
Code:
h1=h*60*60 ; m1=m*60 ; s1=s

Converting back from seconds to hours, minutes and seconds is fairly easy:
1) Divide by 3600 to get the hours, and keep the remainder.
2) Divide the remainder from above by 60 to give minutes. The new remainder is the seconds.


Ps. I can't comment on the code you posted because the syntax is alien to me. Maybe it came off a Linux system?
# 20  
Old 06-28-2012
Quote:
Originally Posted by wnaguib
The all three links is how to transfer a 8-digit date to a normal julian date and do arithmatic actions

My request was on a 14-digit date YYYYmmDDHHMMSS time difference.
Part of your problem is that date math is a royal pain in Solaris. None of the "easy" ways work. Instead of rewriting huge, ugly, and potentially-buggy scripts from scratch, people will refer you to the better scripts people have already written, which we have in that FAQ.

So you're left with the problem of squishing your data to fit into these scripts. If you'd even had pseudocode people would be much more willing to help you. Just demanding that everyone write everything for you, start-to-finish, isn't just impolite -- it looks suspicious. We get many people trying to trick us into doing their homework for them.

You can't write shell code, fine. That doesn't mean you can't describe how you'd try to solve this problem in general. I'd happily give answers to questions like "How do I read a string from file?", "how do I break a string into parts?", "how do I do arithmetic in shell?", and "how do I put these things together like ...?"
# 21  
Old 06-28-2012
Quote:
Originally Posted by wnaguib
im not sure about the
Code:
%s

it didnt work although i read it should work
You're on Solaris, so you probably don't have GNU date. Try date --version to be sure.

If you don't, you can't feed a date into date and have it print it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In HP-UX how to find the date time difference ?

Hello, In HP-UX how to find the date time difference ? Start time: 28-APR-2019 21:36:01 End time : 29-APR-2019 00:36:04 ---------------------- Difference is ---------------------- Much appreciate any pointer or view on this. ... (3 Replies)
Discussion started by: Siva SQL
3 Replies

2. Shell Programming and Scripting

awk script to find time difference between HTTP PUT and HTTP DELETE requests in access.log

Hi, I'm trying to write a script to determine the time gap between HTTP PUT and HTTP DELETE requests in the HTTP Servers access log. Normally client will do HTTP PUT to push content e.g. file_1.txt and 21 seconds later it will do HTTP DELETE, but sometimes the time varies causing some issues... (3 Replies)
Discussion started by: Juha
3 Replies

3. Shell Programming and Scripting

Find time difference based on logfile

Hi All, Firstly thank you for the forum members I need to find time difference b'w two rows of timestamp using awk/shell. Here is the logfile: cat business_file start:skdjh:22:06:2010:10:30:22 sdfnskjoeirg wregn'wergnoeirnfqoeitgherg end:siifneworigo:22:06:2010:10:45:34... (3 Replies)
Discussion started by: Srinivas Gadi
3 Replies

4. Shell Programming and Scripting

How to find time difference?

I have a file wich contains time formats and i need to get the time difference TIME1 TIME2 ================================== 20120624192555.6Z 20120624204006.5Z which means first date 2012/6/24 19:25:55,second date 2012/6/24 20:40:06 so when i get the time... (1 Reply)
Discussion started by: wnaguib
1 Replies

5. Shell Programming and Scripting

Find time difference between two consecutive lines in same file.

Hello I have a file in following format: IV 08:09:07 NM 08:12:01 IC 08:12:00 MN 08:14:20 NM 08:14:15 I need a script to compare time on each line with previous line and show the inconsecutive line. Ex.: 08:12:00 08:14:15 A better way... (6 Replies)
Discussion started by: vilibit
6 Replies

6. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

7. Shell Programming and Scripting

Find Time difference in Unix

Hi, START_TIME :- "10-NOV-2009 00:00:04" STOP_TIME :- "10-NOV-2009 00:05:47" Please help to find difference between these two. Searched for the same topic but did not find an answer for the same time format :( Regards, Robin (3 Replies)
Discussion started by: robinbannis
3 Replies

8. AIX

How to find time difference between 2 timestamps?

HI All, can some one please help me how to fine the difference between two time stamps say a= Nov 10, 2009 9:21:25 AM b= Nov 10, 2009 10:21:25 AM I want to find difference between the a & b I googled and tried with some options but no luck. My OS is AIX (1 Reply)
Discussion started by: bandlan9
1 Replies

9. Shell Programming and Scripting

Help to find the time difference between the lines

Hi guru's, Am new to shell scripting. I am getting the below o/p from the oracle database, when I fire a query. ID JOB_ID ELAPSED_TIME FROM TO ----- ------ ------------------- -------- -------- 62663 11773 01/06/2009 09:49:13 SA CM 62664 11773 ... (4 Replies)
Discussion started by: sathik
4 Replies

10. Shell Programming and Scripting

To find the time difference between two lines of the same log file

Hello Friends, I want to write a script for the following: nlscux62:tibprod> grep "2008 Apr 30 01:" SA_EHV_SPEED_SFC_IN_03-SA_EHV_SPEED_SFC_IN_03-2.log | grep -i post | more 2008 Apr 30 01:01:23:928 GMT +2 SAPAdapter.SA_EHV_SPEED_SFC_IN_03-SA_EHV_SPEED_SFC_IN_03-2 Info AER3-000095 IDOC... (2 Replies)
Discussion started by: satyakam
2 Replies
Login or Register to Ask a Question