47 lines
829 B
Go
47 lines
829 B
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type (
|
|
JSTime time.Time
|
|
)
|
|
|
|
const (
|
|
jsTimeLite = "2006-01-02"
|
|
jsTimeLayout = "2006-01-02 15:04:05"
|
|
)
|
|
|
|
func (ct JSTime) UnmarshalJSON(b []byte) (err error) {
|
|
return ct.UnmarshalParam(string(b))
|
|
}
|
|
|
|
func (ct JSTime) UnmarshalParam(param string) (err error) {
|
|
s := strings.Trim(param, `"`)
|
|
loc, _ := time.LoadLocation("Europe/Madrid")
|
|
nt, err := time.ParseInLocation(jsTimeLite, s, loc)
|
|
ct = JSTime(nt)
|
|
return
|
|
}
|
|
|
|
func (ct JSTime) MarshalJSON() ([]byte, error) {
|
|
return []byte(strconv.Quote(ct.String())), nil
|
|
}
|
|
|
|
func (ct JSTime) String() string {
|
|
t := time.Time(ct)
|
|
return fmt.Sprintf("%s", t.Format(jsTimeLite))
|
|
}
|
|
|
|
func (ct JSTime) Unix() int64 {
|
|
return time.Time(ct).Unix()
|
|
}
|
|
|
|
func (ct JSTime) Local() JSTime {
|
|
return JSTime(time.Time(ct).Local())
|
|
}
|