Skip to content

Commit a3c89d3

Browse files
committed
use UnixMilli() / UnixMicro() to avoid UnixNano limits
... when we are not needing nanosecond accuracy anyhow.
1 parent 5bf0b76 commit a3c89d3

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

expr/builtins/cast.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ func toIntEval(ctx expr.EvalContext, vals []value.Value) (value.Value, bool) {
144144

145145
switch val := vals[0].(type) {
146146
case value.TimeValue:
147-
iv := val.Val().UnixNano() / 1e6 // Milliseconds
148-
return value.NewIntValue(iv), true
147+
return value.NewIntValue(val.Int()), true
149148
case value.NumberValue:
150149
return value.NewIntValue(val.Int()), true
151150
case value.IntValue:

expr/builtins/time.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,16 @@ func timeTruncEvalTwo(ctx expr.EvalContext, args []value.Value) (value.Value, bo
523523
switch itemT := args[0].(type) {
524524
case value.TimeValue:
525525
// Get full Unix timestamps w/ microseconds and milliseconds.
526-
microTs = itemT.Time().In(time.UTC).UnixNano() / 1e3
526+
microTs = itemT.Time().In(time.UTC).UnixMicro()
527527
milliTs = itemT.Int()
528528
default:
529529
// If not a TimeValue, convert to a TimeValue and get Unix w/ milliseconds.
530530
t, ok := value.ValueToTime(args[0])
531531
if !ok || t.IsZero() {
532532
return value.NewStringValue(""), false
533533
}
534-
microTs = t.UnixNano() / 1e3
535-
milliTs = microTs / 1e3
534+
microTs = t.UnixMicro()
535+
milliTs = t.UnixMilli()
536536
}
537537

538538
// Look at the seconds argument to determine the truncation.

value/value.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,11 +927,11 @@ func (m TimeValue) Err() bool { return false }
927927
func (m TimeValue) Type() ValueType { return TimeType }
928928
func (m TimeValue) Value() any { return m.v }
929929
func (m TimeValue) Val() time.Time { return m.v }
930+
func (m TimeValue) Time() time.Time { return m.v }
930931
func (m TimeValue) MarshalJSON() ([]byte, error) { return json.Marshal(m.v) }
931932
func (m TimeValue) ToString() string { return strconv.FormatInt(m.Int(), 10) }
932-
func (m TimeValue) Float() float64 { return float64(m.v.In(time.UTC).UnixNano() / 1e6) }
933-
func (m TimeValue) Int() int64 { return m.v.In(time.UTC).UnixNano() / 1e6 }
934-
func (m TimeValue) Time() time.Time { return m.v }
933+
func (m TimeValue) Float() float64 { return float64(m.Int()) }
934+
func (m TimeValue) Int() int64 { return m.v.In(time.UTC).UnixMilli() }
935935

936936
func NewErrorValue(v error) ErrorValue {
937937
return ErrorValue{v: v}

0 commit comments

Comments
 (0)