Skip to content

Commit b9586f1

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
getMicroseconds/getNanoseconds time functions.
PiperOrigin-RevId: 577869158
1 parent cec2bf1 commit b9586f1

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

base/builtins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ constexpr char kHours[] = "getHours";
7979
constexpr char kMinutes[] = "getMinutes";
8080
constexpr char kSeconds[] = "getSeconds";
8181
constexpr char kMilliseconds[] = "getMilliseconds";
82+
constexpr char kMicroseonds[] = "getMicroseconds";
83+
constexpr char kNanoseconds[] = "getNanoseconds";
8284

8385
// Type conversions
8486
// TODO(issues/23): Add other type conversion methods.

runtime/standard/time_functions.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "absl/strings/match.h"
2222
#include "absl/strings/str_replace.h"
2323
#include "absl/strings/string_view.h"
24+
#include "absl/time/time.h"
2425
#include "base/builtins.h"
2526
#include "base/function_adapter.h"
2627
#include "base/handle.h"
@@ -169,6 +170,24 @@ Handle<Value> GetMilliseconds(ValueFactory& value_factory, absl::Time timestamp,
169170
});
170171
}
171172

173+
Handle<Value> GetMicroseconds(ValueFactory& value_factory, absl::Time timestamp,
174+
absl::string_view tz) {
175+
return GetTimeBreakdownPart(
176+
value_factory, timestamp, tz,
177+
[](const absl::TimeZone::CivilInfo& breakdown) {
178+
return absl::ToInt64Microseconds(breakdown.subsecond);
179+
});
180+
}
181+
182+
Handle<Value> GetNanoseconds(ValueFactory& value_factory, absl::Time timestamp,
183+
absl::string_view tz) {
184+
return GetTimeBreakdownPart(
185+
value_factory, timestamp, tz,
186+
[](const absl::TimeZone::CivilInfo& breakdown) {
187+
return absl::ToInt64Nanoseconds(breakdown.subsecond);
188+
});
189+
}
190+
172191
absl::Status RegisterTimestampFunctions(FunctionRegistry& registry,
173192
const RuntimeOptions& options) {
174193
CEL_RETURN_IF_ERROR(registry.Register(
@@ -333,6 +352,24 @@ absl::Status RegisterTimestampFunctions(FunctionRegistry& registry,
333352
return GetMilliseconds(value_factory, ts, tz.ToString());
334353
})));
335354

355+
CEL_RETURN_IF_ERROR(registry.Register(
356+
BinaryFunctionAdapter<Handle<Value>, absl::Time, const StringValue&>::
357+
CreateDescriptor(builtin::kMicroseonds, true),
358+
BinaryFunctionAdapter<Handle<Value>, absl::Time, const StringValue&>::
359+
WrapFunction([](ValueFactory& value_factory, absl::Time ts,
360+
const StringValue& tz) -> Handle<Value> {
361+
return GetMicroseconds(value_factory, ts, tz.ToString());
362+
})));
363+
364+
CEL_RETURN_IF_ERROR(registry.Register(
365+
BinaryFunctionAdapter<Handle<Value>, absl::Time, const StringValue&>::
366+
CreateDescriptor(builtin::kNanoseconds, true),
367+
BinaryFunctionAdapter<Handle<Value>, absl::Time, const StringValue&>::
368+
WrapFunction([](ValueFactory& value_factory, absl::Time ts,
369+
const StringValue& tz) -> Handle<Value> {
370+
return GetNanoseconds(value_factory, ts, tz.ToString());
371+
})));
372+
336373
return registry.Register(
337374
UnaryFunctionAdapter<Handle<Value>, absl::Time>::CreateDescriptor(
338375
builtin::kMilliseconds, true),

runtime/standard/time_functions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ namespace cel {
3333
// (timestamp).getMinutes(<timezone:string>) -> int
3434
// (timestamp).getSeconds(<timezone:string>) -> int
3535
// (timestamp).getMilliseconds(<timezone:string>) -> int
36+
// (timestamp).getMicroseconds(<timezone:string>) -> int
37+
// (timestamp).getNanoseconds(<timezone:string>) -> int
3638
//
3739
// (duration).getHours() -> int
3840
// (duration).getMinutes() -> int
3941
// (duration).getSeconds() -> int
4042
// (duration).getMilliseconds() -> int
43+
// (duration).getMicroseconds() -> int
44+
// (duration).getNanoseconds() -> int
4145
//
4246
// _+_(timestamp, duration) -> timestamp
4347
// _+_(duration, timestamp) -> timestamp

runtime/standard_runtime_builder_factory_test.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,11 @@ INSTANTIATE_TEST_SUITE_P(
392392
"60",
393393
true},
394394
{"duration_get_milliseconds",
395-
"duration('10h20m30s40ms').getMilliseconds() == 40", true},
395+
"duration('10h20m30s40ms50us60ns').getMilliseconds() == 40", true},
396+
{"duration_get_milliseconds",
397+
"duration('10h20m30s40ms50us60ns').getMicroseconds() == 50", true},
398+
{"duration_get_milliseconds",
399+
"duration('10h20m30s40ms50us60ns').getNanoseconds() == 60", true},
396400
}),
397401
TestCaseName);
398402

0 commit comments

Comments
 (0)