3 ms·
>>> from datetime import datetime, timedelta >>> datetime.now() - (datetime.now() + timedelta(days=1)) datetime.timedelta(-2, 86399, 999969)
by calpaterson 10y ago
>>> from datetime import datetime, timedelta
>>> datetime.now() - (datetime.now() + timedelta(days=1))
datetime.timedelta(-2, 86399, 999969)
- jbg_ 10y agoI’m not entirely sure what you were trying to demonstrate, but clearly the result of datetime.now() changes between the two invocations (it has microsecond precision). Try: >>> from datetime import datetime, timedelta >>> now = datetime.now() >>> now - (now + timedelta(days=1)) datetime.timedelta(-1)
- calpaterson 10y agodatetime.timedelta internally uses a triple of (days,seconds,microseconds) to represent it's data and exposes that to users of the class. That a timedelta can be represented in multiple ways is alone quite surprising but some of the representations that happen can be very confusing. I think the example I gave which represents "-1 day and a few microseconds" as "-2 days + 86399 milliseconds + 999969 microseconds" is very surprising and it commonly happens in practice. For comparison, here's what postgres does: postgres=> select now() - (now() + '12 milliseconds'::interval + '1 day'::interval); ?column? ----------------------- -1 days -00:00:00.012 (1 row)
- sdispater 10y agoThat's a problem Pendulum is trying to solve d1 = datetime(2012, 1, 1, 1, 2, 3, tzinfo=pytz.UTC) d2 = datetime(2011, 12, 31, 22, 2, 3, tzinfo=pytz.UTC) delta = d2 - d1 delta.days -1 delta.seconds 75600 d1 = Pendulum(2012, 1, 1, 1, 2, 3) d2 = Pendulum(2011, 12, 31, 22, 2, 3) delta = d2 - d1 delta.days 0 delta.hours -3