wroberts / pytimeparse

A small Python module to parse various kinds of time expressions.
MIT License
285 stars 39 forks source link

Support for negative timedeltas #23

Open Serfentum opened 4 years ago

Serfentum commented 4 years ago

Hi! Thank you for your library, I just wanna know is it likely that you will add support for negative deltas? For example

timeparse('-1 days -00:00:14.513431')

gives None, why it would be nice (at least for my purpose) to get negative number of seconds in this delta. Probably also output should be the same for such input

timeparse('-1 days 00:00:14.513431')

It's not crucial and could be circumvented of course, but would be nice to have it from the box, thank you)

SGmuwa commented 3 years ago
from pytimeparse.timeparse import timeparse
from datetime import timedelta

t = timedelta(days= -1, seconds=5)
print(t) # -1 day, 0:00:05
t = timedelta(seconds=timeparse(str(t)))
print(t) # -2 days, 23:59:55
t = timedelta(seconds=timeparse(str(t)))
print(t) # -3 days, 0:00:05

t = timedelta(days= -1, seconds=-5)
print(t) # -2 days, 23:59:55
t = timedelta(seconds=timeparse(str(t)))
print(t) # -3 days, 0:00:05
t = timedelta(seconds=timeparse(str(t)))
print(t) # -4 days, 23:59:55