package pgtype

import (
	
	
	
	

	
)

const pgTimestampFormat = "2006-01-02 15:04:05.999999999"
Timestamp represents the PostgreSQL timestamp type. The PostgreSQL timestamp does not have a time zone. This presents a problem when translating to and from time.Time which requires a time zone. It is highly recommended to use timestamptz whenever possible. Timestamp methods either convert to UTC or return an error on non-UTC times.
type Timestamp struct {
	Time             time.Time // Time must always be in UTC.
	Status           Status
	InfinityModifier InfinityModifier
}
Set converts src into a Timestamp and stores in dst. If src is a time.Time in a non-UTC time zone, the time zone is discarded.
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", , )
}
DecodeText decodes from src into dst. The decoded time is considered to be in UTC.
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
}
DecodeBinary decodes from src into dst. The decoded time is considered to be in UTC.
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
}
EncodeText writes the text encoding of src into w. If src.Time is not in the UTC time zone it returns an error.
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
}
EncodeBinary writes the binary encoding of src into w. If src.Time is not in the UTC time zone it returns an error.
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
}
Scan implements the database/sql Scanner interface.
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", )
}
Value implements the database/sql/driver Valuer interface.
func ( Timestamp) () (driver.Value, error) {
	switch .Status {
	case Present:
		if .InfinityModifier != None {
			return .InfinityModifier.String(), nil
		}
		return .Time, nil
	case Null:
		return nil, nil
	default:
		return nil, errUndefined
	}