From 4d2fcbd9f9d0f5c899d903156e3e30e6c05eefef Mon Sep 17 00:00:00 2001 From: Kosmas Papadatos Date: Sat, 27 Jul 2024 10:18:16 +0300 Subject: [PATCH] Add epoch_millis_to_iso8601 transform function --- src/transforms.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/transforms.rs b/src/transforms.rs index b4803b6..6531692 100644 --- a/src/transforms.rs +++ b/src/transforms.rs @@ -76,6 +76,10 @@ lazy_static! { "epoch_seconds_to_iso8601", Box::new(create_epoch_seconds_to_iso8601_fn()), ); + runtime.register_function( + "epoch_millis_to_iso8601", + Box::new(create_epoch_millis_to_iso8601_fn()), + ); runtime.register_function( "epoch_micros_to_iso8601", Box::new(create_epoch_micros_to_iso8601_fn()), @@ -184,6 +188,13 @@ fn create_epoch_seconds_to_iso8601_fn() -> CustomFunction { } // TODO: Consolidate these custom function factories +fn create_epoch_millis_to_iso8601_fn() -> CustomFunction { + CustomFunction::new( + Signature::new(vec![ArgumentType::Number], None), + Box::new(jmespath_epoch_millis_to_iso8601), + ) +} + fn create_epoch_micros_to_iso8601_fn() -> CustomFunction { CustomFunction::new( Signature::new(vec![ArgumentType::Number], None), @@ -214,12 +225,14 @@ fn substr(args: &[Rcvar], context: &mut Context) -> Result enum EpochUnit { Seconds(i64), + Milliseconds(i64), Microseconds(i64), } fn iso8601_from_epoch(epoch_unit: EpochUnit) -> String { let dt = match epoch_unit { EpochUnit::Seconds(s) => Utc.timestamp_nanos(s * 1_000_000_000), + EpochUnit::Milliseconds(ms) => Utc.timestamp_nanos(ms * 1_000_000), EpochUnit::Microseconds(u) => Utc.timestamp_nanos(u * 1000), }; @@ -236,6 +249,16 @@ fn jmespath_epoch_seconds_to_iso8601( Ok(Arc::new(variable)) } +fn jmespath_epoch_millis_to_iso8601( + args: &[Rcvar], + context: &mut Context, +) -> Result { + let millis = i64_from_args(args, context, 0)?; + let value = serde_json::Value::String(iso8601_from_epoch(EpochUnit::Milliseconds(millis))); + let variable = Variable::try_from(value)?; + Ok(Arc::new(variable)) +} + fn jmespath_epoch_micros_to_iso8601( args: &[Rcvar], context: &mut Context,