Script to subtract time stored in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to subtract time stored in a variable
# 1  
Old 12-10-2012
Linux Script to subtract time stored in a variable

Hello,

I am writing a script to find time difference between two timestamp stored in a variable.

i have two variable
Code:
t1=11:48:30
t2=13:13:48

how i can find the difference i.e t2-t1 in seconds.

Please help

Last edited by Franklin52; 12-10-2012 at 07:00 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 12-10-2012
# 3  
Old 12-10-2012
Try this one

Code:
t=$(date -d "20121012 11:48:30" +%s)
t1=$(date -d "20121012 13:13:48" +%s)
diff=$(expr $t1 - $t)
echo $diff

You need the date value to calculate timestamp
# 4  
Old 12-10-2012
If you are using a system on which the date utility does not have a -d option (such as Solaris and OS X systems), there is no shift between standard time and daylight savings time between the two timestamps, and that the time stamps are less than 24 hours apart (with end time being on the day following start time if start time is later in the day than end time), then the following script will do what you want. I used ksh for this example, but any POSIX conforming shell will work. (Note that a traditional Bourne shell will not work, but could be made to work by using expr instead of the arithmetic expansions used in this script.)
Code:
#!/bin/ksh
# secdiff -- calculate seconds between two timestamps
# secdiff [start_time [end_time]]
# Calculate the number of seconds between start_time and end_time.  Both
# timestamps must be in the form:
#               HH:MM:SS
# with HH, MM, and SS each being two digit decimal strings representing the
# hours, minutes, and seconds, respectively, since midnight using 24 hour clock
# notation.  If end_time is earlier in the day than start_time, end_time is
# assumed to be on the day after start_time.  This utility does not work
# correctly if there is a shift to or from daylight savings time between
# start_time and end_time.

# Set t1 and t2 to $1 and $2 with default values from the given test case.
t1=${1:-11:48:30}
t2=${2:-13:13:48}
t1s=${t1##*:}   # Extract the seconds from $t1
t1h=${t1%%:*}   # Extract the hours from $t1
t1m=${t1%:*}    # Extract the minutes from $t1 (step 1)
t1m=${t1m#*:}   # Extract the minutes from $t1 (step 2)
t1ssm=$((t1h * 3600 + t1m * 60 + t1s)) # Calculate $t1 seconds since midnight
t2s=${t2##*:}   # Perform the same operations on $t2
t2h=${t2%%:*}
t2m=${t2%:*}
t2m=${t2m#*:}
t2ssm=$((t2h * 3600 + t2m * 60 + t2s))
if [ $t1ssm -gt $t2ssm ] # If $t1ssm > $t2ssm, $t2 must be on the next day
then    t2ssm=$((t2ssm + 60 * 60 * 24))
fi
printf "%s is %d seconds after %s\n" $t2 $((t2ssm - t1ssm)) $t1

If you save the above script in a file named secdiff, change the 1st line to include an absolute path to a POSIX conforming shell if /bin/ksh isn't an absolute pathname of the Korn shell on your system, make it executable (by running chmod +x secdiff), and run the script using:
Code:
./secdiff
    or
./secdiff 11:48:30 13:13:48

it will give you the number of seconds between the timestamps you gave in your example. If you run the script using:
Code:
./secdiff 23:59:58 00:00:02

it will demonstrate that the script can handle a shift to the next day.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-10-2012
This can be simplified a bit by using the set internal:

Code:
#!/bin/ksh
t1=${1:-11:48:30}
t2=${2:-13:13:48}
OIFS=$IFS
IFS=': '
set -- $t2 $t1
IFS=$OIFS
diff=$(($1*3600+$2*60+$3 - $4*3600-$5*60-$6))
[ $diff -lt 0 ] && diff=$((diff + 3600 * 24))
printf "%s is %d seconds after %s\n" $t2 $diff $t1

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subtract time in two line

INPUT: 16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. (3 Replies)
Discussion started by: vivekn
3 Replies

2. Shell Programming and Scripting

Date command - subtract from given time

the given time is: 12:13:00 how do i subtract a 10 minutes from any given time? date '12:13:00' '-10 min' also tried this: date +12:13:00 '-10 min' (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Shell Programming and Scripting

Add or Subtract the hours,minutes or seconds in the the time variable

Hello All, I am working on script where I need to add hours,minutes or seconds in the time.Time is not the current but it could be future time.I thought I can store that time in variable and add hours.minutes or second but I am not able to add that in the time that is stores in a variable. Time... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

4. Shell Programming and Scripting

Subtract two rows (in Time format)

Hello all, I have written sth like this: #!/bin/bash grep -e XXX -e YYYY myfile.log | grep -v ZZZ | awk '{print $1 " " $2 ";" $3 ";" $9 ";" $11}' > myfile.csv sed -i '1iDate;Time;From;To' myfile.csv => it is clear that it converts log to csv and add a header. Now I want to subtract row... (4 Replies)
Discussion started by: frhling
4 Replies

5. Shell Programming and Scripting

Find the script running time and subtract from sleeptime

HI Guys, I want to find out the script running time and subtract from sleeptime. My Script Below Give me error :- #!/usr/bin/ksh timeout=100 start=$SECONDS sleep 20 end=$SECONDS echo "Time: $((end - start)) " ScTime = $((end - start)) (1 Reply)
Discussion started by: asavaliya
1 Replies

6. Shell Programming and Scripting

How to subtract time by 10 minutes in datecalc tool

Hi guys. I am trying to subtract 10 minutes from the current Unix system date and time. I have the datecalc provided here but it is mainly the date and not the time. Please check on how can i subtract 10 minutes from the current time using datecalc or any other shell scripting that will... (2 Replies)
Discussion started by: bantiloe
2 Replies

7. Shell Programming and Scripting

Add/Subtract Time

need some help on the below requirement: File1: SV,22,20100501140000,JFK,RUH SV,29,20100501073000,BOM,RUH SV,29,20100501073000,SIN,RUH third filed is datetime which is of the format (yyyymmddhh24miss) File2 JFK,+,0500 BLR,-,0530 SIN,-,0800 for every line of file 1, take 4... (9 Replies)
Discussion started by: ssantoshss
9 Replies

8. Shell Programming and Scripting

subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is 20100421043135 (9 Replies)
Discussion started by: tomjones
9 Replies

9. Shell Programming and Scripting

Convert value stored in a variable to epoch time?

Hello I have a the creation date of a file stored in a variable in the following format: Wed May 06 10:14:58 2009Is there a way I can echo the variable and display it in epoch time? I've done a lot of searching on this topic, but haven't managed to get a solution. I'm on Solaris 10. ... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

10. Shell Programming and Scripting

Subtract Time

Hello, Im writing a script using the ksh shell. I have 2 variables in the script: CURRTIME PREVTIME Example, if CURRTIME=13:00, I want to somehow calculate what the time was an hour ago so that PREVTIME=12:00 Right now I have the following: CURRTIME=`date +%H:%M` How can I... (4 Replies)
Discussion started by: xadamz23
4 Replies
Login or Register to Ask a Question