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.
– Matheus Saraiva