cal

package
v0.401.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 11

Documentation

Overview

Package cal provides simple date handling.

Index

Constants

View Source
const RFC3339Milli = "2006-01-02T15:04:05.000Z"

RFC3339Milli is the canonical marshaling format used by Timestamp: RFC3339 in UTC with a Z suffix and millisecond precision.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date struct {
	civil.Date
}

Date represents a simple date without time used most frequently with business documents.

func DateOf added in v0.52.4

func DateOf(t time.Time) Date

DateOf returns the Date in which a time occurs in the time's location.

func MakeDate

func MakeDate(year int, month time.Month, day int) Date

MakeDate provides a new date instance.

func NewDate

func NewDate(year int, month time.Month, day int) *Date

NewDate provides a pointer to a new date instance.

func Today added in v0.40.0

func Today() Date

Today generates a new date instance for today.

func TodayIn added in v0.40.0

func TodayIn(loc *time.Location) Date

TodayIn generates a new date instance for today in the given location.

func (Date) Add added in v0.52.4

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

Add returns a new date with the given number of years, months and days. This uses the time package to do the arithmetic.

func (*Date) Clone added in v0.42.2

func (d *Date) Clone() *Date

Clone returns a new pointer to a copy of the date.

func (Date) JSONSchema

func (Date) JSONSchema() *jsonschema.Schema

JSONSchema returns a custom json schema for the date.

func (Date) Time added in v0.52.4

func (d Date) Time() time.Time

Time returns a time object for the date.

func (Date) TimeIn added in v0.52.4

func (d Date) TimeIn(loc *time.Location) time.Time

TimeIn returns a time object for the date in the given location which may be important when considering timezones and arithmetic.

func (*Date) UnmarshalJSON added in v0.39.0

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

UnmarshalJSON is used to parse a date from json and ensures that we can handle invalid data reasonably.

func (Date) WithTime added in v0.215.0

func (d Date) WithTime(t Time) DateTime

WithTime appends the time to the date to create a DateTime object.

type DateTest added in v0.400.0

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

DateTest is used to validate a date according to the provided rules.

func DateAfter

func DateAfter(date Date) DateTest

DateAfter returns a validation rule which checks to ensure the date is *after* the provided date.

func DateBefore

func DateBefore(date Date) DateTest

DateBefore is used during validation to ensure the date is before the value passed in.

func DateNotZero

func DateNotZero() DateTest

DateNotZero ensures the date is not a zero value.

func (DateTest) Check added in v0.400.0

func (d DateTest) Check(value any) bool

Check will perform the defines date test on the provided value.

func (DateTest) String added in v0.400.0

func (d DateTest) String() string

String provides a description of the test.

func (DateTest) Validate added in v0.400.0

func (d DateTest) Validate(value any) error

Validate is used to check a dates value.

type DateTime added in v0.56.0

type DateTime struct {
	civil.DateTime
}

DateTime represents a combination of date and time without location specific information nor support for millisecond precision.

func DateTimeOf added in v0.56.0

func DateTimeOf(t time.Time) DateTime

DateTimeOf returns the DateTime from the provided time.

func MakeDateTime added in v0.56.0

func MakeDateTime(year int, month time.Month, day, hour, minute, second int) DateTime

MakeDateTime provides a new date time instance.

func NewDateTime added in v0.56.0

func NewDateTime(year int, month time.Month, day, hour, minute, second int) *DateTime

NewDateTime provides a pointer to a new date time instance.

func ThisSecond added in v0.56.0

func ThisSecond() DateTime

ThisSecond produces a new date time instance for the current UTC time to the nearest second.

func ThisSecondIn added in v0.56.0

func ThisSecondIn(loc *time.Location) DateTime

ThisSecondIn provides a new date time using the current time from the provided location as a reference.

func (*DateTime) Clone added in v0.56.0

func (dt *DateTime) Clone() *DateTime

Clone returns a new pointer to a copy of the date time.

func (DateTime) Date added in v0.215.0

func (dt DateTime) Date() Date

Date returns the date component of the date time.

func (DateTime) In added in v0.56.0

func (dt DateTime) In(loc *time.Location) time.Time

In returns a new time.Time instance with the provided location.

func (DateTime) JSONSchema added in v0.56.0

func (DateTime) JSONSchema() *jsonschema.Schema

JSONSchema returns a custom json schema for the date time.

func (DateTime) Time added in v0.215.0

func (dt DateTime) Time() Time

Time returns the time component of the date time.

func (DateTime) TimeZ added in v0.56.0

func (dt DateTime) TimeZ() time.Time

TimeZ returns a new time.Time instance with the UTC location.

func (*DateTime) UnmarshalJSON added in v0.56.0

func (dt *DateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON is used to parse a date from json and ensures that we can handle invalid data reasonably.

type DateTimeTest added in v0.400.0

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

DateTimeTest is used to validate a date time according to the provided rules.

func DateTimeAfter added in v0.56.0

func DateTimeAfter(dt DateTime) DateTimeTest

DateTimeAfter returns a validation rule which checks to ensure the date time is *after* the provided date time.

func DateTimeBefore added in v0.56.0

func DateTimeBefore(dt DateTime) DateTimeTest

DateTimeBefore is used during validation to ensure the date time is before the value passed in.

func DateTimeNotZero added in v0.56.0

func DateTimeNotZero() DateTimeTest

DateTimeNotZero ensures the date time is not a zero value.

func (DateTimeTest) Check added in v0.400.0

func (d DateTimeTest) Check(value any) bool

Check will perform the defined date time test on the provided value.

func (DateTimeTest) String added in v0.400.0

func (d DateTimeTest) String() string

String provides a description of the test.

func (DateTimeTest) Validate added in v0.400.0

func (d DateTimeTest) Validate(value any) error

Validate is used to check a date time's value.

type Period

type Period struct {
	// Label is a short description of the period.
	Label string `json:"label,omitempty" jsonschema:"title=Label"`
	// Start indicates when this period starts.
	Start Date `json:"start" jsonschema:"title=Start"`
	// End indicates when the period ends, and must be after the start date.
	End Date `json:"end" jsonschema:"title=End"`
}

Period represents two dates with a start and finish.

type Time added in v0.215.0

type Time struct {
	civil.Time
}

Time represents a simple time of day without a date component.

func MakeTime added in v0.215.0

func MakeTime(hour, minute, second int) Time

MakeTime provides a new time instance.

func NewTime added in v0.215.0

func NewTime(hour, minute, second int) *Time

NewTime provides a pointer to a new time instance.

func TimeNow added in v0.215.0

func TimeNow() Time

TimeNow generates a new time instance for now.

func TimeNowIn added in v0.215.0

func TimeNowIn(loc *time.Location) Time

TimeNowIn provides the current time of the day in the provided location.

func (Time) IsZero added in v0.401.0

func (t Time) IsZero() bool

IsZero returns true if the time is the zero value (00:00:00). This is used by the `omitzero` JSON tag to determine if the time should be omitted from JSON output.

func (Time) JSONSchema added in v0.215.0

func (Time) JSONSchema() *jsonschema.Schema

JSONSchema returns a custom json schema for the current hour, minute, and seconds of the day.

func (*Time) UnmarshalJSON added in v0.215.0

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON is used to parse a time from json and ensures that we can handle invalid data reasonably.

type Timestamp added in v0.401.0

type Timestamp struct {
	time.Time
}

Timestamp wraps time.Time with JSON serialization in RFC3339 format, always in UTC with a `Z` suffix and millisecond precision. Parsing tolerates optional sub-second precision up to nanoseconds and arbitrary offsets, always normalizing to UTC.

func ParseTimestamp added in v0.401.0

func ParseTimestamp(s string) (Timestamp, error)

ParseTimestamp parses an RFC3339 formatted string (sub-second precision optional) and returns it as a UTC Timestamp.

func TimestampNow added in v0.401.0

func TimestampNow() Timestamp

TimestampNow returns the current time as a UTC Timestamp.

func TimestampOf added in v0.401.0

func TimestampOf(t time.Time) Timestamp

TimestampOf returns t as a Timestamp normalized to UTC.

func (*Timestamp) Clone added in v0.401.0

func (t *Timestamp) Clone() *Timestamp

Clone returns a pointer to a copy of the timestamp.

func (Timestamp) JSONSchema added in v0.401.0

func (Timestamp) JSONSchema() *jsonschema.Schema

JSONSchema returns a custom json schema for the timestamp.

func (Timestamp) MarshalJSON added in v0.401.0

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

MarshalJSON emits the timestamp in canonical RFC3339 UTC millisecond format. A zero timestamp is emitted as `null`.

func (Timestamp) String added in v0.401.0

func (t Timestamp) String() string

String provides the timestamp in RFC3339 UTC format with millisecond precision.

func (*Timestamp) UnmarshalJSON added in v0.401.0

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts an RFC3339 encoded string (with or without sub-second precision, any offset) or a JSON null.

type TimestampTest added in v0.401.0

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

TimestampTest is used to validate a timestamp according to the provided rules.

func TimestampAfter added in v0.401.0

func TimestampAfter(t Timestamp) TimestampTest

TimestampAfter returns a validation rule which checks to ensure the timestamp is *after* the provided timestamp.

func TimestampBefore added in v0.401.0

func TimestampBefore(t Timestamp) TimestampTest

TimestampBefore is used during validation to ensure the timestamp is before the value passed in.

func TimestampNotZero added in v0.401.0

func TimestampNotZero() TimestampTest

TimestampNotZero ensures the timestamp is not a zero value.

func (TimestampTest) Check added in v0.401.0

func (d TimestampTest) Check(value any) bool

Check will perform the defined timestamp test on the provided value.

func (TimestampTest) String added in v0.401.0

func (d TimestampTest) String() string

String provides a description of the test.

func (TimestampTest) Validate added in v0.401.0

func (d TimestampTest) Validate(value any) error

Validate is used to check a timestamp's value.

Jump to

Keyboard shortcuts

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