Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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('=')
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
You can’t perform that action at this time.