Files
threshold/internal/db/timestamp/scanners.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.3 KiB
Go
Raw Normal View History

2023-02-02 17:15:17 -08:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
package timestamp
2020-05-27 18:16:43 -04:00
import (
"database/sql/driver"
"errors"
"math"
2020-05-27 18:16:43 -04:00
"time"
"google.golang.org/protobuf/types/known/timestamppb"
2020-05-27 18:16:43 -04:00
)
var (
NegativeInfinityTS = time.Date(math.MinInt32, time.January, 1, 0, 0, 0, 0, time.UTC)
PositiveInfinityTS = time.Date(math.MaxInt32, time.December, 31, 23, 59, 59, 1e9-1, time.UTC)
)
// Scan implements sql.Scanner for protobuf Timestamp.
2022-10-25 18:29:38 -05:00
func (ts *Timestamp) Scan(value any) error {
2020-05-27 18:16:43 -04:00
switch t := value.(type) {
case time.Time:
ts.Timestamp = timestamppb.New(t) // google proto version
case string:
switch value {
case "-infinity":
ts.Timestamp = timestamppb.New(NegativeInfinityTS)
case "infinity":
ts.Timestamp = timestamppb.New(PositiveInfinityTS)
}
2020-05-27 18:16:43 -04:00
default:
return errors.New("Not a protobuf Timestamp")
}
return nil
}
// Scan implements driver.Valuer for protobuf Timestamp.
2020-05-27 18:16:43 -04:00
func (ts *Timestamp) Value() (driver.Value, error) {
switch {
case ts == nil:
2020-05-27 18:16:43 -04:00
return nil, nil
case ts.AsTime().Equal(NegativeInfinityTS):
return "-infinity", nil
case ts.AsTime().Equal(PositiveInfinityTS):
return "infinity", nil
default:
return ts.Timestamp.AsTime(), nil
2020-05-27 18:16:43 -04:00
}
}
// GormDataType gorm common data type (required)
func (ts *Timestamp) GormDataType() string {
return "timestamp"
}