Documentation ¶
Overview ¶
Package time has some utilities for time.Time values
Index ¶
- Constants
- func As(t time.Time, location *time.Location) time.Time
- func AtGMTOffset(t time.Time, tzOffset int) time.Time
- func DateOnly(t time.Time) time.Time
- func DayDiff(dt1, dt2 time.Time) int
- func FromSqlDateTime(s string) (t time.Time)
- func IsLeap(year int) (isLeap bool)
- func LayoutHasDate(layout string) bool
- func LayoutHasTime(layout string) bool
- func NewDate(year int, month time.Month, day int) time.Time
- func NewDateTime(year int, month time.Month, day, hour, min, sec, nsec int) time.Time
- func NewTime(hour, min, sec, nsec int) time.Time
- func NumLeaps(year int) (leaps int)
- func ParseForgiving(layout, value string) (time.Time, error)
- func ParseInOffset(layout, value string, tz string, tzOffset int) (t time.Time, err error)
- func TimeOnly(t time.Time) time.Time
- func ToSqlDateTime(t time.Time) (s string)
- type Const
Constants ¶
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) )
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 ¶
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
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 ¶
DateOnly returns a new time.Time object set to only the date portion of the given time.
func DayDiff ¶
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 ¶
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 LayoutHasDate ¶
LayoutHasDate returns true if the given parse layout indicates a date.
func LayoutHasTime ¶
LayoutHasTime returns true if the given parse layout indicates a time.
func NewDateTime ¶
NewDateTime creates a time.Time in UTC.
func NumLeaps ¶
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 ¶
ParseForgiving will parse a value allowing for extra spaces or a difference in case for am/pm.
func ParseInOffset ¶
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 ¶
TimeOnly returns a new time.Time object set to only the time portion of the given time.
func ToSqlDateTime ¶
ToSqlDateTime returns a SQL string representing the give time.Time value.