package pgtype
import (
)
const (
jsonISO8601 = "2006-01-02T15:04:05.999999999"
)
type TimestampScanner interface {
ScanTimestamp(v Timestamp) error
}
type TimestampValuer interface {
TimestampValue() (Timestamp, error)
}
type Timestamp struct {
Time time.Time
InfinityModifier InfinityModifier
Valid bool
}
func ( *Timestamp) ( Timestamp) error {
* =
return nil
}
func ( Timestamp) () (Timestamp, error) {
return , nil
}
func ( *Timestamp) ( any) error {
if == nil {
* = Timestamp{}
return nil
}
switch src := .(type) {
case string:
return (&scanPlanTextTimestampToTimestampScanner{}).Scan([]byte(), )
case time.Time:
* = Timestamp{Time: , Valid: true}
return nil
}
return fmt.Errorf("cannot scan %T", )
}
func ( Timestamp) () (driver.Value, error) {
if !.Valid {
return nil, nil
}
if .InfinityModifier != Finite {
return .InfinityModifier.String(), nil
}
return .Time, nil
}
func ( Timestamp) () ([]byte, error) {
if !.Valid {
return []byte("null"), nil
}
var string
switch .InfinityModifier {
case Finite:
= .Time.Format(jsonISO8601)
case Infinity:
= "infinity"
case NegativeInfinity:
= "-infinity"
}
return json.Marshal()
}
func ( *Timestamp) ( []byte) error {
var *string
:= json.Unmarshal(, &)
if != nil {
return
}
if == nil {
* = Timestamp{}
return nil
}
switch * {
case "infinity":
* = Timestamp{Valid: true, InfinityModifier: Infinity}
case "-infinity":
* = Timestamp{Valid: true, InfinityModifier: -Infinity}
default:
:= *
, := time.Parse(time.RFC3339Nano, )
if == nil {
* = Timestamp{Time: , Valid: true}
return nil
}
, = time.ParseInLocation(jsonISO8601, , time.UTC)
if == nil {
* = Timestamp{Time: , Valid: true}
return nil
}
.Valid = false
return fmt.Errorf("cannot unmarshal %s to timestamp with layout %s or %s (%w)",
*, time.RFC3339Nano, jsonISO8601, )
}
return nil
}
type TimestampCodec struct {
ScanLocation *time.Location
}
func (*TimestampCodec) ( int16) bool {
return == TextFormatCode || == BinaryFormatCode
}
func (*TimestampCodec) () int16 {
return BinaryFormatCode
}
func (*TimestampCodec) ( *Map, uint32, int16, any) EncodePlan {
if , := .(TimestampValuer); ! {
return nil
}
switch {
case BinaryFormatCode:
return encodePlanTimestampCodecBinary{}
case TextFormatCode:
return encodePlanTimestampCodecText{}
}
return nil
}
type encodePlanTimestampCodecBinary struct{}
func (encodePlanTimestampCodecBinary) ( any, []byte) ( []byte, error) {
, := .(TimestampValuer).TimestampValue()
if != nil {
return nil,
}
if !.Valid {
return nil, nil
}
var int64
switch .InfinityModifier {
case Finite:
:= discardTimeZone(.Time)
:= .Unix()*1_000_000 + int64(.Nanosecond())/1000
= - microsecFromUnixEpochToY2K
case Infinity:
= infinityMicrosecondOffset
case NegativeInfinity:
= negativeInfinityMicrosecondOffset
}
= pgio.AppendInt64(, )
return , nil
}
type encodePlanTimestampCodecText struct{}
func (encodePlanTimestampCodecText) ( any, []byte) ( []byte, error) {
, := .(TimestampValuer).TimestampValue()
if != nil {
return nil,
}
if !.Valid {
return nil, nil
}
switch .InfinityModifier {
case Finite:
= pgdatetime.AppendTimestamp(, .Time, "")
case Infinity:
= append(, "infinity"...)
case NegativeInfinity:
= append(, "-infinity"...)
}
return , nil
}
func ( time.Time) time.Time {
if .Location() != time.UTC {
return time.Date(.Year(), .Month(), .Day(), .Hour(), .Minute(), .Second(), .Nanosecond(), time.UTC)
}
return
}
func ( *TimestampCodec) ( *Map, uint32, int16, any) ScanPlan {
switch {
case BinaryFormatCode:
if , := .(TimestampScanner); {
return &scanPlanBinaryTimestampToTimestampScanner{location: .ScanLocation}
}
case TextFormatCode:
if , := .(TimestampScanner); {
return &scanPlanTextTimestampToTimestampScanner{location: .ScanLocation}
}
}
return nil
}
type scanPlanBinaryTimestampToTimestampScanner struct{ location *time.Location }
func ( *scanPlanBinaryTimestampToTimestampScanner) ( []byte, any) error {
:= .(TimestampScanner)
if == nil {
return .ScanTimestamp(Timestamp{})
}
, := pgio.Uint64Exact()
if != nil {
return fmt.Errorf("timestamp: %w", )
}
var Timestamp
:= int64()
switch {
case infinityMicrosecondOffset:
= Timestamp{Valid: true, InfinityModifier: Infinity}
case negativeInfinityMicrosecondOffset:
= Timestamp{Valid: true, InfinityModifier: -Infinity}
default:
:= time.Unix(
microsecFromUnixEpochToY2K/1_000_000+/1_000_000,
(microsecFromUnixEpochToY2K%1_000_000*1_000)+(%1_000_000*1000),
).UTC()
if .Before(minDateTime) || !.Before(endTimestamp) {
return fmt.Errorf("timestamp %d microseconds from 2000-01-01 is out of range", )
}
if .location != nil {
= time.Date(.Year(), .Month(), .Day(), .Hour(), .Minute(), .Second(), .Nanosecond(), .location)
}
= Timestamp{Time: , Valid: true}
}
return .ScanTimestamp()
}
type scanPlanTextTimestampToTimestampScanner struct{ location *time.Location }
func ( *scanPlanTextTimestampToTimestampScanner) ( []byte, any) error {
:= .(TimestampScanner)
if == nil {
return .ScanTimestamp(Timestamp{})
}
, := parseTextDateTime()
if != nil {
return
}
var Timestamp
if .infinity != Finite {
= Timestamp{Valid: true, InfinityModifier: .infinity}
} else {
if !.hasTime || .hasOffset {
return badDateTime()
}
, := .toTime(, "timestamp", endTimestamp)
if != nil {
return
}
if .location != nil {
= .in(.location)
}
= Timestamp{Time: , Valid: true}
}
return .ScanTimestamp()
}
func ( *TimestampCodec) ( *Map, uint32, int16, []byte) (driver.Value, error) {
if == nil {
return nil, nil
}
var Timestamp
:= codecScan(, , , , , &)
if != nil {
return nil,
}
if .InfinityModifier != Finite {
return .InfinityModifier.String(), nil
}
return .Time, nil
}
func ( *TimestampCodec) ( *Map, uint32, int16, []byte) (any, error) {
if == nil {
return nil, nil
}
var Timestamp
:= codecScan(, , , , , &)
if != nil {
return nil,
}
if .InfinityModifier != Finite {
return .InfinityModifier, nil
}
return .Time, nil
}