Skip to content

Commit

Permalink
Time,TimeMilli,TimeMicro,TimeNano structs set year,month,day …
Browse files Browse the repository at this point in the history
…when `UnmarshalJSON`
  • Loading branch information
gouguoyin committed Nov 29, 2024
1 parent 706d5f5 commit a157b45
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ func (t *Time) UnmarshalJSON(b []byte) error {
if value == "" || value == "null" {
return nil
}
c := ParseByLayout(value, TimeLayout)
year, month, day := Now().Date()
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeLayout)
if c.Error == nil {
*t = NewTime(c)
}
Expand Down Expand Up @@ -489,7 +490,8 @@ func (t *TimeMilli) UnmarshalJSON(b []byte) error {
if value == "" || value == "null" {
return nil
}
c := ParseByLayout(value, TimeMilliLayout)
year, month, day := Now().Date()
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeMilliLayout)
if c.Error == nil {
*t = NewTimeMilli(c)
}
Expand Down Expand Up @@ -533,7 +535,8 @@ func (t *TimeMicro) UnmarshalJSON(b []byte) error {
if value == "" || value == "null" {
return nil
}
c := ParseByLayout(value, TimeMicroLayout)
year, month, day := Now().Date()
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeMicroLayout)
if c.Error == nil {
*t = NewTimeMicro(c)
}
Expand Down Expand Up @@ -577,7 +580,8 @@ func (t *TimeNano) UnmarshalJSON(b []byte) error {
if value == "" || value == "null" {
return nil
}
c := ParseByLayout(value, TimeNanoLayout)
year, month, day := Now().Date()
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeNanoLayout)
if c.Error == nil {
*t = NewTimeNano(c)
}
Expand Down

0 comments on commit a157b45

Please sign in to comment.