diff --git a/encoding.go b/encoding.go index 0c8a73f..60811d1 100644 --- a/encoding.go +++ b/encoding.go @@ -3,8 +3,365 @@ package carbon import ( "bytes" "fmt" + "strconv" ) +// DateTime defines a DateTime struct. +// 定义 DateTime 结构体 +type DateTime struct { + Carbon +} + +// DateTimeMilli defines a DateTimeMilli struct. +// 定义 DateTimeMilli 结构体 +type DateTimeMilli struct { + Carbon +} + +// DateTimeMicro defines a DateTimeMicro struct. +// 定义 DateTimeMicro 结构体 +type DateTimeMicro struct { + Carbon +} + +// DateTimeNano defines a DateTimeNano struct. +// 定义 DateTimeNano 结构体 +type DateTimeNano struct { + Carbon +} + +// Date defines a Date struct. +// 定义 Date 结构体 +type Date struct { + Carbon +} + +// DateMilli defines a DateMilli struct. +// 定义 DateMilli 结构体 +type DateMilli struct { + Carbon +} + +// DateMicro defines a DateMicro struct. +// 定义 DateMicro 结构体 +type DateMicro struct { + Carbon +} + +// DateNano defines a DateNano struct. +// 定义 DateNano 结构体 +type DateNano struct { + Carbon +} + +// Time defines a Time struct. +// 定义 Time 结构体 +type Time struct { + Carbon +} + +// TimeMilli defines a TimeMilli struct. +// 定义 TimeMilli 结构体 +type TimeMilli struct { + Carbon +} + +// TimeMicro defines a TimeMicro struct. +// 定义 TimeMicro 结构体 +type TimeMicro struct { + Carbon +} + +// TimeNano defines a TimeNano struct. +// 定义 TimeNano 结构体 +type TimeNano struct { + Carbon +} + +// Timestamp defines a Timestamp struct. +// 定义 Timestamp 结构体 +type Timestamp struct { + Carbon +} + +// TimestampMilli defines a TimestampMilli struct. +// 定义 TimestampMilli 结构体 +type TimestampMilli struct { + Carbon +} + +// TimestampMicro defines a TimestampMicro struct. +// 定义 TimestampMicro 结构体 +type TimestampMicro struct { + Carbon +} + +// TimestampNano defines a TimestampNano struct. +// 定义 TimestampNano 结构体 +type TimestampNano struct { + Carbon +} + +// MarshalJSON implements the interface json.Marshal for DateTime struct. +// 实现 MarshalJSON 接口 +func (t DateTime) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for DateTime struct. +// 实现 UnmarshalJSON 接口 +func (t *DateTime) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeLayout, t.Location()) + if c.Error == nil { + *t = DateTime{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for DateTimeMilli struct. +// 实现 MarshalJSON 接口 +func (t DateTimeMilli) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeMilliString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for DateTimeMilli struct. +// 实现 UnmarshalJSON 接口 +func (t *DateTimeMilli) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeMilliLayout, t.Location()) + if c.Error == nil { + *t = DateTimeMilli{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for DateTimeMicro struct. +// 实现 MarshalJSON 接口 +func (t DateTimeMicro) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeMicroString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for DateTimeMicro struct. +// 实现 UnmarshalJSON 接口 +func (t *DateTimeMicro) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeMicroLayout, t.Location()) + if c.Error == nil { + *t = DateTimeMicro{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for DateTimeNano struct. +// 实现 MarshalJSON 接口 +func (t DateTimeNano) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeNanoString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for DateTimeNano struct. +// 实现 UnmarshalJSON 接口 +func (t *DateTimeNano) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeNanoLayout, t.Location()) + if c.Error == nil { + *t = DateTimeNano{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for Date struct. +// 实现 MarshalJSON 接口 +func (t Date) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for Date struct. +// 实现 UnmarshalJSON 接口 +func (t *Date) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateLayout, t.Location()) + if c.Error == nil { + *t = Date{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for DateMilli struct. +// 实现 MarshalJSON 接口 +func (t DateMilli) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateMilliString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for DateMilli struct. +// 实现 UnmarshalJSON 接口 +func (t *DateMilli) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateMilliLayout, t.Location()) + if c.Error == nil { + *t = DateMilli{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for DateMicro struct. +// 实现 MarshalJSON 接口 +func (t DateMicro) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateMicroString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for DateMicro struct. +// 实现 UnmarshalJSON 接口 +func (t *DateMicro) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateMicroLayout, t.Location()) + if c.Error == nil { + *t = DateMicro{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for DateNano struct. +// 实现 MarshalJSON 接口 +func (t DateNano) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToDateNanoString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for DateNano struct. +// 实现 UnmarshalJSON 接口 +func (t *DateNano) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), DateNanoLayout, t.Location()) + if c.Error == nil { + *t = DateNano{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for Time struct. +// 实现 MarshalJSON 接口 +func (t Time) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToTimeString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for Time struct. +// 实现 UnmarshalJSON 接口 +func (t *Time) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), TimeLayout, t.Location()) + if c.Error == nil { + *t = Time{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for TimeMilli struct. +// 实现 MarshalJSON 接口 +func (t TimeMilli) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToTimeMilliString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimeMilli struct. +// 实现 UnmarshalJSON 接口 +func (t *TimeMilli) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), TimeMilliLayout, t.Location()) + if c.Error == nil { + *t = TimeMilli{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for TimeMicro struct. +// 实现 MarshalJSON 接口 +func (t TimeMicro) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToTimeMicroString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimeMicro struct. +// 实现 UnmarshalJSON 接口 +func (t *TimeMicro) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), TimeMicroLayout, t.Location()) + if c.Error == nil { + *t = TimeMicro{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for TimeNano struct. +// 实现 MarshalJSON 接口 +func (t TimeNano) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToTimeNanoString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimeNano struct. +// 实现 UnmarshalJSON 接口 +func (t *TimeNano) UnmarshalJSON(b []byte) error { + c := ParseByLayout(string(bytes.Trim(b, `"`)), TimeNanoLayout, t.Location()) + if c.Error == nil { + *t = TimeNano{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for Timestamp struct. +// 实现 MarshalJSON 接口 +func (t Timestamp) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`%d`, t.Timestamp())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for Timestamp struct. +// 实现 UnmarshalJSON 接口 +func (t *Timestamp) UnmarshalJSON(b []byte) error { + ts, _ := strconv.ParseInt(string(b), 10, 64) + c := CreateFromTimestamp(ts) + if c.Error == nil { + *t = Timestamp{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for TimestampMilli struct. +// 实现 MarshalJSON 接口 +func (t TimestampMilli) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`%d`, t.TimestampMilli())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimestampMilli struct. +// 实现 UnmarshalJSON 接口 +func (t *TimestampMilli) UnmarshalJSON(b []byte) error { + ts, _ := strconv.ParseInt(string(b), 10, 64) + c := CreateFromTimestampMilli(ts) + if c.Error == nil { + *t = TimestampMilli{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface MarshalJSON for TimestampMicro struct. +// 实现 MarshalJSON 接口 +func (t TimestampMicro) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`%d`, t.TimestampMicro())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimestampMicro struct. +// 实现 UnmarshalJSON 接口 +func (t *TimestampMicro) UnmarshalJSON(b []byte) error { + ts, _ := strconv.ParseInt(string(b), 10, 64) + c := CreateFromTimestampMicro(ts) + if c.Error == nil { + *t = TimestampMicro{Carbon: c} + } + return c.Error +} + +// MarshalJSON implements the interface json.Marshal for TimestampNano struct. +// 实现 MarshalJSON 接口 +func (t TimestampNano) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`%d`, t.TimestampNano())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimestampNano struct. +// 实现 UnmarshalJSON 接口 +func (t *TimestampNano) UnmarshalJSON(b []byte) error { + ts, _ := strconv.ParseInt(string(b), 10, 64) + c := CreateFromTimestampNano(ts) + if c.Error == nil { + *t = TimestampNano{Carbon: c} + } + return c.Error +} + // MarshalJSON implements the json.Marshaler interface. // 实现 json.Marshaler 接口 func (c Carbon) MarshalJSON() ([]byte, error) { @@ -50,3 +407,315 @@ func (c *Carbon) UnmarshalJSON(b []byte) error { } return c.Error } + +// Int64 outputs timestamp with second. +// 输出秒级时间戳 +func (t Timestamp) Int64() int64 { + return t.Timestamp() +} + +// Int64 outputs timestamp with millisecond. +// 输出豪秒级时间戳 +func (t TimestampMilli) Int64() int64 { + return t.TimestampMilli() +} + +// Int64 outputs timestamp with microsecond. +// 输出微秒级时间戳 +func (t TimestampMicro) Int64() int64 { + return t.TimestampMicro() +} + +// Int64 outputs timestamp with nanosecond. +// 输出纳秒级时间戳 +func (t TimestampNano) Int64() int64 { + return t.TimestampNano() +} + +// String implements the interface Stringer for DateTime struct. +// 实现 Stringer 接口 +func (t DateTime) String() string { + return t.ToDateTimeString() +} + +// String implements the interface Stringer for DateTimeMilli struct. +// 实现 Stringer 接口 +func (t DateTimeMilli) String() string { + return t.ToDateTimeMilliString() +} + +// String implements the interface Stringer for DateTimeMicro struct. +// 实现 Stringer 接口 +func (t DateTimeMicro) String() string { + return t.ToDateTimeMicroString() +} + +// String implements the interface Stringer for DateTimeNano struct. +// 实现 Stringer 接口 +func (t DateTimeNano) String() string { + return t.ToDateTimeNanoString() +} + +// String implements the interface Stringer for Date struct. +// 实现 Stringer 接口 +func (t Date) String() string { + return t.ToDateString() +} + +// String implements the interface Stringer for DateMilli struct. +// 实现 Stringer 接口 +func (t DateMilli) String() string { + return t.ToDateMilliString() +} + +// String implements the interface Stringer for DateMicro struct. +// 实现 Stringer 接口 +func (t DateMicro) String() string { + return t.ToDateMicroString() +} + +// String implements the interface Stringer for DateNano struct. +// 实现 Stringer 接口 +func (t DateNano) String() string { + return t.ToDateNanoString() +} + +// String implements the interface Stringer for Time struct. +// 实现 Stringer 接口 +func (t Time) String() string { + return t.ToTimeString() +} + +// String implements the interface Stringer for TimeMilli struct. +// 实现 Stringer 接口 +func (t TimeMilli) String() string { + return t.ToTimeMilliString() +} + +// String implements the interface Stringer for TimeMicro struct. +// 实现 Stringer 接口 +func (t TimeMicro) String() string { + return t.ToTimeMicroString() +} + +// String implements the interface Stringer for TimeNano struct. +// 实现 Stringer 接口 +func (t TimeNano) String() string { + return t.ToTimeNanoString() +} + +// String implements the interface Stringer for Timestamp struct. +// 实现 Stringer 接口 +func (t Timestamp) String() string { + return strconv.FormatInt(t.Timestamp(), 10) +} + +// String implements the interface Stringer for TimestampMilli struct. +// 实现 Stringer 接口 +func (t TimestampMilli) String() string { + return strconv.FormatInt(t.TimestampMilli(), 10) +} + +// String implements the interface Stringer for TimestampMicro struct. +// 实现 Stringer 接口 +func (t TimestampMicro) String() string { + return strconv.FormatInt(t.TimestampMicro(), 10) +} + +// String implements the interface Stringer for TimestampNano struct. +// 实现 Stringer 接口 +func (t TimestampNano) String() string { + return strconv.FormatInt(t.TimestampNano(), 10) +} + +// GormDataType implements the interface GormDataTypeInterface for DateTime struct. +// 实现 GormDataTypeInterface 接口 +func (t DateTime) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for DateTimeMilli struct. +// 实现 GormDataTypeInterface 接口 +func (t DateTimeMilli) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for DateTimeMicro struct. +// 实现 GormDataTypeInterface 接口 +func (t DateTimeMicro) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for DateTimeNano struct. +// 实现 GormDataTypeInterface 接口 +func (t DateTimeNano) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for Date struct. +// 实现 GormDataTypeInterface 接口 +func (t Date) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for DateMilli struct. +// 实现 GormDataTypeInterface 接口 +func (t DateMilli) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for DateMicro struct. +// 实现 GormDataTypeInterface 接口 +func (t DateMicro) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for DateNano struct. +// 实现 GormDataTypeInterface 接口 +func (t DateNano) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for Time struct. +// 实现 GormDataTypeInterface 接口 +func (t Time) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for TimeMilli struct. +// 实现 GormDataTypeInterface 接口 +func (t TimeMilli) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for TimeMicro struct. +// 实现 GormDataTypeInterface 接口 +func (t TimeMicro) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for TimeNano struct. +// 实现 GormDataTypeInterface 接口 +func (t TimeNano) GormDataType() string { + return "time" +} + +// GormDataType implements the interface GormDataTypeInterface for Timestamp struct. +// 实现 GormDataTypeInterface 接口 +func (t Timestamp) GormDataType() string { + return "int" +} + +// GormDataType implements the interface GormDataTypeInterface for TimestampMilli struct. +// 实现 GormDataTypeInterface 接口 +func (t TimestampMilli) GormDataType() string { + return "int" +} + +// GormDataType implements the interface GormDataTypeInterface for TimestampMicro struct. +// 实现 GormDataTypeInterface 接口 +func (t TimestampMicro) GormDataType() string { + return "int" +} + +// GormDataType implements the interface GormDataTypeInterface for TimestampNano struct. +// 实现 GormDataTypeInterface 接口 +func (t TimestampNano) GormDataType() string { + return "int" +} + +// ToDateTimeStruct converts Carbon to DateTime. +// 将 Carbon 结构体转换成 DateTime 结构体 +func (c Carbon) ToDateTimeStruct() DateTime { + return DateTime{Carbon: c} +} + +// ToDateTimeMilliStruct converts Carbon to DateTimeMilli. +// 将 Carbon 结构体转换成 DateTimeMilli 结构体 +func (c Carbon) ToDateTimeMilliStruct() DateTimeMilli { + return DateTimeMilli{Carbon: c} +} + +// ToDateTimeMicroStruct converts Carbon to DateTimeMicro. +// 将 Carbon 结构体转换成 DateTimeMicro 结构体 +func (c Carbon) ToDateTimeMicroStruct() DateTimeMicro { + return DateTimeMicro{Carbon: c} +} + +// ToDateTimeNanoStruct converts Carbon to DateTimeNano. +// 将 Carbon 结构体转换成 DateTimeNano 结构体 +func (c Carbon) ToDateTimeNanoStruct() DateTimeNano { + return DateTimeNano{Carbon: c} +} + +// ToDateStruct converts Carbon to Date. +// 将 Carbon 结构体转换成 Date 结构体 +func (c Carbon) ToDateStruct() Date { + return Date{Carbon: c} +} + +// ToDateMilliStruct converts Carbon to DateMilli. +// 将 Carbon 结构体转换成 DateMilli 结构体 +func (c Carbon) ToDateMilliStruct() DateMilli { + return DateMilli{Carbon: c} +} + +// ToDateMicroStruct converts Carbon to DateMicro. +// 将 Carbon 结构体转换成 DateMicro 结构体 +func (c Carbon) ToDateMicroStruct() DateMicro { + return DateMicro{Carbon: c} +} + +// ToDateNanoStruct converts Carbon to DateNano. +// 将 Carbon 结构体转换成 DateNano 结构体 +func (c Carbon) ToDateNanoStruct() DateNano { + return DateNano{Carbon: c} +} + +// ToTimeStruct converts Carbon to Time. +// 将 Carbon 结构体转换成 Time 结构体 +func (c Carbon) ToTimeStruct() Time { + return Time{Carbon: c} +} + +// ToTimeMilliStruct converts Carbon to TimeMilli. +// 将 Carbon 结构体转换成 TimeMilli 结构体 +func (c Carbon) ToTimeMilliStruct() TimeMilli { + return TimeMilli{Carbon: c} +} + +// ToTimeMicroStruct converts Carbon to TimeMicro. +// 将 Carbon 结构体转换成 TimeMicro 结构体 +func (c Carbon) ToTimeMicroStruct() TimeMicro { + return TimeMicro{Carbon: c} +} + +// ToTimeNanoStruct converts Carbon to TimeNano. +// 将 Carbon 结构体转换成 TimeNano 结构体 +func (c Carbon) ToTimeNanoStruct() TimeNano { + return TimeNano{Carbon: c} +} + +// ToTimestampStruct converts Carbon to Timestamp. +// 将 Carbon 结构体转换成 Timestamp 结构体 +func (c Carbon) ToTimestampStruct() Timestamp { + return Timestamp{Carbon: c} +} + +// ToTimestampMilliStruct converts Carbon to TimestampMilli. +// 将 Carbon 结构体转换成 TimestampMilli 结构体 +func (c Carbon) ToTimestampMilliStruct() TimestampMilli { + return TimestampMilli{Carbon: c} +} + +// ToTimestampMicroStruct converts Carbon to TimestampMicro. +// 将 Carbon 结构体转换成 TimestampMicro 结构体 +func (c Carbon) ToTimestampMicroStruct() TimestampMicro { + return TimestampMicro{Carbon: c} +} + +// ToTimestampNanoStruct converts Carbon to TimestampNano. +// 将 Carbon 结构体转换成 TimestampNano 结构体 +func (c Carbon) ToTimestampNanoStruct() TimestampNano { + return TimestampNano{Carbon: c} +} diff --git a/encoding_unit_test.go b/encoding_unit_test.go index 785dd9f..a1f6905 100644 --- a/encoding_unit_test.go +++ b/encoding_unit_test.go @@ -32,6 +32,26 @@ type Person struct { Birthday15 Carbon `json:"birthday15" carbon:"type:timestampMilli" tz:"PRC"` Birthday16 Carbon `json:"birthday16" carbon:"type:timestampMicro" tz:"PRC"` Birthday17 Carbon `json:"birthday17" carbon:"type:timestampNano" tz:"PRC"` + + Birthday18 DateTime `json:"birthday18"` + Birthday19 DateTimeMilli `json:"birthday19"` + Birthday20 DateTimeMicro `json:"birthday20"` + Birthday21 DateTimeNano `json:"birthday21"` + + Birthday22 Date `json:"birthday22"` + Birthday23 DateMilli `json:"birthday23"` + Birthday24 DateMicro `json:"birthday24"` + Birthday25 DateNano `json:"birthday25"` + + Birthday26 Time `json:"birthday26"` + Birthday27 TimeMilli `json:"birthday27"` + Birthday28 TimeMicro `json:"birthday28"` + Birthday29 TimeNano `json:"birthday29"` + + Birthday30 Timestamp `json:"birthday30"` + Birthday31 TimestampMilli `json:"birthday31"` + Birthday32 TimestampMicro `json:"birthday32"` + Birthday33 TimestampNano `json:"birthday33"` } func TestCarbon_MarshalJSON(t *testing.T) { @@ -56,6 +76,26 @@ func TestCarbon_MarshalJSON(t *testing.T) { Birthday15: c, Birthday16: c, Birthday17: c, + + Birthday18: c.ToDateTimeStruct(), + Birthday19: c.ToDateTimeMilliStruct(), + Birthday20: c.ToDateTimeMicroStruct(), + Birthday21: c.ToDateTimeNanoStruct(), + + Birthday22: c.ToDateStruct(), + Birthday23: c.ToDateMilliStruct(), + Birthday24: c.ToDateMicroStruct(), + Birthday25: c.ToDateNanoStruct(), + + Birthday26: c.ToTimeStruct(), + Birthday27: c.ToTimeMilliStruct(), + Birthday28: c.ToTimeMicroStruct(), + Birthday29: c.ToTimeNanoStruct(), + + Birthday30: c.ToTimestampStruct(), + Birthday31: c.ToTimestampMilliStruct(), + Birthday32: c.ToTimestampMicroStruct(), + Birthday33: c.ToTimestampNanoStruct(), } data, marshalErr := json.Marshal(&person) @@ -152,6 +192,89 @@ func TestCarbon_MarshalJSON(t *testing.T) { actual: person.Birthday17.String(), want: "2020-08-05 13:14:15", }, + { + name: "birthday18", + actual: person.Birthday18.String(), + want: "2020-08-05 13:14:15", + }, + { + name: "birthday19", + actual: person.Birthday19.String(), + want: "2020-08-05 13:14:15.999", + }, + { + name: "birthday20", + actual: person.Birthday20.String(), + want: "2020-08-05 13:14:15.999999", + }, + { + name: "birthday21", + actual: person.Birthday21.String(), + want: "2020-08-05 13:14:15.999999999", + }, + + { + name: "birthday22", + actual: person.Birthday22.String(), + want: "2020-08-05", + }, + { + name: "birthday23", + actual: person.Birthday23.String(), + want: "2020-08-05.999", + }, + { + name: "birthday24", + actual: person.Birthday24.String(), + want: "2020-08-05.999999", + }, + { + name: "birthday25", + actual: person.Birthday25.String(), + want: "2020-08-05.999999999", + }, + + { + name: "birthday26", + actual: person.Birthday26.String(), + want: "13:14:15", + }, + { + name: "birthday27", + actual: person.Birthday27.String(), + want: "13:14:15.999", + }, + { + name: "birthday28", + actual: person.Birthday28.String(), + want: "13:14:15.999999", + }, + { + name: "birthday29", + actual: person.Birthday29.String(), + want: "13:14:15.999999999", + }, + + { + name: "birthday30", + actual: person.Birthday30.String(), + want: "1596604455", + }, + { + name: "birthday31", + actual: person.Birthday31.String(), + want: "1596604455999", + }, + { + name: "birthday32", + actual: person.Birthday32.String(), + want: "1596604455999999", + }, + { + name: "birthday33", + actual: person.Birthday33.String(), + want: "1596604455999999999", + }, } for _, tt := range tests { @@ -181,7 +304,23 @@ func TestCarbon_UnmarshalJSON(t *testing.T) { "birthday14":"2020-08-05 13:14:15", "birthday15":"2020-08-05 13:14:15", "birthday16":"2020-08-05 13:14:15", - "birthday17":"2020-08-05 13:14:15" + "birthday17":"2020-08-05 13:14:15", + "birthday18":"2020-08-05 13:14:15", + "birthday19":"2020-08-05 13:14:15.999", + "birthday20":"2020-08-05 13:14:15.999999", + "birthday21":"2020-08-05 13:14:15.999999999", + "birthday22":"2020-08-05", + "birthday23":"2020-08-05.999", + "birthday24":"2020-08-05.999999", + "birthday25":"2020-08-05.999999999", + "birthday26":"13:14:15", + "birthday27":"13:14:15.999", + "birthday28":"13:14:15.999999", + "birthday29":"13:14:15.999999999", + "birthday30":1596604455, + "birthday31":1596604455999, + "birthday32":1596604455999999, + "birthday33":1596604455999999999 }` var person Person @@ -279,6 +418,88 @@ func TestCarbon_UnmarshalJSON(t *testing.T) { actual: person.Birthday17.String(), want: "2020-08-05 13:14:15", }, + { + name: "birthday18", + actual: person.Birthday18.String(), + want: "2020-08-05 13:14:15", + }, + { + name: "birthday19", + actual: person.Birthday19.String(), + want: "2020-08-05 13:14:15.999", + }, + { + name: "birthday20", + actual: person.Birthday20.String(), + want: "2020-08-05 13:14:15.999999", + }, + { + name: "birthday21", + actual: person.Birthday21.String(), + want: "2020-08-05 13:14:15.999999999", + }, + { + name: "birthday22", + actual: person.Birthday22.String(), + want: "2020-08-05", + }, + { + name: "birthday23", + actual: person.Birthday23.String(), + want: "2020-08-05.999", + }, + { + name: "birthday24", + actual: person.Birthday24.String(), + want: "2020-08-05.999999", + }, + { + name: "birthday25", + actual: person.Birthday25.String(), + want: "2020-08-05.999999999", + }, + + { + name: "birthday26", + actual: person.Birthday26.String(), + want: "13:14:15", + }, + { + name: "birthday27", + actual: person.Birthday27.String(), + want: "13:14:15.999", + }, + { + name: "birthday28", + actual: person.Birthday28.String(), + want: "13:14:15.999999", + }, + { + name: "birthday29", + actual: person.Birthday29.String(), + want: "13:14:15.999999999", + }, + + { + name: "birthday30", + actual: person.Birthday30.String(), + want: "1596604455", + }, + { + name: "birthday31", + actual: person.Birthday31.String(), + want: "1596604455999", + }, + { + name: "birthday32", + actual: person.Birthday32.String(), + want: "1596604455999999", + }, + { + name: "birthday33", + actual: person.Birthday33.String(), + want: "1596604455999999999", + }, } for _, tt := range tests { @@ -310,6 +531,26 @@ func TestCarbon_MarshalJSON_LoadTag(t *testing.T) { Birthday15: c, Birthday16: c, Birthday17: c, + + Birthday18: DateTime{Carbon: c}, + Birthday19: DateTimeMilli{Carbon: c}, + Birthday20: DateTimeMicro{Carbon: c}, + Birthday21: DateTimeNano{Carbon: c}, + + Birthday22: Date{Carbon: c}, + Birthday23: DateMilli{Carbon: c}, + Birthday24: DateMicro{Carbon: c}, + Birthday25: DateNano{Carbon: c}, + + Birthday26: Time{Carbon: c}, + Birthday27: TimeMilli{Carbon: c}, + Birthday28: TimeMicro{Carbon: c}, + Birthday29: TimeNano{Carbon: c}, + + Birthday30: Timestamp{Carbon: c}, + Birthday31: TimestampMilli{Carbon: c}, + Birthday32: TimestampMicro{Carbon: c}, + Birthday33: TimestampNano{Carbon: c}, } loadErr := LoadTag(&person) @@ -410,6 +651,11 @@ func TestCarbon_MarshalJSON_LoadTag(t *testing.T) { actual: person.Birthday17.String(), want: "1596604455999999999", }, + { + name: "birthday18", + actual: person.Birthday18.String(), + want: "2020-08-05 13:14:15", + }, } for _, tt := range tests { @@ -439,7 +685,8 @@ func TestCarbon_UnmarshalJSON_LoadTag(t *testing.T) { "birthday14":"1596604455", "birthday15":"1596604455999", "birthday16":"1596604455999999", - "birthday17":"1596604455999999999" + "birthday17":"1596604455999999999", + "birthday18":"2020-08-05 13:14:15" }` var person Person loadErr := LoadTag(&person) @@ -538,6 +785,11 @@ func TestCarbon_UnmarshalJSON_LoadTag(t *testing.T) { actual: person.Birthday17.String(), want: "1596604455999999999", }, + { + name: "birthday18", + actual: person.Birthday18.String(), + want: "2020-08-05 13:14:15", + }, } for _, tt := range tests { @@ -547,6 +799,51 @@ func TestCarbon_UnmarshalJSON_LoadTag(t *testing.T) { } } +func TestCarbon_TimestampToInt64(t *testing.T) { + jsonStr := `{ + "birthday30":1596604455, + "birthday31":1596604455999, + "birthday32":1596604455999999, + "birthday33":1596604455999999999 + }` + var person Person + unmarshalErr := json.Unmarshal([]byte(jsonStr), &person) + assert.Nil(t, unmarshalErr) + fmt.Printf("json decode:\n%+v\n", person) + + tests := []struct { + name string + want int64 + actual int64 + }{ + { + name: "birthday30", + actual: person.Birthday30.Int64(), + want: 1596604455, + }, + { + name: "birthday31", + actual: person.Birthday31.Int64(), + want: 1596604455999, + }, + { + name: "birthday32", + actual: person.Birthday32.Int64(), + want: 1596604455999999, + }, + { + name: "birthday33", + actual: person.Birthday33.Int64(), + want: 1596604455999999999, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, tt.actual, "json.Unmarshal()") + }) + } +} func TestError_Json(t *testing.T) { type Student struct { Birthday1 Carbon `json:"birthday1" carbon:"dateTime"`