_pure_format_time.fish 854 B

1234567891011121314151617181920212223242526272829303132
  1. set fail 1
  2. function _pure_format_time \
  3. --description="Format milliseconds to a human readable format" \
  4. --argument-names milliseconds threshold
  5. if test $milliseconds -lt 0; return $fail; end
  6. set --local seconds (math -s0 "$milliseconds / 1000 % 60")
  7. set --local minutes (math -s0 "$milliseconds / 60000 % 60")
  8. set --local hours (math -s0 "$milliseconds / 3600000 % 24")
  9. set --local days (math -s0 "$milliseconds / 86400000")
  10. set --local time
  11. if test $days -gt 0
  12. set time $time (printf "%sd" $days)
  13. end
  14. if test $hours -gt 0
  15. set time $time (printf "%sh" $hours)
  16. end
  17. if test $minutes -gt 0
  18. set time $time (printf "%sm" $minutes)
  19. end
  20. if test $seconds -gt $threshold
  21. set time $time (printf "%ss" $seconds)
  22. end
  23. echo -e (string join ' ' $time)
  24. end