def htime(time_difference):
  """Time formatting function.

  Depending on the value of `time_difference` outputs time in an appropriate
  time format.

  Args:
    time_difference: Float, time difference between the two events.

  Returns:
    time: String representing time in an appropriate time format.
  """
  time_difference = round(time_difference)

  days = time_difference // 86400
  hours = time_difference // 3600 % 24
  minutes = time_difference // 60 % 60
  seconds = time_difference % 60

  if days > 0:
    return '{:d}d {:d}h {:d}m {:d}s'.format(days, hours, minutes, seconds)
  if hours > 0:
    return '{:d}h {:d}m {:d}s'.format(hours, minutes, seconds)
  if minutes > 0:
    return '{:d}m {:d}s'.format(minutes, seconds)
  return '{:d}s'.format(seconds)