Find out if a timedelta object has any negative attribute

Asked

Viewed 69 times

1

I have a function that takes a parameter of type timedelta. I’m looking for ways to know if the object passed by parameter has any of its attributes (days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) negative. The solution that came to mind is to make a sum with the current date/ time, that is:

>>> dat = datetime.now()
>>> td = timedelta(days=-1)
>>> if (dat+td) < dat:
...   print('timedalta negativo')
... 
timedalta negativo
>>> 

But I do not like very logics that are "tied" to the calendar of the machine, because in this way its logic is heavily dependent on an external factor. The other way would be a if large checking one by one of the attributes.

Some third alternative?

2 answers

2


You can test the value returned by .total_seconds() in the timedelta object. If the value is negative, then the timedelta is negative. There is no need to compare with any actual date.

>>> td = timedelta(days=-1)
>>> if td.total_seconds() < 0:
...     ...

Remembering that an ojbeto "timedelta" is a length of time then it is positive, or it is negative, and it makes no sense to speak of "some negative attribute". If I create a timedelta object of "2 days and -4 hours", the total duration is 1 day and 20 hours, or 44 hours, or that number in seconds - it makes no sense to want to see if ". Seconds()" are negative in parts.

0

If I understand what you are asking I think your example does not work to detect if it has any negative "attribute", at least not in python3. Python converts everything to days and seconds, See the example below:

import datetime

moment = datetime.datetime.now()
td = datetime.timedelta(weeks=1, days=-7, hours=24, minutes=360) 
td.__repr__()
'datetime.timedelta(days=1, seconds=21600)'

moment+td < moment
False

Note that although I threw a negative number to the parameter days, the representation of the object counts the days in weeks and subtracts days which becomes zero, then the number in hours is converted to 1 day days=1 and minutes are converted to sgundos (360*60) seconds=21600. In fact, with the exception of days, seconds and microseconds, all other parameters relating to the assignment of "time" are accessible only for assignment and composition of the other three, if you try to "access" any of them you will get an error. You can check the accessible attributes of a timedelta with the function dir, see:

dir(datetime.timedelta)
Out[108]: 
['__abs__',
 '__add__',
 '__bool__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__divmod__',
 '__doc__',
 '__eq__',
 '__floordiv__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__neg__',
 '__new__',
 '__pos__',
 '__radd__',
 '__rdivmod__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rfloordiv__',
 '__rmod__',
 '__rmul__',
 '__rsub__',
 '__rtruediv__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__truediv__',
 'days',
 'max',
 'microseconds',
 'min',
 'resolution',
 'seconds',
 'total_seconds']

You can also "play" with it as follows:

'days' in dir(td1)
True

'weeks' in dir(td1)
False

'minutes' in dir(td1)
False

'seconds' in dir(td1)
True

Without knowing its context, I venture to say that probably Voce will have to create another strategy to deal with the problem (again: if I understood), maybe instead of receiving an instance of timedelta, a dictionary.

  • It’s actually more like @jsbueno quoted. I need to know if timedelta is negative or positive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.