Documentation
¶
Overview ¶
Package for time processing.
The core type in this package is Time. Time is an enhanced wrapper of time.Time. You can use Time directly in your codebase or only use it as a tool for time.Time processing, e.g.,
var monday time.Time = atom.WrapTime(time.Now()).StartOfWeek(time.Monday).Unwrap()
Time implements [sql.Scanner] and driver.Valuer for database values and [json.Marshaler] and [json.Unmarshaler] for json processing.
Marshaling and unmarshaling behaviours are fully customizable.
Use SetTimeMarshalFormat to change the marshal format, by default Time is marshalled as millseconds since unix epoch.
Time can be unmarshaled from various formats, e.g,
- time.RFC3339
- time.RFC3339Nano
- `2006-01-02 15:04:05.999999`
- `2006-01-02`
- `2006-01-02T15:04:05.999999`
- `millseconds since unix epoch`
- `seconds since unix epoch`
You can add extra unmarshalling formats using AddTimeParseFormat, or overwrite the unmarshalling formats entirely using SetTimeParseFormat.
Index ¶
- Constants
- func AddTimeParseFormat(fmt ...string)
- func FuzzParseTime(formats []string, value string) (time.Time, error)
- func FuzzParseTimeLoc(formats []string, value string, loc *time.Location) (time.Time, error)
- func NewLoc(offsetHours float64) *time.Location
- func NewLocWithName(offsetHours float64) *time.Location
- func ParseClassicDateTime(val string, loc *time.Location) (time.Time, error)
- func SetTimeMarshalFormat(fmt string)
- func SetTimeParseFormat(fmt ...string)
- type Time
- func MayParseTime(v any) Time
- func MayParseTimeLoc(v any, loc *time.Location) Time
- func Now() Time
- func NowIn(offsetHours float64) Time
- func NowPtr() *Time
- func NowUTC() Time
- func NowUTCPtr() *Time
- func ParseTime(v any) (Time, error)
- func ParseTimeLoc(v any, loc *time.Location) (Time, error)
- func WrapTime(t time.Time) Time
- func (t Time) Add(d time.Duration) Time
- func (t Time) AddDate(years int, months int, days int) Time
- func (t Time) After(u Time) bool
- func (t Time) Before(u Time) bool
- func (t Time) Compare(u Time) int
- func (t Time) EndOfDay() Time
- func (t Time) EndOfHour() Time
- func (t Time) EndOfMin() Time
- func (t Time) EndOfMonth() Time
- func (t Time) EndOfWeek(start time.Weekday) Time
- func (t Time) FormatClassic() string
- func (t Time) FormatClassicLocale() string
- func (t Time) FormatDate() string
- func (t Time) FormatRFC3339() string
- func (t Time) FormatRFC3339Nano() string
- func (t Time) FormatStd() string
- func (t Time) FormatStdLocale() string
- func (t Time) FormatStdMilli() string
- func (t Time) GoString() string
- func (t Time) InLoc(z *time.Location) Time
- func (t Time) InZone(hourOffset float64) Time
- func (t Time) LastWeekday(w time.Weekday) Time
- func (t Time) MarshalJSON() ([]byte, error)
- func (t Time) NextWeekday(w time.Weekday) Time
- func (et *Time) Scan(value interface{}) error
- func (et *Time) ScanLoc(value interface{}, loc *time.Location) error
- func (t Time) StartOfDay() Time
- func (t Time) StartOfHour() Time
- func (t Time) StartOfMin() Time
- func (t Time) StartOfMonth() Time
- func (t Time) StartOfWeek(start time.Weekday) Time
- func (t Time) String() string
- func (t Time) Sub(u Time) time.Duration
- func (t Time) ToTime() time.Timedeprecated
- func (t *Time) UnmarshalJSON(b []byte) error
- func (t Time) Unwrap() time.Time
- func (t Time) Value() (driver.Value, error)
Constants ¶
const ( ClassicDateTimeLocaleFormat = "2006/01/02 15:04:05 (MST)" ClassicDateTimeFormat = "2006/01/02 15:04:05" StdDateTimeFormat = "2006-01-02 15:04:05" StdDateTimeMilliFormat = "2006-01-02 15:04:05.000" StdDateTimeLocaleFormat = "2006-01-02 15:04:05 (MST)" SQLDateTimeFormat = "2006-01-02 15:04:05.999999" SQLDateTimeFormatWithT = "2006-01-02T15:04:05.999999" SQLDateFormat = "2006-01-02" )
Variables ¶
This section is empty.
Functions ¶
func AddTimeParseFormat ¶
func AddTimeParseFormat(fmt ...string)
func FuzzParseTimeLoc ¶
func NewLocWithName ¶ added in v0.4.8
func ParseClassicDateTime ¶
Parse classic datetime format using patterns: "2006-01-02 15:04:05", "2006/01/02 15:04:05".
func SetTimeMarshalFormat ¶
func SetTimeMarshalFormat(fmt string)
func SetTimeParseFormat ¶
func SetTimeParseFormat(fmt ...string)
Types ¶
type Time ¶
Enhanced wrapper of time.Time.
This type implements sql.Scanner and driver.Valuer, it can be safely used in GORM just like time.Time.
It also implements json/encoding Marshaler and Unmarshaler to support json marshalling.
In previous releases, Time was a type alias to time.Time. Since v0.1.2, Time embeds time.Time to access all of it's methods.
To cast from time.Time to Time, use WrapTime method. To cast from Time to time.Time, use Time.Unwrap method.
By default, Time support following unmarshaling formats:
- time.RFC3339
- time.RFC3339Nano
- 2006-01-02 15:04:05.999999
- 2006-01-02
- 2006-01-02T15:04:05.999999
- millseconds since unix epoch
- seconds since unix epoch
By default, Time is marshaled as millseconds since unix epoch. You can change this behaviour though SetTimeMarshalFormat.
func MayParseTime ¶
func (Time) EndOfMonth ¶
func (Time) FormatClassicLocale ¶
Format as 2006/01/02 15:04:05 (MST)
func (Time) FormatStdLocale ¶
Format as 2006-01-02 15:04:05 (MST)
func (Time) FormatStdMilli ¶
Format as 2006-01-02 15:04:05.000
func (Time) MarshalJSON ¶
Implements encoding/json Marshaler
func (Time) StartOfHour ¶
func (Time) StartOfMin ¶
func (Time) StartOfMonth ¶
func (Time) ToTime
deprecated
Deprecated: change to Time.Unwrap.
func (*Time) UnmarshalJSON ¶
Implements encoding/json Unmarshaler.