3 ms·
Python already contains separate date and time objects for this purpose[1][2]! [1]: https://docs.python.org/3/library/datetime.html#date-objects https://docs.p
by jeffbr13 10y ago
Python already contains separate date and time objects for this purpose[1][2]!
[1]: https://docs.python.org/3/library/datetime.html#date-objects https://docs.python.org/3/library/datetime.html#date-objects
[2]: https://docs.python.org/3/library/datetime.html#time-objects https://docs.python.org/3/library/datetime.html#time-objects
- BoppreH 10y agoI think that would only work for day long ranges. What if I want the events that happened in the last hour, or that are scheduled for May?
- robochat 10y agoThe pandas library has a period data type if you're looking for one.
- jeffbr13 10y agoCombine pure dates and times with timedelta[1]? Python's multiple comparison expressions help here too. For events in the last hour, the following expression should evaluate to True: (datetime.now() - timedelta(hours=1)) <= event_datetime Checking whether event_datetime is within May of the current year is a bit more involved, but you may not need to convert everything to a date (would have to check in an interpreter): date.today().replace(month=5, day=1) <= event_datetime.date() <= date.today().replace(month=5, day=31) The point is that the language already has good support for sensible time operations, but it's parsing is weak. And there's Arrow for that[2]! [1]: https://docs.python.org/3/library/datetime.html#timedelta-objects https://docs.python.org/3/library/datetime.html#timedelta-ob... [2]: http://arrow.readthedocs.io/en/latest/ http://arrow.readthedocs.io/en/latest/
- RubyPinch 10y agoprobably would be easier to hide it away in a small class with `__contains__` event_datetime in daterange(timedelta(hours=1), before=datetime.now())