nushell/crates/nu-std/tests/test_dt.nu
Bob Hyman bc437da5c7
std dt datetime-diff: fix uninitialized field ref when borrowing (#10466)
fixes #10455 

@KAAtheWiseGit, I'm sorry, I didn't mean to block your first PR #10461,
didn't see you had submitted it till I got around to submitting this. If
you want to incoporate useful ideas from this PR into yours, I do not
mind deferring to you.


# Description
Changes made in `datetime-diff`:
* Initialize millisecond and microsecond fields in `$current`, to fix
the error when borrow needs to refer to them.
* Fix `borrow_nanoseconds` to borrow from seconds, not from (unused)
microseconds.
* Added error check to insist that first argument is >= second argument.
`datetime-diff` doesn't represent negative durations correctly (it tries
to borrow out of the year, resulting in negative year and positive all
other fields). We don't currently have a use case requiring negative
durations.
* Add comments so help is a bit clearer (I was surprised that the first
argument, named `$from` was actually supposed to be the *later*
datetime. The order of arguments is reasonable (reminiscent of <later>
<minus> <earlier>), so I just changed the param name to match its
purpose.

Changes made in `pretty-print-duration`:
* changed type of argument from `duration` to `record`. (it's not clear
why Nu was not complaining about this!)
* changed test for skipping a clause from `> 0` to `!= 0`. Even though
`datetime-diff` won't present a negative field in the record, user might
call `pretty-print-duration` with one, might as well handle it. (but I
think `hour:-2` will be rendered as `-2hr`, not `-2hrs`...).
* added help and an example.

# User-Facing Changes
none requiring code changes.

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`
- 
- # After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-09-24 10:53:56 +02:00

44 lines
No EOL
1.7 KiB
Text

use std assert
use std dt *
#[test]
def equal_times [] {
let t1 = (date now)
assert equal (datetime-diff $t1 $t1) ({year:0, month:0, day:0, hour:0, minute:0, second:0, millisecond:0, microsecond:0 nanosecond:0})
}
#[test]
def one_ns_later [] {
let t1 = (date now)
assert equal (datetime-diff ($t1 + 1ns) $t1) ({year:0, month:0, day:0, hour:0, minute:0, second:0, millisecond:0, microsecond:0 nanosecond:1})
}
#[test]
def one_yr_later [] {
let t1 = ('2022-10-1T0:1:2z' | into datetime) # a date for which one year later is 365 days, since duration doesn't support year or month
assert equal (datetime-diff ($t1 + 365day) $t1) ({year:1, month:0, day:0, hour:0, minute:0, second:0, millisecond:0, microsecond:0 nanosecond:0})
}
#[test]
def carry_ripples [] {
let t1 = ('2023-10-9T0:0:0z' | into datetime)
let t2 = ('2022-10-9T0:0:0.000000001z' | into datetime)
assert equal (datetime-diff $t1 $t2) ({year:0, month:11, day:30, hour:23, minute:59, second:59, millisecond:999, microsecond:999 nanosecond:999})
}
#[test]
def earlier_arg_must_be_less_or_equal_later [] {
let t1 = ('2022-10-9T0:0:0.000000001z' | into datetime)
let t2 = ('2023-10-9T0:0:0z' | into datetime)
assert error {|| (datetime-diff $t1 $t2)}
}
#[test]
def pp_skips_zeros [] {
assert equal (pretty-print-duration {year:1, month:0, day:0, hour:0, minute:0, second:0, millisecond:0, microsecond:0 nanosecond:0}) "1yr "
}
#[test]
def pp_doesnt_skip_neg [] { # datetime-diff can't return negative units, but prettyprint shouldn't skip them (if passed handcrafted record)
assert equal (pretty-print-duration {year:-1, month:0, day:0, hour:0, minute:0, second:0, millisecond:0, microsecond:0 nanosecond:0}) "-1yr "
}