Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added times string conversion support #176

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
160 changes: 136 additions & 24 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand Down