@@ -3,7 +3,6 @@ package carbon
33import (
44 "bytes"
55 "database/sql/driver"
6- "fmt"
76 "strconv"
87 "time"
98)
@@ -105,13 +104,13 @@ func (t *timestampType[T]) Value() (driver.Value, error) {
105104// MarshalJSON implements json.Marshal interface for TimestampType generic struct.
106105// 实现 json.Marshaler 接口
107106func (t * timestampType [T ]) MarshalJSON () ([]byte , error ) {
108- ts := int64 (0 )
109107 if t .IsNil () || t .IsZero () {
110- return []byte (fmt . Sprintf ( `%d` , ts ) ), nil
108+ return []byte (`0` ), nil
111109 }
112110 if t .HasError () {
113- return []byte (fmt . Sprintf ( `%d` , ts ) ), t .Error
111+ return []byte (`0` ), t .Error
114112 }
113+ var ts int64
115114 switch t .getPrecision () {
116115 case precisionSecond :
117116 ts = t .Timestamp ()
@@ -122,13 +121,13 @@ func (t *timestampType[T]) MarshalJSON() ([]byte, error) {
122121 case precisionNanosecond :
123122 ts = t .TimestampNano ()
124123 }
125- return []byte (fmt . Sprintf ( `%d` , ts )), nil
124+ return []byte (strconv . FormatInt ( ts , 10 )), nil
126125}
127126
128127// UnmarshalJSON implements json.Unmarshal interface for timestampType generic struct.
129128// 实现 json.Unmarshaler 接口
130- func (t * timestampType [T ]) UnmarshalJSON (b []byte ) error {
131- value := string (bytes .Trim (b , `"` ))
129+ func (t * timestampType [T ]) UnmarshalJSON (bs []byte ) error {
130+ value := string (bytes .Trim (bs , `"` ))
132131 if value == "" || value == "null" || value == "0" {
133132 return nil
134133 }
@@ -154,17 +153,16 @@ func (t *timestampType[T]) UnmarshalJSON(b []byte) error {
154153// String implements Stringer interface for timestampType generic struct.
155154// 实现 Stringer 接口
156155func (t * timestampType [T ]) String () string {
157- if t == nil || t .IsInvalid () || t .IsZero () {
156+ if t .IsInvalid () || t .IsZero () {
158157 return "0"
159158 }
160159 return strconv .FormatInt (t .Int64 (), 10 )
161160}
162161
163162// Int64 returns the timestamp value.
164163// 返回时间戳
165- func (t * timestampType [T ]) Int64 () int64 {
166- ts := int64 (0 )
167- if t == nil || t .IsInvalid () || t .IsZero () {
164+ func (t * timestampType [T ]) Int64 () (ts int64 ) {
165+ if t .IsInvalid () || t .IsZero () {
168166 return ts
169167 }
170168 switch t .getPrecision () {
@@ -177,7 +175,7 @@ func (t *timestampType[T]) Int64() int64 {
177175 case precisionNanosecond :
178176 ts = t .TimestampNano ()
179177 }
180- return ts
178+ return
181179}
182180
183181// GormDataType sets gorm data type for timestampType generic struct.
0 commit comments