I want to use the date command to perform arithmetic on a time-of-day value while treating it as a simple clock time, without timezone or daylight-saving effects. For example, starting with 21:00:00, I want to add 02:02:30 and subtract 01:10, producing 23:01:20. What is the best way to do this?
3 Answers
For pure clock arithmetic, Bash integer arithmetic avoids date and timezone behavior entirely: `s=$((3600*(21+2)+60*(0+2-1)+(0+30-10))); printf '%02d:%02d:%02dn' $((s/3600%24)) $((s/60%60)) $((s%60))`. That produces `23:01:20`. You can adapt the expressions if the values come from variables.
A portable approach is to convert each time to seconds, do the arithmetic with the shell, and format the result afterward: `a=$(date -d '21:00:00' +%s); b=$(date -d '02:02:30' +%s); c=$(date -d '00:01:10' +%s); total=$((a+b-c)); date -d @$total +%T`. The result is `23:01:20`. This relies on GNU date for parsing and conversion.
With GNU date, spell out the relative adjustments instead of using HH:MM:SS values directly. Also, if -01:10 means one minute and ten seconds, write it explicitly that way: `date -d '21:00:00 2 hours 2 minutes 30 seconds ago 1 minute 10 seconds' +%T`. This prints `23:01:20`. For predictable results around daylight-saving transitions, set a timezone without seasonal changes, such as `TZ=UTC date ...`.

Related Questions
Can't Load PhpMyadmin On After Server Update
Redirect www to non-www in Apache Conf
How To Check If Your SSL Cert Is SHA 1
Windows TrackPad Gestures