2020-09-14 10:15:23 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2024-11-25 16:18:25 +08:00
|
|
|
"bytes"
|
2020-10-12 09:45:17 +08:00
|
|
|
"database/sql/driver"
|
2024-11-25 01:05:42 +08:00
|
|
|
"errors"
|
2020-09-14 10:15:23 +08:00
|
|
|
"fmt"
|
2024-11-25 16:18:25 +08:00
|
|
|
"strconv"
|
2020-10-12 09:45:17 +08:00
|
|
|
"time"
|
2020-09-14 10:15:23 +08:00
|
|
|
)
|
|
|
|
|
2024-11-25 01:05:42 +08:00
|
|
|
// returns a failed scan error.
|
|
|
|
// 失败的扫描错误
|
|
|
|
var failedScanError = func(src interface{}) error {
|
|
|
|
return errors.New(fmt.Sprintf("failed to scan value: %v", src))
|
|
|
|
}
|
|
|
|
|
2021-07-24 01:03:39 +08:00
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
2024-11-25 01:05:42 +08:00
|
|
|
func (c *Carbon) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*c = Parse(string(v))
|
|
|
|
case string:
|
|
|
|
*c = Parse(v)
|
|
|
|
case time.Time:
|
|
|
|
*c = CreateFromStdTime(v)
|
|
|
|
}
|
2024-11-25 04:19:40 +08:00
|
|
|
if c.Error == nil {
|
|
|
|
return nil
|
2020-10-12 09:45:17 +08:00
|
|
|
}
|
2024-11-25 04:19:40 +08:00
|
|
|
return failedScanError(src)
|
2020-10-12 09:45:17 +08:00
|
|
|
}
|
2020-10-12 09:51:16 +08:00
|
|
|
|
2021-08-10 10:57:22 +08:00
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
2020-10-12 09:45:17 +08:00
|
|
|
func (c Carbon) Value() (driver.Value, error) {
|
2021-04-07 12:03:31 +08:00
|
|
|
if c.IsZero() {
|
2020-10-12 09:45:17 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
return c.StdTime(), nil
|
2023-12-22 16:06:38 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// MarshalJSON implements the interface json.Marshal for Carbon struct.
|
|
|
|
// 实现 json.Marshaler 接口
|
|
|
|
func (c Carbon) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, c.Layout(c.layout))), nil
|
2020-10-12 09:45:17 +08:00
|
|
|
}
|
2024-10-17 15:15:30 +08:00
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for Carbon struct.
|
|
|
|
// 实现 json.Unmarshaler 接口
|
|
|
|
func (c *Carbon) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*c = ParseByLayout(value, c.layout)
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *DateTime) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDateTime(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDateTime(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDateTime(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *DateTime) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for DateTime struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *DateTime) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateTimeLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDateTime(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *DateTimeMilli) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDateTimeMilli(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDateTimeMilli(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDateTimeMilli(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *DateTimeMilli) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for DateTimeMilli struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *DateTimeMilli) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateTimeMilliLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDateTimeMilli(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *DateTimeMicro) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDateTimeMicro(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDateTimeMicro(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDateTimeMicro(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *DateTimeMicro) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for DateTimeMicro struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *DateTimeMicro) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateTimeMicroLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDateTimeMicro(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *DateTimeNano) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDateTimeNano(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDateTimeNano(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDateTimeNano(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *DateTimeNano) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for DateTimeNano struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *DateTimeNano) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateTimeNanoLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDateTimeNano(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *Date) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDate(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDate(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDate(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *Date) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for Date struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *Date) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDate(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *DateMilli) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDateMilli(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDateMilli(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDateMilli(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *DateMilli) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for DateMilli struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *DateMilli) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateMilliLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDateMilli(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *DateMicro) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDateMicro(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDateMicro(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDateMicro(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *DateMicro) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for DateMicro struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *DateMicro) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateMicroLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDateMicro(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *DateNano) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewDateNano(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewDateNano(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewDateNano(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *DateNano) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for DateNano struct.
|
|
|
|
// 实现 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 接口
|
|
|
|
func (t *DateNano) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := ParseByLayout(value, DateNanoLayout)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewDateNano(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *Time) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTime(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTime(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTime(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *Time) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for Time struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t Time) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, t.ToTimeString())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for Time struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *Time) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-29 11:23:13 +08:00
|
|
|
year, month, day := Now().Date()
|
|
|
|
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeLayout)
|
2024-11-25 16:18:25 +08:00
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTime(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *TimeMilli) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTimeMilli(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTimeMilli(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTimeMilli(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *TimeMilli) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for TimeMilli struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t TimeMilli) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, t.ToTimeMilliString())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimeMilli struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *TimeMilli) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-29 11:23:13 +08:00
|
|
|
year, month, day := Now().Date()
|
|
|
|
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeMilliLayout)
|
2024-11-25 16:18:25 +08:00
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTimeMilli(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *TimeMicro) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTimeMicro(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTimeMicro(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTimeMicro(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *TimeMicro) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for TimeMicro struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t TimeMicro) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, t.ToTimeMicroString())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimeMicro struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *TimeMicro) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-29 11:23:13 +08:00
|
|
|
year, month, day := Now().Date()
|
|
|
|
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeMicroLayout)
|
2024-11-25 16:18:25 +08:00
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTimeMicro(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *TimeNano) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTimeNano(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTimeNano(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTimeNano(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *TimeNano) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for TimeNano struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t TimeNano) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, t.ToTimeNanoString())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimeNano struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *TimeNano) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-29 11:23:13 +08:00
|
|
|
year, month, day := Now().Date()
|
|
|
|
c := ParseByLayout(fmt.Sprintf("%d-%d-%d %s", year, month, day, value), DateTimeNanoLayout)
|
2024-11-25 16:18:25 +08:00
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTimeNano(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *Timestamp) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTimestamp(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTimestamp(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTimestamp(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *Timestamp) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for Timestamp struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t Timestamp) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.Timestamp())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for Timestamp struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *Timestamp) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ts, _ := strconv.ParseInt(value, 10, 64)
|
|
|
|
c := CreateFromTimestamp(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTimestamp(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *TimestampMilli) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTimestampMilli(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTimestampMilli(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTimestampMilli(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *TimestampMilli) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for TimestampMilli struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t TimestampMilli) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.TimestampMilli())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimestampMilli struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *TimestampMilli) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ts, _ := strconv.ParseInt(value, 10, 64)
|
|
|
|
c := CreateFromTimestampMilli(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTimestampMilli(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *TimestampMicro) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTimestampMicro(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTimestampMicro(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTimestampMicro(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *TimestampMicro) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface MarshalJSON for TimestampMicro struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t TimestampMicro) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.TimestampMicro())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimestampMicro struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *TimestampMicro) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ts, _ := strconv.ParseInt(value, 10, 64)
|
|
|
|
c := CreateFromTimestampMicro(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTimestampMicro(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
|
|
|
func (t *TimestampNano) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*t = NewTimestampNano(Parse(string(v)))
|
|
|
|
case string:
|
|
|
|
*t = NewTimestampNano(Parse(v))
|
|
|
|
case time.Time:
|
|
|
|
*t = NewTimestampNano(CreateFromStdTime(v))
|
|
|
|
}
|
|
|
|
if t.Error == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return failedScanError(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
|
|
|
func (t *TimestampNano) Value() (driver.Value, error) {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.StdTime(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface json.Marshal for TimestampNano struct.
|
|
|
|
// 实现 MarshalJSON 接口
|
|
|
|
func (t TimestampNano) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`%d`, t.TimestampNano())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface json.Unmarshal for TimestampNano struct.
|
|
|
|
// 实现 UnmarshalJSON 接口
|
|
|
|
func (t *TimestampNano) UnmarshalJSON(b []byte) error {
|
|
|
|
value := fmt.Sprintf("%s", bytes.Trim(b, `"`))
|
|
|
|
if value == "" || value == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ts, _ := strconv.ParseInt(value, 10, 64)
|
|
|
|
c := CreateFromTimestampNano(ts)
|
|
|
|
if c.Error == nil {
|
|
|
|
*t = NewTimestampNano(c)
|
|
|
|
}
|
|
|
|
return c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 outputs timestamp with second.
|
|
|
|
// 输出秒级时间戳
|
|
|
|
func (t Timestamp) Int64() int64 {
|
|
|
|
return t.Timestamp()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 outputs timestamp with millisecond.
|
|
|
|
// 输出豪秒级时间戳
|
|
|
|
func (t TimestampMilli) Int64() int64 {
|
|
|
|
return t.TimestampMilli()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 outputs timestamp with microsecond.
|
|
|
|
// 输出微秒级时间戳
|
|
|
|
func (t TimestampMicro) Int64() int64 {
|
|
|
|
return t.TimestampMicro()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 outputs timestamp with nanosecond.
|
|
|
|
// 输出纳秒级时间戳
|
|
|
|
func (t TimestampNano) Int64() int64 {
|
|
|
|
return t.TimestampNano()
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the interface Stringer for DateTime struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t DateTime) String() string {
|
|
|
|
return t.ToDateTimeString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for DateTimeMilli struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t DateTimeMilli) String() string {
|
|
|
|
return t.ToDateTimeMilliString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for DateTimeMicro struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t DateTimeMicro) String() string {
|
|
|
|
return t.ToDateTimeMicroString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for DateTimeNano struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t DateTimeNano) String() string {
|
|
|
|
return t.ToDateTimeNanoString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for Date struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t Date) String() string {
|
|
|
|
return t.ToDateString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for DateMilli struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t DateMilli) String() string {
|
|
|
|
return t.ToDateMilliString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for DateMicro struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t DateMicro) String() string {
|
|
|
|
return t.ToDateMicroString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for DateNano struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t DateNano) String() string {
|
|
|
|
return t.ToDateNanoString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for Time struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t Time) String() string {
|
|
|
|
return t.ToTimeString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for TimeMilli struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t TimeMilli) String() string {
|
|
|
|
return t.ToTimeMilliString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for TimeMicro struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t TimeMicro) String() string {
|
|
|
|
return t.ToTimeMicroString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for TimeNano struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t TimeNano) String() string {
|
|
|
|
return t.ToTimeNanoString()
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for Timestamp struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t Timestamp) String() string {
|
|
|
|
return strconv.FormatInt(t.Timestamp(), 10)
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for TimestampMilli struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t TimestampMilli) String() string {
|
|
|
|
return strconv.FormatInt(t.TimestampMilli(), 10)
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for TimestampMicro struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t TimestampMicro) String() string {
|
|
|
|
return strconv.FormatInt(t.TimestampMicro(), 10)
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 16:18:25 +08:00
|
|
|
// String implements the interface Stringer for TimestampNano struct.
|
|
|
|
// 实现 Stringer 接口
|
|
|
|
func (t TimestampNano) String() string {
|
|
|
|
return strconv.FormatInt(t.TimestampNano(), 10)
|
2024-10-17 15:15:30 +08:00
|
|
|
}
|