2021-07-03 15:31:42 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2021-07-19 09:53:30 +08:00
|
|
|
"bytes"
|
2021-07-03 15:31:42 +08:00
|
|
|
"fmt"
|
2021-07-19 09:53:30 +08:00
|
|
|
"strconv"
|
2021-07-03 15:31:42 +08:00
|
|
|
)
|
|
|
|
|
2021-08-16 09:45:48 +08:00
|
|
|
// DateTime defines a DateTime struct.
|
|
|
|
type DateTime struct {
|
|
|
|
Carbon
|
|
|
|
}
|
|
|
|
|
|
|
|
// Date defines a Date struct.
|
|
|
|
type Date struct {
|
|
|
|
Carbon
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timestamp defines a Timestamp struct.
|
|
|
|
type Timestamp struct {
|
|
|
|
Carbon
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:23:19 +08:00
|
|
|
// TimestampMilli defines a TimestampMilli struct.
|
|
|
|
type TimestampMilli struct {
|
2021-08-16 09:45:48 +08:00
|
|
|
Carbon
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:23:19 +08:00
|
|
|
// TimestampMicro defines a TimestampMicro struct.
|
|
|
|
type TimestampMicro struct {
|
|
|
|
Carbon
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimestampNano defines a TimestampNano struct.
|
|
|
|
type TimestampNano struct {
|
|
|
|
Carbon
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// MarshalJSON implements the interface json.Marshal for Date struct.
|
2021-08-16 09:45:48 +08:00
|
|
|
func (t DateTime) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeString())), nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for Date struct.
|
2021-08-16 09:45:48 +08:00
|
|
|
func (t *DateTime) UnmarshalJSON(b []byte) error {
|
|
|
|
c := Parse(string(bytes.Trim(b, `"`)))
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = DateTime{c}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// MarshalJSON implements the interface json.Marshal for Date struct.
|
2021-08-16 09:45:48 +08:00
|
|
|
func (t Date) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, t.ToDateString())), nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for Date struct.
|
2021-08-16 09:45:48 +08:00
|
|
|
func (t *Date) UnmarshalJSON(b []byte) error {
|
|
|
|
c := Parse(string(bytes.Trim(b, `"`)))
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = Date{c}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// MarshalJSON implements the interface json.Marshal for Timestamp struct.
|
2021-08-16 09:45:48 +08:00
|
|
|
func (t Timestamp) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.Timestamp())), nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for Timestamp struct.
|
2021-08-16 09:45:48 +08:00
|
|
|
func (t *Timestamp) UnmarshalJSON(b []byte) error {
|
2022-05-01 09:07:33 +08:00
|
|
|
ts, _ := strconv.ParseInt(string(b), 10, 64)
|
2021-08-16 09:45:48 +08:00
|
|
|
c := CreateFromTimestamp(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = Timestamp{c}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// MarshalJSON implements the interface json.Marshal for TimestampMilli struct.
|
2022-04-12 23:23:19 +08:00
|
|
|
func (t TimestampMilli) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.TimestampMilli())), nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimestampMilli struct.
|
2022-04-12 23:23:19 +08:00
|
|
|
func (t *TimestampMilli) UnmarshalJSON(b []byte) error {
|
2022-05-01 09:07:33 +08:00
|
|
|
ts, _ := strconv.ParseInt(string(b), 10, 64)
|
2022-04-12 23:23:19 +08:00
|
|
|
c := CreateFromTimestampMilli(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = TimestampMilli{c}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// MarshalJSON implements the interface MarshalJSON for TimestampMicro struct.
|
2022-04-12 23:23:19 +08:00
|
|
|
func (t TimestampMicro) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.TimestampMicro())), nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimestampMicro struct.
|
2022-04-12 23:23:19 +08:00
|
|
|
func (t *TimestampMicro) UnmarshalJSON(b []byte) error {
|
2022-05-01 09:07:33 +08:00
|
|
|
ts, _ := strconv.ParseInt(string(b), 10, 64)
|
2022-04-12 23:23:19 +08:00
|
|
|
c := CreateFromTimestampMicro(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = TimestampMicro{c}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimestampNano struct.
|
2022-04-12 23:23:19 +08:00
|
|
|
func (t *TimestampNano) UnmarshalJSON(b []byte) error {
|
2022-05-01 09:07:33 +08:00
|
|
|
ts, _ := strconv.ParseInt(string(b), 10, 64)
|
2022-04-12 23:23:19 +08:00
|
|
|
c := CreateFromTimestampNano(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = TimestampNano{c}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
// MarshalJSON implements the interface json.Marshal for TimestampNano struct.
|
2022-04-12 23:23:19 +08:00
|
|
|
func (t TimestampNano) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.TimestampNano())), nil
|
|
|
|
}
|