Find out if a timedelta object has any negative attributes

2

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 (days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) negative attributes. The solution that came to mind is to make a sum with the current date / time, ie:

>>> 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 machine calendar, because in this way your logic is heavily dependent on an external factor. The other form would be a if large checking one by one of the attributes.

Some third alternative?

    
asked by anonymous 29.12.2018 / 12:52

2 answers

2

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

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

Remembering that a timedelta is a duration of time then it is positive, or it is negative, and it does not make sense to speak of "some negative attribute". If I create a timed 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.

    
29.12.2018 / 16:23
0

If I've gotten what you're asking I guess your example does not work to detect if it has any negative "attributes", 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

Notice that even though I have entered a negative number for the days parameter, the object representation counts the days in% with% and subtracts% with_% that becomes zero, then the number in% with% is converted to 1 day weeks and minutes are converted to seconds (360 * 60) days . In fact, with the exception of hours , days=1 and seconds=21600 , all other parameters related to the assignment of "time" are only accessible for assignment and composition of the other three, if you try to "access" any of them vc you will get an error. You can check the accessible attributes of a days instance with the seconds function, 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 like this:

'days' in dir(td1)
True

'weeks' in dir(td1)
False

'minutes' in dir(td1)
False

'seconds' in dir(td1)
True

Without knowing your context, I'll probably say that you'll have to create another strategy to deal with the problem (again: if I understand it), perhaps instead of getting an instance of microseconds , a dictionary.

    
29.12.2018 / 15:48