Files
isp-config-service/entity/xtypes/time.go
T
2024-05-08 15:40:14 +03:00

35 lines
580 B
Go

package xtypes
import (
"strconv"
"time"
)
type Time time.Time
func (l Time) MarshalJSON() ([]byte, error) {
return l.MarshalText()
}
func (l *Time) UnmarshalJSON(data []byte) error {
return l.UnmarshalText(data)
}
func (l Time) MarshalText() ([]byte, error) {
t := time.Time(l)
value := strconv.Itoa(int(t.Unix()))
return []byte(value), nil
}
func (l *Time) UnmarshalText(data []byte) error {
if string(data) == "null" {
return nil
}
value, err := strconv.Atoi(string(data))
if err != nil {
return err
}
*l = Time(time.Unix(int64(value), 0))
return nil
}