atom

package
v0.4.13 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

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

View Source
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 FuzzParseTime

func FuzzParseTime(formats []string, value string) (time.Time, error)

func FuzzParseTimeLoc

func FuzzParseTimeLoc(formats []string, value string, loc *time.Location) (time.Time, error)

func NewLoc added in v0.4.8

func NewLoc(offsetHours float64) *time.Location

func NewLocWithName added in v0.4.8

func NewLocWithName(offsetHours float64) *time.Location

func ParseClassicDateTime

func ParseClassicDateTime(val string, loc *time.Location) (time.Time, error)

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

type Time struct {
	time.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 MayParseTime(v any) Time

func MayParseTimeLoc

func MayParseTimeLoc(v any, loc *time.Location) Time

func Now

func Now() Time

func NowIn added in v0.4.7

func NowIn(offsetHours float64) Time

func NowPtr

func NowPtr() *Time

func NowUTC

func NowUTC() Time

func NowUTCPtr

func NowUTCPtr() *Time

func ParseTime

func ParseTime(v any) (Time, error)

func ParseTimeLoc

func ParseTimeLoc(v any, loc *time.Location) (Time, error)

func WrapTime

func WrapTime(t time.Time) Time

func (Time) Add

func (t Time) Add(d time.Duration) Time

func (Time) AddDate

func (t Time) AddDate(years int, months int, days int) Time

func (Time) After

func (t Time) After(u Time) bool

func (Time) Before

func (t Time) Before(u Time) bool

func (Time) Compare added in v0.4.7

func (t Time) Compare(u Time) int

func (Time) EndOfDay

func (t Time) EndOfDay() Time

At 23:59:59.999999.

func (Time) EndOfHour

func (t Time) EndOfHour() Time

func (Time) EndOfMin

func (t Time) EndOfMin() Time

func (Time) EndOfMonth

func (t Time) EndOfMonth() Time

func (Time) EndOfWeek

func (t Time) EndOfWeek(start time.Weekday) Time

func (Time) FormatClassic

func (t Time) FormatClassic() string

Format as 2006/01/02 15:04:05

func (Time) FormatClassicLocale

func (t Time) FormatClassicLocale() string

Format as 2006/01/02 15:04:05 (MST)

func (Time) FormatDate

func (t Time) FormatDate() string

Format as 2006-01-02

func (Time) FormatRFC3339

func (t Time) FormatRFC3339() string

Format as time.RFC3339

func (Time) FormatRFC3339Nano

func (t Time) FormatRFC3339Nano() string

Format as time.RFC3339Nano

func (Time) FormatStd

func (t Time) FormatStd() string

Format as 2006-01-02 15:04:05

func (Time) FormatStdLocale

func (t Time) FormatStdLocale() string

Format as 2006-01-02 15:04:05 (MST)

func (Time) FormatStdMilli

func (t Time) FormatStdMilli() string

Format as 2006-01-02 15:04:05.000

func (Time) GoString

func (t Time) GoString() string

func (Time) InLoc added in v0.4.7

func (t Time) InLoc(z *time.Location) Time

func (Time) InZone

func (t Time) InZone(hourOffset float64) Time

Change to time zone offset in hours.

func (Time) LastWeekday

func (t Time) LastWeekday(w time.Weekday) Time

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

Implements encoding/json Marshaler

func (Time) NextWeekday

func (t Time) NextWeekday(w time.Weekday) Time

func (*Time) Scan

func (et *Time) Scan(value interface{}) error

Implements sql.Scanner in database/sql.

func (*Time) ScanLoc

func (et *Time) ScanLoc(value interface{}, loc *time.Location) error

func (Time) StartOfDay

func (t Time) StartOfDay() Time

At 00:00:00.000000.

func (Time) StartOfHour

func (t Time) StartOfHour() Time

func (Time) StartOfMin

func (t Time) StartOfMin() Time

func (Time) StartOfMonth

func (t Time) StartOfMonth() Time

func (Time) StartOfWeek

func (t Time) StartOfWeek(start time.Weekday) Time

func (Time) String

func (t Time) String() string

func (Time) Sub

func (t Time) Sub(u Time) time.Duration

func (Time) ToTime deprecated

func (t Time) ToTime() time.Time

Deprecated: change to Time.Unwrap.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

Implements encoding/json Unmarshaler.

func (Time) Unwrap

func (t Time) Unwrap() time.Time

func (Time) Value

func (t Time) Value() (driver.Value, error)

Implements driver.Valuer in database/sql.

Jump to

Keyboard shortcuts

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