代码拉取完成,页面将自动刷新
package carbon
import (
"bytes"
"database/sql/driver"
)
// FormatTyper defines a FormatTyper interface.
type FormatTyper interface {
~string
Format() string
}
// FormatType defines a FormatType generic struct.
type FormatType[T FormatTyper] struct {
*Carbon
}
// NewFormatType returns a new FormatType generic instance.
func NewFormatType[T FormatTyper](c *Carbon) *FormatType[T] {
return &FormatType[T]{
Carbon: c,
}
}
// Scan implements driver.Scanner interface for FormatType generic struct.
func (t *FormatType[T]) Scan(src any) error {
var c *Carbon
switch v := src.(type) {
case nil:
return nil
case []byte:
c = Parse(string(v), DefaultTimezone)
case string:
c = Parse(v, DefaultTimezone)
case int64:
c = CreateFromTimestamp(v, DefaultTimezone)
case StdTime:
c = CreateFromStdTime(v, DefaultTimezone)
case *StdTime:
c = CreateFromStdTime(*v, DefaultTimezone)
default:
return ErrFailedScan(v)
}
*t = *NewFormatType[T](c)
return t.Error
}
// Value implements driver.Valuer interface for FormatType generic struct.
func (t FormatType[T]) Value() (driver.Value, error) {
if t.IsNil() || t.IsZero() || t.IsEmpty() {
return nil, nil
}
if t.HasError() {
return nil, t.Error
}
return t.StdTime(), nil
}
// MarshalJSON implements json.Marshal interface for FormatType generic struct.
func (t FormatType[T]) MarshalJSON() ([]byte, error) {
if t.IsNil() || t.IsZero() || t.IsEmpty() {
return []byte(`null`), nil
}
if t.HasError() {
return []byte(`null`), t.Error
}
v := t.Format(t.getFormat())
b := make([]byte, 0, len(v)+2)
b = append(b, '"')
b = append(b, v...)
b = append(b, '"')
return b, nil
}
// UnmarshalJSON implements json.Unmarshal interface for FormatType generic struct.
func (t *FormatType[T]) UnmarshalJSON(src []byte) error {
v := string(bytes.Trim(src, `"`))
if v == "" || v == "null" {
return nil
}
*t = *NewFormatType[T](ParseByFormat(v, t.getFormat()))
return t.Error
}
// String implements Stringer interface for FormatType generic struct.
func (t *FormatType[T]) String() string {
if t == nil || t.IsInvalid() || t.IsZero() {
return ""
}
return t.Format(t.getFormat())
}
// getFormat returns the set format.
func (t *FormatType[T]) getFormat() string {
var typer T
return typer.Format()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。