nushell / nushell

A new type of shell
https://www.nushell.sh/
MIT License
32.38k stars 1.67k forks source link

Relative dates sometimes reported incorrectly #12288

Open swiftvimla opened 7 months ago

swiftvimla commented 7 months ago

Describe the bug

Nushell helpfully prints datetimes with the absolute value and then a value relative to the current time at the end.

The bug relates to the relative representation, eg. in 2 hours. It seems to be rounding values in a (to me at least) non-intuitive way.

How to reproduce

Use date now and then add a duration

> (date now) + 5hr                                                                           03/26/2024 08:36:04 AM
Tue, 26 Mar 2024 13:36:04 +0100 (in 4 hours)
> (date now)                                                                                 03/26/2024 08:36:07 AM
Tue, 26 Mar 2024 08:36:07 +0100 (now)
> (date now) + 1hr                                                                           03/26/2024 08:36:30 AM
Tue, 26 Mar 2024 09:36:30 +0100 (in an hour)
> (date now) + 2hr                                                                           03/26/2024 08:36:32 AM
Tue, 26 Mar 2024 10:36:32 +0100 (in 2 hours)
> (date now) + 3hr                                                                           03/26/2024 08:36:34 AM
Tue, 26 Mar 2024 11:36:34 +0100 (in 2 hours)
> (date now) + 4hr                                                                           03/26/2024 08:36:37 AM
Tue, 26 Mar 2024 12:36:37 +0100 (in 3 hours)
> (date now) + 5hr                                                                           03/26/2024 08:36:40 AM
Tue, 26 Mar 2024 13:36:40 +0100 (in 4 hours)
>             

Expected behavior

Round off to the nearest hour (or whichever unit is appropriate) when displaying an absolute datetime relative to the current datetime.

Screenshots

No response

Configuration

key value
version 0.91.0
branch
commit_hash
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.76.0 (07dca489a 2024-02-04) (Homebrew)
cargo_version cargo 1.76.0
build_time 2024-03-05 20:28:40 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, extra, sqlite, trash, which, zip
installed_plugins

Additional context

No response

fdncred commented 7 months ago

Agreed. We'd love to have that fixed.

fdncred commented 7 months ago

I tracked this down to HumanTime::from().

❯ (date now) + 5hr
lhs: 2024-03-26T15:58:40.874977-05:00, rhs: 18000000000000
to_expanded_string val: 2024-03-26T20:58:40.874977-05:00
Tue, 26 Mar 2024 20:58:40 -0500 (in 4 hours)

So, that val in the to_expanded_string function is being converted to (in 4 hours) in the HumanTime crate. The datetime math looks correct but the estimate is a little off. Not sure what we can do about it though.

ito-hiroki commented 3 months ago

I tried to solve this issue, however I couldn't come up with a good solution. I'll leave the info I found in case it helps.

The following behavior is due to this implementation of chrono-humanize-rs.

> (date now) + 1hr                                                                           03/26/2024 08:36:30 AM
Tue, 26 Mar 2024 09:36:30 +0100 (in an hour)
> (date now) + 2hr                                                                           03/26/2024 08:36:32 AM
Tue, 26 Mar 2024 10:36:32 +0100 (in 2 hours)
> (date now) + 3hr                                                                           03/26/2024 08:36:34 AM
Tue, 26 Mar 2024 11:36:34 +0100 (in 2 hours)

When calculating the difference from the current time, it will be slightly off, resulting in the reproduced example.

~/oss_dev/nushell> (date now) + 3hr                                     07/24/2024 01:14:49 AM
[crates/nu-protocol/src/value/mod.rs:838:21] HumanTime::from(*val) = HumanTime(
    TimeDelta {
        secs: 10799,
        nanos: 999416966,
    },
)
Wed, 24 Jul 2024 04:14:51 +0900 (in 2 hours)
~/oss_dev/nushell> (date now) + 3hr + 1sec                              07/24/2024 01:14:51 AM
[crates/nu-protocol/src/value/mod.rs:838:21] HumanTime::from(*val) = HumanTime(
    TimeDelta {
        secs: 10800,
        nanos: 999203834,
    },
)
Wed, 24 Jul 2024 04:14:55 +0900 (in 3 hours)