timex

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 7 Imported by: 0

README

timex

build-img pkg-img reportcard-img coverage-img version-img

Better time package for Go.

Rationale

TODO

Features

  • Simple.

See GUIDE.md for more details.

Install

Go version 1.17+

go get github.com/cristalhq/timex

Example

TODO

Documentation

See these docs.

License

MIT License.

Documentation

Index

Constants

View Source
const (
	Week        = 7 * Day
	Day         = 24 * Hour
	HalfDay     = 12 * Hour
	Hour        = time.Hour
	Minute      = time.Minute
	Second      = time.Second
	Millisecond = time.Millisecond
	Microsecond = time.Microsecond
	Nanosecond  = time.Nanosecond
)

Variables

This section is empty.

Functions

func Compare added in v0.2.0

func Compare(a, b time.Time) int

Compare function for two time.Time instances.

func DropDate added in v0.2.0

func DropDate(t time.Time) time.Time

DropDate replaces the date with January 1, year 0. It preserves the clock components and location.

func DropTime added in v0.2.0

func DropTime(t time.Time) time.Time

DropTime replaces the clock components with midnight. It preserves the date and location.

func Max added in v0.2.0

func Max(values ...time.Time) time.Time

Max returns the latest nonzero time. It returns the zero time if there are no nonzero values.

func Min added in v0.2.0

func Min(values ...time.Time) time.Time

Min returns the earliest nonzero time. It returns the zero time if there are no nonzero values.

func Now added in v0.1.1

func Now() time.Time

Now returns currect time.

func Parse added in v0.2.0

func Parse(value string, layouts ...string) (time.Time, error)

Parse value using the first matching layout. At least one layout is required.

func ParseInZone added in v0.2.1

func ParseInZone(zone *Zone, value string, layouts ...string) (time.Time, error)

ParseInZone value using the first matching layout. At least one layout is required.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock provides the current time.

type Dur added in v0.2.0

type Dur = time.Duration

Dur is an alias for time.Duration.

type ManualClock

type ManualClock struct {
	// contains filtered or unexported fields
}

ManualClock is a clock that can be controlled manually. Its zero value returns the zero time. Methods are safe for concurrent use. A ManualClock must not be copied after first use.

func NewManualClock

func NewManualClock(curr time.Time) *ManualClock

NewManualClock returns a manual clock initialized to curr.

func (*ManualClock) Add

func (c *ManualClock) Add(d time.Duration)

Add advances the clock by the given duration.

func (*ManualClock) Now

func (c *ManualClock) Now() time.Time

Now returns the clock's current time.

func (*ManualClock) Set

func (c *ManualClock) Set(t time.Time)

Set replaces the clock's current time.

type Range added in v0.2.0

type Range struct {
	Start  time.Time
	Finish time.Time
}

Range represents a half-open interval [Start, Finish). Equal endpoints form a valid empty range. The zero value is a valid empty range.

func NewRange added in v0.2.0

func NewRange(start, finish time.Time) (Range, error)

NewRange returns an error if finish is before start.

func ParseRange added in v0.2.0

func ParseRange(s string) (Range, error)

func ParseRangeInZone added in v0.2.0

func ParseRangeInZone(s string, zone *time.Location) (Range, error)

func (Range) Has added in v0.2.0

func (r Range) Has(t time.Time) bool

Has reports whether t belongs to [Start, Finish). Invalid and empty ranges contain no instants.

func (Range) Intersect added in v0.2.0

func (r Range) Intersect(other Range) (Range, bool)

Intersect returns the non-empty intersection of the ranges. If there is no intersection, it returns Range{} and false.

func (Range) IsEmpty added in v0.2.0

func (r Range) IsEmpty() bool

IsEmpty reports whether the endpoints represent the same instant.

func (Range) IsValid added in v0.2.0

func (r Range) IsValid() bool

IsValid reports whether Finish is at or after Start.

func (Range) MarshalJSON added in v0.2.0

func (r Range) MarshalJSON() ([]byte, error)

MarshalJSON encodes the range as a JSON string in UTC.

func (Range) MarshalText added in v0.2.0

func (r Range) MarshalText() ([]byte, error)

MarshalText encodes the range with both endpoints in UTC.

func (Range) Overlaps added in v0.2.0

func (r Range) Overlaps(other Range) bool

Overlaps reports whether the ranges share a non-empty interval. Adjacent, empty, and invalid ranges do not overlap.

func (Range) Span added in v0.2.0

func (r Range) Span() Span

Span returns Finish.Sub(Start). Invalid ranges produce a negative duration. Results outside Span's range saturate, following time.Time.Sub.

func (Range) String added in v0.2.0

func (r Range) String() string

String formats the range using its endpoints' local dates and clocks.

The date is omitted when both endpoints have date 0001-01-01. A shared date is printed once; different dates are printed separately. Seconds are included for both endpoints if either has seconds or nanoseconds. Fractional seconds are printed only when non-zero.

Timezone information is omitted. Use endpoints in the same location when an unambiguous local display is required.

func (*Range) UnmarshalJSON added in v0.2.0

func (r *Range) UnmarshalJSON(data []byte) error

func (*Range) UnmarshalText added in v0.2.0

func (r *Range) UnmarshalText(data []byte) error

type Span added in v0.2.0

type Span time.Duration

Span represents a duration in nanoseconds. Similar to time.Duration but with improved marshaling.

func ParseSpan added in v0.2.0

func ParseSpan(s string) (Span, error)

ParseSpan parses a duration using time.ParseDuration.

func (Span) Dur added in v0.2.0

func (s Span) Dur() time.Duration

Dur returns a time.Duration.

func (Span) MarshalJSON added in v0.2.0

func (s Span) MarshalJSON() ([]byte, error)

func (Span) MarshalText added in v0.2.0

func (s Span) MarshalText() ([]byte, error)

func (Span) Mul added in v0.2.0

func (s Span) Mul(x float64) Span

Mul returns s multiplied by x.

The calculation uses float64 and may lose precision for large durations. Both x and the product must be finite, and the product must be in [-2^63, 2^63). These requirements are not checked.

func (Span) String added in v0.2.0

func (s Span) String() string

String returns the duration formatted like time.Duration.String Omits zero minutes and zero seconds. For example, 1h0m0s becomes 1h, and 1m0s becomes 1m.

func (*Span) UnmarshalJSON added in v0.2.0

func (s *Span) UnmarshalJSON(data []byte) error

func (*Span) UnmarshalText added in v0.2.0

func (s *Span) UnmarshalText(text []byte) error

type Spans added in v0.2.0

type Spans []Span

Spans is a slice of Span.

func (Spans) Durs added in v0.2.0

func (ss Spans) Durs() []time.Duration

Durs returns each span as a time.Duration.

func (Spans) String added in v0.2.0

func (ss Spans) String() string

func (Spans) Strings added in v0.2.0

func (ss Spans) Strings() []string

type SystemClock added in v0.2.0

type SystemClock struct{}

SystemClock provides the current local time, including its monotonic reading.

func (SystemClock) Now added in v0.2.0

func (SystemClock) Now() time.Time

Now returns the current local time.

type Timestamp added in v0.1.1

type Timestamp struct {
	time.Time
}

func AsTimestamp added in v0.1.1

func AsTimestamp(t time.Time) Timestamp

AsTimestamp returns time.Time wrapped as Timestamp.

func TimestampFromMilli added in v0.1.2

func TimestampFromMilli(msec int64) Timestamp

TimestampFromMilli returns Timestamp from the Unix time in milliseconds.

func TimestampFromString added in v0.1.2

func TimestampFromString(s string) (Timestamp, error)

TimestampFromString returns Timestamp fomr the string representing Unix time.

func (Timestamp) MarshalJSON added in v0.1.1

func (ts Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Timestamp) UnmarshalJSON added in v0.1.1

func (ts *Timestamp) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler interface.

type UTCClock added in v0.2.0

type UTCClock struct{}

UTCClock provides the current time in UTC. Its results do not contain a monotonic clock reading.

func (UTCClock) Now added in v0.2.0

func (UTCClock) Now() time.Time

Now returns the current time in UTC.

type Zone added in v0.2.1

type Zone = time.Location

Zone is an alias for time.Location.

func MustZone added in v0.2.1

func MustZone(zone string) *Zone

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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