Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating time (adding minutes) bash

I got stuck in part of the script. I have time: for example "16:00" and duration in minutes like: 410.

Is there any easy way to add those two values? I've tried a lot of combinations with date -d, but can't solve it.

like image 873
Kysu Avatar asked Jan 18 '14 18:01

Kysu


People also ask

How does bash calculate time?

Then, we used the $SECONDS variable, which is the Bash shell's internal variable that holds the elapsed time since the current shell started. After that, we used the TIMEFORMAT built-in variable along with the %R option for calculating the elapsed time with millisecond precision.

How does unix calculate time difference?

Use unix time instead, date +%s , then subtract to get the difference in seconds.

What does ## mean in bash?

In bash , it removes a prefix pattern. Here, it's basically giving you everything after the last path separator / , by greedily removing the prefix */ , any number of characters followed by / ): pax> fspec=/path/to/some/file.txt ; echo ${fspec##*/} file.txt. Greedy in this context means matches as much as possible.

What is date +% s in bash?

date +%S. Displays seconds [00-59] date +%N. Displays in Nanoseconds. date +%T.


1 Answers

Try this (Kysu's version):

date -d "16:00 410 minutes" +'%H:%M'

or this:

date -d "16:00 today + 410 minutes" +'%H:%M'

But do not use this:

date -d "16:00 + 410 minutes" +'%H:%M'   # BAD!

Strange things happen if you omit the word today but keep the +. (I think the + 410 is being parsed as a timezone modifier, and then the minutes is interpreted as "add one minute".)

like image 66
joeytwiddle Avatar answered Sep 21 '22 15:09

joeytwiddle