Add (n hours + q minutes) to time variable

Hello All,
I am running Ubuntu and I have found a long way to achieve expected output but I would like to know how you would have been doing.

Below crappy script adds 2hour and 15minutes to each time variable. The first variable is 15:00 let's say.
What's the modern way to do it?

for file in *.txt
do
stime=$(grep -A0 "ST: " "$file" | sed "s|.*: ||g") *#it reads 15:00*
hour=$(echo "$stime" | sed "s|:.*||g")
min=$(echo "$stime" | sed "s|.*:||g")
new_hour1=$((hour+2))
new_min1=$((min+15))
new_hour=`echo "$new_hour1:$new_min1"` *#this gives 17:15*
done

Thank you so much
Boris

for file in *.txt
do
  stime=$( sed -n "s|.*ST: ||p" "$file" )
  hour=${stime%:*}
  min=${stime#*:}
  new_hour1=$((hour+2))
  new_min1=$((min+15))
  new_hour="$new_hour1:$new_min1"
done

Thank you so much Dear @MadeInGermany

What about e.g. 23:49? :stuck_out_tongue:

  new_hour1=$(( (hour+2)%24 ))
  new_min1=$(( (min+15)%60 ))

And it would still give you 1:4 instead of 01:04 in the output, so additional output formatting may be required, e.g.

  new_hour="$(printf '%02d:%02d' "$new_hour1" "$new_min1")"

Going POSIX time for calc, then converting for required variables using gnu date or awk would be best approach which would minimize possible exceptions.

There is also a notion of (if not using UTC) of daylight savings, which can skew the results if stars align.

So, use UTC and POSIX date, convert for user in application layers - scripts, programs whatever.

Regards.
Peasant.

Dear @Matt-Kita,
Thank you. You are right, this is more precise.
I have set the time to 23:00
The first one gives 25:15, yours returns 01:15

root@father:~# ./test.sh
25:15 01:15
finished

Kind regards
Boris

With a carryover a 23:49 gives a 02:04

  new_hour1=$(( (hour+2+(min+15)/60)%24 ))
  new_min1=$(( (min+15)%60 ))

The redundant +15 is ugly. I suggest

add_hour=2
add_min=15
for ...
do
  ...
  new_hour1=$(( (hour+add_hour+(min+add_min)/60)%24 ))
  new_min1=$(( (min+add_min)%60 ))
  ...
done

You're right! I missed that one as well.