muse/storage/jstime.go

50 lines
907 B
Go

package storage
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type (
JSTime time.Time
)
const (
jsTimeLite = "2006-01-02"
jsTimeLayout = "2006-01-02 15:04:05"
)
func (ct *JSTime) UnmarshalJSON(blob []byte) (err error) {
var jsonString string
if err = json.Unmarshal(blob, &jsonString); err == nil {
err = ct.UnmarshalParam(jsonString)
}
return
}
func (ct *JSTime) UnmarshalParam(param string) (err error) {
loc, _ := time.LoadLocation("Europe/Madrid")
nt, err := time.ParseInLocation(jsTimeLite, param, 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())
}