Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ import (
// duration("42s").round(duration("10s")) // return "40s"
// now.round(duration("1h")) // return "2022-03-30T11:00:00Z"
//
// # Truncate
//
// Truncate returns the timestamp or duration truncated down to the nearest multiple of the
// duration parameter using [time.Time.Truncate] for timestamps and [time.Duration.Truncate]
// for durations:
//
// <timestamp>.truncate(<duration>) -> <timestamp>
// <duration>.truncate(<duration>) -> <duration>
//
// Examples:
//
// duration("42s").truncate(duration("10s")) // return "40s"
// now.truncate(duration("1h")) // return "2022-03-30T11:00:00Z"
//
// # Global Variables
//
// A collection of global variable are provided to give access to the start
Expand Down Expand Up @@ -176,6 +190,20 @@ func (timeLib) CompileOptions() []cel.EnvOption {
cel.BinaryBinding(roundTimestamp),
),
),
cel.Function("truncate",
cel.MemberOverload(
"duration_truncate_duration_duration",
[]*cel.Type{cel.DurationType, cel.DurationType},
cel.DurationType,
cel.BinaryBinding(truncateDuration),
),
cel.MemberOverload(
"time_truncate_duration_time",
[]*cel.Type{cel.TimestampType, cel.DurationType},
cel.TimestampType,
cel.BinaryBinding(truncateTimestamp),
),
),
}
}

Expand Down Expand Up @@ -289,3 +317,27 @@ func roundTimestamp(arg, layout ref.Val) ref.Val {
}
return types.Timestamp{Time: t.Time.Round(res.Duration)}
}

func truncateDuration(arg, layout ref.Val) ref.Val {
d, ok := arg.(types.Duration)
if !ok {
return types.ValOrErr(d, "no such overload for duration truncate: %s", arg.Type())
}
res, ok := layout.(types.Duration)
if !ok {
return types.ValOrErr(res, "no such overload for duration truncate: %s", layout.Type())
}
return types.Duration{Duration: d.Duration.Truncate(res.Duration)}
}

func truncateTimestamp(arg, layout ref.Val) ref.Val {
t, ok := arg.(types.Timestamp)
if !ok {
return types.ValOrErr(t, "no such overload for timestamp truncate: %s", arg.Type())
}
res, ok := layout.(types.Duration)
if !ok {
return types.ValOrErr(res, "no such overload for timestamp truncate: %s", layout.Type())
}
return types.Timestamp{Time: t.Time.Truncate(res.Duration)}
}
14 changes: 14 additions & 0 deletions testdata/time_truncate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mito -use time src.cel
! stderr .
cmp stdout want.txt

-- src.cel --
{
"47s": duration("47s").truncate(duration("10s")),
"RFC3339": "2006-01-02T15:54:05Z".parse_time(time_layout.RFC3339).truncate(duration("1h")),
}
-- want.txt --
{
"47s": "40s",
"RFC3339": "2006-01-02T15:00:00Z"
}
Loading