forked from google/cel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp.go
311 lines (285 loc) · 9.45 KB
/
timestamp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/authzed/cel-go/common/overloads"
"github.com/authzed/cel-go/common/types/ref"
anypb "google.golang.org/protobuf/types/known/anypb"
structpb "google.golang.org/protobuf/types/known/structpb"
tpb "google.golang.org/protobuf/types/known/timestamppb"
)
// Timestamp type implementation which supports add, compare, and subtract
// operations. Timestamps are also capable of participating in dynamic
// function dispatch to instance methods.
type Timestamp struct {
time.Time
}
func timestampOf(t time.Time) Timestamp {
// Note that this function does not validate that time.Time is in our supported range.
return Timestamp{Time: t}
}
const (
// The number of seconds between year 1 and year 1970. This is borrowed from
// https://golang.org/src/time/time.go.
unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * (60 * 60 * 24)
// Number of seconds between `0001-01-01T00:00:00Z` and the Unix epoch.
minUnixTime int64 = -62135596800
// Number of seconds between `9999-12-31T23:59:59.999999999Z` and the Unix epoch.
maxUnixTime int64 = 253402300799
)
// Add implements traits.Adder.Add.
func (t Timestamp) Add(other ref.Val) ref.Val {
switch other.Type() {
case DurationType:
return other.(Duration).Add(t)
}
return MaybeNoSuchOverloadErr(other)
}
// Compare implements traits.Comparer.Compare.
func (t Timestamp) Compare(other ref.Val) ref.Val {
if TimestampType != other.Type() {
return MaybeNoSuchOverloadErr(other)
}
ts1 := t.Time
ts2 := other.(Timestamp).Time
switch {
case ts1.Before(ts2):
return IntNegOne
case ts1.After(ts2):
return IntOne
default:
return IntZero
}
}
// ConvertToNative implements ref.Val.ConvertToNative.
func (t Timestamp) ConvertToNative(typeDesc reflect.Type) (any, error) {
// If the timestamp is already assignable to the desired type return it.
if reflect.TypeOf(t.Time).AssignableTo(typeDesc) {
return t.Time, nil
}
if reflect.TypeOf(t).AssignableTo(typeDesc) {
return t, nil
}
switch typeDesc {
case anyValueType:
// Pack the underlying time as a tpb.Timestamp into an Any value.
return anypb.New(tpb.New(t.Time))
case jsonValueType:
// CEL follows the proto3 to JSON conversion which formats as an RFC 3339 encoded JSON
// string.
v := t.ConvertToType(StringType)
if IsError(v) {
return nil, v.(*Err)
}
return structpb.NewStringValue(string(v.(String))), nil
case timestampValueType:
// Unwrap the underlying tpb.Timestamp.
return tpb.New(t.Time), nil
}
return nil, fmt.Errorf("type conversion error from 'Timestamp' to '%v'", typeDesc)
}
// ConvertToType implements ref.Val.ConvertToType.
func (t Timestamp) ConvertToType(typeVal ref.Type) ref.Val {
switch typeVal {
case StringType:
return String(t.Format(time.RFC3339Nano))
case IntType:
// Return the Unix time in seconds since 1970
return Int(t.Unix())
case TimestampType:
return t
case TypeType:
return TimestampType
}
return NewErr("type conversion error from '%s' to '%s'", TimestampType, typeVal)
}
// Equal implements ref.Val.Equal.
func (t Timestamp) Equal(other ref.Val) ref.Val {
otherTime, ok := other.(Timestamp)
return Bool(ok && t.Time.Equal(otherTime.Time))
}
// IsZeroValue returns true if the timestamp is epoch 0.
func (t Timestamp) IsZeroValue() bool {
return t.IsZero()
}
// Receive implements traits.Receiver.Receive.
func (t Timestamp) Receive(function string, overload string, args []ref.Val) ref.Val {
switch len(args) {
case 0:
if f, found := timestampZeroArgOverloads[function]; found {
return f(t.Time)
}
case 1:
if f, found := timestampOneArgOverloads[function]; found {
return f(t.Time, args[0])
}
}
return NoSuchOverloadErr()
}
// Subtract implements traits.Subtractor.Subtract.
func (t Timestamp) Subtract(subtrahend ref.Val) ref.Val {
switch subtrahend.Type() {
case DurationType:
dur := subtrahend.(Duration)
val, err := subtractTimeDurationChecked(t.Time, dur.Duration)
if err != nil {
return WrapErr(err)
}
return timestampOf(val)
case TimestampType:
t2 := subtrahend.(Timestamp).Time
val, err := subtractTimeChecked(t.Time, t2)
if err != nil {
return WrapErr(err)
}
return durationOf(val)
}
return MaybeNoSuchOverloadErr(subtrahend)
}
// Type implements ref.Val.Type.
func (t Timestamp) Type() ref.Type {
return TimestampType
}
// Value implements ref.Val.Value.
func (t Timestamp) Value() any {
return t.Time
}
var (
timestampValueType = reflect.TypeOf(&tpb.Timestamp{})
timestampZeroArgOverloads = map[string]func(time.Time) ref.Val{
overloads.TimeGetFullYear: timestampGetFullYear,
overloads.TimeGetMonth: timestampGetMonth,
overloads.TimeGetDayOfYear: timestampGetDayOfYear,
overloads.TimeGetDate: timestampGetDayOfMonthOneBased,
overloads.TimeGetDayOfMonth: timestampGetDayOfMonthZeroBased,
overloads.TimeGetDayOfWeek: timestampGetDayOfWeek,
overloads.TimeGetHours: timestampGetHours,
overloads.TimeGetMinutes: timestampGetMinutes,
overloads.TimeGetSeconds: timestampGetSeconds,
overloads.TimeGetMilliseconds: timestampGetMilliseconds}
timestampOneArgOverloads = map[string]func(time.Time, ref.Val) ref.Val{
overloads.TimeGetFullYear: timestampGetFullYearWithTz,
overloads.TimeGetMonth: timestampGetMonthWithTz,
overloads.TimeGetDayOfYear: timestampGetDayOfYearWithTz,
overloads.TimeGetDate: timestampGetDayOfMonthOneBasedWithTz,
overloads.TimeGetDayOfMonth: timestampGetDayOfMonthZeroBasedWithTz,
overloads.TimeGetDayOfWeek: timestampGetDayOfWeekWithTz,
overloads.TimeGetHours: timestampGetHoursWithTz,
overloads.TimeGetMinutes: timestampGetMinutesWithTz,
overloads.TimeGetSeconds: timestampGetSecondsWithTz,
overloads.TimeGetMilliseconds: timestampGetMillisecondsWithTz}
)
type timestampVisitor func(time.Time) ref.Val
func timestampGetFullYear(t time.Time) ref.Val {
return Int(t.Year())
}
func timestampGetMonth(t time.Time) ref.Val {
// CEL spec indicates that the month should be 0-based, but the Time value
// for Month() is 1-based.
return Int(t.Month() - 1)
}
func timestampGetDayOfYear(t time.Time) ref.Val {
return Int(t.YearDay() - 1)
}
func timestampGetDayOfMonthZeroBased(t time.Time) ref.Val {
return Int(t.Day() - 1)
}
func timestampGetDayOfMonthOneBased(t time.Time) ref.Val {
return Int(t.Day())
}
func timestampGetDayOfWeek(t time.Time) ref.Val {
return Int(t.Weekday())
}
func timestampGetHours(t time.Time) ref.Val {
return Int(t.Hour())
}
func timestampGetMinutes(t time.Time) ref.Val {
return Int(t.Minute())
}
func timestampGetSeconds(t time.Time) ref.Val {
return Int(t.Second())
}
func timestampGetMilliseconds(t time.Time) ref.Val {
return Int(t.Nanosecond() / 1000000)
}
func timestampGetFullYearWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetFullYear)(t)
}
func timestampGetMonthWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetMonth)(t)
}
func timestampGetDayOfYearWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetDayOfYear)(t)
}
func timestampGetDayOfMonthZeroBasedWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetDayOfMonthZeroBased)(t)
}
func timestampGetDayOfMonthOneBasedWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetDayOfMonthOneBased)(t)
}
func timestampGetDayOfWeekWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetDayOfWeek)(t)
}
func timestampGetHoursWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetHours)(t)
}
func timestampGetMinutesWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetMinutes)(t)
}
func timestampGetSecondsWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetSeconds)(t)
}
func timestampGetMillisecondsWithTz(t time.Time, tz ref.Val) ref.Val {
return timeZone(tz, timestampGetMilliseconds)(t)
}
func timeZone(tz ref.Val, visitor timestampVisitor) timestampVisitor {
return func(t time.Time) ref.Val {
if StringType != tz.Type() {
return MaybeNoSuchOverloadErr(tz)
}
val := string(tz.(String))
ind := strings.Index(val, ":")
if ind == -1 {
loc, err := time.LoadLocation(val)
if err != nil {
return WrapErr(err)
}
return visitor(t.In(loc))
}
// If the input is not the name of a timezone (for example, 'US/Central'), it should be a numerical offset from UTC
// in the format ^(+|-)(0[0-9]|1[0-4]):[0-5][0-9]$. The numerical input is parsed in terms of hours and minutes.
hr, err := strconv.Atoi(string(val[0:ind]))
if err != nil {
return WrapErr(err)
}
min, err := strconv.Atoi(string(val[ind+1:]))
if err != nil {
return WrapErr(err)
}
var offset int
if string(val[0]) == "-" {
offset = hr*60 - min
} else {
offset = hr*60 + min
}
secondsEastOfUTC := int((time.Duration(offset) * time.Minute).Seconds())
timezone := time.FixedZone("", secondsEastOfUTC)
return visitor(t.In(timezone))
}
}