unix_time.go 569 字节
package jtime

import (
	"fmt"
	"strconv"
	"time"
)

type UnixTimeSecond time.Time

// MarshalJSON implements json.Marshaler.
func (t UnixTimeSecond) MarshalJSON() ([]byte, error) {
	stamp := fmt.Sprintf("%d", time.Time(t).Unix())
	return []byte(stamp), nil
}

// MarshalJSON implements json.Unmarshaler.
func (t UnixTimeSecond) UnmarshalJSON(v []byte) error {
	str := string(v)
	number, err := strconv.Atoi(str)
	if err != nil {
		return fmt.Errorf("时间类型需要使用时间戳传参:%w", err)
	}
	t = UnixTimeSecond(time.Unix(int64(number), 0))
	return nil
}