Skip to content

spec and behavior of into datetime is ambiguous #8244

Closed

Description

@baehyunsol

Question

<int> | into datetime converts an integer to a datetime. If the number is big, it's in microseconds, otherwise it's second. But the spec doesn't tell us how big the number should be. Also, the implementation is ambiguous. Below is the implementation.

link

// ...
match ts.to_string().len() {
  x if x > 13 => Value::Date {
    val: Utc.timestamp_nanos(ts).into(),
    span: head,
  },
  x if x > 10 => match_datetime!(Utc.timestamp_millis_opt(ts)),
  _ => match_datetime!(Utc.timestamp_opt(ts, 0)),
}
// ...

ts is an i64 value which is converted to datetime. I have no idea why it uses .to_string().len() instead of .abs(). It has two issues.

  1. Performance: .abs() is way faster than .to_string().len(). But the difference would be small.
  2. Behavior: "-999"'s length is 4 and "999"'s length is 3 while their absolute values are the same! It's confusing..

Additional context and details

No response

Activity

  1. added
    deprecated:question
    on Feb 27, 2023
  2. added
    category:inconsistent-behavior
    on Feb 27, 2023
  3. baehyunsol commented on Feb 28, 2023

    @baehyunsol
    ContributorAuthor
    if ts.abs() > 1_000_000_000_000 {
      Value::Date {
        val: Utc.timestamp_nanos(ts).into(),
        span: head,
      }
    }
    
    else if ts.abs() > 1_000_000_000 {
      match_datetime!(Utc.timestamp_millis_opt(ts))
    }
    
    else {
      match_datetime!(Utc.timestamp_opt(ts, 0))
    }

    I'd like to fix it that way. If everyone's okay with it, I'd make a PR.

  4. bobhy commented on Mar 1, 2023

    @bobhy
    Contributor

    I think this change would maintain backward compatibility, but I think it's a bad feature to have.

    A timestamp in seconds could be mistaken for a timestamp with millisecond resolution that's 1000 times further into the future (from the epoch date). The only way to resolve the ambiguity is to assume an upper limit for the timestamp in seconds. If that's unix time, it would mean depending on the 2038 rollover of the unix time, which doesn't seem very forward looking. Nushell's native datetime (64 bits, nanosecond resolution) doesn't have this issue until 2554, maybe.

    I wanted to propose that <??>| into integer be modified to convert all 3 forms of date/time into only the int with nanosecond precision (but zero filled). However, I don't see code in into int that handles millisecond or nanosecond datetimes. Does anybody know where those are handled?

    But I think it would be safe for upscale all times when converted to int. Today, there's no way to specify the precision for: <int> | into datetime, so the vulnerable apps are only working today by accident (because they haven't yet been exposed to a far future date in the ambiguous range).

    And we could implement a <int> | into datetime --truncate-to <scale>. I would actually like to see such a function if I could reliably truncate to a day, week or month boundary. This would make financial calculations easier.

    If someone could point me in the right direction, I'd take a crack at this.

  5. baehyunsol commented on Mar 2, 2023

    @baehyunsol
    ContributorAuthor

    Totally agree with you. I want the behavior to be more consistent (ex: always dealing with millisecond). Adding --convert-to flag also sounds nice.

  6. bobhy commented on Mar 5, 2023

    @bobhy
    Contributor

    I'm working on a resolution for this. The best I can come up with in short order is to restrict the range of timestamps that can be handled, in order to preserve the nanosecond resolution. You would need 96 bits to represent nanosecond resolution over a range of timestamps that matches the range of datetime that Nu already supports. I think the only way to do that is to add an explicit timestamp type in Nushell, which might be an interesting enhancement project for someone (else).

    What I'm working on for now is to use int type (signed 64 bit) as a container for extended unix timestamp. Timestamp is always nanosecond resolution.

    • <date> | into int produces number of nanoseconds since unix epoch (1970-1-1 GMT). Error if <date> is outside range 1670 AD to 2270 AD. At least the timestamp beats 32 bit unix timestamp 2038 problem.
    • ' | into datetime' is the inverse, and will not produce a datetime outside the above range.
    • <int> | date to-record' and 'to-table' get an extra field or column called nanosecond` which holds the residual sub-second value.

    I think the above is pretty straightforward, but I had to follow a long and winding path to come to it. Please (all) let me know if you spot problems / don't like the approach.

  7. added this to the v0.77.0 milestone on Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:inconsistent-behaviordeprecated:question

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions