std/time
import "github.com/eugenioenko/goalchemy/std/time"Package time measures and displays time. Times are always UTC and carry no monotonic reading: Now reads the host wall clock, and Format and Parse accept only the RFC3339 and RFC3339Nano layouts.
const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute)Common durations.
const ( RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00")Layouts accepted by Format and Parse.
func Sleep(d Duration)Sleep suspends the calling task for at least d of scheduler time, like lib/time.Sleep (cooperative gate).
type Duration
Section titled “type Duration”type Duration int64A Duration is the elapsed time between two instants as an int64 nanosecond count. Sleep accepts it directly; it is a distinct type from lib/time.Duration, so convert explicitly when calling lib/time or lib/context APIs.
ParseDuration
Section titled “ParseDuration”func ParseDuration(s string) (Duration, error)ParseDuration parses a duration string such as “300ms”, “-1.5h” or “2h45m”. Valid units are “ns”, “us” (or “µs”), “ms”, “s”, “m” and “h”.
func Since(t Time) DurationSince returns the time elapsed since t; it is Now().Sub(t).
func Until(t Time) DurationUntil returns the duration until t; it is t.Sub(Now()).
Duration.Abs
Section titled “Duration.Abs”func (d Duration) Abs() DurationAbs returns the absolute value of d, mapping the minimum Duration to the maximum.
Duration.Hours
Section titled “Duration.Hours”func (d Duration) Hours() float64Hours returns the duration as a floating-point number of hours.
Duration.Microseconds
Section titled “Duration.Microseconds”func (d Duration) Microseconds() int64Microseconds returns the duration as an integer microsecond count.
Duration.Milliseconds
Section titled “Duration.Milliseconds”func (d Duration) Milliseconds() int64Milliseconds returns the duration as an integer millisecond count.
Duration.Minutes
Section titled “Duration.Minutes”func (d Duration) Minutes() float64Minutes returns the duration as a floating-point number of minutes.
Duration.Nanoseconds
Section titled “Duration.Nanoseconds”func (d Duration) Nanoseconds() int64Nanoseconds returns the duration as an integer nanosecond count.
Duration.Round
Section titled “Duration.Round”func (d Duration) Round(m Duration) DurationRound returns d rounded to the nearest multiple of m, halfway values away from zero, saturating on overflow. If m <= 0, it returns d unchanged.
Duration.Seconds
Section titled “Duration.Seconds”func (d Duration) Seconds() float64Seconds returns the duration as a floating-point number of seconds.
Duration.String
Section titled “Duration.String”func (d Duration) String() stringString returns the duration in the form “72h3m0.5s”, with leading zero units omitted and sub-second durations in smaller units such as “1.2ms”.
Duration.Truncate
Section titled “Duration.Truncate”func (d Duration) Truncate(m Duration) DurationTruncate returns d rounded toward zero to a multiple of m. If m <= 0, it returns d unchanged.
type Location
Section titled “type Location”type Location struct {}A Location names a time zone. Only UTC exists.
var UTC *Location = &utcLocUTC is Coordinated Universal Time, the location of every Time.
Location.String
Section titled “Location.String”func (l *Location) String() stringString returns the location name.
type Month
Section titled “type Month”type Month intA Month specifies a month of the year (January = 1, …).
const ( January Month = 1 + iota February March April May June July August September October November December)Months of the year.
Month.String
Section titled “Month.String”func (m Month) String() stringString returns the English name of the month (“January”, “February”, …).
type ParseError
Section titled “type ParseError”type ParseError struct { Layout string Value string LayoutElem string ValueElem string Message string}ParseError describes a problem parsing a time string.
ParseError.Error
Section titled “ParseError.Error”func (e *ParseError) Error() stringError returns the string representation of a ParseError.
type Time
Section titled “type Time”type Time struct {}A Time is an instant with nanosecond precision, always in UTC. The zero value is January 1, year 1, 00:00:00 UTC.
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) TimeDate returns the Time for the given date and time of day. Values outside their usual ranges are normalized, as in Go; loc must be non-nil.
func Now() TimeNow returns the current host wall-clock time. It has no monotonic reading, so Since and Sub follow host clock adjustments.
func Parse(layout, value string) (Time, error)Parse parses value in layout, which must be RFC3339 or RFC3339Nano, and returns the instant in UTC: a numeric offset is applied, then dropped. Both layouts accept and reject the same inputs as Go, with the same errors.
func Unix(sec int64, nsec int64) TimeUnix returns the Time corresponding to sec seconds and nsec nanoseconds since January 1, 1970 UTC. nsec may be outside [0, 999999999].
UnixMicro
Section titled “UnixMicro”func UnixMicro(usec int64) TimeUnixMicro returns the Time corresponding to usec microseconds since the Unix epoch.
UnixMilli
Section titled “UnixMilli”func UnixMilli(msec int64) TimeUnixMilli returns the Time corresponding to msec milliseconds since the Unix epoch.
Time.Add
Section titled “Time.Add”func (t Time) Add(d Duration) TimeAdd returns t+d. Results beyond the representable range saturate.
Time.AddDate
Section titled “Time.AddDate”func (t Time) AddDate(years int, months int, days int) TimeAddDate returns t with the given numbers of years, months and days added, normalized like Date.
Time.After
Section titled “Time.After”func (t Time) After(u Time) boolAfter reports whether t is after u.
Time.AppendFormat
Section titled “Time.AppendFormat”func (t Time) AppendFormat(b []byte, layout string) []byteAppendFormat is like Format but appends to b.
Time.Before
Section titled “Time.Before”func (t Time) Before(u Time) boolBefore reports whether t is before u.
Time.Clock
Section titled “Time.Clock”func (t Time) Clock() (int, int, int)Clock returns the hour, minute and second within the day of t.
Time.Compare
Section titled “Time.Compare”func (t Time) Compare(u Time) intCompare returns -1 if t is before u, 0 if they are equal, and +1 if t is after u.
Time.Date
Section titled “Time.Date”func (t Time) Date() (int, Month, int)Date returns the year, month and day of t.
Time.Day
Section titled “Time.Day”func (t Time) Day() intDay returns the day of the month of t.
Time.Equal
Section titled “Time.Equal”func (t Time) Equal(u Time) boolEqual reports whether t and u are the same instant.
Time.Format
Section titled “Time.Format”func (t Time) Format(layout string) stringFormat returns t in layout, which must be RFC3339 or RFC3339Nano; any other layout panics. The offset is always written as Z.
Time.Hour
Section titled “Time.Hour”func (t Time) Hour() intHour returns the hour within the day of t, in [0, 23].
Time.In
Section titled “Time.In”func (t Time) In(loc *Location) TimeIn returns t; only UTC exists. It panics if loc is nil.
Time.IsZero
Section titled “Time.IsZero”func (t Time) IsZero() boolIsZero reports whether t is the zero time, January 1, year 1, 00:00:00 UTC.
Time.Location
Section titled “Time.Location”func (t Time) Location() *LocationLocation returns UTC.
Time.Minute
Section titled “Time.Minute”func (t Time) Minute() intMinute returns the minute within the hour of t, in [0, 59].
Time.Month
Section titled “Time.Month”func (t Time) Month() MonthMonth returns the month of the year of t.
Time.Nanosecond
Section titled “Time.Nanosecond”func (t Time) Nanosecond() intNanosecond returns the nanosecond offset within the second, in [0, 999999999].
Time.Round
Section titled “Time.Round”func (t Time) Round(d Duration) TimeRound returns t rounded to the nearest multiple of d since the zero time, rounding halfway values up.
Time.Second
Section titled “Time.Second”func (t Time) Second() intSecond returns the second within the minute of t, in [0, 59].
Time.String
Section titled “Time.String”func (t Time) String() stringString returns t formatted as “2006-01-02 15:04:05.999999999 +0000 UTC”.
Time.Sub
Section titled “Time.Sub”func (t Time) Sub(u Time) DurationSub returns t-u, saturating at the minimum or maximum Duration.
Time.Truncate
Section titled “Time.Truncate”func (t Time) Truncate(d Duration) TimeTruncate returns t rounded down to a multiple of d since the zero time.
Time.UTC
Section titled “Time.UTC”func (t Time) UTC() TimeUTC returns t; every Time is already in UTC.
Time.Unix
Section titled “Time.Unix”func (t Time) Unix() int64Unix returns t as seconds since January 1, 1970 UTC.
Time.UnixMicro
Section titled “Time.UnixMicro”func (t Time) UnixMicro() int64UnixMicro returns t as microseconds since January 1, 1970 UTC.
Time.UnixMilli
Section titled “Time.UnixMilli”func (t Time) UnixMilli() int64UnixMilli returns t as milliseconds since January 1, 1970 UTC.
Time.UnixNano
Section titled “Time.UnixNano”func (t Time) UnixNano() int64UnixNano returns t as nanoseconds since January 1, 1970 UTC. The result overflows outside the years 1678 to 2262.
Time.Weekday
Section titled “Time.Weekday”func (t Time) Weekday() WeekdayWeekday returns the day of the week of t.
Time.Year
Section titled “Time.Year”func (t Time) Year() intYear returns the year of t.
Time.YearDay
Section titled “Time.YearDay”func (t Time) YearDay() intYearDay returns the day of the year of t, in [1, 365] or [1, 366] in leap years.
type Weekday
Section titled “type Weekday”type Weekday intA Weekday specifies a day of the week (Sunday = 0, …).
const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturday)Days of the week.
Weekday.String
Section titled “Weekday.String”func (d Weekday) String() stringString returns the English name of the day (“Sunday”, “Monday”, …).