42 lines
713 B
Go
42 lines
713 B
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"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) {
|
||
|
s := strings.Trim(string(b), `"`)
|
||
|
loc, _ := time.LoadLocation("Europe/Madrid")
|
||
|
nt, err := time.ParseInLocation(jsTimeLayout, s, loc)
|
||
|
*ct = JSTime(nt)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (ct JSTime) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(ct.String()), nil
|
||
|
}
|
||
|
|
||
|
func (ct JSTime) String() string {
|
||
|
t := time.Time(ct)
|
||
|
return fmt.Sprintf("%q", t.Format(jsTimeLayout))
|
||
|
}
|
||
|
|
||
|
func (ct JSTime) Unix() int64 {
|
||
|
return time.Time(ct).Unix()
|
||
|
}
|
||
|
|
||
|
func (ct JSTime) Local() JSTime {
|
||
|
return JSTime(time.Time(ct).Local())
|
||
|
}
|