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.