time

package
v0.31.10 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package time has some utilities for time.Time values

Index

Constants

View Source
const (
	// Zero will generate a zero time
	Zero = Const(0)
	// Now will generate a current time
	Now = Const(1)
	// Current is a synonym for Now
	Current = Const(1)
)
View Source
const (
	ANSIC       = "Mon Jan _2 15:04:05 2006"
	UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
	RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
	RFC822      = "02 Jan 06 15:04 MST"
	RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
	RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
	RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
	RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
	RFC3339     = "2006-01-02T15:04:05Z07:00"
	RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
	Kitchen     = "3:04PM"
	KitchenLc   = "3:04pm"

	Stamp      = "Jan _2 15:04:05"
	StampMilli = "Jan _2 15:04:05.000"
	StampMicro = "Jan _2 15:04:05.000000"
	StampNano  = "Jan _2 15:04:05.000000000"

	DateOnlyFormat = "2006-01-02"
	TimeOnlyFormat = "15:04:05"

	UsDate              = "1/2/2006"
	ShortUsDate         = "1/2/06"
	EuroDate            = "2/1/2006"
	UsDateTime          = "1/2/2006 3:04 PM"
	UsDateTimeLc        = "1/2/2006 3:04 pm"
	EuroDateTime        = "2/1/2006 15:04"
	UsTime              = "3:04 PM"
	UsTimeLc            = "3:04 pm"
	EuroTime            = "15:04"
	UsDateTimeSeconds   = "1/2/2006 3:04:05 PM"
	UsDateTimeSecondsLc = "1/2/2006 3:04:05 pm"
	EuroDateTimeSeconds = "2/1/2006 15:04:00"
	LongDateDOW         = "Monday, January 2, 2006"
	LongDate            = "January 2, 2006"
	SqlDate             = "2006-01-02 15:04:05.000000-07"
)

Variables

This section is empty.

Functions

func As

func As(t time.Time, location *time.Location) time.Time

As will express the date and time at a particular location. In other words, if the date and time is 4:30, it will be 4:30 in the given timezone.

func AtGMTOffset added in v0.19.2

func AtGMTOffset(t time.Time, tzOffset int) time.Time

AtGMTOffset returns a new time that represents the same instant as the given time, but with the location changed to the given offset in minutes from GMT, such that the resulting time will now be in the local time of the timezone specified by the offset.

func DateOnly

func DateOnly(t time.Time) time.Time

DateOnly returns a new time.Time object set to only the date portion of the given time.

func DayDiff

func DayDiff(dt1, dt2 time.Time) int

DayDiff returns the number of days between the two dates, taking into consideration only the day of the month. So 1 am on the 2nd and 11 pm on the 1st are a day apart.

If dt1 is before dt2, the result will be negative. If they are the same day, the result is zero. The dates should already be set to the timezone that will determine what day the datetime falls on. This function also takes into account leap years (assuming that you are dealing with Gregorian dates).

func FromSqlDateTime

func FromSqlDateTime(s string) (t time.Time)

FromSqlDateTime will convert a SQL Date, Time, DateTime or Timestamp string to a time.Time. Various SQL databases express their times slightly differently, and this tries to interpret what is attempting to be expressed. It can also handle unix time strings that are +- from the 1970 epoch, including fractional times up to the microsecond level.

If the SQL date time string does not have timezone information, the resulting value will be in UTC time. If an error occurs, the returned value will be the zero date.

func IsLeap

func IsLeap(year int) (isLeap bool)

IsLeap returns true if the year is a leap year.

func LayoutHasDate

func LayoutHasDate(layout string) bool

LayoutHasDate returns true if the given parse layout indicates a date.

func LayoutHasTime

func LayoutHasTime(layout string) bool

LayoutHasTime returns true if the given parse layout indicates a time.

func NewDate

func NewDate(year int, month time.Month, day int) time.Time

NewDate creates a time.Time that is treated as a date only.

func NewDateTime

func NewDateTime(year int, month time.Month, day, hour, min, sec, nsec int) time.Time

NewDateTime creates a time.Time in UTC.

func NewTime

func NewTime(hour, min, sec, nsec int) time.Time

NewTime creates a time.Time that only represents a time of day.

func NumLeaps

func NumLeaps(year int) (leaps int)

NumLeaps returns the number of leap years that have occurred since year zero. Note that leap years changed slightly with the Gregorian calendar in 1582.

func ParseForgiving

func ParseForgiving(layout, value string) (time.Time, error)

ParseForgiving will parse a value allowing for extra spaces or a difference in case for am/pm.

func ParseInOffset

func ParseInOffset(layout, value string, tz string, tzOffset int) (t time.Time, err error)

ParseInOffset is like time.ParseInLocation but uses the given timezone name and offset in minutes from UTC to be the location of the parsed time. If the timezone name is blank, the offset will only be used.

func TimeOnly

func TimeOnly(t time.Time) time.Time

TimeOnly returns a new time.Time object set to only the time portion of the given time.

func ToSqlDateTime

func ToSqlDateTime(t time.Time) (s string)

ToSqlDateTime returns a SQL string representing the give time.Time value.

Types

type Const

type Const int

Const is a constant that represents a time to be instantiated later.

func (Const) Time

func (c Const) Time() (t time.Time)

Time returns the time corresponding to the given constant

Jump to

Keyboard shortcuts

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