Skip to content

Instantly share code, notes, and snippets.

@andrewebdev
Last active December 20, 2015 20:59
Show Gist options
  • Save andrewebdev/6194588 to your computer and use it in GitHub Desktop.
Save andrewebdev/6194588 to your computer and use it in GitHub Desktop.
Django: time conversion to drop the miliseconds
def format_date_for_sforce(timestamp):
"""
Returns a datetime formatted as 2012-02-15T19:03:32-0800
"""
if not timestamp:
return timestamp
total_seconds = timestamp.utcoffset().total_seconds()
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
time_string = timestamp.strftime("%Y-%m-%dT%H:%M:%S%%+03d%%02d")
return time_string % (hours, minutes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment