github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/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 func ParseMySQLTimestampFromStringWithLayout(input, layout string) (MySQLTimestamp, error) { 28 t, err := time.ParseInLocation(layout, input, CST) 29 if err != nil { 30 return MySQLTimestampUnixZero, err 31 } 32 return MySQLTimestamp(t), nil 33 } 34 35 var _ interface { 36 sql.Scanner 37 driver.Valuer 38 } = (*MySQLTimestamp)(nil) 39 40 func (dt *MySQLTimestamp) Scan(value interface{}) error { 41 switch v := value.(type) { 42 case []byte: 43 n, err := strconv.ParseInt(string(v), 10, 64) 44 if err != nil { 45 return fmt.Errorf("sql.Scan() strfmt.MySQLTimestamp from: %#v failed: %s", v, err.Error()) 46 } 47 *dt = MySQLTimestamp(time.Unix(n, 0)) 48 case int64: 49 if v < 0 { 50 *dt = MySQLTimestamp{} 51 } else { 52 *dt = MySQLTimestamp(time.Unix(v, 0)) 53 } 54 case nil: 55 *dt = MySQLTimestampZero 56 default: 57 return fmt.Errorf("cannot sql.Scan() strfmt.MySQLTimestamp from: %#v", v) 58 } 59 return nil 60 } 61 62 func (dt MySQLTimestamp) Value() (driver.Value, error) { 63 return (time.Time)(dt).Unix(), nil 64 } 65 66 func (dt MySQLTimestamp) String() string { 67 return time.Time(dt).In(CST).Format(time.RFC3339) 68 } 69 70 func (dt MySQLTimestamp) Format(layout string) string { 71 return time.Time(dt).In(CST).Format(layout) 72 } 73 74 var _ interface { 75 encoding.TextMarshaler 76 encoding.TextUnmarshaler 77 } = (*MySQLTimestamp)(nil) 78 79 func (dt MySQLTimestamp) MarshalText() ([]byte, error) { 80 if dt.IsZero() { 81 return []byte(""), nil 82 } 83 str := dt.String() 84 return []byte(str), nil 85 } 86 87 func (dt *MySQLTimestamp) UnmarshalText(data []byte) (err error) { 88 str := string(data) 89 if len(str) > 1 { 90 if str[0] == '"' && str[len(str)-1] == '"' { 91 str = str[1 : len(str)-1] 92 } 93 } 94 if len(str) == 0 || str == "0" { 95 str = MySQLDatetimeZero.String() 96 } 97 *dt, err = ParseMySQLTimestampFromString(str) 98 return 99 } 100 101 func (dt MySQLTimestamp) Unix() int64 { 102 return time.Time(dt).Unix() 103 } 104 105 func (dt MySQLTimestamp) IsZero() bool { 106 unix := dt.Unix() 107 return unix == 0 || unix == MySQLTimestampZero.Unix() 108 } 109 110 func (dt MySQLTimestamp) In(loc *time.Location) MySQLTimestamp { 111 return MySQLTimestamp(time.Time(dt).In(loc)) 112 } 113 114 // 获取当天最后一秒(东8区) 115 func (dt MySQLTimestamp) GetTodayLastSecCST() MySQLTimestamp { 116 return MySQLTimestamp(GetTodayLastSecInLocation(time.Time(dt), CST)) 117 } 118 119 // 添加 N 个工作日(东8区) 120 func (dt MySQLTimestamp) AddWorkingDaysCST(days int) MySQLTimestamp { 121 return MySQLTimestamp(AddWorkingDaysInLocation(time.Time(dt), days, CST)) 122 } 123 124 // 获取当天0点(东8区) 125 func (dt MySQLTimestamp) GetTodayFirstSecCST() MySQLTimestamp { 126 return MySQLTimestamp(GetTodayFirstSecInLocation(time.Time(dt), CST)) 127 } 128 129 // 获取当天最后一秒(当前时区) 130 func (dt MySQLTimestamp) GetTodayLastSec() MySQLTimestamp { 131 t := time.Time(dt) 132 return MySQLTimestamp(GetTodayLastSecInLocation(t, t.Location())) 133 } 134 135 // 添加 N 个工作日(当前时区) 136 func (dt MySQLTimestamp) AddWorkingDays(days int) MySQLTimestamp { 137 t := time.Time(dt) 138 return MySQLTimestamp(AddWorkingDaysInLocation(t, days, t.Location())) 139 } 140 141 // 获取当天0点(当前时区) 142 func (dt MySQLTimestamp) GetTodayFirstSec() MySQLTimestamp { 143 t := time.Time(dt) 144 return MySQLTimestamp(GetTodayFirstSecInLocation(t, t.Location())) 145 }