LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple Script Needed to Calculate How Many "15" Minutes are in a Time Stamp (https://www.linuxquestions.org/questions/linux-newbie-8/simple-script-needed-to-calculate-how-many-15-minutes-are-in-a-time-stamp-4175544042/)

deepGC 05-30-2015 11:39 AM

Simple Script Needed to Calculate How Many "15" Minutes are in a Time Stamp
 
Hello,

I am performing some work on some mp3 files. One the functions requires me to know how many "15 minutes" are in a particular file.

For instance, I need some code that will produce the following values from the given time stamp:

01:52:34.40 = 7
00:47:15.32 = 3
00:02:56.41 = 0
00:36:01.09 = 2

Any ideas?

deepGC 05-30-2015 12:06 PM

If I echo : $("$t" | awk -F: '{ print ($1 * 60) + ($2)}' - I get the correct number of minutes

But if I try m=$("$t" | awk -F: '{ print ($1 * 60) + ($2)}') where $t is the timestamp, I get an error message:

./processmix: line 27: 00:32:34.40: command not found

jpollard 05-30-2015 12:07 PM

??? just convert everything to a minute (add 1 for any seconds), mod 15. add 1 for any nonzero remainder.

deepGC 05-30-2015 12:09 PM

Quote:

Originally Posted by jpollard (Post 5369847)
??? just convert everything to a minute (add 1 for any seconds), mod 15. add 1 for any nonzero remainder.

At the current moment, I'm struggling to assign the return value from this to a variable: "$t" | awk -F: '{ print ($1 * 60) + ($2)}'

jpollard 05-30-2015 12:09 PM

Quote:

Originally Posted by deepGC (Post 5369845)
If I echo : $("$t" | awk -F: '{ print ($1 * 60) + ($2)}' - I get the correct number of minutes

But if I try m=$("$t" | awk -F: '{ print ($1 * 60) + ($2)}') where $t is the timestamp, I get an error message:

./processmix: line 27: 00:32:34.40: command not found

You need the echo after the "$(". as in $(echo $t | ...

I also think you need the seconds to be included - otherwise you might get some truncated video (up to a minutes worth). Oh, and include 1 any remainder from the modulus as otherwise you would loose up to 14 minutes worth.

deepGC 05-30-2015 12:13 PM

Thanks, that works a treat now :)

jlinkels 05-30-2015 12:21 PM

Whenever you are working with times and involve calculation, use seconds.
Code:

echo $(date -d "1970-01-01 1:52:34" +%s) 900 / p | dc
jlinkels

schneidz 05-30-2015 01:03 PM

Quote:

Originally Posted by jlinkels (Post 5369854)
Whenever you are working with times and involve calculation, use seconds.
Code:

echo $(date -d "1970-01-01 1:52:34" +%s) 900 / p | dc
jlinkels

maybe you need -u to ignore time zones ?

jlinkels 05-30-2015 02:14 PM

Quote:

Originally Posted by schneidz (Post 5369871)
maybe you need -u to ignore time zones ?

Yes you need that. But I wasn't aware of it as I set my system clock always to UTC.

Which is the way it should be set up IMHO. Time is time, and converting it to the local time zone should be performed in the display layer, not on the machine level. I run servers in different time zones and timestamps are a nightmare when different times are displayed.

Anyways, -u is a mandatory option if your system time isn't UTC.

jlinkels


All times are GMT -5. The time now is 12:15 PM.