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...

Your Answer

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

Browse other questions tagged or ask your own question.