Convert Date from File and Calculate Duration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Date from File and Calculate Duration
# 1  
Old 05-08-2012
Convert Date from File and Calculate Duration

Hi - I am looking for a little help to read in 2 date fields from a file in format:

20120508134012.3
yyyymmddhhmmss.tenths of a second

So i want to:
1. Read in the 1st date from the file
2. Read in the second date from the file
3. Calculate the difference in minutes (or seconds)
4. Append the line of the original file with the "duration".

I am guessing the best approach would be to convert the date/times to epoch time, and subract. I have been searching the threads, but have not come up with anything. Any thoughts?

Thanks!
# 2  
Old 05-08-2012
You've got the right idea, but converting that to epoch time can either be easy or painful depending on what your system is. What is it?
# 3  
Old 05-08-2012
Thanks - Here is output from uname -a

Linux as1 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
# 4  
Old 05-08-2012
date -d saves you a lot of pain when you have it, but is Linux-only.

I have in filename:
Code:
2010100912000145
2010100912000955

And the script:
Code:
#!/bin/bash

exec 5<filename    # Open file into FD 5
read T1 <&5
read T2 <&5

# Arrange them how GNU date wants it YYYYMMDD HH:MM:SS.SS
T1="${T1:0:8} ${T1:8:2}:${T1:10:2}:${T1:12:2}.${T1:14}"
T2="${T2:0:8} ${T2:8:2}:${T2:10:2}:${T2:12:2}.${T2:14}"

# Convert into epoch seconds
E1=$(date -d "$T1" +%s)
E2=$(date -d "$T2" +%s)

echo "E2-E1 = $((E2-E1))"

exec 5<&-       # Close FD 5

does this:

Code:
E2-E1 = 8

These 2 Users Gave Thanks to Corona688 For This Post:
# 5  
Old 05-08-2012
Sweet! This is perfect!
# 6  
Old 05-08-2012
If you are using the ksh93 shell:
Code:
exec 5<filename
read T1 <&5
read T2 <&5
exec 5<&-

E1=$( printf "%(%s)T" "${T1:0:8} ${T1:8:2}:${T1:10:2}:${T1:12:2}.${T1:14}" )
E2=$( printf "%(%s)T" "${T2:0:8} ${T2:8:2}:${T2:10:2}:${T2:12:2}.${T2:14}" )

echo "E2-E1 = $((E2-E1))"

These 2 Users Gave Thanks to fpmurphy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

2. UNIX for Beginners Questions & Answers

How to calculate time duration in Linux?

I want to calculate duration for below file in this format SID | Date | Starttime |Date |End time 1607 |2019-04-05|13:06:42|2019-04-05|13:07:12 2327 |2019-04-05|13:57:26|2019-04-05|13:57:43 O/p should be like this: SID | Date | Starttime |Date |Endtime... (4 Replies)
Discussion started by: anupmishra
4 Replies

3. Shell Programming and Scripting

Compare fraction number and convert duration to seconds

Hi friends, I have a file with contents below: 01.m4a 00:14:45.82, 01.mp4 00:03:46.05, -659.770000 05.m4a 00:27:43.51, 05.mp4 00:27:45.10, 1.590000 06.m4a 00:11:39.73, 06.mp4 00:11:44.60, 4.870000 If 5th column value more than 3 or less than -3 then I should get its name (from first... (2 Replies)
Discussion started by: magnus29
2 Replies

4. UNIX for Dummies Questions & Answers

Script shell calculate mean arrival request duration

hello, I have implemented this command : tshark -eth0 -T fiels -e frame.time et sip.Request-Line -z sip,stat > test2.txt the result of this command : test.txt: Aug 27, 2013 23:06:47.334270000 INVITE Aug 27, 2013 23:06:47.335045000 SIP/2.0 401 Unauthorized Aug 27, 2013... (1 Reply)
Discussion started by: Amouna
1 Replies

5. Shell Programming and Scripting

Getting the Start, End time and duration using date command

Oracle Enterprise Linux We want to track how long a process takes to complete its execution. This is what we want in the schell script Before the process is started , get the time with date, hours and minutes execute the process After the process has ended , get the time with date,... (5 Replies)
Discussion started by: omega3
5 Replies

6. Shell Programming and Scripting

Convert duration of the process to seconds

Hi, I am looking to write a script to kill the process which are running for more than 7 days. So i have a command like "ps -eo pid,etime,args | grep -i xxxx" ( process which has xxx in it and running for more than 7 days needs to be killed ). When i exeucte the above command , i am... (2 Replies)
Discussion started by: forums123456
2 Replies

7. Shell Programming and Scripting

convert date inside a file

Hi guys I've got a file with this line inside. 200,2010,318,1000,4.377,70.9,.835,.592,.243,-.438,0,881 The line always begins with number 100 or number 200, follow the year, the day in the year, the hour, and other stuff Here, the important fields are, 2, 3 and 4. -Filed 2 --> year... (4 Replies)
Discussion started by: iga3725
4 Replies

8. Shell Programming and Scripting

date duration fail to calculate

Hi Everyone, I was very sad after a long way but still cannot figure out the duration between two date. $date1="20090812 23:48:56"; $date2="20090813 00:01:37"; The output will be "001241". I did the following tries, like print localtime(UnixDate(ParseDate("20090812 23:48:56"),"%s"));... (2 Replies)
Discussion started by: jimmy_y
2 Replies

9. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

10. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies
Login or Register to Ask a Question