From 47d9ae90152e16763685e8b28c71ee4205932e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Riegel?= Date: Wed, 12 Mar 2025 00:21:30 +0100 Subject: [PATCH] feat: into duration accepts floats --- crates/nu-command/src/conversions/into/duration.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/nu-command/src/conversions/into/duration.rs b/crates/nu-command/src/conversions/into/duration.rs index 3d11b83afb..d8719b723e 100644 --- a/crates/nu-command/src/conversions/into/duration.rs +++ b/crates/nu-command/src/conversions/into/duration.rs @@ -15,6 +15,7 @@ impl Command for SubCommand { Signature::build("into duration") .input_output_types(vec![ (Type::Int, Type::Duration), + (Type::Float, Type::Duration), (Type::String, Type::Duration), (Type::Duration, Type::Duration), (Type::table(), Type::table()), @@ -109,6 +110,11 @@ impl Command for SubCommand { example: "1_234 | into duration --unit ms", result: Some(Value::test_duration(1_234 * 1_000_000)), }, + Example { + description: "Convert a floating point number of an arbitrary unit to duration", + example: "1.234 | into duration --unit sec", + result: Some(Value::test_duration(1_234 * 1_000_000)), + }, ] } } @@ -240,6 +246,13 @@ fn action(input: &Value, unit: &str, span: Span) -> Value { Ok(val) => Value::duration(val, span), Err(error) => Value::error(error, span), }, + Value::Float { val, .. } => { + let val_str = format!("{}{}", val, unit); + match compound_to_duration(&val_str, value_span) { + Ok(val) => Value::duration(val, span), + Err(error) => Value::error(error, span), + } + } Value::Int { val, .. } => { let ns = match unit { "ns" => 1,