package pgtype

import (
	
	
	
	
	
	

	
)

const (
	microsecondsPerSecond = 1000000
	microsecondsPerMinute = 60 * microsecondsPerSecond
	microsecondsPerHour   = 60 * microsecondsPerMinute
	microsecondsPerDay    = 24 * microsecondsPerHour
	microsecondsPerMonth  = 30 * microsecondsPerDay
)

type Interval struct {
	Microseconds int64
	Days         int32
	Months       int32
	Status       Status
}

func ( *Interval) ( interface{}) error {
	if  == nil {
		* = Interval{Status: Null}
		return nil
	}

	if ,  := .(interface{ () interface{} });  {
		 := .()
		if  !=  {
			return .()
		}
	}

	switch value := .(type) {
	case time.Duration:
		* = Interval{Microseconds: int64() / 1000, Status: Present}
	default:
		if ,  := underlyingPtrType();  {
			return .()
		}
		return fmt.Errorf("cannot convert %v to Interval", )
	}

	return nil
}

func ( Interval) () interface{} {
	switch .Status {
	case Present:
		return 
	case Null:
		return nil
	default:
		return .Status
	}
}

func ( *Interval) ( interface{}) error {
	switch .Status {
	case Present:
		switch v := .(type) {
		case *time.Duration:
			 := int64(.Months)*microsecondsPerMonth + int64(.Days)*microsecondsPerDay + .Microseconds
			* = time.Duration() * time.Microsecond
			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 ( *Interval) ( *ConnInfo,  []byte) error {
	if  == nil {
		* = Interval{Status: Null}
		return nil
	}

	var  int64
	var  int32
	var  int32

	 := strings.Split(string(), " ")

	for  := 0;  < len()-1;  += 2 {
		,  := strconv.ParseInt([], 10, 64)
		if  != nil {
			return fmt.Errorf("bad interval format")
		}

		switch [+1] {
		case "year", "years":
			 += int32( * 12)
		case "mon", "mons":
			 += int32()
		case "day", "days":
			 = int32()
		}
	}

	if len()%2 == 1 {
		 := strings.SplitN([len()-1], ":", 3)
		if len() != 3 {
			return fmt.Errorf("bad interval format")
		}

		var  bool
		if [0][0] == '-' {
			 = true
			[0] = [0][1:]
		}

		,  := strconv.ParseInt([0], 10, 64)
		if  != nil {
			return fmt.Errorf("bad interval hour format: %s", [0])
		}

		,  := strconv.ParseInt([1], 10, 64)
		if  != nil {
			return fmt.Errorf("bad interval minute format: %s", [1])
		}

		 := strings.SplitN([2], ".", 2)

		,  := strconv.ParseInt([0], 10, 64)
		if  != nil {
			return fmt.Errorf("bad interval second format: %s", [0])
		}

		var  int64
		if len() == 2 {
			,  = strconv.ParseInt([1], 10, 64)
			if  != nil {
				return fmt.Errorf("bad interval decimal format: %s", [1])
			}

			for  := 0;  < 6-len([1]); ++ {
				 *= 10
			}
		}

		 =  * microsecondsPerHour
		 +=  * microsecondsPerMinute
		 +=  * microsecondsPerSecond
		 += 

		if  {
			 = -
		}
	}

	* = Interval{Months: , Days: , Microseconds: , Status: Present}
	return nil
}

func ( *Interval) ( *ConnInfo,  []byte) error {
	if  == nil {
		* = Interval{Status: Null}
		return nil
	}

	if len() != 16 {
		return fmt.Errorf("Received an invalid size for a interval: %d", len())
	}

	 := int64(binary.BigEndian.Uint64())
	 := int32(binary.BigEndian.Uint32([8:]))
	 := int32(binary.BigEndian.Uint32([12:]))

	* = Interval{Microseconds: , Days: , Months: , Status: Present}
	return nil
}

func ( Interval) ( *ConnInfo,  []byte) ([]byte, error) {
	switch .Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	if .Months != 0 {
		 = append(, strconv.FormatInt(int64(.Months), 10)...)
		 = append(, " mon "...)
	}

	if .Days != 0 {
		 = append(, strconv.FormatInt(int64(.Days), 10)...)
		 = append(, " day "...)
	}

	 := .Microseconds
	if  < 0 {
		 = -
		 = append(, '-')
	}

	 :=  / microsecondsPerHour
	 := ( % microsecondsPerHour) / microsecondsPerMinute
	 := ( % microsecondsPerMinute) / microsecondsPerSecond
	 :=  % microsecondsPerSecond

	 := fmt.Sprintf("%02d:%02d:%02d.%06d", , , , )
	return append(, ...), nil
}
EncodeBinary encodes src into w.
func ( Interval) ( *ConnInfo,  []byte) ([]byte, error) {
	switch .Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	 = pgio.AppendInt64(, .Microseconds)
	 = pgio.AppendInt32(, .Days)
	return pgio.AppendInt32(, .Months), nil
}
Scan implements the database/sql Scanner interface.
func ( *Interval) ( interface{}) error {
	if  == nil {
		* = Interval{Status: Null}
		return nil
	}

	switch src := .(type) {
	case string:
		return .DecodeText(nil, []byte())
	case []byte:
		 := make([]byte, len())
		copy(, )
		return .DecodeText(nil, )
	}

	return fmt.Errorf("cannot scan %T", )
}
Value implements the database/sql/driver Valuer interface.
func ( Interval) () (driver.Value, error) {
	return EncodeValueText()