Skip to content

Instantly share code, notes, and snippets.

@jlewin
Created June 25, 2020 04:31
Show Gist options
  • Save jlewin/56ce0cf89825c7e877468cd394ec7308 to your computer and use it in GitHub Desktop.
Save jlewin/56ce0cf89825c7e877468cd394ec7308 to your computer and use it in GitHub Desktop.
Basic tests to for timespan serialization
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime, timedelta
# Store start
>>> start = datetime.now()
# Get timespan by subtracting start from now
>>> elapsed = datetime.now() - start
# total_seconds can roundtrip to/from string
>>> elapsed.total_seconds()
14.94
# Dump full value for comparision
>>> elapsed.__str__()
'0:00:14.940000'
# Deserialize from string total_seconds back into timedelta
>>> from_string = timedelta(seconds=14.94)
# Validate
>>> from_string.total_seconds()
14.94
# Validate (matches original)
>>> from_string.__str__()
'0:00:14.940000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment