2

I want to work with time (for example to fill time spending job reports) like:

11:12:35 - 11:10:10     # is 0:02:25
12:00 - 9:30            # is 2:30
18:15-16:20 + 13:45-11:20  # is xx:yy
2h + 3h20m - 50m        # is 4h30m
1d4h + 5h + 7h + 2d     # is 3d16h

I know that date can operate with dates via -v option but I do not need calendar ))

I wrote simple bash function like:

datediff() {
  [[ -n "$1" ]] || { echo first argument is missing; return 1; }
  [[ -n "$2" ]] || { echo second argument is missing; return 1; }
  local diff=$(( $(date -d "$1" +%s) - $(date -d "$2" +%s) ))
  local sec=$(( diff % 60 ))
  diff=$(( diff / 60 ))
  local min=$(( diff % 60 ))
  diff=$(( diff / 60 ))
  local hour=$(( diff % 24 ))
  local day=$(( diff / 24 ))
  echo $day days $hour hours $min minutes $sec seconds
}

which can be used like:

bash# datediff 3:3:20 2:40:00
0 days 0 hours 23 minutes 20 seconds

bash# datediff 3:20 2:40
0 days 0 hours 40 minutes 0 seconds

bash# datediff 2000-10-03T03:20 2000-01-02T02:40
274 days 23 hours 40 minutes 0 seconds

but it's a toy function...

0

Postgre SQL interval syntax allow that:

select interval '8h' - interval '3h45m' - interval '2h30m';

UPDATE Recently discovered Emacs is capable of HMS arithmetic (compatible with degree/minutes/seconds):

2@30' - 1@45' + 0@12' (evaluates to 0@ 57' 0")
0

I wrote some C+bash tools a couple decades ago. They operate on date/times in YYYY[MM[DD[HH[MM[SS]]]]] format, available here: https://gitlab.com/dargaud/StrfTime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.