def format_duration(dur_ms):
  """Return a time string representing the duration in human readable format."""
  if not dur_ms:
    return "0ms"

  ms = dur_ms % 1000
  dur_ms = (dur_ms - ms) / 1000
  secs = dur_ms % 60
  dur_ms = (dur_ms - secs) / 60
  mins = dur_ms % 60
  hrs = (dur_ms - mins) / 60

  out = ""
  if hrs > 0:
    out += "%dh" % hrs
  if mins > 0:
    out += "%dm" % mins
  if secs > 0:
    out += "%ds" % secs
  if ms > 0 or not out:
    out += "%dms" % ms
  return out