Bash script to calculate duration between two time stamps, and also report minutes, hours, days, weeks, months and years
This is an improvement on an old script to calculate duration for clip trimming.
Typical usage example is to calculate duration of a clip for trimming a video with a program like mencoder (this is needed because it expects start position in HH:MM:SS format, but end position as duration since start position).
Anyway, let's say I want to trim a video starting at 00:01:22 until 00:23:44, to get duration, I'd run:
$ calc-duration 00.01.22 00.23.44
And get back:
00:22:22 (duration in HH:MM:SS format)
Or:
1342 seconds
22 minutes
My mencoder syntax would then be:
$ mencoder -ss 00.01.22 -endpos 00:22:22 -oac copy -ovc copy in.avi -o out.avi
In addition, the script will report duration in years/months/hours/etc. For example, number of hours I will have lived if I die in 2050:
$ calc-duration 1974-06-16 00:00:05 2050-01-01 02:30:10
Output:
662235:30:05 (duration in HH:MM:SS format)
Or:
2384047805 seconds
39734130 minutes
662235 hours
27593 days
3941 weeks
906 months
75 years
Let's see how old my daughter will be then:
$ calc-duration 2008-11-13 13.30.00 2050-01-01 02:30:10
360565:00:10 (duration in HH:MM:SS format)
Or:
1298034010 seconds
21633900 minutes
360565 hours
15023 days
2146 weeks
493 months
41 years
And here's the script:
#!/bin/bash
# calculates duration from supplied arguments.
# if 2 args, assumes time1 time2 (and uses current date as date1 and date2)
# if 4 args, assumes date1 time1 date2 time2
Usage()
{
cat <
Leave a comment