Fixed to/from toml date format (#11846)

With this PR i try to resolve #11751 

# Description
I am rather new to Rust so if anything is not the way it should be
please let me know.

As described in the title I just fixed the date conversion in the to and
from toml commands as i thought it would be a good first issue. The
example of the original issue will now work as follows:

```
~> {date: 2024-02-02} | to toml
date = "2024-02-02T00:00:00+00:00"
~> "dob = 1979-05-27T07:32:00-08:00" | from toml
╭─────┬───────────────────────────╮
│ dob │ 44 years ago              |
╰─────┴───────────────────────────╯
```

The `from toml` command now returns a nushell date which is displayed as
`44 years ago` in this case.

# User-Facing Changes
none

# Tests + Formatting
all tests pass and formatting has been applied

---------

Co-authored-by: dannou812 <dannou281@gmail.com>
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
dannou812
2024-02-14 19:13:33 +01:00
committed by GitHub
parent 4073783d6e
commit b0600449e5
2 changed files with 105 additions and 15 deletions

View File

@ -1,3 +1,4 @@
use chrono::SecondsFormat;
use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -51,7 +52,9 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
Value::Int { val, .. } => toml::Value::Integer(*val),
Value::Filesize { val, .. } => toml::Value::Integer(*val),
Value::Duration { val, .. } => toml::Value::String(val.to_string()),
Value::Date { val, .. } => toml::Value::String(val.to_string()),
Value::Date { val, .. } => {
toml::Value::String(val.to_rfc3339_opts(SecondsFormat::AutoSi, false))
}
Value::Range { .. } => toml::Value::String("<Range>".to_string()),
Value::Float { val, .. } => toml::Value::Float(*val),
Value::String { val, .. } | Value::QuotedString { val, .. } => {
@ -171,6 +174,7 @@ fn to_toml(
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
#[test]
fn test_examples() {
@ -179,6 +183,25 @@ mod tests {
test_examples(ToToml {})
}
#[test]
fn to_toml_creates_correct_date() {
let engine_state = EngineState::new();
let test_date = Value::date(
chrono::FixedOffset::east_opt(60 * 120)
.unwrap()
.with_ymd_and_hms(1980, 10, 12, 10, 12, 44)
.unwrap(),
Span::test_data(),
);
let reference_date = toml::Value::String(String::from("1980-10-12T10:12:44+02:00"));
let result = helper(&engine_state, &test_date);
assert!(result.is_ok_and(|res| res == reference_date));
}
#[test]
fn test_value_to_toml_value() {
//