From b7e5790cd1ae217d6706fb5a05719d765ff5d15b Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 30 Nov 2022 17:10:28 -0600 Subject: [PATCH] fix dfr datetime conversion (#7264) # Description Closes #7257 This fixes an issue where dataframes would always try to convert datetimes with milliseconds. Since the timeunit is passed in, I make use of it and try to choose the appropriate divisor. # User-Facing Changes # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # 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. --- .../src/dataframe/values/nu_dataframe/conversion.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/dataframe/values/nu_dataframe/conversion.rs b/crates/nu-command/src/dataframe/values/nu_dataframe/conversion.rs index c4d448a1d7..138dafd5bf 100644 --- a/crates/nu-command/src/dataframe/values/nu_dataframe/conversion.rs +++ b/crates/nu-command/src/dataframe/values/nu_dataframe/conversion.rs @@ -498,7 +498,7 @@ pub fn create_column( Ok(Column::new(casted.name().into(), values)) } - DataType::Datetime(_, _) => { + DataType::Datetime(time_unit, _) => { let casted = series.datetime().map_err(|e| { ShellError::GenericError( "Error casting column to datetime".into(), @@ -508,15 +508,19 @@ pub fn create_column( Vec::new(), ) })?; - let values = casted .into_iter() .skip(from_row) .take(size) .map(|v| match v { Some(a) => { - // elapsed time in milliseconds since 1970-01-01 - let seconds = a / 1000; + let unit_divisor = match time_unit { + TimeUnit::Nanoseconds => 1_000_000_000, + TimeUnit::Microseconds => 1_000_000, + TimeUnit::Milliseconds => 1_000, + }; + // elapsed time in nano/micro/milliseconds since 1970-01-01 + let seconds = a / unit_divisor; let naive_datetime = match NaiveDateTime::from_timestamp_opt(seconds, 0) { Some(val) => val, None => {