Source File
timestamp.go
Belonging Package
github.com/jackc/pgtype
package pgtype
import (
)
const pgTimestampFormat = "2006-01-02 15:04:05.999999999"
type Timestamp struct {
Time time.Time // Time must always be in UTC.
Status Status
InfinityModifier InfinityModifier
}
func ( *Timestamp) ( interface{}) error {
if == nil {
* = Timestamp{Status: Null}
return nil
}
if , := .(interface{ () interface{} }); {
:= .()
if != {
return .()
}
}
switch value := .(type) {
case time.Time:
* = Timestamp{Time: time.Date(.Year(), .Month(), .Day(), .Hour(), .Minute(), .Second(), .Nanosecond(), time.UTC), Status: Present}
case *time.Time:
if == nil {
* = Timestamp{Status: Null}
} else {
return .(*)
}
case InfinityModifier:
* = Timestamp{InfinityModifier: , Status: Present}
default:
if , := underlyingTimeType(); {
return .()
}
return fmt.Errorf("cannot convert %v to Timestamp", )
}
return nil
}
func ( Timestamp) () interface{} {
switch .Status {
case Present:
if .InfinityModifier != None {
return .InfinityModifier
}
return .Time
case Null:
return nil
default:
return .Status
}
}
func ( *Timestamp) ( interface{}) error {
switch .Status {
case Present:
switch v := .(type) {
case *time.Time:
if .InfinityModifier != None {
return fmt.Errorf("cannot assign %v to %T", , )
}
* = .Time
return nil
default:
if , := GetAssignToDstType(); {
return .()
}
return fmt.Errorf("unable to assign to %T", )
}
case Null:
return NullAssignTo()
}
return fmt.Errorf("cannot decode %#v into %T", , )
}
func ( *Timestamp) ( *ConnInfo, []byte) error {
if == nil {
* = Timestamp{Status: Null}
return nil
}
:= string()
switch {
case "infinity":
* = Timestamp{Status: Present, InfinityModifier: Infinity}
case "-infinity":
* = Timestamp{Status: Present, InfinityModifier: -Infinity}
default:
, := time.Parse(pgTimestampFormat, )
if != nil {
return
}
* = Timestamp{Time: , Status: Present}
}
return nil
}
func ( *Timestamp) ( *ConnInfo, []byte) error {
if == nil {
* = Timestamp{Status: Null}
return nil
}
if len() != 8 {
return fmt.Errorf("invalid length for timestamp: %v", len())
}
:= int64(binary.BigEndian.Uint64())
switch {
case infinityMicrosecondOffset:
* = Timestamp{Status: Present, InfinityModifier: Infinity}
case negativeInfinityMicrosecondOffset:
* = Timestamp{Status: Present, InfinityModifier: -Infinity}
default:
:= microsecFromUnixEpochToY2K +
:= time.Unix(/1000000, (%1000000)*1000).UTC()
* = Timestamp{Time: , Status: Present}
}
return nil
}
func ( Timestamp) ( *ConnInfo, []byte) ([]byte, error) {
switch .Status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
if .Time.Location() != time.UTC {
return nil, fmt.Errorf("cannot encode non-UTC time into timestamp")
}
var string
switch .InfinityModifier {
case None:
= .Time.Truncate(time.Microsecond).Format(pgTimestampFormat)
case Infinity:
= "infinity"
case NegativeInfinity:
= "-infinity"
}
return append(, ...), nil
}
func ( Timestamp) ( *ConnInfo, []byte) ([]byte, error) {
switch .Status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
if .Time.Location() != time.UTC {
return nil, fmt.Errorf("cannot encode non-UTC time into timestamp")
}
var int64
switch .InfinityModifier {
case None:
:= .Time.Unix()*1000000 + int64(.Time.Nanosecond())/1000
= - microsecFromUnixEpochToY2K
case Infinity:
= infinityMicrosecondOffset
case NegativeInfinity:
= negativeInfinityMicrosecondOffset
}
return pgio.AppendInt64(, ), nil
}
func ( *Timestamp) ( interface{}) error {
if == nil {
* = Timestamp{Status: Null}
return nil
}
switch src := .(type) {
case string:
return .DecodeText(nil, []byte())
case []byte:
:= make([]byte, len())
copy(, )
return .DecodeText(nil, )
case time.Time:
* = Timestamp{Time: , Status: Present}
return nil
}
return fmt.Errorf("cannot scan %T", )
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |