From b9586f111cf23b5808d32ddd2da12e1c6c7509f5 Mon Sep 17 00:00:00 2001 From: CEL Dev Team Date: Mon, 30 Oct 2023 09:29:31 -0700 Subject: [PATCH] getMicroseconds/getNanoseconds time functions. PiperOrigin-RevId: 577869158 --- base/builtins.h | 2 + runtime/standard/time_functions.cc | 37 +++++++++++++++++++ runtime/standard/time_functions.h | 4 ++ .../standard_runtime_builder_factory_test.cc | 6 ++- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/base/builtins.h b/base/builtins.h index ec402699..cf83bbbf 100644 --- a/base/builtins.h +++ b/base/builtins.h @@ -79,6 +79,8 @@ constexpr char kHours[] = "getHours"; constexpr char kMinutes[] = "getMinutes"; constexpr char kSeconds[] = "getSeconds"; constexpr char kMilliseconds[] = "getMilliseconds"; +constexpr char kMicroseonds[] = "getMicroseconds"; +constexpr char kNanoseconds[] = "getNanoseconds"; // Type conversions // TODO(issues/23): Add other type conversion methods. diff --git a/runtime/standard/time_functions.cc b/runtime/standard/time_functions.cc index 3c746849..6a360faa 100644 --- a/runtime/standard/time_functions.cc +++ b/runtime/standard/time_functions.cc @@ -21,6 +21,7 @@ #include "absl/strings/match.h" #include "absl/strings/str_replace.h" #include "absl/strings/string_view.h" +#include "absl/time/time.h" #include "base/builtins.h" #include "base/function_adapter.h" #include "base/handle.h" @@ -169,6 +170,24 @@ Handle GetMilliseconds(ValueFactory& value_factory, absl::Time timestamp, }); } +Handle GetMicroseconds(ValueFactory& value_factory, absl::Time timestamp, + absl::string_view tz) { + return GetTimeBreakdownPart( + value_factory, timestamp, tz, + [](const absl::TimeZone::CivilInfo& breakdown) { + return absl::ToInt64Microseconds(breakdown.subsecond); + }); +} + +Handle GetNanoseconds(ValueFactory& value_factory, absl::Time timestamp, + absl::string_view tz) { + return GetTimeBreakdownPart( + value_factory, timestamp, tz, + [](const absl::TimeZone::CivilInfo& breakdown) { + return absl::ToInt64Nanoseconds(breakdown.subsecond); + }); +} + absl::Status RegisterTimestampFunctions(FunctionRegistry& registry, const RuntimeOptions& options) { CEL_RETURN_IF_ERROR(registry.Register( @@ -333,6 +352,24 @@ absl::Status RegisterTimestampFunctions(FunctionRegistry& registry, return GetMilliseconds(value_factory, ts, tz.ToString()); }))); + CEL_RETURN_IF_ERROR(registry.Register( + BinaryFunctionAdapter, absl::Time, const StringValue&>:: + CreateDescriptor(builtin::kMicroseonds, true), + BinaryFunctionAdapter, absl::Time, const StringValue&>:: + WrapFunction([](ValueFactory& value_factory, absl::Time ts, + const StringValue& tz) -> Handle { + return GetMicroseconds(value_factory, ts, tz.ToString()); + }))); + + CEL_RETURN_IF_ERROR(registry.Register( + BinaryFunctionAdapter, absl::Time, const StringValue&>:: + CreateDescriptor(builtin::kNanoseconds, true), + BinaryFunctionAdapter, absl::Time, const StringValue&>:: + WrapFunction([](ValueFactory& value_factory, absl::Time ts, + const StringValue& tz) -> Handle { + return GetNanoseconds(value_factory, ts, tz.ToString()); + }))); + return registry.Register( UnaryFunctionAdapter, absl::Time>::CreateDescriptor( builtin::kMilliseconds, true), diff --git a/runtime/standard/time_functions.h b/runtime/standard/time_functions.h index d8fc2e87..6224b739 100644 --- a/runtime/standard/time_functions.h +++ b/runtime/standard/time_functions.h @@ -33,11 +33,15 @@ namespace cel { // (timestamp).getMinutes() -> int // (timestamp).getSeconds() -> int // (timestamp).getMilliseconds() -> int +// (timestamp).getMicroseconds() -> int +// (timestamp).getNanoseconds() -> int // // (duration).getHours() -> int // (duration).getMinutes() -> int // (duration).getSeconds() -> int // (duration).getMilliseconds() -> int +// (duration).getMicroseconds() -> int +// (duration).getNanoseconds() -> int // // _+_(timestamp, duration) -> timestamp // _+_(duration, timestamp) -> timestamp diff --git a/runtime/standard_runtime_builder_factory_test.cc b/runtime/standard_runtime_builder_factory_test.cc index 4d727bbb..a9c6e32d 100644 --- a/runtime/standard_runtime_builder_factory_test.cc +++ b/runtime/standard_runtime_builder_factory_test.cc @@ -392,7 +392,11 @@ INSTANTIATE_TEST_SUITE_P( "60", true}, {"duration_get_milliseconds", - "duration('10h20m30s40ms').getMilliseconds() == 40", true}, + "duration('10h20m30s40ms50us60ns').getMilliseconds() == 40", true}, + {"duration_get_milliseconds", + "duration('10h20m30s40ms50us60ns').getMicroseconds() == 50", true}, + {"duration_get_milliseconds", + "duration('10h20m30s40ms50us60ns').getNanoseconds() == 60", true}, }), TestCaseName);