github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/timelib/mysql_timestamp.go (about)

     1  package timelib
     2  
     3  import (
     4  	"database/sql"
     5  	"database/sql/driver"
     6  	"encoding"
     7  	"fmt"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  var (
    13  	MySQLTimestampZero     = MySQLTimestamp(time.Time{})
    14  	MySQLTimestampUnixZero = MySQLTimestamp(time.Unix(0, 0))
    15  )
    16  
    17  // swagger:strfmt date-time
    18  type MySQLTimestamp time.Time
    19  
    20  func ParseMySQLTimestampFromString(s string) (dt MySQLTimestamp, err error) {
    21  	var t time.Time
    22  	t, err = time.Parse(time.RFC3339, s)
    23  	dt = MySQLTimestamp(t)
    24  	return
    25  }
    26  
    27  var _ interface {
    28  	sql.Scanner
    29  	driver.Valuer
    30  } = (*MySQLTimestamp)(nil)
    31  
    32  func (dt *MySQLTimestamp) Scan(value interface{}) error {
    33  	switch v := value.(type) {
    34  	case []byte:
    35  		n, err := strconv.ParseInt(string(v), 10, 64)
    36  		if err != nil {
    37  			return fmt.Errorf("sql.Scan() strfmt.MySQLTimestamp from: %#v failed: %s", v, err.Error())
    38  		}
    39  		*dt = MySQLTimestamp(time.Unix(n, 0))
    40  	case int64:
    41  		if v < 0 {
    42  			*dt = MySQLTimestamp{}
    43  		} else {
    44  			*dt = MySQLTimestamp(time.Unix(v, 0))
    45  		}
    46  	case float64:
    47  		if v < 0 {
    48  			*dt = MySQLTimestamp{}
    49  		} else {
    50  			*dt = MySQLTimestamp(time.Unix(int64(v), 0))
    51  		}
    52  	case nil:
    53  		*dt = MySQLTimestampZero
    54  	default:
    55  		return fmt.Errorf("cannot sql.Scan() strfmt.MySQLTimestamp from: %#v", v)
    56  	}
    57  	return nil
    58  }
    59  
    60  func (dt MySQLTimestamp) Value() (driver.Value, error) {
    61  	return (time.Time)(dt).Unix(), nil
    62  }
    63  
    64  func (dt MySQLTimestamp) String() string {
    65  	return time.Time(dt).In(CST).Format(time.RFC3339)
    66  }
    67  
    68  func (dt MySQLTimestamp) Format(layout string) string {
    69  	return time.Time(dt).In(CST).Format(layout)
    70  }
    71  
    72  var _ interface {
    73  	encoding.TextMarshaler
    74  	encoding.TextUnmarshaler
    75  } = (*MySQLTimestamp)(nil)
    76  
    77  func (dt MySQLTimestamp) MarshalText() ([]byte, error) {
    78  	if dt.IsZero() {
    79  		return []byte(""), nil
    80  	}
    81  	str := dt.String()
    82  	return []byte(str), nil
    83  }
    84  
    85  func (dt *MySQLTimestamp) UnmarshalText(data []byte) (err error) {
    86  	str := string(data)
    87  	if len(str) > 1 {
    88  		if str[0] == '"' && str[len(str)-1] == '"' {
    89  			str = str[1 : len(str)-1]
    90  		}
    91  	}
    92  	if len(str) == 0 || str == "0" {
    93  		str = MySQLDatetimeZero.String()
    94  	}
    95  	*dt, err = ParseMySQLTimestampFromString(str)
    96  	return
    97  }
    98  
    99  func (dt MySQLTimestamp) Unix() int64 {
   100  	return time.Time(dt).Unix()
   101  }
   102  
   103  func (dt MySQLTimestamp) IsZero() bool {
   104  	unix := dt.Unix()
   105  	return unix == 0 || unix == MySQLTimestampZero.Unix()
   106  }
   107  
   108  func (dt MySQLTimestamp) In(loc *time.Location) MySQLTimestamp {
   109  	return MySQLTimestamp(time.Time(dt).In(loc))
   110  }
   111  
   112  // 获取当天最后一秒(东8区)
   113  func (dt MySQLTimestamp) GetTodayLastSecCST() MySQLTimestamp {
   114  	return MySQLTimestamp(GetTodayLastSecInLocation(time.Time(dt), CST))
   115  }
   116  
   117  // 添加 N 个工作日(东8区)
   118  func (dt MySQLTimestamp) AddWorkingDaysCST(days int) MySQLTimestamp {
   119  	return MySQLTimestamp(AddWorkingDaysInLocation(time.Time(dt), days, CST))
   120  }
   121  
   122  // 获取当天0点(东8区)
   123  func (dt MySQLTimestamp) GetTodayFirstSecCST() MySQLTimestamp {
   124  	return MySQLTimestamp(GetTodayFirstSecInLocation(time.Time(dt), CST))
   125  }