time

package module
v1.27.11 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: BSD-2-Clause Imports: 13 Imported by: 47

README

Time

Go Reference Go Report Card

A comprehensive Go library providing enhanced time and date utilities with type safety, dependency injection support, and extended functionality beyond the standard time package.

Features

  • 🎯 Type-Safe Time Operations - Strongly typed wrappers around Go's time types
  • 💉 Dependency Injection Ready - Interfaces for testable time operations
  • 📅 Rich Time Types - DateTime, Date, TimeOfDay, Duration, UnixTime, and Timezone types
  • ⏱️ Extended Duration Support - Parse human-readable durations like "1w2d3h4m5s"
  • 🔄 JSON Marshaling - Built-in JSON support for all time types
  • ✅ Validation - Input validation with meaningful error messages
  • 🧪 Testing Utilities - Helper functions for controlled time in tests
  • 🌍 Timezone Handling - Enhanced timezone operations with caching

Installation

go get github.com/bborbe/time

Quick Start

Basic Usage
import libtime "github.com/bborbe/time"

// Parse various time formats
dateTime, _ := libtime.ParseDateTime(ctx, "2023-12-25T15:30:00Z")
date, _ := libtime.ParseDate(ctx, "2023-12-25")
duration, _ := libtime.ParseDuration(ctx, "1w2d3h") // 1 week, 2 days, 3 hours

// Create time types
now := libtime.DateTime(time.Now())
unixTime := libtime.UnixTime(1703520600)
timeOfDay, _ := libtime.ParseTimeOfDay(ctx, "15:30:45")
Dependency Injection Pattern
type Service struct {
    currentDateTime libtime.CurrentDateTime
}

// Production code
func NewService() *Service {
    return &Service{
        currentDateTime: libtime.NewCurrentDateTime(),
    }
}

func (s *Service) ProcessOrder() {
    now := s.currentDateTime.Now()
    // Use now for timestamps...
}

// Test code
func TestService(t *testing.T) {
    service := &Service{
        currentDateTime: libtime.NewCurrentDateTime(),
    }
    
    // Control time in tests
    fixedTime := libtimetest.ParseDateTime("2023-12-25T00:00:00Z")
    service.currentDateTime.SetNow(fixedTime)
    
    // Test with predictable time
    service.ProcessOrder()
}

Core Types

DateTime

Enhanced time.Time wrapper with validation and JSON support:

dt := libtime.DateTime(time.Now())
json, _ := dt.MarshalJSON()           // RFC3339Nano format
formatted := dt.Format("2006-01-02") // Standard Go formatting
ptr := dt.Ptr()                      // Get pointer for optional fields
Duration

Extended duration with weeks and days support:

// Parse human-readable durations
duration, _ := libtime.ParseDuration(ctx, "2w3d4h30m")  // 2 weeks, 3 days, 4.5 hours
duration, _ := libtime.ParseDuration(ctx, "1.5h")       // 1.5 hours
duration, _ := libtime.ParseDuration(ctx, "30s")        // 30 seconds

// Use constants
totalTime := libtime.Week + 2*libtime.Day + 3*libtime.Hour
Date

Date-only type without time component:

date, _ := libtime.ParseDate(ctx, "2023-12-25")
tomorrow := date.AddDate(0, 0, 1)
isWeekend := date.Weekday() == time.Saturday || date.Weekday() == time.Sunday
TimeOfDay

Time component without date:

timeOfDay, _ := libtime.ParseTimeOfDay(ctx, "15:30:45")
hour := timeOfDay.Hour()
minute := timeOfDay.Minute()
second := timeOfDay.Second()
UnixTime

Unix timestamp handling:

unixTime := libtime.UnixTime(1703520600)
dateTime := unixTime.DateTime()
formatted := unixTime.String() // RFC3339Nano format

Testing Support

The library provides extensive testing utilities in the /test package:

import libtimetest "github.com/bborbe/time/test"

// Parse without error handling (panics on error - use only in tests)
dt := libtimetest.ParseDateTime("2023-12-25T15:30:00Z")
date := libtimetest.ParseDate("2023-12-25")
duration := libtimetest.ParseDuration("1h30m")

// Control time in dependency injection
currentDateTime := libtime.NewCurrentDateTime()
currentDateTime.SetNow(libtimetest.ParseDateTime("2023-12-25T00:00:00Z"))

Advanced Features

Validation

All types implement validation interfaces:

dateTime, err := libtime.ParseDateTime(ctx, "invalid")
if err != nil {
    // Handle validation error with context
}
Interfaces for Polymorphism
// HasTime interface - for types containing time information
var hasTime libtime.HasTime = libtime.DateTime(time.Now())
timeValue := hasTime.Time()

// HasDuration interface - for types representing durations  
var hasDuration libtime.HasDuration = libtime.Duration(time.Hour)
durationValue := hasDuration.Duration()
Timezone Operations
tz, _ := libtime.ParseTimezone(ctx, "America/New_York")
location := tz.Location()
dateTimeInTZ := dateTime.In(location)

Development

Running Tests
make test                    # Run all tests with coverage
go test -cover -race ./...   # Manual test execution
Code Quality
make precommit              # Complete workflow (format, test, check, etc.)
make format                 # Format code
make check                  # Static analysis
Mock Generation
make generate               # Generate mocks using counterfeiter

Dependencies

  • Runtime: github.com/bborbe/collection, github.com/bborbe/errors, github.com/bborbe/parse, github.com/bborbe/validation
  • Testing: Ginkgo v2, Gomega, Counterfeiter
  • Development: goimports-reviser, addlicense, govulncheck

License

BSD-style license. See LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	Nanosecond  Duration = 1
	Microsecond          = 1000 * Nanosecond
	Millisecond          = 1000 * Microsecond
	Second               = 1000 * Millisecond
	Minute               = 60 * Second
	Hour                 = 60 * Minute
	Day                  = 24 * Hour
	Week                 = 7 * Day
)
View Source
const TimeOfDayLayout = "15:04:05.999999999Z07:00"

Variables

View Source
var Now = time.Now

Now allow to modify now, please use CurrentTime if possible

View Source
var UnitMap = map[string]Duration{
	"ns": Nanosecond,
	"us": Microsecond,
	"ms": Millisecond,
	"s":  Second,
	"m":  Minute,
	"h":  Hour,
	"d":  Day,
	"w":  Week,
}

UnitMap contains units to duration mapping

Functions

func BeginningOfDay

func BeginningOfDay(t stdtime.Time) stdtime.Time

BeginningOfDay returns the start of the day (00:00:00.000000000) for the given time. Preserves the original timezone.

func BeginningOfDayFromHasTime

func BeginningOfDayFromHasTime(hasTime HasTime) stdtime.Time

BeginningOfDayFromHasTime returns the start of the day for any type implementing HasTime interface.

func BeginningOfMonth

func BeginningOfMonth(t stdtime.Time) stdtime.Time

BeginningOfMonth returns the start of the month (1st day 00:00:00.000000000) for the given time. Preserves the original timezone.

func BeginningOfMonthFromHasTime

func BeginningOfMonthFromHasTime(hasTime HasTime) stdtime.Time

BeginningOfMonthFromHasTime returns the start of the month for any type implementing HasTime interface.

func BeginningOfQuarter

func BeginningOfQuarter(t stdtime.Time) stdtime.Time

BeginningOfQuarter returns the start of the quarter (1st day of quarter 00:00:00.000000000) for the given time. Quarters are: Q1=Jan-Mar, Q2=Apr-Jun, Q3=Jul-Sep, Q4=Oct-Dec. Preserves the original timezone.

func BeginningOfQuarterFromHasTime

func BeginningOfQuarterFromHasTime(hasTime HasTime) stdtime.Time

BeginningOfQuarterFromHasTime returns the start of the quarter for any type implementing HasTime interface.

func BeginningOfWeek

func BeginningOfWeek(t stdtime.Time) stdtime.Time

BeginningOfWeek returns the start of the week (Monday 00:00:00.000000000) for the given time. Uses ISO 8601 standard where Monday is the first day of the week. Preserves the original timezone.

func BeginningOfWeekFromHasTime

func BeginningOfWeekFromHasTime(hasTime HasTime) stdtime.Time

BeginningOfWeekFromHasTime returns the start of the week for any type implementing HasTime interface.

func BeginningOfYear

func BeginningOfYear(t stdtime.Time) stdtime.Time

BeginningOfYear returns the start of the year (January 1st 00:00:00.000000000) for the given time. Preserves the original timezone.

func BeginningOfYearFromHasTime

func BeginningOfYearFromHasTime(hasTime HasTime) stdtime.Time

BeginningOfYearFromHasTime returns the start of the year for any type implementing HasTime interface.

func Compare

func Compare(x, y time.Time) int

func EndOfDay

func EndOfDay(t stdtime.Time) stdtime.Time

EndOfDay returns the end of the day (23:59:59.999999999) for the given time. This is calculated as the start of the next day minus 1 nanosecond.

func EndOfDayFromHasTime

func EndOfDayFromHasTime(hasTime HasTime) stdtime.Time

EndOfDayFromHasTime returns the end of the day for any type implementing HasTime interface.

func EndOfMonth

func EndOfMonth(t stdtime.Time) stdtime.Time

EndOfMonth returns the end of the month (last day 23:59:59.999999999) for the given time. This is calculated as the start of the next month minus 1 nanosecond.

func EndOfMonthFromHasTime

func EndOfMonthFromHasTime(hasTime HasTime) stdtime.Time

EndOfMonthFromHasTime returns the end of the month for any type implementing HasTime interface.

func EndOfQuarter

func EndOfQuarter(t stdtime.Time) stdtime.Time

EndOfQuarter returns the end of the quarter (last day of quarter 23:59:59.999999999) for the given time. This is calculated as the start of the next quarter minus 1 nanosecond.

func EndOfQuarterFromHasTime

func EndOfQuarterFromHasTime(hasTime HasTime) stdtime.Time

EndOfQuarterFromHasTime returns the end of the quarter for any type implementing HasTime interface.

func EndOfWeek

func EndOfWeek(t stdtime.Time) stdtime.Time

EndOfWeek returns the end of the week (Sunday 23:59:59.999999999) for the given time. This is calculated as the start of the next week minus 1 nanosecond.

func EndOfWeekFromHasTime

func EndOfWeekFromHasTime(hasTime HasTime) stdtime.Time

EndOfWeekFromHasTime returns the end of the week for any type implementing HasTime interface.

func EndOfYear

func EndOfYear(t stdtime.Time) stdtime.Time

EndOfYear returns the end of the year (December 31st 23:59:59.999999999) for the given time. This is calculated as the start of the next year minus 1 nanosecond.

func EndOfYearFromHasTime

func EndOfYearFromHasTime(hasTime HasTime) stdtime.Time

EndOfYearFromHasTime returns the end of the year for any type implementing HasTime interface.

func FormatTime

func FormatTime(t *time.Time) string

func HasEqualDate

func HasEqualDate(t1, t2 time.Time) bool

func LoadLocation

func LoadLocation(ctx context.Context, name string) (*stdtime.Location, error)

func Max

func Max(a, b time.Time) time.Time

func ParseLocation

func ParseLocation(ctx context.Context, value any) (*stdtime.Location, error)

func ParseTime

func ParseTime(ctx context.Context, value interface{}) (*stdtime.Time, error)

func ParseTimeDefault

func ParseTimeDefault(
	ctx context.Context,
	value interface{},
	defaultValue stdtime.Time,
) stdtime.Time

Types

type CurrentDateTime

type CurrentDateTime interface {
	CurrentDateTimeGetter
	CurrentDateTimeSetter
}

func NewCurrentDateTime

func NewCurrentDateTime() CurrentDateTime

type CurrentDateTimeGetter

type CurrentDateTimeGetter interface {
	Now() DateTime
}

type CurrentDateTimeGetterFunc

type CurrentDateTimeGetterFunc func() DateTime

func (CurrentDateTimeGetterFunc) Now

type CurrentDateTimeSetter

type CurrentDateTimeSetter interface {
	SetNow(now DateTime)
}

type CurrentTime

type CurrentTime interface {
	CurrentTimeGetter
	CurrentTimeSetter
}

func NewCurrentTime

func NewCurrentTime() CurrentTime

type CurrentTimeGetter

type CurrentTimeGetter interface {
	Now() time.Time
}

type CurrentTimeGetterFunc

type CurrentTimeGetterFunc func() DateTime

func (CurrentTimeGetterFunc) Now

type CurrentTimeSetter

type CurrentTimeSetter interface {
	SetNow(now time.Time)
}

type Date

type Date stdtime.Time

func DateFromBinary

func DateFromBinary(ctx context.Context, value []byte) (*Date, error)

func DatePtr

func DatePtr(value *stdtime.Time) *Date

func NewDate

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

NewDate creates a Date representing the date specified by the given parameters. It wraps the standard library's time.Date function with the same parameter signature. Note: hour, min, sec, nsec and loc parameters are typically ignored for Date operations.

func ParseDate

func ParseDate(ctx context.Context, value interface{}) (*Date, error)

func ToDate

func ToDate(value stdtime.Time) Date

func (Date) Add

func (d Date) Add(duration HasDuration) Date

func (Date) AddDate

func (d Date) AddDate(years int, months int, days int) Date

func (Date) AddTime deprecated

func (d Date) AddTime(years int, months int, days int) Date

Deprecated: Use AddDate instead. AddTime adds the given years, months, and days to the Date but will be removed in future versions.

func (Date) After

func (d Date) After(other HasTime) bool

func (Date) Before

func (d Date) Before(other HasTime) bool

func (Date) Clone

func (d Date) Clone() Date

func (*Date) ClonePtr

func (d *Date) ClonePtr() *Date

func (Date) Compare

func (d Date) Compare(stdTime Date) int

func (*Date) ComparePtr

func (d *Date) ComparePtr(stdTime *Date) int

func (Date) Day

func (d Date) Day() int

func (Date) Equal

func (d Date) Equal(other Date) bool

func (*Date) EqualPtr

func (d *Date) EqualPtr(other *Date) bool

func (Date) Format

func (d Date) Format(layout string) string

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether d represents the zero time instant.

func (Date) MarshalBinary

func (d Date) MarshalBinary() ([]byte, error)

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

func (Date) MarshalText

func (d Date) MarshalText() ([]byte, error)

func (Date) Month

func (d Date) Month() stdtime.Month

func (Date) Ptr

func (d Date) Ptr() *Date

func (Date) String

func (d Date) String() string

func (Date) Sub

func (d Date) Sub(time HasTime) Duration

func (Date) Time

func (d Date) Time() stdtime.Time

func (*Date) TimePtr

func (d *Date) TimePtr() *stdtime.Time

func (Date) Truncate

func (d Date) Truncate(duration HasDuration) Date

func (Date) UTC

func (d Date) UTC() Date

func (Date) Unix

func (d Date) Unix() int64

func (Date) UnixMicro

func (d Date) UnixMicro() int64

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

func (*Date) UnmarshalText

func (d *Date) UnmarshalText(b []byte) error

func (Date) Validate

func (d Date) Validate(ctx context.Context) error

func (Date) Weekday

func (d Date) Weekday() Weekday

func (Date) Year

func (d Date) Year() int

type DateOrDateTime

type DateOrDateTime stdtime.Time

func DateOrDateTimeFromBinary

func DateOrDateTimeFromBinary(ctx context.Context, value []byte) (*DateOrDateTime, error)

func DateOrDateTimePtr

func DateOrDateTimePtr(value *stdtime.Time) *DateOrDateTime

func NewDateOrDateTime

func NewDateOrDateTime(
	year int,
	month stdtime.Month,
	day, hour, min, sec, nsec int,
	loc *stdtime.Location,
) DateOrDateTime

NewDateOrDateTime creates a DateOrDateTime representing the date and time specified by the given parameters. It wraps the standard library's time.Date function with the same parameter signature.

func ParseDateOrDateTime

func ParseDateOrDateTime(ctx context.Context, value interface{}) (*DateOrDateTime, error)

func ParseDateOrDateTimeDefault

func ParseDateOrDateTimeDefault(
	ctx context.Context,
	value interface{},
	defaultValue DateOrDateTime,
) DateOrDateTime

func (DateOrDateTime) Add

func (d DateOrDateTime) Add(duration HasDuration) DateOrDateTime

func (DateOrDateTime) AddDate

func (d DateOrDateTime) AddDate(years int, months int, days int) DateOrDateTime

func (DateOrDateTime) AddTime deprecated

func (d DateOrDateTime) AddTime(years int, months int, days int) DateOrDateTime

Deprecated: Use AddDate instead. AddTime adds the given years, months, and days to the DateOrDateTime but will be removed in future versions.

func (DateOrDateTime) After

func (d DateOrDateTime) After(other HasTime) bool

func (DateOrDateTime) AsDate

func (d DateOrDateTime) AsDate() Date

AsDate returns the date component of d as a Date value.

func (DateOrDateTime) AsDateTime

func (d DateOrDateTime) AsDateTime() DateTime

AsDateTime returns d as a DateTime value.

func (DateOrDateTime) Before

func (d DateOrDateTime) Before(other HasTime) bool

func (DateOrDateTime) Clone

func (d DateOrDateTime) Clone() DateOrDateTime

func (*DateOrDateTime) ClonePtr

func (d *DateOrDateTime) ClonePtr() *DateOrDateTime

func (DateOrDateTime) Compare

func (d DateOrDateTime) Compare(other DateOrDateTime) int

func (*DateOrDateTime) ComparePtr

func (d *DateOrDateTime) ComparePtr(other *DateOrDateTime) int

func (DateOrDateTime) Day

func (d DateOrDateTime) Day() int

func (DateOrDateTime) Equal

func (d DateOrDateTime) Equal(other DateOrDateTime) bool

func (*DateOrDateTime) EqualPtr

func (d *DateOrDateTime) EqualPtr(other *DateOrDateTime) bool

func (DateOrDateTime) Format

func (d DateOrDateTime) Format(layout string) string

func (DateOrDateTime) Hour

func (d DateOrDateTime) Hour() int

func (DateOrDateTime) IsDateOnly

func (d DateOrDateTime) IsDateOnly() bool

IsDateOnly reports whether d represents a date-only value (midnight UTC).

func (DateOrDateTime) IsZero

func (d DateOrDateTime) IsZero() bool

IsZero reports whether d represents the zero time instant.

func (DateOrDateTime) MarshalBinary

func (d DateOrDateTime) MarshalBinary() ([]byte, error)

func (DateOrDateTime) MarshalJSON

func (d DateOrDateTime) MarshalJSON() ([]byte, error)

func (DateOrDateTime) MarshalText

func (d DateOrDateTime) MarshalText() ([]byte, error)

func (DateOrDateTime) Minute

func (d DateOrDateTime) Minute() int

func (DateOrDateTime) Month

func (d DateOrDateTime) Month() stdtime.Month

func (DateOrDateTime) Nanosecond

func (d DateOrDateTime) Nanosecond() int

func (DateOrDateTime) Ptr

func (d DateOrDateTime) Ptr() *DateOrDateTime

func (DateOrDateTime) Second

func (d DateOrDateTime) Second() int

func (DateOrDateTime) String

func (d DateOrDateTime) String() string

func (DateOrDateTime) Sub

func (d DateOrDateTime) Sub(other HasTime) Duration

func (DateOrDateTime) Time

func (d DateOrDateTime) Time() stdtime.Time

func (*DateOrDateTime) TimePtr

func (d *DateOrDateTime) TimePtr() *stdtime.Time

func (DateOrDateTime) Truncate

func (d DateOrDateTime) Truncate(duration HasDuration) DateOrDateTime

func (DateOrDateTime) UTC

func (DateOrDateTime) Unix

func (d DateOrDateTime) Unix() int64

func (DateOrDateTime) UnixMicro

func (d DateOrDateTime) UnixMicro() int64

func (*DateOrDateTime) UnmarshalJSON

func (d *DateOrDateTime) UnmarshalJSON(b []byte) error

func (*DateOrDateTime) UnmarshalText

func (d *DateOrDateTime) UnmarshalText(b []byte) error

func (DateOrDateTime) Validate

func (d DateOrDateTime) Validate(ctx context.Context) error

func (DateOrDateTime) Weekday

func (d DateOrDateTime) Weekday() Weekday

func (DateOrDateTime) Year

func (d DateOrDateTime) Year() int

type DateOrDateTimes

type DateOrDateTimes []DateOrDateTime

func (DateOrDateTimes) Interfaces

func (d DateOrDateTimes) Interfaces() []interface{}

func (DateOrDateTimes) Strings

func (d DateOrDateTimes) Strings() []string

type DateRange

type DateRange struct {
	From  Date `json:"from,omitempty"`
	Until Date `json:"until,omitempty"`
}

func DateRangeFromTime

func DateRangeFromTime(from, until stdtime.Time) DateRange

DateRangeFromTime creates a DateRange from two time.Time values. It converts the from and until times to Date types and returns a DateRange.

func DayDateRange

func DayDateRange(d Date) DateRange

DayDateRange creates a DateRange covering the entire day containing the given date. The range spans from 00:00:00.000000000 to 23:59:59.999999999 of that day.

func MonthDateRange

func MonthDateRange(d Date) DateRange

MonthDateRange creates a DateRange covering the entire month containing the given date. The range spans from the 1st day to the last day of that month.

func QuarterDateRange

func QuarterDateRange(d Date) DateRange

QuarterDateRange creates a DateRange covering the entire quarter containing the given date. Quarters are defined as: Q1=Jan-Mar, Q2=Apr-Jun, Q3=Jul-Sep, Q4=Oct-Dec. The range spans from the 1st day of the quarter to the last day of that quarter.

func WeekDateRange

func WeekDateRange(d Date) DateRange

WeekDateRange creates a DateRange covering the entire week containing the given date. The range spans from Monday to Sunday of that week. Uses ISO 8601 standard where Monday is the first day of the week.

func YearDateRange

func YearDateRange(d Date) DateRange

YearDateRange creates a DateRange covering the entire year containing the given date. The range spans from January 1st to December 31st of that year.

func (DateRange) Ptr

func (r DateRange) Ptr() *DateRange

func (DateRange) TimeRange

func (r DateRange) TimeRange() TimeRange

TimeRange converts a DateRange to a TimeRange.

func (DateRange) Validate

func (r DateRange) Validate(ctx context.Context) error

type DateRanges

type DateRanges []DateRange

func (DateRanges) Max

func (ranges DateRanges) Max() *DateRange

Max returns the maximum DateRange that encompasses all ranges in the list. It finds the earliest From date and the latest Until date across all ranges. Returns nil if the list is empty.

func (DateRanges) Min

func (ranges DateRanges) Min() *DateRange

Min returns the minimum DateRange that is contained within all ranges in the list. It finds the latest From date and the earliest Until date across all ranges. Returns nil if the list is empty or if there is no overlap between ranges.

type DateTime

type DateTime stdtime.Time

func DateTimeFromBinary

func DateTimeFromBinary(ctx context.Context, value []byte) (*DateTime, error)

func DateTimeFromUnixMicro

func DateTimeFromUnixMicro(ms int64) DateTime

func DateTimePtr

func DateTimePtr(time *stdtime.Time) *DateTime

func NewDateTime

func NewDateTime(
	year int,
	month stdtime.Month,
	day, hour, min, sec, nsec int,
	loc *stdtime.Location,
) DateTime

NewDateTime creates a DateTime representing the date and time specified by the given parameters. It wraps the standard library's time.Date function with the same parameter signature.

func ParseDateTime

func ParseDateTime(ctx context.Context, value interface{}) (*DateTime, error)

func ParseDateTimeDefault

func ParseDateTimeDefault(ctx context.Context, value interface{}, defaultValue DateTime) DateTime

func (DateTime) Add

func (d DateTime) Add(duration HasDuration) DateTime

func (DateTime) AddDate

func (d DateTime) AddDate(years int, months int, days int) DateTime

func (DateTime) AddTime deprecated

func (d DateTime) AddTime(years int, months int, days int) DateTime

Deprecated: Use AddDate instead. AddTime adds the given years, months, and days to the DateTime but will be removed in future versions.

func (DateTime) After

func (d DateTime) After(time HasTime) bool

func (DateTime) Before

func (d DateTime) Before(time HasTime) bool

func (DateTime) Clone

func (d DateTime) Clone() DateTime

func (*DateTime) ClonePtr

func (d *DateTime) ClonePtr() *DateTime

func (DateTime) Compare

func (d DateTime) Compare(stdTime DateTime) int

func (*DateTime) ComparePtr

func (d *DateTime) ComparePtr(stdTime *DateTime) int

func (DateTime) Day

func (d DateTime) Day() int

func (DateTime) Equal

func (d DateTime) Equal(stdTime DateTime) bool

func (*DateTime) EqualPtr

func (d *DateTime) EqualPtr(stdTime *DateTime) bool

func (DateTime) Format

func (d DateTime) Format(layout string) string

func (DateTime) Hour

func (d DateTime) Hour() int

func (DateTime) IsZero

func (d DateTime) IsZero() bool

IsZero reports whether d represents the zero time instant.

func (DateTime) MarshalBinary

func (d DateTime) MarshalBinary() ([]byte, error)

func (DateTime) MarshalJSON

func (d DateTime) MarshalJSON() ([]byte, error)

func (DateTime) MarshalText

func (d DateTime) MarshalText() ([]byte, error)

func (DateTime) Minute

func (d DateTime) Minute() int

func (DateTime) Month

func (d DateTime) Month() stdtime.Month

func (DateTime) Nanosecond

func (d DateTime) Nanosecond() int

func (DateTime) Ptr

func (d DateTime) Ptr() *DateTime

func (DateTime) Second

func (d DateTime) Second() int

func (DateTime) String

func (d DateTime) String() string

func (DateTime) Sub

func (d DateTime) Sub(time HasTime) Duration

func (DateTime) Time

func (d DateTime) Time() stdtime.Time

func (*DateTime) TimePtr

func (d *DateTime) TimePtr() *stdtime.Time

func (DateTime) Truncate

func (d DateTime) Truncate(duration HasDuration) DateTime

func (DateTime) UTC

func (d DateTime) UTC() DateTime

func (DateTime) Unix

func (d DateTime) Unix() int64

func (DateTime) UnixMicro

func (d DateTime) UnixMicro() int64

func (*DateTime) UnmarshalJSON

func (d *DateTime) UnmarshalJSON(b []byte) error

func (*DateTime) UnmarshalText

func (d *DateTime) UnmarshalText(b []byte) error

func (DateTime) Validate

func (d DateTime) Validate(ctx context.Context) error

func (DateTime) Weekday

func (d DateTime) Weekday() Weekday

func (DateTime) Year

func (d DateTime) Year() int

type DateTimeRange

type DateTimeRange struct {
	From  DateTime `json:"from,omitempty"`
	Until DateTime `json:"until,omitempty"`
}

func DateTimeRangeFromTime

func DateTimeRangeFromTime(from, until stdtime.Time) DateTimeRange

DateTimeRangeFromTime creates a DateTimeRange from two time.Time values. It converts the from and until times to DateTime types and returns a DateTimeRange.

func DayDateTimeRange

func DayDateTimeRange(dt DateTime) DateTimeRange

DayDateTimeRange creates a DateTimeRange covering the entire day containing the given datetime. The range spans from 00:00:00.000000000 to 23:59:59.999999999 of that day.

func MonthDateTimeRange

func MonthDateTimeRange(dt DateTime) DateTimeRange

MonthDateTimeRange creates a DateTimeRange covering the entire month containing the given datetime. The range spans from the 1st day 00:00:00.000000000 to the last day 23:59:59.999999999 of that month.

func QuarterDateTimeRange

func QuarterDateTimeRange(dt DateTime) DateTimeRange

QuarterDateTimeRange creates a DateTimeRange covering the entire quarter containing the given datetime. Quarters are defined as: Q1=Jan-Mar, Q2=Apr-Jun, Q3=Jul-Sep, Q4=Oct-Dec. The range spans from the 1st day of the quarter 00:00:00.000000000 to the last day 23:59:59.999999999 of that quarter.

func WeekDateTimeRange

func WeekDateTimeRange(dt DateTime) DateTimeRange

WeekDateTimeRange creates a DateTimeRange covering the entire week containing the given datetime. The range spans from Monday 00:00:00.000000000 to Sunday 23:59:59.999999999 of that week. Uses ISO 8601 standard where Monday is the first day of the week.

func YearDateTimeRange

func YearDateTimeRange(dt DateTime) DateTimeRange

YearDateTimeRange creates a DateTimeRange covering the entire year containing the given datetime. The range spans from January 1st 00:00:00.000000000 to December 31st 23:59:59.999999999 of that year.

func (DateTimeRange) Ptr

func (r DateTimeRange) Ptr() *DateTimeRange

func (DateTimeRange) TimeRange

func (r DateTimeRange) TimeRange() TimeRange

TimeRange converts a DateTimeRange to a TimeRange.

func (DateTimeRange) Validate

func (r DateTimeRange) Validate(ctx context.Context) error

type DateTimeRanges

type DateTimeRanges []DateTimeRange

func (DateTimeRanges) Max

func (ranges DateTimeRanges) Max() *DateTimeRange

Max returns the maximum DateTimeRange that encompasses all ranges in the list. It finds the earliest From time and the latest Until time across all ranges. Returns nil if the list is empty.

func (DateTimeRanges) Min

func (ranges DateTimeRanges) Min() *DateTimeRange

Min returns the minimum DateTimeRange that is contained within all ranges in the list. It finds the latest From time and the earliest Until time across all ranges. Returns nil if the list is empty or if there is no overlap between ranges.

type DateTimes

type DateTimes []DateTime

func (DateTimes) Interfaces

func (t DateTimes) Interfaces() []interface{}

func (DateTimes) Strings

func (t DateTimes) Strings() []string

type Dates

type Dates []Date

func (Dates) Interfaces

func (d Dates) Interfaces() []interface{}

func (Dates) Strings

func (d Dates) Strings() []string

type Duration

type Duration stdtime.Duration

func DurationPtr

func DurationPtr(time *stdtime.Duration) *Duration

func ParseDuration

func ParseDuration(ctx context.Context, value interface{}) (*Duration, error)

func ParseDurationDefault

func ParseDurationDefault(ctx context.Context, value interface{}, defaultValue Duration) Duration

func (Duration) Abs

func (d Duration) Abs() Duration

func (Duration) Duration

func (d Duration) Duration() stdtime.Duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

func (Duration) MarshalText

func (d Duration) MarshalText() ([]byte, error)

func (Duration) Ptr

func (d Duration) Ptr() *Duration

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(b []byte) error

type Durations

type Durations []Duration

func (Durations) Interfaces

func (t Durations) Interfaces() []interface{}

func (Durations) Strings

func (t Durations) Strings() []string

type HasDuration

type HasDuration interface {
	Duration() stdtime.Duration
}

type HasTime

type HasTime interface {
	Time() stdtime.Time
}

type Layout

type Layout string

Layout is one of (millis,seconds,nano) or any time.Layout like time.RFC3339Nano

const (
	DateLayout   Layout = "2006-01-02"
	SecondLayout Layout = "second"
	MilliLayout  Layout = "milli"
	MicroLayout  Layout = "micro"
	NanoLayout   Layout = "nano"
	RFC3339      Layout = time.RFC3339
	RFC3339Nano  Layout = time.RFC3339Nano
)

func (Layout) Format

func (l Layout) Format(time time.Time) string

func (Layout) Parse

func (l Layout) Parse(ctx context.Context, value interface{}) (*time.Time, error)

func (Layout) String

func (l Layout) String() string

type Layouts

type Layouts []Layout

func (Layouts) Parse

func (l Layouts) Parse(ctx context.Context, value interface{}) (*time.Time, error)

type TimeOfDay

type TimeOfDay struct {
	Hour       int
	Minute     int
	Second     int
	Nanosecond int
	Location   *stdtime.Location
}

func ParseTimeOfDay

func ParseTimeOfDay(ctx context.Context, value interface{}) (*TimeOfDay, error)

func ParseTimeOfDayDefault

func ParseTimeOfDayDefault(
	ctx context.Context,
	value interface{},
	defaultValue TimeOfDay,
) TimeOfDay

func TimeOfDayFromTime

func TimeOfDayFromTime(date stdtime.Time) TimeOfDay

func (TimeOfDay) After

func (t TimeOfDay) After(stdTime TimeOfDay) bool

func (TimeOfDay) Before

func (t TimeOfDay) Before(stdTime TimeOfDay) bool

func (TimeOfDay) Date

func (t TimeOfDay) Date(year int, month stdtime.Month, day int) (*stdtime.Time, error)

func (TimeOfDay) DateTime

func (t TimeOfDay) DateTime(year int, month stdtime.Month, day int) DateTime

func (TimeOfDay) Equal

func (t TimeOfDay) Equal(stdTime TimeOfDay) bool

func (TimeOfDay) Format

func (t TimeOfDay) Format(layout string) string

func (TimeOfDay) MarshalJSON

func (t TimeOfDay) MarshalJSON() ([]byte, error)

func (TimeOfDay) MarshalText

func (t TimeOfDay) MarshalText() ([]byte, error)

func (TimeOfDay) Ptr

func (t TimeOfDay) Ptr() *TimeOfDay

func (TimeOfDay) String

func (t TimeOfDay) String() string

func (TimeOfDay) Time

func (t TimeOfDay) Time(year int, month stdtime.Month, day int) stdtime.Time

func (*TimeOfDay) UnmarshalJSON

func (t *TimeOfDay) UnmarshalJSON(b []byte) error

func (*TimeOfDay) UnmarshalText

func (t *TimeOfDay) UnmarshalText(b []byte) error

type TimeOfDays

type TimeOfDays []TimeOfDay

func (TimeOfDays) Interfaces

func (t TimeOfDays) Interfaces() []interface{}

func (TimeOfDays) Strings

func (t TimeOfDays) Strings() []string

type TimeRange

type TimeRange struct {
	From  stdtime.Time `json:"from,omitempty"`
	Until stdtime.Time `json:"until,omitempty"`
}

func DayTimeRange

func DayTimeRange(t stdtime.Time) TimeRange

DayTimeRange creates a TimeRange covering the entire day containing the given time. The range spans from 00:00:00.000000000 to 23:59:59.999999999 of that day.

func MonthTimeRange

func MonthTimeRange(t stdtime.Time) TimeRange

MonthTimeRange creates a TimeRange covering the entire month containing the given time. The range spans from the 1st day 00:00:00.000000000 to the last day 23:59:59.999999999 of that month.

func QuarterTimeRange

func QuarterTimeRange(t stdtime.Time) TimeRange

QuarterTimeRange creates a TimeRange covering the entire quarter containing the given time. Quarters are defined as: Q1=Jan-Mar, Q2=Apr-Jun, Q3=Jul-Sep, Q4=Oct-Dec. The range spans from the 1st day of the quarter to the last day 23:59:59.999999999 of that quarter.

func WeekTimeRange

func WeekTimeRange(t stdtime.Time) TimeRange

WeekTimeRange creates a TimeRange covering the entire week containing the given time. The range spans from Monday 00:00:00.000000000 to Sunday 23:59:59.999999999 of that week. Uses ISO 8601 standard where Monday is the first day of the week.

func YearTimeRange

func YearTimeRange(t stdtime.Time) TimeRange

YearTimeRange creates a TimeRange covering the entire year containing the given time. The range spans from January 1st 00:00:00.000000000 to December 31st 23:59:59.999999999 of that year.

func (TimeRange) Ptr

func (r TimeRange) Ptr() *TimeRange

func (TimeRange) Validate

func (r TimeRange) Validate(ctx context.Context) error

type UnixTime

type UnixTime stdtime.Time

func NewUnixTime

func NewUnixTime(
	year int,
	month stdtime.Month,
	day, hour, min, sec, nsec int,
	loc *stdtime.Location,
) UnixTime

NewUnixTime creates a UnixTime representing the date and time specified by the given parameters. It wraps the standard library's time.Date function with the same parameter signature.

func ParseUnixTime

func ParseUnixTime(ctx context.Context, value interface{}) (*UnixTime, error)

func ParseUnixTimeDefault

func ParseUnixTimeDefault(ctx context.Context, value interface{}, defaultValue UnixTime) UnixTime

func UnixTimeFromBinary

func UnixTimeFromBinary(ctx context.Context, value []byte) (*UnixTime, error)

func UnixTimeFromMicro

func UnixTimeFromMicro(usec int64) UnixTime

func UnixTimeFromMilli

func UnixTimeFromMilli(msec int64) UnixTime

func UnixTimeFromSeconds

func UnixTimeFromSeconds(seconds int64) UnixTime

func UnixTimePtr

func UnixTimePtr(time *stdtime.Time) *UnixTime

func (UnixTime) Add

func (u UnixTime) Add(duration HasDuration) UnixTime

func (UnixTime) AddDate

func (u UnixTime) AddDate(years int, months int, days int) UnixTime

func (UnixTime) AddTime deprecated

func (u UnixTime) AddTime(years int, months int, days int) UnixTime

Deprecated: Use AddDate instead. AddTime adds the given years, months, and days to the UnixTime but will be removed in future versions.

func (UnixTime) After

func (u UnixTime) After(time HasTime) bool

func (UnixTime) Before

func (u UnixTime) Before(time HasTime) bool

func (UnixTime) Clone

func (u UnixTime) Clone() UnixTime

func (*UnixTime) ClonePtr

func (u *UnixTime) ClonePtr() *UnixTime

func (UnixTime) Compare

func (u UnixTime) Compare(other UnixTime) int

func (*UnixTime) ComparePtr

func (u *UnixTime) ComparePtr(other *UnixTime) int

func (UnixTime) DateTime

func (u UnixTime) DateTime() DateTime

func (UnixTime) Day

func (u UnixTime) Day() int

func (UnixTime) Equal

func (u UnixTime) Equal(unixTime UnixTime) bool

func (*UnixTime) EqualPtr

func (u *UnixTime) EqualPtr(unixTime *UnixTime) bool

func (UnixTime) Format

func (u UnixTime) Format(layout string) string

func (UnixTime) Hour

func (u UnixTime) Hour() int

func (UnixTime) IsZero

func (u UnixTime) IsZero() bool

IsZero reports whether u represents the zero time instant.

func (UnixTime) MarshalBinary

func (u UnixTime) MarshalBinary() ([]byte, error)

func (UnixTime) MarshalJSON

func (u UnixTime) MarshalJSON() ([]byte, error)

func (UnixTime) MarshalText

func (u UnixTime) MarshalText() ([]byte, error)

func (UnixTime) Minute

func (u UnixTime) Minute() int

func (UnixTime) Month

func (u UnixTime) Month() stdtime.Month

func (UnixTime) Nanosecond

func (u UnixTime) Nanosecond() int

func (UnixTime) Ptr

func (u UnixTime) Ptr() *UnixTime

func (UnixTime) Second

func (u UnixTime) Second() int

func (UnixTime) String

func (u UnixTime) String() string

func (UnixTime) Sub

func (u UnixTime) Sub(time HasTime) Duration

func (UnixTime) Time

func (u UnixTime) Time() stdtime.Time

func (*UnixTime) TimePtr

func (u *UnixTime) TimePtr() *stdtime.Time

func (UnixTime) Truncate

func (u UnixTime) Truncate(duration HasDuration) UnixTime

func (UnixTime) UTC

func (u UnixTime) UTC() UnixTime

func (UnixTime) Unix

func (u UnixTime) Unix() int64

func (UnixTime) UnixMicro

func (u UnixTime) UnixMicro() int64

func (*UnixTime) UnmarshalJSON

func (u *UnixTime) UnmarshalJSON(b []byte) error

func (*UnixTime) UnmarshalText

func (u *UnixTime) UnmarshalText(b []byte) error

func (UnixTime) Validate

func (u UnixTime) Validate(ctx context.Context) error

func (UnixTime) Weekday

func (u UnixTime) Weekday() Weekday

func (UnixTime) Year

func (u UnixTime) Year() int

type UnixTimeRange

type UnixTimeRange struct {
	From  UnixTime `json:"from,omitempty"`
	Until UnixTime `json:"until,omitempty"`
}

func DayUnixTimeRange

func DayUnixTimeRange(ut UnixTime) UnixTimeRange

DayUnixTimeRange creates a UnixTimeRange covering the entire day containing the given unix time. The range spans from 00:00:00.000000000 to 23:59:59.999999999 of that day.

func MonthUnixTimeRange

func MonthUnixTimeRange(ut UnixTime) UnixTimeRange

MonthUnixTimeRange creates a UnixTimeRange covering the entire month containing the given unix time. The range spans from the 1st day 00:00:00.000000000 to the last day 23:59:59.999999999 of that month.

func QuarterUnixTimeRange

func QuarterUnixTimeRange(ut UnixTime) UnixTimeRange

QuarterUnixTimeRange creates a UnixTimeRange covering the entire quarter containing the given unix time. Quarters are defined as: Q1=Jan-Mar, Q2=Apr-Jun, Q3=Jul-Sep, Q4=Oct-Dec. The range spans from the 1st day of the quarter 00:00:00.000000000 to the last day 23:59:59.999999999 of that quarter.

func UnixTimeRangeFromTime

func UnixTimeRangeFromTime(from, until stdtime.Time) UnixTimeRange

UnixTimeRangeFromTime creates a UnixTimeRange from two time.Time values. It converts the from and until times to UnixTime types and returns a UnixTimeRange.

func WeekUnixTimeRange

func WeekUnixTimeRange(ut UnixTime) UnixTimeRange

WeekUnixTimeRange creates a UnixTimeRange covering the entire week containing the given unix time. The range spans from Monday 00:00:00.000000000 to Sunday 23:59:59.999999999 of that week. Uses ISO 8601 standard where Monday is the first day of the week.

func YearUnixTimeRange

func YearUnixTimeRange(ut UnixTime) UnixTimeRange

YearUnixTimeRange creates a UnixTimeRange covering the entire year containing the given unix time. The range spans from January 1st 00:00:00.000000000 to December 31st 23:59:59.999999999 of that year.

func (UnixTimeRange) Ptr

func (r UnixTimeRange) Ptr() *UnixTimeRange

func (UnixTimeRange) TimeRange

func (r UnixTimeRange) TimeRange() TimeRange

TimeRange converts a UnixTimeRange to a TimeRange.

func (UnixTimeRange) Validate

func (r UnixTimeRange) Validate(ctx context.Context) error

type UnixTimeRanges

type UnixTimeRanges []UnixTimeRange

func (UnixTimeRanges) Max

func (ranges UnixTimeRanges) Max() *UnixTimeRange

Max returns the maximum UnixTimeRange that encompasses all ranges in the list. It finds the earliest From time and the latest Until time across all ranges. Returns nil if the list is empty.

func (UnixTimeRanges) Min

func (ranges UnixTimeRanges) Min() *UnixTimeRange

Min returns the minimum UnixTimeRange that is contained within all ranges in the list. It finds the latest From time and the earliest Until time across all ranges. Returns nil if the list is empty or if there is no overlap between ranges.

type UnixTimes

type UnixTimes []UnixTime

func (UnixTimes) Interfaces

func (t UnixTimes) Interfaces() []interface{}

func (UnixTimes) Strings

func (t UnixTimes) Strings() []string

type WaiterDuration

type WaiterDuration interface {
	Wait(ctx context.Context, duration Duration) error
}

func NewWaiterDuration

func NewWaiterDuration() WaiterDuration

type WaiterDurationFunc

type WaiterDurationFunc func(ctx context.Context, duration Duration) error

func (WaiterDurationFunc) Wait

func (w WaiterDurationFunc) Wait(ctx context.Context, duration Duration) error

type WaiterUntil

type WaiterUntil interface {
	WaitUntil(ctx context.Context, until DateTime) error
}

func NewWaiterUntil

func NewWaiterUntil(currentDateTime CurrentDateTimeGetter) WaiterUntil

type WaiterUntilFunc

type WaiterUntilFunc func(ctx context.Context, until DateTime) error

func (WaiterUntilFunc) WaitUntil

func (w WaiterUntilFunc) WaitUntil(ctx context.Context, until DateTime) error

type Weekday

type Weekday stdtime.Weekday
const (
	Sunday Weekday = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
)

func AsWeekday

func AsWeekday[T ~int](value T) Weekday

func ParseWeekday

func ParseWeekday(ctx context.Context, value any) (*Weekday, error)

func (Weekday) Ptr

func (w Weekday) Ptr() *Weekday

func (Weekday) String

func (w Weekday) String() string

func (Weekday) Validate

func (w Weekday) Validate(ctx context.Context) error

func (Weekday) Weekday

func (w Weekday) Weekday() stdtime.Weekday

type Weekdays

type Weekdays []Weekday

func AsWeekdays

func AsWeekdays[T ~int](values []T) Weekdays

func ParseWeekdays

func ParseWeekdays(ctx context.Context, values any) (Weekdays, error)

func (Weekdays) Contains

func (w Weekdays) Contains(value Weekday) bool

func (Weekdays) Validate

func (w Weekdays) Validate(ctx context.Context) error

func (Weekdays) Weekdays

func (w Weekdays) Weekdays() []stdtime.Weekday

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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