5 ms·
Awesome! Timekeeping is hard and I'm glad we now have one more tool do deal with it. One thing that is bothering me is that when you ask for `maya.when('tomorr
by BoppreH 10y ago
Awesome! Timekeeping is hard and I'm glad we now have one more tool do deal with it.
One thing that is bothering me is that when you ask for `maya.when('tomorrow')`, or give only a date, you get back a timestamp with millisecond precision, representing 00:00 of that day. I understand this simplifies the implementation, but shouldn't `tomorrow` be a range, from 00:00 to 23:59?
Treating imprecise dates as ranges would allow for stuff like
# Did this event happen "yesterday" according to the US/Eastern time zone?
timestamp in maya.when('yesterday', to_timezone='US/Eastern')
# Get me all events that happened last Friday (UTC)
[event for event in events if event.time in maya.when('2016-12-16')]
Maybe I'm being naive, and there's a reason why this won't work, but this seems the way most humans deal with time.
PS: it failed to install on Windows, so I opened an issue at https://github.com/kennethreitz/maya/issues/10 https://github.com/kennethreitz/maya/issues/10
- tokenizerrr 10y agoHow would you serialize that range to, for example, ISO8601?
- cheapsteak 10y agoFrom wiki: ISO8601 has four ways to express a time interval: 1. Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z". 2. Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M" 3. Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z" 4. Duration only, such as "P1Y2M10DT2H30M", with additional context information http://en.wikipedia.org/wiki/ISO_8601#Time_intervals http://en.wikipedia.org/wiki/ISO_8601#Time_intervals
- BoppreH 10y agoExcellent question. ISO 8601 has features to encode time intervals ( https://en.wikipedia.org/wiki/ISO_8601#Time_intervals https://en.wikipedia.org/wiki/ISO_8601#Time_intervals ). If that's too verbose, and we only need to represent integer intervals, one could store a truncated representation, where the omitted parts mean "any" (like * in cron). For example, `2016-11` could mean the entire month of November. But those are just ideas for the representation. The important feature is that not all objects are precise points in time, but may be a block/interval/range.
- Klathmon 10y agoHonestly I feel like it would be easier to just have the "precision" of a date/time be adjustable. Just off the top of my head, ISO8601 is already in the right order, just letting users "leave off" anything that's not important and then having a library interpret those "partial dates" as a range internally so it's easy to check identity or if one date is "in" another.
- BoppreH 10y agoThat's exactly what I meant. Omitted parts should not be treated as zeroes, but as "any".
- supergreg 10y agoMaybe `when` should be renamed or an alias to `at` and add an `in` method to make it more explicit.
- AdamN 10y agoThis would need to be done in Python itself, not Maya. I'd be strongly in favor of it though.
- yunruse 10y agoNot necessarily. Python objects can declare a `__contains__` function (such that `x in y` corresponds to `x.__contains__(y)`): https://docs.python.org/3.5/reference/datamodel.html#object.__contains__ https://docs.python.org/3.5/reference/datamodel.html#object....
- jeffbr13 10y agoPython 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())
- aorth 10y agoImportant note from the comments in that issue: Kenneth says that Maya is only tested on Python 2.7 so far. Python 3 support is coming later. https://github.com/kennethreitz/maya/issues/10#issuecomment-267835648 https://github.com/kennethreitz/maya/issues/10#issuecomment-...