You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
spec and behavior of into datetime is ambiguous #8244
<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.
// ...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.
Performance: .abs() is way faster than .to_string().len(). But the difference would be small.
Behavior: "-999"'s length is 4 and "999"'s length is 3 while their absolute values are the same! It's confusing..
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.
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.
Activity
baehyunsol commentedon Feb 28, 2023
I'd like to fix it that way. If everyone's okay with it, I'd make a PR.
bobhy commentedon Mar 1, 2023
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 integerbe 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 ininto intthat 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.
baehyunsol commentedon Mar 2, 2023
Totally agree with you. I want the behavior to be more consistent (ex: always dealing with millisecond). Adding
--convert-toflag also sounds nice.bobhy commentedon Mar 5, 2023
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
datetimethat Nu already supports. I think the only way to do that is to add an explicittimestamptype in Nushell, which might be an interesting enhancement project for someone (else).What I'm working on for now is to use
inttype (signed 64 bit) as a container for extended unix timestamp. Timestamp is always nanosecond resolution.<date> | into intproduces 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.<int> | date to-record' and 'to-table' get an extra field or column callednanosecond` 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.
Fix 8244 -- store timestamps with nanosecond resolution (consistently) (