diff --git a/base/src/test/mod.rs b/base/src/test/mod.rs index bcfb400..2e41417 100644 --- a/base/src/test/mod.rs +++ b/base/src/test/mod.rs @@ -75,6 +75,7 @@ mod test_log; mod test_log10; mod test_mod_quotient; mod test_networkdays; +mod test_now; mod test_percentage; mod test_set_functions_error_handling; mod test_sheet_names; diff --git a/base/src/test/test_now.rs b/base/src/test/test_now.rs new file mode 100644 index 0000000..b86bb99 --- /dev/null +++ b/base/src/test/test_now.rs @@ -0,0 +1,30 @@ +#![allow(clippy::unwrap_used)] + +use crate::{mock_time, test::util::new_empty_model}; + +// 14:44 20 Mar 2023 Berlin +const TIMESTAMP_2023: i64 = 1679319865208; + +#[test] +fn arguments() { + let mut model = new_empty_model(); + + model._set("A1", "=NOW(1)"); + model.evaluate(); + + assert_eq!( + model._get_text("A1"), + "#ERROR!", + "NOW should not accept arguments" + ); +} + +#[test] +fn returns_date_time() { + mock_time::set_mock_time(TIMESTAMP_2023); + let mut model = new_empty_model(); + model._set("A1", "=NOW()"); + model.evaluate(); + let text = model._get_text("A1"); + assert_eq!(text, *"20/03/2023 13:44:25"); +} diff --git a/base/src/test/test_today.rs b/base/src/test/test_today.rs index a3b541b..ce68d8c 100644 --- a/base/src/test/test_today.rs +++ b/base/src/test/test_today.rs @@ -33,7 +33,8 @@ fn now_basic_utc() { model.evaluate(); assert_eq!(model._get_text("A1"), *"20/03/2023"); - assert_eq!(model._get_text("A2"), *"45005.572511574"); + // 45005.572511574 + assert_eq!(model._get_text("A2"), *"20/03/2023 13:44:25"); } #[test] @@ -46,5 +47,5 @@ fn now_basic_europe_berlin() { assert_eq!(model._get_text("A1"), *"20/03/2023"); // This is UTC + 1 hour: 45005.572511574 + 1/24 - assert_eq!(model._get_text("A2"), *"45005.614178241"); + assert_eq!(model._get_text("A2"), *"20/03/2023 14:44:25"); } diff --git a/base/src/units.rs b/base/src/units.rs index 533f0e7..8ea49ee 100644 --- a/base/src/units.rs +++ b/base/src/units.rs @@ -329,6 +329,7 @@ impl Model { Function::Tbillyield => self.units_fn_percentage_2(args, cell), Function::Date => self.units_fn_dates(args, cell), Function::Today => self.units_fn_dates(args, cell), + Function::Now => self.units_fn_date_times(args, cell), _ => None, } } @@ -375,4 +376,8 @@ impl Model { // TODO: update locale and use it here Some(Units::Date("dd/mm/yyyy".to_string())) } + + fn units_fn_date_times(&self, _args: &[Node], _cell: &CellReferenceIndex) -> Option { + Some(Units::Date("dd/mm/yyyy hh:mm:ss".to_string())) + } }