Skip to content

Proposal: Dates #977

Closed
Closed

Description

@mlucy

I propose that we introduce three new pseudo-types: dates (by which I mean date, time, and optional timezone), durations, and intervals, all cribbed from ISO 8601 (http://en.wikipedia.org/wiki/ISO_8601), which we represent as strings.

We introduce the following terms:

date.date_sub(other_date) => duration
date.dur_sub(duration) => other_date
date.dur_add(duration) => other_date
duration.dur_sub(other_duration) => third_duration
duration.dur_add(other_duration) => third_duration

date.date_{lt/gt/le/ge/eq/ne}(other_date) => bool
duration.dur_{lt/gt/le/ge/eq/ne}(other_date) => bool

# intervals can also be specified directly as strings
r.interval(date, other_date) => interval
r.interval(date, duration) => interval
date.during(interval) => bool

We should also update the drivers to automatically render native dates/durations/intervals as the appropriate strings.

So, for example, in Ruby you could get all rows inserted in the last day with:

table.between(Date.today, Date.today - 1, :index => :timestamp)

Or, if Ruby didn't have easy date manipulation:

table.between(Time.now, r(Time.now).dur_sub("P1M"), :index => :timestamp)

Activity

  1. ghost assigned on Jun 10, 2013
  2. AtnNn commented on Jun 10, 2013

    @AtnNn
    Member

    Can we spend some more time on this and add separate data types?

  3. jdoliner commented on Jun 10, 2013

    @jdoliner
    Contributor

    @AtnNn I'm in your boat on this one but let's wait until we have the discussion phase.

  4. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    Moving back to backlog. Nested objects are more important.

  5. Linicks commented on Jun 11, 2013

    @Linicks

    Having built in Date types and tools would be a major +1 :)

  6. perone commented on Jun 24, 2013

    @perone

    +1

  7. hcarvalhoalves commented on Jun 28, 2013

    @hcarvalhoalves

    Why reimplementing date manipulation primitives would be necessary? Shouldn't suffice to have queries take native date types and have the driver convert to a wire format?

  8. coffeemug commented on Jun 28, 2013

    @coffeemug
    Contributor

    @hcarvalhoalves suppose you wanted a query that says something like "Give me every event that happened on a Monday". In order for that to work on the server side, it would need a db-level function that determines the day of the date, otherwise you'd have to transfer all the data to the client first and do the processing on the client.

  9. hcarvalhoalves commented on Jun 29, 2013

    @hcarvalhoalves

    @coffeemug Right, I get what you mean now. I just didn't get where the above methods fit in your example though. I expected date manipulation would look something like this:

    r.table('events').filter(
      r.row('takes_place_in').weekday().eq(1) # Monday
    ).run(conn, callback)
    

    No?

    Sorry if my questions are stupid, I'm still figuring out RethinkDB, I'm not sure what's possible or not.

  10. coffeemug commented on Jun 29, 2013

    @coffeemug
    Contributor

    Yep, this is exactly right. Barring some syntactic debate, this is exactly how we'd do it, I think.

  11. AtnNn commented on Jul 2, 2013

    @AtnNn
    Member

    I would like to propose a few modifications and additions to @mlucy's proposal.

    I'm going to call these objects times instead of dates.

    In the iso8601 representation, the time component should be optional as well as the time zone component.

    The P and T prefixes are optional for durations. One can write "1M" or "1:30" instead of "P1M" and "PT1:30". Durations can also be represented simply as a number of seconds.

    All operations assume that all days have exactly 86400 seconds. "23:60" is considered equal to "23:59".

    r.run() takes an additional time_zone argument that specifies the default time zone used in the query. The client drivers automatically set the time_zone to the local time zone of the client. The default time zone is only used where it is explicitly mentioned in this proposal. If time_zone is set to the empty string, time zone defaulting is disabled and all operation that would require using the default time zone fail.


    time.time_sub(other_time) => duration
    time.time_{lt/gt/le/ge/eq/ne}(other_time) => bool
    

    When only one of time or other_time has a time zone, the other value is considered to be in the default time zone. If both time and other_time have no time zone, neither does the result.

    time.dur_sub(duration) => other_time
    time.dur_add(duration) => other_time
    duration.dur_{lt/gt/le/ge/eq/ne}(other_duration) => bool
    duration.dur_sub(other_duration) => third_duration
    duration.dur_add(other_duration) => third_duration
    
    time.during(begin_time, end_time) => true
    time.during(begin_time, duration) => true
    time.during(duration, end_time) => true
    

    There are no intervals. during simply takes two arguments.

    Times default to the default time zone if the time zone component is missing, unless all times have no time zones.

    time.date() => time  # the date component
    time.time_of_day() => duration  # the time component
    time.time_zone() => string  # time zone component
    time.year() => number
    time.month() => number
    time.day() => number
    time.week_day() => number  # 0 = Sunday, 1 = Monday, ..., 6=Saturday
    time.year_day() => number  # 1-366
    time.hour() => number
    time.minute() => number
    time.seconds() => number  # floating point
    duration.totalSeconds() => number
    

    If the time is not present, the corresponding accessors return null. Ditto for the time zone.

    r.time(year, month, day, hour, minute, seconds, tz) => time
    r.time(year, month, day, hour, minute, seconds) => time
    r.time(year, month, day) => time
    

    A constructor.

    r.sunday = 0
    r.monday = 1
    ...
    r.saturday = 6
    r.january = 1
    ...
    r.december = 12
    
    time.epoch_time() => number
    

    Equivalent to time.time_sub("1970-01-01T00:00Z").totalSeconds()

    r.now() => string
    r.now(tz) => string
    

    The current time in the tz time zone. If tz is not specified, use the default time zone. If the default time zone is not specified, use "Z".

    time.format_time(format) => string
    string.parse_time(format) => time
    

    Format and parse times like strftime and strptime.

    time.with_time_zone() => time
    time.with_time_zone(tz) => time
    time.set_time_zone(tz) => time
    

    with_time_zone returns the same time in a different time zone. If the time has no time zone, it converts from the default time zone. With no arguments, it converts to the default time zone.

    set_time_zone simply changes the time zone component.


    Instead of using iso8601 string representation, times could instead be represented as a number of seconds since epoch and durations as a number of seconds. There are a few advantages:

    • using regular comparison and arithmetic operators instead of time_* and dur_*
    • using existing r.between instead of adding a new r.during
    • faster: no need to parse and format the time for each operation

    There are also some disadvantages:

    • debugging is harder with seemingly opaque numbers rather than with nicely formatted strings
    • time zone information is lost, all times would be stored as UTC
    • representing a local time (with no time zone) is impossible

    A better representation would be a new native type instead of a pseudo-type. This would have all these advantages and none of these disadvantages.

  12. coffeemug commented on Jul 2, 2013

    @coffeemug
    Contributor

    FYI, this issue looks like the only one in 1.8-required that's non-trivial and wide open (since we didn't discuss it much). Let's try to hammer this out. (P.S. my feedback on the specific proposal coming soon)

  13. mlucy commented on Jul 2, 2013

    @mlucy
    MemberAuthor

    The P and T prefixes are optional for durations. One can write "1M" or "1:30" instead of "P1M" and "PT1:30". Durations can also be represented simply as a number of seconds.

    This is ambiguous. In ISO8610 P1M means one month and PT1M means one minute.

    "23:60" is considered equal to "23:59".

    What's the motivation for this? That sound wacky.

    duration.totalSeconds() => number

    Could we call this dur_seconds for consistency?

    r.sunday = 0
    r.january = 1

    Why is sunday numbered from 0 when january is numbered from 1?

    time.epoch_time() => number

    This name sounds slightly off to me. See below for discussion of how I think we should handle ISO8601 vs. epoch time.

    Format and parse times like strftime and strptime.

    This seems strange to have in the server. Shouldn't people be doing this in the clients once they've retrieved the date? Also, we'd be violating the standard unless we want the clients to send the whole locale to the server.

    with_time_zone

    Could we call this in_time_zone? It seems less ambiguous to me.


    Instead of using iso8601 string representation, times could instead be represented as a number of seconds since epoch and durations as a number of seconds.

    I think this is the biggest thing left to decide. Here's what I think we should do (although I need to think about it more):

    • Store everything internally as seconds since epoch. People know how to deal with this, it's easy, and it always sorts correctly. Use negative numbers for dates before 1970.
    • Have the functions dur_to_iso8601 and time_to_iso8601 for when people want to do regexp matching or something. Have the functions dur_to_epoch and time_to_epoch for when people have an ISO8601 string that they want in seconds. (Maybe with better names?)
    • Make all the functions (date_add, dur_sub, etc.) polymorphic, and let them accept either numbers (which are interpreted as seconds) or ISO8601 strings, but have them always return seconds since epoch.
  14. coffeemug commented on Jul 2, 2013

    @coffeemug
    Contributor

    Sorry to complicate matters, but I must. First, a few random thoughts:

    • I think that if we implement things like time_sub, instead of adding a proper time type and making sub polymorphic, we will very quickly grow to regret this decision.
    • I also think that on the driver side people should be able to operate with standard time/date types. E.g. r.expr(datetime.now()) should work in Python. They should be able to say table.insert({time: datetime.now()}), and get a native Python datetime object if they get the row back IMO. (at the very least, we should consider this carefully)

    I propose the following:

    • Store time object like this on the server: { __reql_type__: 'time', ms_since_epoch_utc: X }
    • Whenever possible, use existing operations for math and logic (i.e. make add, gt, etc. polymorphic on time)
    • In the drivers, convert __reql_type__: 'time' back to native object (e.g. datetime in Python, Date in JS, etc.)
    • On the server, implement all reasonable time functions (e.g. things listed here: http://www.tutorialspoint.com/javascript/javascript_date_object.htm -- stuff like getYear and setYear)
    • I think the server shouldn't ever store timezone information, just milliseconds since epoch in UTC. When the user says something like r.row('time_field').getDay.eq('Monday') they either pass the timezone they want via run (which the drivers would default to local timezone), or the server assumes they're speaking in UTC if the driver passes nothing.
    • The user can translate things to string representations on the server and store it in attributes, but those are strings, not times. They can't run time ops on it unless they call r.parse_time.
  15. mlucy commented on Jul 2, 2013

    @mlucy
    MemberAuthor

    So, in this one particular case the __rql_type__ solution works fairly well, because our naive ordering on objects exactly matches the ordering we want (since _ is less than all letters and ms_since_epoch_utc will sort correctly).

    So I wouldn't be incredibly opposed to doing it that way. The only large cost I can think of is that people could no longer use times as primary keys.

    What do you think, @AtnNn ?

  16. 138 remaining items

  17. wmrowan commented on Aug 7, 2013

    @wmrowan
    Contributor

    @neumino time.toISO8601()

  18. neumino commented on Aug 7, 2013

    @neumino
    Member

    Err, I should have ack with insensitive case. Thanks @wmrowan

  19. wmrowan commented on Aug 7, 2013

    @wmrowan
    Contributor

    @neumino I think implementing floor is a better solution to your problem then truncating the output of seconds adding milliseconds.

  20. neumino commented on Aug 8, 2013

    @neumino
    Member

    The review for docs has been completed and merge in dates.

  21. mlucy commented on Aug 8, 2013

    @mlucy
    MemberAuthor

    This is finally in next.

  22. jdoliner commented on Aug 8, 2013

    @jdoliner
    Contributor

    Commit hash?

    On Thu, Aug 8, 2013 at 4:30 PM, Michael Lucy [email protected]:

    This is finally in next.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/977#issuecomment-22366007
    .

  23. coffeemug commented on Aug 8, 2013

    @coffeemug
    Contributor

    Slow clap

  24. mlucy commented on Aug 8, 2013

    @mlucy
    MemberAuthor
  25. coffeemug commented on Aug 8, 2013

    @coffeemug
    Contributor

    Ok, so @mglukhovsky just pointed out that slow clap is usually a sarcastic clap. Let's write this one off to cultural differences :)

  26. mglukhovsky commented on Aug 8, 2013

    @mglukhovsky
    Member

    🎆 Can't wait to play with this!

  27. jdoliner commented on Aug 8, 2013

    @jdoliner
    Contributor

    I think the comedy of errors that made up this issue's circumstances might justify the slow clap here by american standards.

  28. neumino commented on Aug 8, 2013

    @neumino
    Member
    r.db('test').table('posts').filter(function(post) {
      return post("date").day().eq(r.monday)
    })

    That's pretty cool :)

    I suggest now that we should have a flag "lang" in the driver, so I can use r.lundi instead of r.monday etc.

  29. wmrowan commented on Aug 9, 2013

    @wmrowan
    Contributor

    @neumino that wouldn't be very clean. We should host a francophone version of ReQL on a different port and make sure that all user facing text (including server generated error messages) is written in idiomatic French.

  30. AtnNn commented on Aug 9, 2013

    @AtnNn
    Member

    Before we can support French and other languages that are not ASCII we need to fix our string encoding (#1181)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions