ttypes

package
v0.0.41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2025 License: MIT Imports: 5 Imported by: 5

Documentation

Overview

Package ttypes 定义特殊类型支持,比如日期类型, golang的日期类型在转换成json时不能指定日期的格式,ttypes.NormalDate 可以按照 DateFormat指定的格式输出到json中或者fmt.Println. sql包中的NullTime也不支持格式和json格式输出,对应的NilableDateTime支持。本包包括:

NormalDate 支持格式和json输出的日期类型, 格式由 DateFormat 变量指定

NormalDatetime 支持格式和json输出的时间类型,格式由 DatetimeFormat 指定

NilableDate 可以为空的日期类型,扩展 sql.NullTime, 支持格式及json输出,格式由 DateFormat 变量指定

NilableDatetime 可以为空的时间类型,扩展 sql.NullTime, 支持格式及json输出 ,格式由 DatetimeFormat 指定

NilableString, 可以为空的String类型,扩展 sql.NullString,支持json输出,提供与string的转换及其他字符串操作功能

Index

Constants

This section is empty.

Variables

View Source
var (
	// DateFormat 指定日期格式
	DateFormat = "2006-01-02"
	// DatetimeFormat 指定时间格式
	DatetimeFormat = "2006-01-02 15:04:05"

	ZeroTimeAsNil = true
)

Functions

This section is empty.

Types

type NilableDate

type NilableDate struct {
	sql.NullTime
}

NilableDate 可以为空的日期类型,扩展NullTime, 支持格式及json输出,格式由 DateFormat 变量指定,实现 fmt.Stringer, json.Unmarshaler, json.Marshaler 接口

func FromDate

func FromDate(d time.Time) *NilableDate

FromDate 转换time.Time类型为 NilableDate 类型,返回值为指针,如果接收变量为 NilableDate 类型,需要使用加*号来解引用: *nilableDate

func GetNilDateValue added in v0.0.29

func GetNilDateValue() *NilableDate

func (NilableDate) MarshalJSON

func (ndt NilableDate) MarshalJSON() ([]byte, error)

MarshalJSON 实现 json.Marshaler 接口

func (NilableDate) String

func (ndt NilableDate) String() string

String 实现 fmt.Stringer 接口

func (*NilableDate) ToTimePointer added in v0.0.18

func (ndt *NilableDate) ToTimePointer() *time.Time

func (*NilableDate) UnmarshalJSON

func (ndt *NilableDate) UnmarshalJSON(b []byte) error

UnmarshalJSON 实现 json.Unmarshaler

type NilableDatetime

type NilableDatetime struct {
	sql.NullTime
}

NilableDatetime 可以为空的时间类型,扩展 sql.NullTime, 支持格式及json输出,格式由 DatetimeFormat 变量指定

func FromDatetime

func FromDatetime(d time.Time) *NilableDatetime

FromDatetime 转换 time.Time 类型为 NilableDatetime 类型,返回值为指针,如果接收变量为 NilableDatetime 类型,需要使用加*号来解引用: *nilableDatetime

func GetNilDatetimeValue added in v0.0.29

func GetNilDatetimeValue() *NilableDatetime

func (NilableDatetime) MarshalJSON

func (s NilableDatetime) MarshalJSON() ([]byte, error)

MarshalJSON 实现 json.Marshaler 接口

func (NilableDatetime) String

func (s NilableDatetime) String() string

String 实现 fmt.Stringer 接口

func (*NilableDatetime) ToTimePointer added in v0.0.18

func (s *NilableDatetime) ToTimePointer() *time.Time

func (*NilableDatetime) UnmarshalJSON

func (s *NilableDatetime) UnmarshalJSON(b []byte) error

UnmarshalJSON 实现 json.Unmarshaler

type NilableString

type NilableString struct {
	sql.NullString
}

NilableString 可以为空的string类型,扩展 sql.NullString, 支持友好的json输出,提供常用的操作函数

func FromString

func FromString(s string) *NilableString

FromString 转换string类型为 NilableString 类型,返回值为指针,如果接收变量为 NilableString 类型,需要使用加*号来解引用: *nilableString

func FromStringEmptyAsNil added in v0.0.38

func FromStringEmptyAsNil(s string) *NilableString

func GetNilString added in v0.0.29

func GetNilString() *NilableString

func (NilableString) MarshalText added in v0.0.15

func (s NilableString) MarshalText() ([]byte, error)

MarshalText 实现 encoding.TextMarshaler 接口

func (*NilableString) StringNilAsDefault

func (s *NilableString) StringNilAsDefault(def string) string

StringNilAsDefault 返回当前对象包含的string值,如果当前对象是nil,返回 def 参数指定的字符串

func (*NilableString) StringNilAsEmpty

func (s *NilableString) StringNilAsEmpty() string

StringNilAsEmpty 返回当前对象包含的string值,如果当前对象是nil,则返回""字符串

func (*NilableString) UnmarshalText added in v0.0.15

func (s *NilableString) UnmarshalText(b []byte) error

UnmarshalText 实现 encoding.TextUnmarshaler 接口

type NormalDate

type NormalDate time.Time

NormalDate 支持按日期格式输出的日期类型, 格式由 DateFormat 全局变量指定, 实现 fmt.Stringer, driver.Valuer, json.Unmarshaler, json.Marshaler 接口

func ParseNormalDate added in v0.0.29

func ParseNormalDate(sDate string) (*NormalDate, error)

func ParseNormalDateInLocal added in v0.0.31

func ParseNormalDateInLocal(sDate string, loc *time.Location) (*NormalDate, error)

func ParseNormalDateInUTC added in v0.0.31

func ParseNormalDateInUTC(sDate string) (*NormalDate, error)

func (NormalDate) MarshalJSON

func (ndt NormalDate) MarshalJSON() ([]byte, error)

MarshalJSON 实现 json.Marshaler 接口

func (*NormalDate) MonthFmtString added in v0.0.29

func (ndt *NormalDate) MonthFmtString() string

func (NormalDate) String

func (ndt NormalDate) String() string

String 实现 fmt.Stringer 接口

func (*NormalDate) ToTimePointer added in v0.0.18

func (ndt *NormalDate) ToTimePointer() *time.Time

func (*NormalDate) UnmarshalJSON

func (ndt *NormalDate) UnmarshalJSON(b []byte) error

UnmarshalJSON 实现 json.Unmarshaler

func (NormalDate) Value

func (ndt NormalDate) Value() (driver.Value, error)

Value 实现 driver.Valuer

type NormalDatetime

type NormalDatetime time.Time

NormalDatetime 支持按日期格式输出的日期类型, 格式由 DatetimeFormat 全局变量指定, 实现fmt.Stringer, driver.Valuer, json.Unmarshaler, json.Marshaler 接口

func ParseNormalDatetime added in v0.0.29

func ParseNormalDatetime(sDate string) (*NormalDatetime, error)

func ParseNormalDatetimeInLocation added in v0.0.31

func ParseNormalDatetimeInLocation(sDate string, loc *time.Location) (*NormalDatetime, error)

func ParseNormalDatetimeInUTC added in v0.0.31

func ParseNormalDatetimeInUTC(sDate string) (*NormalDatetime, error)

func (NormalDatetime) MarshalJSON

func (ndt NormalDatetime) MarshalJSON() ([]byte, error)

MarshalJSON 实现 json.Marshaler 接口

func (NormalDatetime) String

func (ndt NormalDatetime) String() string

String 实现 fmt.Stringer 接口

func (*NormalDatetime) ToTimePointer added in v0.0.18

func (ndt *NormalDatetime) ToTimePointer() *time.Time

func (*NormalDatetime) UnmarshalJSON

func (ndt *NormalDatetime) UnmarshalJSON(b []byte) error

UnmarshalJSON 实现 json.Unmarshaler

func (NormalDatetime) Value

func (ndt NormalDatetime) Value() (driver.Value, error)

Value 实现 driver.Valuer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL