Skip to content

Instantly share code, notes, and snippets.

@MattMS
Created August 28, 2015 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattMS/cca303ff11b7d5b6ff86 to your computer and use it in GitHub Desktop.
Save MattMS/cca303ff11b7d5b6ff86 to your computer and use it in GitHub Desktop.
Create a Python timedelta from text.
from datetime import timedelta
import logging
logger = logging.getLogger(__name__)
def get_time_delta_from_text(text):
kwargs = dict()
for pair in text.split(','):
name, sep, value = pair.partition('=')
try:
kwargs[name] = int(value)
except ValueError as e:
logger.error('Non-integer value (%s) for %s.', value, name)
raise e
return timedelta(**kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment