Skip to content

Instantly share code, notes, and snippets.

@cypreess
Last active June 29, 2018 10:38
Show Gist options
  • Save cypreess/e7dd79ebb81ed31e392809527a34ec25 to your computer and use it in GitHub Desktop.
Save cypreess/e7dd79ebb81ed31e392809527a34ec25 to your computer and use it in GitHub Desktop.
Formatting negative timedelta in more natural way
import datetime
def timedelta_to_string(timedelta):
total_seconds = timedelta.total_seconds()
if total_seconds < 0:
timedelta_str = '-' + str(datetime.timedelta() - timedelta)
else:
timedelta_str = '+' + str(timedelta)
return timedelta_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment