Skip to content

Commit d128093

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Add getDays function for Duration.
This change introduces a new standard function `getDays` that can be called on a `Duration` value to extract the number of full days. The implementation uses `java.time.Duration.toDays()` when canonical type evaluation is enabled, and a custom utility function otherwise. PiperOrigin-RevId: 855230705
1 parent 777d089 commit d128093

File tree

8 files changed

+140
-0
lines changed

8 files changed

+140
-0
lines changed

checker/src/main/java/dev/cel/checker/CelStandardDeclarations.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public enum StandardFunction {
213213
"getDayOfWeek",
214214
Overload.DateTime.TIMESTAMP_TO_DAY_OF_WEEK,
215215
Overload.DateTime.TIMESTAMP_TO_DAY_OF_WEEK_WITH_TZ),
216+
GET_DAYS("getDays", Overload.DateTime.DURATION_TO_DAYS),
216217
GET_HOURS(
217218
"getHours",
218219
Overload.DateTime.TIMESTAMP_TO_HOURS,
@@ -907,6 +908,10 @@ public enum DateTime implements StandardOverload {
907908
SimpleType.TIMESTAMP,
908909
SimpleType.STRING)),
909910

911+
DURATION_TO_DAYS(
912+
CelOverloadDecl.newMemberOverload(
913+
"duration_to_days", "get days from duration", SimpleType.INT, SimpleType.DURATION)),
914+
910915
TIMESTAMP_TO_HOURS(
911916
CelOverloadDecl.newMemberOverload(
912917
"timestamp_to_hours",

checker/src/test/resources/standardEnvDump.baseline

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ declare getDayOfYear {
200200
function timestamp_to_day_of_year google.protobuf.Timestamp.() -> int
201201
function timestamp_to_day_of_year_with_tz google.protobuf.Timestamp.(string) -> int
202202
}
203+
declare getDays {
204+
function duration_to_days google.protobuf.Duration.() -> int
205+
}
203206
declare getFullYear {
204207
function timestamp_to_year google.protobuf.Timestamp.() -> int
205208
function timestamp_to_year_with_tz google.protobuf.Timestamp.(string) -> int

common/src/main/java/dev/cel/common/internal/ProtoTimeUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public final class ProtoTimeUtils {
6868

6969
private static final long SECONDS_PER_MINUTE = 60L;
7070
private static final long SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60;
71+
private static final long SECONDS_PER_DAY = SECONDS_PER_HOUR * 24;
7172

7273
private static final ThreadLocal<SimpleDateFormat> TIMESTAMP_FORMAT =
7374
new ThreadLocal<SimpleDateFormat>() {
@@ -164,6 +165,14 @@ public static long toHours(Duration duration) {
164165
return checkValid(duration).getSeconds() / SECONDS_PER_HOUR;
165166
}
166167

168+
/**
169+
* Convert a Duration to the number of days. The result will be rounded towards 0 to the nearest
170+
* day.
171+
*/
172+
public static long toDays(Duration duration) {
173+
return checkValid(duration).getSeconds() / SECONDS_PER_DAY;
174+
}
175+
167176
/**
168177
* Convert a Duration to the number of minutes. The result will be rounded towards 0 to the
169178
* nearest minute.

runtime/src/main/java/dev/cel/runtime/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ java_library(
628628
"//runtime/standard:get_day_of_month",
629629
"//runtime/standard:get_day_of_week",
630630
"//runtime/standard:get_day_of_year",
631+
"//runtime/standard:get_days",
631632
"//runtime/standard:get_full_year",
632633
"//runtime/standard:get_hours",
633634
"//runtime/standard:get_milliseconds",
@@ -686,6 +687,7 @@ cel_android_library(
686687
"//runtime/standard:get_day_of_month_android",
687688
"//runtime/standard:get_day_of_week_android",
688689
"//runtime/standard:get_day_of_year_android",
690+
"//runtime/standard:get_days_android",
689691
"//runtime/standard:get_full_year_android",
690692
"//runtime/standard:get_hours_android",
691693
"//runtime/standard:get_milliseconds_android",

runtime/src/main/java/dev/cel/runtime/CelStandardFunctions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import dev.cel.runtime.standard.GetDayOfWeekFunction.GetDayOfWeekOverload;
5656
import dev.cel.runtime.standard.GetDayOfYearFunction;
5757
import dev.cel.runtime.standard.GetDayOfYearFunction.GetDayOfYearOverload;
58+
import dev.cel.runtime.standard.GetDaysFunction;
59+
import dev.cel.runtime.standard.GetDaysFunction.GetDaysOverload;
5860
import dev.cel.runtime.standard.GetFullYearFunction;
5961
import dev.cel.runtime.standard.GetFullYearFunction.GetFullYearOverload;
6062
import dev.cel.runtime.standard.GetHoursFunction;
@@ -158,6 +160,7 @@ public final class CelStandardFunctions {
158160
GetDayOfMonthFunction.create(),
159161
GetDayOfWeekFunction.create(),
160162
GetDayOfYearFunction.create(),
163+
GetDaysFunction.create(),
161164
GetFullYearFunction.create(),
162165
GetHoursFunction.create(),
163166
GetMillisecondsFunction.create(),
@@ -311,6 +314,8 @@ public enum StandardFunction {
311314
GetDayOfWeekOverload.TIMESTAMP_TO_DAY_OF_WEEK,
312315
GetDayOfWeekOverload.TIMESTAMP_TO_DAY_OF_WEEK_WITH_TZ),
313316

317+
GET_DAYS("getDays", GetDaysOverload.DURATION_TO_DAYS),
318+
314319
GET_HOURS(
315320
"getHours",
316321
GetHoursOverload.TIMESTAMP_TO_HOURS,

runtime/src/main/java/dev/cel/runtime/standard/BUILD.bazel

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,41 @@ cel_android_library(
519519
],
520520
)
521521

522+
java_library(
523+
name = "get_days",
524+
srcs = ["GetDaysFunction.java"],
525+
tags = [
526+
],
527+
deps = [
528+
":standard_function",
529+
":standard_overload",
530+
"//common:options",
531+
"//common/internal:proto_time_utils",
532+
"//runtime:function_binding",
533+
"//runtime:runtime_equality",
534+
"//runtime/standard:standard_function",
535+
"@maven//:com_google_guava_guava",
536+
"@maven//:com_google_protobuf_protobuf_java",
537+
],
538+
)
539+
540+
cel_android_library(
541+
name = "get_days_android",
542+
srcs = ["GetDaysFunction.java"],
543+
tags = [
544+
],
545+
deps = [
546+
":standard_function_android",
547+
":standard_overload_android",
548+
"//common:options",
549+
"//common/internal:proto_time_utils_android",
550+
"//runtime:function_binding_android",
551+
"//runtime:runtime_equality_android",
552+
"@maven_android//:com_google_guava_guava",
553+
"@maven_android//:com_google_protobuf_protobuf_javalite",
554+
],
555+
)
556+
522557
java_library(
523558
name = "get_full_year",
524559
srcs = ["GetFullYearFunction.java"],
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.runtime.standard;
16+
17+
import com.google.common.collect.ImmutableSet;
18+
import com.google.protobuf.Duration;
19+
import dev.cel.common.CelOptions;
20+
import dev.cel.common.internal.ProtoTimeUtils;
21+
import dev.cel.runtime.CelFunctionBinding;
22+
import dev.cel.runtime.RuntimeEquality;
23+
import java.util.Arrays;
24+
25+
/** Standard function for {@code getDays}. */
26+
public final class GetDaysFunction extends CelStandardFunction {
27+
private static final GetDaysFunction ALL_OVERLOADS = create(GetDaysOverload.values());
28+
29+
public static GetDaysFunction create() {
30+
return ALL_OVERLOADS;
31+
}
32+
33+
public static GetDaysFunction create(GetDaysFunction.GetDaysOverload... overloads) {
34+
return create(Arrays.asList(overloads));
35+
}
36+
37+
public static GetDaysFunction create(Iterable<GetDaysFunction.GetDaysOverload> overloads) {
38+
return new GetDaysFunction(ImmutableSet.copyOf(overloads));
39+
}
40+
41+
/** Overloads for the standard function. */
42+
public enum GetDaysOverload implements CelStandardOverload {
43+
DURATION_TO_DAYS(
44+
(celOptions, runtimeEquality) -> {
45+
if (celOptions.evaluateCanonicalTypesToNativeValues()) {
46+
return CelFunctionBinding.from(
47+
"duration_to_days", java.time.Duration.class, java.time.Duration::toDays);
48+
} else {
49+
return CelFunctionBinding.from(
50+
"duration_to_days", Duration.class, ProtoTimeUtils::toDays);
51+
}
52+
}),
53+
;
54+
55+
private final CelStandardOverload standardOverload;
56+
57+
@Override
58+
public CelFunctionBinding newFunctionBinding(
59+
CelOptions celOptions, RuntimeEquality runtimeEquality) {
60+
return standardOverload.newFunctionBinding(celOptions, runtimeEquality);
61+
}
62+
63+
GetDaysOverload(CelStandardOverload standardOverload) {
64+
this.standardOverload = standardOverload;
65+
}
66+
}
67+
68+
private GetDaysFunction(ImmutableSet<CelStandardOverload> overloads) {
69+
super("getDays", overloads);
70+
}
71+
}

runtime/standard/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ cel_android_library(
156156
exports = ["//runtime/src/main/java/dev/cel/runtime/standard:get_date_android"],
157157
)
158158

159+
java_library(
160+
name = "get_days",
161+
exports = ["//runtime/src/main/java/dev/cel/runtime/standard:get_days"],
162+
)
163+
164+
cel_android_library(
165+
name = "get_days_android",
166+
exports = ["//runtime/src/main/java/dev/cel/runtime/standard:get_days_android"],
167+
)
168+
159169
java_library(
160170
name = "get_full_year",
161171
exports = ["//runtime/src/main/java/dev/cel/runtime/standard:get_full_year"],

0 commit comments

Comments
 (0)