Skip to content

Commit

Permalink
Add JSONTime.Time() method (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
FurmanovD committed Nov 18, 2023
1 parent 2b6db8d commit 550751b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions format/json/jsontime/jsontime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

// JSONTime takes in account golang time can be an zero/empty and marshals it to an empty string
// instead of "0001-01-01T00:00:00Z"
// instead of "0001-01-01T00:00:00Z" and vice versa.
type JSONTime time.Time

// MarshalJSON marshals a zeop time to an empty string
// MarshalJSON marshals a zeop time to an empty string.
func (t JSONTime) MarshalJSON() ([]byte, error) {
timeVal := time.Time(t)
if timeVal.IsZero() {
Expand All @@ -19,7 +19,7 @@ func (t JSONTime) MarshalJSON() ([]byte, error) {
return []byte(`"` + timeVal.Format(time.RFC3339Nano) + `"`), nil
}

// UnmarshalJSON unmarshals an empty string and 'mull' to a zero time value
// UnmarshalJSON unmarshals an empty string and 'mull' to a zero time value.
func (t *JSONTime) UnmarshalJSON(b []byte) error {

s := strings.Trim(string(b), "`'\" ")
Expand All @@ -39,3 +39,7 @@ func (t *JSONTime) UnmarshalJSON(b []byte) error {
func Now() JSONTime {
return JSONTime(time.Now().UTC())
}

func (t JSONTime) Time() time.Time {
return time.Time(t)
}

0 comments on commit 550751b

Please sign in to comment.