github.com/eden-framework/sqlx@v0.0.2/datatypes/timestamp.go (about)

     1  package datatypes
     2  
     3  import (
     4  	"database/sql"
     5  	"database/sql/driver"
     6  	"encoding"
     7  	"fmt"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  var (
    13  	UTC               = time.UTC
    14  	CST               = time.FixedZone("CST", 8*60*60)
    15  	TimestampZero     = Timestamp(time.Time{})
    16  	TimestampUnixZero = Timestamp(time.Unix(0, 0))
    17  )
    18  
    19  type MySQLTimestamp = Timestamp
    20  
    21  // openapi:strfmt date-time
    22  type Timestamp time.Time
    23  
    24  func (dt *Timestamp) DataType(engine string) string {
    25  	return "bigint"
    26  }
    27  
    28  func ParseTimestampFromString(s string) (dt Timestamp, err error) {
    29  	var t time.Time
    30  	t, err = time.Parse(time.RFC3339, s)
    31  	dt = Timestamp(t)
    32  	return
    33  }
    34  
    35  func ParseTimestampFromStringWithLayout(input, layout string) (Timestamp, error) {
    36  	t, err := time.ParseInLocation(layout, input, CST)
    37  	if err != nil {
    38  		return TimestampUnixZero, err
    39  	}
    40  	return Timestamp(t), nil
    41  }
    42  
    43  var _ interface {
    44  	sql.Scanner
    45  	driver.Valuer
    46  } = (*Timestamp)(nil)
    47  
    48  func (dt *Timestamp) Scan(value interface{}) error {
    49  	switch v := value.(type) {
    50  	case []byte:
    51  		n, err := strconv.ParseInt(string(v), 10, 64)
    52  		if err != nil {
    53  			return fmt.Errorf("sql.Scan() strfmt.Timestamp from: %#v failed: %s", v, err.Error())
    54  		}
    55  		*dt = Timestamp(time.Unix(n, 0))
    56  	case int64:
    57  		if v < 0 {
    58  			*dt = Timestamp{}
    59  		} else {
    60  			*dt = Timestamp(time.Unix(v, 0))
    61  		}
    62  	case nil:
    63  		*dt = TimestampZero
    64  	default:
    65  		return fmt.Errorf("cannot sql.Scan() strfmt.Timestamp from: %#v", v)
    66  	}
    67  	return nil
    68  }
    69  
    70  func (dt Timestamp) Value() (driver.Value, error) {
    71  	s := (time.Time)(dt).Unix()
    72  	if s < 0 {
    73  		s = 0
    74  	}
    75  	return s, nil
    76  }
    77  
    78  func (dt Timestamp) String() string {
    79  	if dt.IsZero() {
    80  		return ""
    81  	}
    82  	return time.Time(dt).In(CST).Format(time.RFC3339)
    83  }
    84  
    85  func (dt Timestamp) Format(layout string) string {
    86  	return time.Time(dt).In(CST).Format(layout)
    87  }
    88  
    89  var _ interface {
    90  	encoding.TextMarshaler
    91  	encoding.TextUnmarshaler
    92  } = (*Timestamp)(nil)
    93  
    94  func (dt Timestamp) MarshalText() ([]byte, error) {
    95  	return []byte(dt.String()), nil
    96  }
    97  
    98  func (dt *Timestamp) UnmarshalText(data []byte) (err error) {
    99  	str := string(data)
   100  	if len(str) == 0 || str == "0" {
   101  		return nil
   102  	}
   103  	*dt, err = ParseTimestampFromString(str)
   104  	return
   105  }
   106  
   107  func (dt Timestamp) Unix() int64 {
   108  	return time.Time(dt).Unix()
   109  }
   110  
   111  func (dt Timestamp) IsZero() bool {
   112  	unix := dt.Unix()
   113  	return unix == 0 || unix == TimestampZero.Unix()
   114  }
   115  
   116  func (dt Timestamp) In(loc *time.Location) Timestamp {
   117  	return Timestamp(time.Time(dt).In(loc))
   118  }