github.com/goravel/framework@v1.13.9/support/carbon/json.go (about) 1 package carbon 2 3 import ( 4 "bytes" 5 "fmt" 6 "strconv" 7 8 "github.com/golang-module/carbon/v2" 9 ) 10 11 // DateTime defines a DateTime struct. 12 // 定义 DateTime 结构体 13 type DateTime struct { 14 Carbon 15 } 16 17 func NewDateTime(carbon Carbon) DateTime { 18 return DateTime{Carbon: carbon} 19 } 20 21 // DateTimeMilli defines a DateTimeMilli struct. 22 // 定义 DateTimeMilli 结构体 23 type DateTimeMilli struct { 24 Carbon 25 } 26 27 func NewDateTimeMilli(carbon Carbon) DateTimeMilli { 28 return DateTimeMilli{Carbon: carbon} 29 } 30 31 // DateTimeMicro defines a DateTimeMicro struct. 32 // 定义 DateTimeMicro 结构体 33 type DateTimeMicro struct { 34 Carbon 35 } 36 37 func NewDateTimeMicro(carbon Carbon) DateTimeMicro { 38 return DateTimeMicro{Carbon: carbon} 39 } 40 41 // DateTimeNano defines a DateTimeNano struct. 42 // 定义 DateTimeNano 结构体 43 type DateTimeNano struct { 44 Carbon 45 } 46 47 func NewDateTimeNano(carbon Carbon) DateTimeNano { 48 return DateTimeNano{Carbon: carbon} 49 } 50 51 // Date defines a Date struct. 52 // 定义 Date 结构体 53 type Date struct { 54 Carbon 55 } 56 57 func NewDate(carbon Carbon) Date { 58 return Date{Carbon: carbon} 59 } 60 61 // DateMilli defines a DateMilli struct. 62 // 定义 DateMilli 结构体 63 type DateMilli struct { 64 Carbon 65 } 66 67 func NewDateMilli(carbon Carbon) DateMilli { 68 return DateMilli{Carbon: carbon} 69 } 70 71 // DateMicro defines a DateMicro struct. 72 // 定义 DateMicro 结构体 73 type DateMicro struct { 74 Carbon 75 } 76 77 func NewDateMicro(carbon Carbon) DateMicro { 78 return DateMicro{Carbon: carbon} 79 } 80 81 // DateNano defines a DateNano struct. 82 // 定义 DateNano 结构体 83 type DateNano struct { 84 Carbon 85 } 86 87 func NewDateNano(carbon Carbon) DateNano { 88 return DateNano{Carbon: carbon} 89 } 90 91 // Timestamp defines a Timestamp struct. 92 // 定义 Timestamp 结构体 93 type Timestamp struct { 94 Carbon 95 } 96 97 func NewTimestamp(carbon Carbon) Timestamp { 98 return Timestamp{Carbon: carbon} 99 } 100 101 // TimestampMilli defines a TimestampMilli struct. 102 // 定义 TimestampMilli 结构体 103 type TimestampMilli struct { 104 Carbon 105 } 106 107 func NewTimestampMilli(carbon Carbon) TimestampMilli { 108 return TimestampMilli{Carbon: carbon} 109 } 110 111 // TimestampMicro defines a TimestampMicro struct. 112 // 定义 TimestampMicro 结构体 113 type TimestampMicro struct { 114 Carbon 115 } 116 117 func NewTimestampMicro(carbon Carbon) TimestampMicro { 118 return TimestampMicro{Carbon: carbon} 119 } 120 121 // TimestampNano defines a TimestampNano struct. 122 // 定义 TimestampNano 结构体 123 type TimestampNano struct { 124 Carbon 125 } 126 127 func NewTimestampNano(carbon Carbon) TimestampNano { 128 return TimestampNano{Carbon: carbon} 129 } 130 131 // MarshalJSON implements the interface json.Marshal for DateTime struct. 132 // 实现 MarshalJSON 接口 133 func (t DateTime) MarshalJSON() ([]byte, error) { 134 return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeString())), nil 135 } 136 137 // UnmarshalJSON implements the interface json.Unmarshal for DateTime struct. 138 // 实现 UnmarshalJSON 接口 139 func (t *DateTime) UnmarshalJSON(b []byte) error { 140 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateTimeLayout) 141 if c.Error == nil { 142 *t = DateTime{c} 143 } 144 return c.Error 145 } 146 147 // String implements the interface Stringer for DateTime struct. 148 // 实现 Stringer 接口 149 func (t DateTime) String() string { 150 return t.ToDateTimeString() 151 } 152 153 func (t DateTime) GormDataType() string { 154 return "time" 155 } 156 157 // MarshalJSON implements the interface json.Marshal for DateTimeMilli struct. 158 // 实现 MarshalJSON 接口 159 func (t DateTimeMilli) MarshalJSON() ([]byte, error) { 160 return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeMilliString())), nil 161 } 162 163 // UnmarshalJSON implements the interface json.Unmarshal for DateTimeMilli struct. 164 // 实现 UnmarshalJSON 接口 165 func (t *DateTimeMilli) UnmarshalJSON(b []byte) error { 166 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateTimeMilliLayout) 167 if c.Error == nil { 168 *t = DateTimeMilli{c} 169 } 170 return c.Error 171 } 172 173 // String implements the interface Stringer for DateTimeMilli struct. 174 // 实现 Stringer 接口 175 func (t DateTimeMilli) String() string { 176 return t.ToDateTimeMilliString() 177 } 178 179 func (t DateTimeMilli) GormDataType() string { 180 return "time" 181 } 182 183 // MarshalJSON implements the interface json.Marshal for DateTimeMicro struct. 184 // 实现 MarshalJSON 接口 185 func (t DateTimeMicro) MarshalJSON() ([]byte, error) { 186 return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeMicroString())), nil 187 } 188 189 // UnmarshalJSON implements the interface json.Unmarshal for DateTimeMicro struct. 190 // 实现 UnmarshalJSON 接口 191 func (t *DateTimeMicro) UnmarshalJSON(b []byte) error { 192 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateTimeMicroLayout) 193 if c.Error == nil { 194 *t = DateTimeMicro{c} 195 } 196 return c.Error 197 } 198 199 // String implements the interface Stringer for DateTimeMicro struct. 200 // 实现 Stringer 接口 201 func (t DateTimeMicro) String() string { 202 return t.ToDateTimeMicroString() 203 } 204 205 func (t DateTimeMicro) GormDataType() string { 206 return "time" 207 } 208 209 // MarshalJSON implements the interface json.Marshal for DateTimeNano struct. 210 // 实现 MarshalJSON 接口 211 func (t DateTimeNano) MarshalJSON() ([]byte, error) { 212 return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeNanoString())), nil 213 } 214 215 // UnmarshalJSON implements the interface json.Unmarshal for DateTimeNano struct. 216 // 实现 UnmarshalJSON 接口 217 func (t *DateTimeNano) UnmarshalJSON(b []byte) error { 218 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateTimeNanoLayout) 219 if c.Error == nil { 220 *t = DateTimeNano{c} 221 } 222 return c.Error 223 } 224 225 // String implements the interface Stringer for DateTimeNano struct. 226 // 实现 Stringer 接口 227 func (t DateTimeNano) String() string { 228 return t.ToDateTimeNanoString() 229 } 230 231 func (t DateTimeNano) GormDataType() string { 232 return "time" 233 } 234 235 // MarshalJSON implements the interface json.Marshal for Date struct. 236 // 实现 MarshalJSON 接口 237 func (t Date) MarshalJSON() ([]byte, error) { 238 return []byte(fmt.Sprintf(`"%s"`, t.ToDateString())), nil 239 } 240 241 // UnmarshalJSON implements the interface json.Unmarshal for Date struct. 242 // 实现 UnmarshalJSON 接口 243 func (t *Date) UnmarshalJSON(b []byte) error { 244 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateLayout) 245 if c.Error == nil { 246 *t = Date{c} 247 } 248 return c.Error 249 } 250 251 // String implements the interface Stringer for Date struct. 252 // 实现 Stringer 接口 253 func (t Date) String() string { 254 return t.ToDateString() 255 } 256 257 func (t Date) GormDataType() string { 258 return "time" 259 } 260 261 // MarshalJSON implements the interface json.Marshal for DateMilli struct. 262 // 实现 MarshalJSON 接口 263 func (t DateMilli) MarshalJSON() ([]byte, error) { 264 return []byte(fmt.Sprintf(`"%s"`, t.ToDateMilliString())), nil 265 } 266 267 // UnmarshalJSON implements the interface json.Unmarshal for DateMilli struct. 268 // 实现 UnmarshalJSON 接口 269 func (t *DateMilli) UnmarshalJSON(b []byte) error { 270 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateMilliLayout) 271 if c.Error == nil { 272 *t = DateMilli{c} 273 } 274 return c.Error 275 } 276 277 // String implements the interface Stringer for DateMilli struct. 278 // 实现 Stringer 接口 279 func (t DateMilli) String() string { 280 return t.ToDateMilliString() 281 } 282 283 func (t DateMilli) GormDataType() string { 284 return "time" 285 } 286 287 // MarshalJSON implements the interface json.Marshal for DateMicro struct. 288 // 实现 MarshalJSON 接口 289 func (t DateMicro) MarshalJSON() ([]byte, error) { 290 return []byte(fmt.Sprintf(`"%s"`, t.ToDateMicroString())), nil 291 } 292 293 // UnmarshalJSON implements the interface json.Unmarshal for DateMicro struct. 294 // 实现 UnmarshalJSON 接口 295 func (t *DateMicro) UnmarshalJSON(b []byte) error { 296 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateMicroLayout) 297 if c.Error == nil { 298 *t = DateMicro{c} 299 } 300 return c.Error 301 } 302 303 // String implements the interface Stringer for DateMicro struct. 304 // 实现 Stringer 接口 305 func (t DateMicro) String() string { 306 return t.ToDateMicroString() 307 } 308 309 func (t DateMicro) GormDataType() string { 310 return "time" 311 } 312 313 // MarshalJSON implements the interface json.Marshal for DateNano struct. 314 // 实现 MarshalJSON 接口 315 func (t DateNano) MarshalJSON() ([]byte, error) { 316 return []byte(fmt.Sprintf(`"%s"`, t.ToDateNanoString())), nil 317 } 318 319 // UnmarshalJSON implements the interface json.Unmarshal for DateNano struct. 320 // 实现 UnmarshalJSON 接口 321 func (t *DateNano) UnmarshalJSON(b []byte) error { 322 c := ParseByLayout(string(bytes.Trim(b, `"`)), carbon.DateNanoLayout) 323 if c.Error == nil { 324 *t = DateNano{c} 325 } 326 return c.Error 327 } 328 329 // String implements the interface Stringer for DateNano struct. 330 // 实现 Stringer 接口 331 func (t DateNano) String() string { 332 return t.ToDateNanoString() 333 } 334 335 func (t DateNano) GormDataType() string { 336 return "time" 337 } 338 339 // MarshalJSON implements the interface json.Marshal for Timestamp struct. 340 // 实现 MarshalJSON 接口 341 func (t Timestamp) MarshalJSON() ([]byte, error) { 342 return []byte(fmt.Sprintf(`%d`, t.Timestamp())), nil 343 } 344 345 // UnmarshalJSON implements the interface json.Unmarshal for Timestamp struct. 346 // 实现 UnmarshalJSON 接口 347 func (t *Timestamp) UnmarshalJSON(b []byte) error { 348 ts, _ := strconv.ParseInt(string(b), 10, 64) 349 c := FromTimestamp(ts) 350 if c.Error == nil { 351 *t = Timestamp{c} 352 } 353 return c.Error 354 } 355 356 // String implements the interface Stringer for Timestamp struct. 357 // 实现 Stringer 接口 358 func (t Timestamp) String() string { 359 return t.ToDateTimeString() 360 } 361 362 func (t Timestamp) GormDataType() string { 363 return "time" 364 } 365 366 // MarshalJSON implements the interface json.Marshal for TimestampMilli struct. 367 // 实现 MarshalJSON 接口 368 func (t TimestampMilli) MarshalJSON() ([]byte, error) { 369 return []byte(fmt.Sprintf(`%d`, t.TimestampMilli())), nil 370 } 371 372 // UnmarshalJSON implements the interface json.Unmarshal for TimestampMilli struct. 373 // 实现 UnmarshalJSON 接口 374 func (t *TimestampMilli) UnmarshalJSON(b []byte) error { 375 ts, _ := strconv.ParseInt(string(b), 10, 64) 376 c := FromTimestampMilli(ts) 377 if c.Error == nil { 378 *t = TimestampMilli{c} 379 } 380 return c.Error 381 } 382 383 // String implements the interface Stringer for TimestampMilli struct. 384 // 实现 Stringer 接口 385 func (t TimestampMilli) String() string { 386 return t.ToDateTimeMilliString() 387 } 388 389 func (t TimestampMilli) GormDataType() string { 390 return "int" 391 } 392 393 // MarshalJSON implements the interface MarshalJSON for TimestampMicro struct. 394 // 实现 MarshalJSON 接口 395 func (t TimestampMicro) MarshalJSON() ([]byte, error) { 396 return []byte(fmt.Sprintf(`%d`, t.TimestampMicro())), nil 397 } 398 399 // UnmarshalJSON implements the interface json.Unmarshal for TimestampMicro struct. 400 // 实现 UnmarshalJSON 接口 401 func (t *TimestampMicro) UnmarshalJSON(b []byte) error { 402 ts, _ := strconv.ParseInt(string(b), 10, 64) 403 c := FromTimestampMicro(ts) 404 if c.Error == nil { 405 *t = TimestampMicro{c} 406 } 407 return c.Error 408 } 409 410 // String implements the interface Stringer for TimestampMicro struct. 411 // 实现 Stringer 接口 412 func (t TimestampMicro) String() string { 413 return t.ToDateTimeMicroString() 414 } 415 416 func (t TimestampMicro) GormDataType() string { 417 return "int" 418 } 419 420 // MarshalJSON implements the interface json.Marshal for TimestampNano struct. 421 // 实现 MarshalJSON 接口 422 func (t TimestampNano) MarshalJSON() ([]byte, error) { 423 return []byte(fmt.Sprintf(`%d`, t.TimestampNano())), nil 424 } 425 426 // UnmarshalJSON implements the interface json.Unmarshal for TimestampNano struct. 427 // 实现 UnmarshalJSON 接口 428 func (t *TimestampNano) UnmarshalJSON(b []byte) error { 429 ts, _ := strconv.ParseInt(string(b), 10, 64) 430 c := FromTimestampNano(ts) 431 if c.Error == nil { 432 *t = TimestampNano{c} 433 } 434 return c.Error 435 } 436 437 // String implements the interface Stringer for TimestampNano struct. 438 // 实现 Stringer 接口 439 func (t TimestampNano) String() string { 440 return t.ToDateTimeNanoString() 441 } 442 443 func (t TimestampNano) GormDataType() string { 444 return "int" 445 }