Sponsored Content
Top Forums Shell Programming and Scripting Subtracting time with awk - BASH/Debian GNU Linux Post 302395349 by rickenbacherus on Monday 15th of February 2010 07:51:23 PM
Old 02-15-2010
Finally it is working:
ChapStart is an array with start times in this format: 00:00:00 which gets converted to "Seconds since 1970-01-01"with 'date'. This allows us to do the math. Then, we convert it back to 00:00:00 format.

Code:
#~ Time conversion and calculations
for ((s=0; s<=$CHAPTERS; s++)) ## Convert ChapStart 00:00:00 to ChapTimeStart seconds since 1970-01-01
	do ChapTimeStart[$s]=$(date +%s -d "${ChapStart[$s]}") 
done

for ((e=0; e<$CHAPTERS; e++)) ## Convert ChapEnd 00:00:00 to ChapTimeEnd seconds since 1970-01-01
	do ChapTimeEnd[$e]=$(date +%s -d "${ChapEnd[$e]}")
done
	unset ChapTimeEnd[0] ##Remove the first chap start time 00:00:00
	ChapTimeEnd[$CHAPTERS]=$Disc ## Add $Disc as last array element
	ChapTimeEnd=( "${ChapTimeEnd[@]}" ) ## Pack the array
	
	unset ChapEnd[0] ## Offset the time differences - this is printed to screen as "End: $"
	ChapEnd[$CHAPTERS]=$DiscTime ## Add $DiscTime as last array element
	ChapEnd=( "${ChapEnd[@]}" )     ## Pack the array
for ((t=0; t<$CHAPTERS; t++)) 
     do TimeDiff[$t]=$((${ChapTimeEnd[$t]} - ${ChapTimeStart[$t]}))  
Duration=$(echo - |awk '{printf "%02d:%02d:%02d","'"${TimeDiff[$t]}"'"/(60*60),"'"${TimeDiff[$t]}"'"%(60*60)/60,"'"${TimeDiff[$t]}"'"%60}';echo)
echo "Chapter: `printf "%02d" $(($t+1))`  Start: "${ChapStart[$t]}" End: "${ChapEnd[$t]}" Duration: $Duration"
done

 
Time::Seconds(3pm)					 Perl Programmers Reference Guide					Time::Seconds(3pm)

NAME
Time::Seconds - a simple API to convert seconds to other date values SYNOPSIS
use Time::Piece; use Time::Seconds; my $t = localtime; $t += ONE_DAY; my $t2 = localtime; my $s = $t - $t2; print "Difference is: ", $s->days, " "; DESCRIPTION
This module is part of the Time::Piece distribution. It allows the user to find out the number of minutes, hours, days, weeks or years in a given number of seconds. It is returned by Time::Piece when you delta two Time::Piece objects. Time::Seconds also exports the following constants: ONE_DAY ONE_WEEK ONE_HOUR ONE_MINUTE ONE_MONTH ONE_YEAR ONE_FINANCIAL_MONTH LEAP_YEAR NON_LEAP_YEAR Since perl does not (yet?) support constant objects, these constants are in seconds only, so you cannot, for example, do this: "print ONE_WEEK->minutes;" METHODS
The following methods are available: my $val = Time::Seconds->new(SECONDS) $val->seconds; $val->minutes; $val->hours; $val->days; $val->weeks; $val->months; $val->financial_months; # 30 days $val->years; The methods make the assumption that there are 24 hours in a day, 7 days in a week, 365.24225 days in a year and 12 months in a year. (from The Calendar FAQ at http://www.tondering.dk/claus/calendar.html) AUTHOR
Matt Sergeant, matt@sergeant.org Tobias Brox, tobiasb@tobiasb.funcom.com BalieXXzs SzabieXX (dLux), dlux@kapu.hu LICENSE
Please see Time::Piece for the license. Bugs Currently the methods aren't as efficient as they could be, for reasons of clarity. This is probably a bad idea. perl v5.12.1 2010-04-26 Time::Seconds(3pm)
All times are GMT -4. The time now is 03:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy