diff --git a/carbon.go b/carbon.go index fbde3704..cc60b083 100644 --- a/carbon.go +++ b/carbon.go @@ -186,6 +186,13 @@ type Carbon struct { Error error } +// NewCarbonPointer returns a new Carbon instance. +// 初始化 Carbon 结构体 +func NewCarbonPointer() *Carbon { + c := NewCarbon() + return &c +} + // NewCarbon returns a new Carbon instance. // 初始化 Carbon 结构体 func NewCarbon() Carbon { diff --git a/creator.go b/creator.go index 9d1657ab..f51be6f0 100755 --- a/creator.go +++ b/creator.go @@ -4,6 +4,58 @@ import ( "time" ) +// CreateFromTimeLayoutString creates a Carbon instance from a given time string ("15:04:05"). +// 从给定的秒级时间戳创建 Carbon 实例 +func (c *Carbon) CreateFromTimeLayoutString(timeString string, timezone ...string) Carbon { + *c = ParseByLayout(timeString, TimeLayout, timezone...) + return *c +} + +// CreateFromTimeLayoutString creates a Carbon instance from a given time string ("15:04:05"). +// 从给定的秒级时间戳创建 Carbon 实例 +func CreateFromTimeLayoutString(timeString string, timezone ...string) Carbon { + return NewCarbonPointer().CreateFromTimeLayoutString(timeString, timezone...) +} + +// CreateFromTimeMilliLayoutString creates a Carbon instance from a given timeMilli string ("15:04:05.999"). +// 从给定的秒级时间戳创建 Carbon 实例 +func (c *Carbon) CreateFromTimeMilliLayoutString(timeMilliString string, timezone ...string) Carbon { + *c = ParseByLayout(timeMilliString, TimeMilliLayout, timezone...) + return *c +} + +// CreateFromTimeMilliLayoutString creates a Carbon instance from a given timeMilli string ("15:04:05.999"). +// 从给定的秒级时间戳创建 Carbon 实例 +func CreateFromTimeMilliLayoutString(timeMilliString string, timezone ...string) Carbon { + return NewCarbonPointer().CreateFromTimeMilliLayoutString(timeMilliString, timezone...) +} + +// CreateFromTimeMicroLayoutString creates a Carbon instance from a given timeMicro string ("15:04:05.999999"). +// 从给定的秒级时间戳创建 Carbon 实例 +func (c *Carbon) CreateFromTimeMicroLayoutString(timeMicroString string, timezone ...string) Carbon { + *c = ParseByLayout(timeMicroString, TimeMicroLayout, timezone...) + return *c +} + +// CreateFromTimeMicroLayoutString creates a Carbon instance from a given timeMicro string ("15:04:05.999999"). +// 从给定的秒级时间戳创建 Carbon 实例 +func CreateFromTimeMicroLayoutString(timeMicroString string, timezone ...string) Carbon { + return NewCarbonPointer().CreateFromTimeMicroLayoutString(timeMicroString, timezone...) +} + +// CreateFromTimeNanoLayoutString creates a Carbon instance from a given timeNano string ("15:04:05.999999999"). +// 从给定的秒级时间戳创建 Carbon 实例 +func (c *Carbon) CreateFromTimeNanoLayoutString(timeNanoString string, timezone ...string) Carbon { + *c = ParseByLayout(timeNanoString, TimeNanoLayout, timezone...) + return *c +} + +// CreateFromTimeNanoLayoutString creates a Carbon instance from a given timeNano string ("15:04:05.999999999"). +// 从给定的秒级时间戳创建 Carbon 实例 +func CreateFromTimeNanoLayoutString(timeNanoString string, timezone ...string) Carbon { + return NewCarbonPointer().CreateFromTimeNanoLayoutString(timeNanoString, timezone...) +} + // CreateFromTimestamp creates a Carbon instance from a given timestamp with second. // 从给定的秒级时间戳创建 Carbon 实例 func (c Carbon) CreateFromTimestamp(timestamp int64, timezone ...string) Carbon { diff --git a/json.go b/json.go index 0526eea2..d065b6cd 100644 --- a/json.go +++ b/json.go @@ -6,6 +6,30 @@ import ( "strconv" ) +// 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 +} + // DateTime defines a DateTime struct. // 定义 DateTime 结构体 type DateTime struct { @@ -78,13 +102,101 @@ type TimestampNano struct { Carbon } -// MarshalJSON implements the interface json.Marshal for DateTime struct. +// MarshalJSON implements the interface json.Marshal for Time struct ("15:04:05"). +// 实现 MarshalJSON 接口 +func (t Time) MarshalJSON() ([]byte, error) { + return []byte(t.ToTimeString()), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for Time struct ("15:04:05"). +// 实现 UnmarshalJSON 接口 +func (t *Time) UnmarshalJSON(b []byte) error { + c := CreateFromTimeLayoutString(string(bytes.Trim(b, `"`))) + if c.Error == nil { + *t = Time{c} + } + return c.Error +} + +// String implements the interface Stringer for Time struct ("15:04:05"). +// 实现 Stringer 接口 +func (t Time) String() string { + return t.ToTimeString() +} + +// MarshalJSON implements the interface json.Marshal for TimeMilli struct ("15:04:05.999"). +// 实现 MarshalJSON 接口 +func (t TimeMilli) MarshalJSON() ([]byte, error) { + return []byte(t.ToTimeMilliString()), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimeMilli struct ("15:04:05.999"). +// 实现 UnmarshalJSON 接口 +func (t *TimeMilli) UnmarshalJSON(b []byte) error { + c := CreateFromTimeMilliLayoutString(string(bytes.Trim(b, `"`))) + if c.Error == nil { + *t = TimeMilli{c} + } + return c.Error +} + +// String implements the interface Stringer for TimeMilli struct ("15:04:05.999"). +// 实现 Stringer 接口 +func (t TimeMilli) String() string { + return t.ToTimeMilliString() +} + +// MarshalJSON implements the interface json.Marshal for TimeMicro struct ("15:04:05.999999"). +// 实现 MarshalJSON 接口 +func (t TimeMicro) MarshalJSON() ([]byte, error) { + return []byte(t.ToTimeMicroString()), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimeMicro struct ("15:04:05.999999"). +// 实现 UnmarshalJSON 接口 +func (t *TimeMicro) UnmarshalJSON(b []byte) error { + c := CreateFromTimeMicroLayoutString(string(bytes.Trim(b, `"`))) + if c.Error == nil { + *t = TimeMicro{c} + } + return c.Error +} + +// String implements the interface Stringer for TimeMicro struct ("15:04:05.999999"). +// 实现 Stringer 接口 +func (t TimeMicro) String() string { + return t.ToTimeMicroString() +} + +// MarshalJSON implements the interface json.Marshal for TimeNano struct ("15:04:05.999999"). +// 实现 MarshalJSON 接口 +func (t TimeNano) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%s"`, t.ToTimeNanoString())), nil +} + +// UnmarshalJSON implements the interface json.Unmarshal for TimeNano struct ("15:04:05.999999"). +// 实现 UnmarshalJSON 接口 +func (t *TimeNano) UnmarshalJSON(b []byte) error { + c := CreateFromTimeNanoLayoutString(string(bytes.Trim(b, `"`))) + if c.Error == nil { + *t = TimeNano{c} + } + return c.Error +} + +// String implements the interface Stringer for TimeNano struct ("15:04:05.999999"). +// 实现 Stringer 接口 +func (t TimeNano) String() string { + return t.ToTimeNanoString() +} + +// MarshalJSON implements the interface json.Marshal for DateTime struct ("2006-01-02 15:04:05"). // 实现 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 implements the interface json.Unmarshal for DateTime struct ("2006-01-02 15:04:05"). // 实现 UnmarshalJSON 接口 func (t *DateTime) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeLayout) @@ -94,19 +206,19 @@ func (t *DateTime) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for DateTime struct. +// String implements the interface Stringer for DateTime struct ("2006-01-02 15:04:05"). // 实现 Stringer 接口 func (t DateTime) String() string { return t.ToDateTimeString() } -// MarshalJSON implements the interface json.Marshal for DateTimeMilli struct. +// MarshalJSON implements the interface json.Marshal for DateTimeMilli struct ("2006-01-02 15:04:05.999"). // 实现 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 implements the interface json.Unmarshal for DateTimeMilli struct ("2006-01-02 15:04:05.999"). // 实现 UnmarshalJSON 接口 func (t *DateTimeMilli) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeMilliLayout) @@ -116,19 +228,19 @@ func (t *DateTimeMilli) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for DateTimeMilli struct. +// String implements the interface Stringer for DateTimeMilli struct ("2006-01-02 15:04:05.999"). // 实现 Stringer 接口 func (t DateTimeMilli) String() string { return t.ToDateTimeMilliString() } -// MarshalJSON implements the interface json.Marshal for DateTimeMicro struct. +// MarshalJSON implements the interface json.Marshal for DateTimeMicro struct ("2006-01-02 15:04:05.999999"). // 实现 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 implements the interface json.Unmarshal for DateTimeMicro struct ("2006-01-02 15:04:05.999999"). // 实现 UnmarshalJSON 接口 func (t *DateTimeMicro) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeMicroLayout) @@ -138,19 +250,19 @@ func (t *DateTimeMicro) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for DateTimeMicro struct. +// String implements the interface Stringer for DateTimeMicro struct ("2006-01-02 15:04:05.999999"). // 实现 Stringer 接口 func (t DateTimeMicro) String() string { return t.ToDateTimeMicroString() } -// MarshalJSON implements the interface json.Marshal for DateTimeNano struct. +// MarshalJSON implements the interface json.Marshal for DateTimeNano struct ("2006-01-02 15:04:05.999999999"). // 实现 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 implements the interface json.Unmarshal for DateTimeNano struct ("2006-01-02 15:04:05.999999999"). // 实现 UnmarshalJSON 接口 func (t *DateTimeNano) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeNanoLayout) @@ -160,19 +272,19 @@ func (t *DateTimeNano) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for DateTimeNano struct. +// String implements the interface Stringer for DateTimeNano struct ("2006-01-02 15:04:05.999999999"). // 实现 Stringer 接口 func (t DateTimeNano) String() string { return t.ToDateTimeNanoString() } -// MarshalJSON implements the interface json.Marshal for Date struct. +// MarshalJSON implements the interface json.Marshal for Date struct ("2006-01-02"). // 实现 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 implements the interface json.Unmarshal for Date struct ("2006-01-02"). // 实现 UnmarshalJSON 接口 func (t *Date) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateLayout) @@ -182,19 +294,19 @@ func (t *Date) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for Date struct. +// String implements the interface Stringer for Date struct ("2006-01-02"). // 实现 Stringer 接口 func (t Date) String() string { return t.ToDateString() } -// MarshalJSON implements the interface json.Marshal for DateMilli struct. +// MarshalJSON implements the interface json.Marshal for DateMilli struct ("2006-01-02.999"). // 实现 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 implements the interface json.Unmarshal for DateMilli struct ("2006-01-02.999"). // 实现 UnmarshalJSON 接口 func (t *DateMilli) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateMilliLayout) @@ -204,19 +316,19 @@ func (t *DateMilli) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for DateMilli struct. +// String implements the interface Stringer for DateMilli struct ("2006-01-02.999"). // 实现 Stringer 接口 func (t DateMilli) String() string { return t.ToDateMilliString() } -// MarshalJSON implements the interface json.Marshal for DateMicro struct. +// MarshalJSON implements the interface json.Marshal for DateMicro struct ("2006-01-02.999999"). // 实现 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 implements the interface json.Unmarshal for DateMicro struct ("2006-01-02.999999"). // 实现 UnmarshalJSON 接口 func (t *DateMicro) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateMicroLayout) @@ -226,19 +338,19 @@ func (t *DateMicro) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for DateMicro struct. +// String implements the interface Stringer for DateMicro struct ("2006-01-02.999999"). // 实现 Stringer 接口 func (t DateMicro) String() string { return t.ToDateMicroString() } -// MarshalJSON implements the interface json.Marshal for DateNano struct. +// MarshalJSON implements the interface json.Marshal for DateNano struct ("2006-01-02.999999999"). // 实现 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 implements the interface json.Unmarshal for DateNano struct ("2006-01-02.999999999"). // 实现 UnmarshalJSON 接口 func (t *DateNano) UnmarshalJSON(b []byte) error { c := ParseByLayout(string(bytes.Trim(b, `"`)), DateNanoLayout) @@ -248,7 +360,7 @@ func (t *DateNano) UnmarshalJSON(b []byte) error { return c.Error } -// String implements the interface Stringer for DateNano struct. +// String implements the interface Stringer for DateNano struct ("2006-01-02.999999999"). // 实现 Stringer 接口 func (t DateNano) String() string { return t.ToDateNanoString()