0

If I have everyday datetime - how to find out, the event has already occurred or not, by subtraction with datetime.now()

Let we had everyday meeting at 15:35. Today John came earlier - at 12:45, but Alex was late for 2 h. and 15 min. (came at 17:40).

meet_dt = datetime(year=2015, month=8, day=19, hour=15, minute=35)
john_dt = datetime(year=2015, month=8, day=19, hour=12, minute=45)
alex_dt = datetime(year=2015, month=8, day=19, hour=17, minute=40)

print(meat_dt - john_dt) # came before > 2:50:00
print(meat_dt - alex_dt) # came after  > -1 day, 21:55:00

If I take away from the big date less - then everything is fine, but conversely I recive -1 day, 21:55:00 why not -2:15:00, what a minus day?

khex
  • 2,778
  • 6
  • 32
  • 56

1 Answers1

2

Because timedeltas are normalized

All of the parts of the timedelta other than the days field are always nonnegative, as described in the documentation.

Incidentally, if you want to see what happened first, don't do this subtraction. Just compare directly with <:

if then < datetime.datetime.now():
    # then is in the past
Kevin
  • 28,963
  • 9
  • 62
  • 81
  • unrelated: [use `.utcnow()` to handle DST transitions transparently.](http://stackoverflow.com/q/26313520/4279) – jfs Aug 20 '15 at 06:01
  • @J.F.Sebastian: I would strongly recommend using `now(timezone.utc)` instead if you want UTC time. – Kevin Aug 20 '15 at 13:20