timex

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TimeZoneNames = []string{}/* 515 elements not displayed */

TimeZoneNames is an alphabetically sorted list of most time zone names.

View Source
var USTimeZoneNames = []string{
	"US/Alaska",
	"US/Aleutian",
	"US/Arizona",
	"US/Central",
	"US/East-Indiana",
	"US/Eastern",
	"US/Hawaii",
	"US/Indiana-Starke",
	"US/Michigan",
	"US/Mountain",
	"US/Pacific",
	"US/Samoa",
}

USTimeZoneNames is an alphabetically sorted list of US time zone names.

Functions

func Since

func Since(t Timex) time.Duration

Since returns the time elapsed since t. It is shorthand for timex.Now().Sub(t).

func TimeZoneOfGeography

func TimeZoneOfGeography(country string, state string) (timeZone string)

TimeZoneOfGeography returns the time zone name of the given country and (optional) state. If one cannot be determined, the empty string is returned.

func Until

func Until(t Timex) time.Duration

Until returns the duration until t. It is shorthand for t.Sub(timex.Now()).

Types

type Timex

type Timex struct {
	time.Time `dv8:"main"`
}

Timex is an extension of the standard time.Time. It improves on parsing and overrides JSON serialization and database formatting for the zero time.

To create a new timex.Timex from time.Time:

timex := New(stdTime)

To obtain time.Time from a Timex:

stdTime := timex.Time

func Date

func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Timex

Date returns the Time corresponding to

yyyy-mm-dd hh:mm:ss + nsec nanoseconds

in the appropriate zone for that time in the given location.

The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.

A daylight savings time transition skips or repeats times. For example, in the United States, March 13, 2011 2:15am never occurred, while November 6, 2011 1:15am occurred twice. In such cases, the choice of time zone, and therefore the time, is not well-defined. Date returns a time that is correct in one of the two zones involved in the transition, but it does not guarantee which.

Date panics if loc is nil.

func MustParse

func MustParse(layout string, value string) Timex

MustParse is the same as Parse but panics on error.

func MustParseInLocation

func MustParseInLocation(layout string, value string, loc *time.Location) Timex

MustParse is the same as Parse but panics on error.

func New

func New(t time.Time) Timex

New creates a new timex from a standard time.Time.

func Now

func Now() Timex

Now returns the current local time.

func Parse

func Parse(layout string, value string) (Timex, error)

Parse creates a new timex from a string. The empty string is parsed to the zero time. Layout is optional. If not provided, layout is assumed to be RFC3339Nano "2006-01-02T15:04:05.999999999Z07:00", RFC3339 "2006-01-02T15:04:05Z07:00", "2006-01-02T15:04:05", "2006-01-02 15:04:05", or "2006-01-02" based on the length of the value.

func ParseInLocation

func ParseInLocation(layout string, value string, loc *time.Location) (Timex, error)

ParseInLocation is like Parse but differs in two important ways. First, in the absence of time zone information, Parse interprets a time as UTC; ParseInLocation interprets the time as in the given location. Second, when given a zone offset or abbreviation, Parse tries to match it against the Local location; ParseInLocation uses the given location.

func Unix

func Unix(sec int64, nsec int64) Timex

Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC. It is valid to pass nsec outside the range [0, 999999999]. Not all sec values have a corresponding time value. One such value is 1<<63-1 (the largest int64 value).

func UnixMicro

func UnixMicro(usec int64) Timex

UnixMicro returns the local Time corresponding to the given Unix time, usec microseconds since January 1, 1970 UTC.

func UnixMilli

func UnixMilli(msec int64) Timex

UnixMilli returns the local Time corresponding to the given Unix time, msec milliseconds since January 1, 1970 UTC.

func (Timex) Add

func (tx Timex) Add(d time.Duration) Timex

Add returns the time t+d.

func (Timex) AddDate

func (tx Timex) AddDate(years int, months int, days int) Timex

AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

func (Timex) After

func (tx Timex) After(u Timex) bool

After reports whether the time instant t is after u.

func (Timex) Before

func (tx Timex) Before(u Timex) bool

Before reports whether the time instant t is before u.

func (Timex) Compare

func (tx Timex) Compare(u Timex) int

Compare compares the time instant t with u. If t is before u, it returns -1; if t is after u, it returns +1; if they're the same, it returns 0.

func (Timex) Equal

func (tx Timex) Equal(u Timex) bool

Equal reports whether t and u represent the same time instant. Two times can be equal even if they are in different locations. For example, 6:00 +0200 and 4:00 UTC are Equal. See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

func (Timex) Format

func (tx Timex) Format(layout string) string

Format returns a textual representation of the time value formatted according to the layout defined by the argument. See the documentation for the constant called Layout to see how to represent the layout format.

The executable example for Time.Format demonstrates the working of the layout string in detail and is a good reference.

The zero time is formatted as an empty string.

func (Timex) In

func (tx Timex) In(loc *time.Location) Timex

In returns a copy of t representing the same time instant, but with the copy's location information set to loc for display purposes.

In panics if loc is nil.

func (Timex) JSONSchemaAlias

func (tx Timex) JSONSchemaAlias() any

JSONSchemaAlias indicates to use the JSON schema of time.

func (Timex) Local

func (tx Timex) Local() Timex

Local returns t with the location set to local time.

func (Timex) MarshalJSON

func (tx Timex) MarshalJSON() ([]byte, error)

MarshalJSON overrides JSON serialization of the zero value to null.

func (Timex) MarshalYAML

func (tx Timex) MarshalYAML() (interface{}, error)

MarshalYAML overrides YAML serialization of the zero value to null.

func (Timex) Round

func (tx Timex) Round(d time.Duration) Timex

Round returns the result of rounding t to the nearest multiple of d (since the zero time). The rounding behavior for halfway values is to round up. If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.

Round operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, Round(Hour) may return a time with a non-zero minute, depending on the time's Location.

func (*Timex) Scan

func (tx *Timex) Scan(value interface{}) error

Scan implements the Scanner interface used by SQL.

func (Timex) Sub

func (tx Timex) Sub(u Timex) time.Duration

Sub returns the duration t-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute t-d for a duration d, use t.Add(-d).

func (Timex) Truncate

func (tx Timex) Truncate(d time.Duration) Timex

Truncate returns the result of rounding t down to a multiple of d (since the zero time). If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.

Truncate operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, Truncate(Hour) may return a time with a non-zero minute, depending on the time's Location.

func (Timex) UTC

func (tx Timex) UTC() Timex

UTC returns t with the location set to UTC.

func (*Timex) UnmarshalJSON

func (tx *Timex) UnmarshalJSON(b []byte) error

UnmarshalJSON overrides JSON deserialization, interpreting "null" and "" as the zero time.

func (*Timex) UnmarshalYAML

func (tx *Timex) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML overrides YAML deserialization, interpreting "null" and "" as the zero time.

func (Timex) Value

func (tx Timex) Value() (driver.Value, error)

Value implements the driver Valuer interface used by SQL.

func (Timex) ZoneBounds

func (tx Timex) ZoneBounds() (start, end Timex)

ZoneBounds returns the bounds of the time zone in effect at time t. The zone begins at start and the next zone begins at end. If the zone begins at the beginning of time, start will be returned as a zero Time. If the zone goes on forever, end will be returned as a zero Time. The Location of the returned times will be the same as t.

Jump to

Keyboard shortcuts

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