Documentation
¶
Overview ¶
Package date provides tools for working with dates, extending the standard library `time` package.
This package provides helpers for converting from a full `time.Time{}` to a `Date{}` and back, providing validation along the way. Many methods from `time.Time{}` are also provided as equivalents here (`After()`, `Before()`, `Sub()`, etc.). Additionally, custom serialization methods are provided both for JSON and SQL.
The Go standard library contains no native type for dates without times. Instead, common convention is to use a `time.Time{}` with only the year, month, and day set. For example, this convention is followed when a timestamp of the form YYYY-MM-DD is parsed via `time.Parse(time.DateOnly, value)`.
Index ¶
- func NullTimeFromPtr(d *Date, opts ...ConvertOption) sql.NullTime
- type ConvertConfig
- type ConvertOption
- type Date
- func (d Date) AddDays(days int) Date
- func (d Date) AddMonths(months int) Date
- func (d Date) AddMonthsStdlib(months int) Date
- func (d Date) AddYears(years int) Date
- func (d Date) AddYearsStdlib(years int) Date
- func (d Date) After(other Date) bool
- func (d Date) Before(other Date) bool
- func (d Date) Compare(other Date) int
- func (d Date) Date() (int, time.Month, int)
- func (d Date) Equal(other Date) bool
- func (d Date) Format(layout string) string
- func (d Date) GoString() string
- func (d Date) ISOWeek() (year, week int)
- func (d Date) IsZero() bool
- func (d Date) MarshalJSON() ([]byte, error)
- func (d Date) MarshalText() ([]byte, error)
- func (d Date) MonthEnd() Date
- func (d Date) MonthStart() Date
- func (d *Date) Scan(src any) error
- func (d Date) String() string
- func (d Date) Sub(other Date) int64
- func (d Date) SubErr(other Date) (int64, error)
- func (d Date) ToTime(opts ...ConvertOption) time.Time
- func (d *Date) UnmarshalJSON(data []byte) error
- func (d *Date) UnmarshalText(data []byte) error
- func (d Date) Value() (driver.Value, error)
- func (d Date) Weekday() time.Weekday
- func (d Date) YearDay() int
- type NullDate
- type TodayConfig
- type TodayOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NullTimeFromPtr ¶
func NullTimeFromPtr(d *Date, opts ...ConvertOption) sql.NullTime
NullTimeFromPtr converts a date to a native Go `sql.NullTime`; the convention in Go is that a **date-only** is parsed (via `time.DateOnly`) as `time.Date(YYYY, MM, DD, 0, 0, 0, 0, time.UTC)`.
Types ¶
type ConvertConfig ¶
ConvertConfig helps customize the behavior of conversion functions like `NullTimeFromPtr()`.
It allows setting the fields in a `time.Time{}` **other** than year, month, and day (i.e. the fields that aren't present in a date). By default, these are: - hour=0 - minute=0 - second=0 - nanosecond=0 - timezone/loc=time.UTC
type ConvertOption ¶
type ConvertOption func(*ConvertConfig)
ConvertOption defines a function that will be applied to a convert config.
func OptConvertHour ¶
func OptConvertHour(hour int) ConvertOption
OptConvertHour returns an option that sets the hour on a convert config.
func OptConvertMinute ¶
func OptConvertMinute(minute int) ConvertOption
OptConvertMinute returns an option that sets the minute on a convert config.
func OptConvertNanosecond ¶
func OptConvertNanosecond(nanosecond int) ConvertOption
OptConvertNanosecond returns an option that sets the nanosecond on a convert config.
func OptConvertSecond ¶
func OptConvertSecond(second int) ConvertOption
OptConvertSecond returns an option that sets the second on a convert config.
func OptConvertTimezone ¶
func OptConvertTimezone(tz *time.Location) ConvertOption
OptConvertTimezone returns an option that sets the timezone on a convert config.
type Date ¶
Date is a simple date (i.e. without timestamp). This is intended to be JSON serialized / deserialized as YYYY-MM-DD.
func FromString ¶
FromString parses a string of the form YYYY-MM-DD into a `Date{}`.
func FromTime ¶
FromTime validates that a `time.Time{}` contains a date and converts it to a `Date{}`.
func InTimezone ¶
InTimezone translates a timestamp into a timezone and then captures the date in that timezone.
func NewDate ¶
NewDate returns a new `Date` struct. This is a pure convenience function to make it more ergonomic to create a `Date` struct.
func Today ¶
func Today(opts ...TodayOption) Date
Today determines the **current** `Date`, shifted to a given timezone if need be.
Defaults to using UTC and `time.Now()` to determine the current time.
func (Date) AddMonths ¶
AddMonths returns the date corresponding to adding the given number of months. This accounts for leap years and variable length months. Typically the only change is in the month and year but for changes that would exceed the number of days in the target month, the last day of the month is used.
For example: - adding 1 month to 2020-05-11 results in 2020-06-11 - adding 1 month to 2022-01-31 results in 2022-02-28 - adding 3 months to 2024-01-31 results in 2024-04-30 - subtracting 2 months from 2022-01-31 results in 2022-11-30
NOTE: This behavior is very similar to but distinct from `time.Time{}.AddDate()` specialized to `months` only.
func (Date) AddMonthsStdlib ¶
AddMonthsStdlib returns the date corresponding to adding the given number of months, using `time.Time{}.AddDate()` from the standard library. This may "overshoot" if the target date is not a valid date in that month, e.g. 2020-02-31.
For example: - adding 1 month to 2020-05-11 results in 2020-06-11 - adding 1 month to 2022-01-31 results in 2022-03-03 - adding 3 months to 2024-01-31 results in 2024-05-01 - subtracting 2 months from 2022-01-31 results in 2022-12-01
func (Date) AddYears ¶
AddYears returns the date corresponding to adding the given number of years, using `time.Time{}.AddDate()` from the standard library. This may "overshoot" if the target date is not a valid date in that month, e.g. 2020-02-31.
For example: - adding 1 year to 2020-02-29 results in 2021-03-01 - adding 1 year to 2023-02-28 results in 2024-02-28 - adding 10 years to 2010-05-01 results in 2020-05-01 - subtracting 10 years from 2010-05-01 results in 2000-05-01
NOTE: This behavior is very similar to but distinct from `time.Time{}.AddDate()` specialized to `years` only.
func (Date) AddYearsStdlib ¶
AddYearsStdlib returns the date corresponding to adding the given number of years. This accounts for leap years and variable length months. Typically the only change is in the month and year but for changes that would exceed the number of days in the target month, the last day of the month is used.
For example: - adding 1 year to 2020-02-29 results in 2021-02-28 - adding 1 year to 2023-02-28 results in 2024-02-28 - adding 10 years to 2010-05-01 results in 2020-05-01 - subtracting 10 years from 2010-05-01 results in 2000-05-01
NOTE: This behavior is very similar to but distinct from `time.Time{}.AddDate()` specialized to `years` only.
func (Date) Compare ¶
Compare compares the date d with other. If d is before other, it returns -1; if d is after other, it returns +1; if they're the same, it returns 0.
func (Date) Date ¶
Date returns the year, month, and day in which `d` occurs.
This is here for parity with `time.Time{}.Date()` and is likely not needed.
func (Date) Format ¶
Format returns a textual representation of the date value formatted according to the provided layout. This uses `time.Time{}.Format()` directly and is provided here for convenience.
func (Date) ISOWeek ¶
ISOWeek returns the ISO 8601 year and week number in which `d` occurs. Week ranges from 1 to 53. Jan 01 to Jan 03 of year `n` might belong to week 52 or 53 of year `n-1`, and Dec 29 to Dec 31 might belong to week 1 of year `n+1`.
func (Date) MarshalJSON ¶
MarshalJSON implements `json.Marshaler`; formats the date as YYYY-MM-DD.
func (Date) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (Date) MonthStart ¶
MonthStart returns the first date in the month of the current date.
func (*Date) Scan ¶
Scan implements `sql.Scanner`; it unmarshals values of the type `time.Time` onto the current `Date` struct.
func (Date) Sub ¶
Sub returns the number of days `d - other`; this converts both dates to a `time.Time{}` UTC and then dispatches to `time.Time{}.Sub()`.
func (Date) SubErr ¶
SubErr returns the number of days `d - other`; this converts both dates to a `time.Time{}` UTC and then dispatches to `time.Time{}.Sub()`.
If the number of days is not a whole number (due to overflow), an error is returned.
func (Date) ToTime ¶
func (d Date) ToTime(opts ...ConvertOption) time.Time
ToTime converts the date to a native Go `time.Time`; the convention in Go is that a **date-only** is parsed (via `time.DateOnly`) as `time.Date(YYYY, MM, DD, 0, 0, 0, 0, time.UTC)`.
func (*Date) UnmarshalJSON ¶
UnmarshalJSON implements `json.Unmarshaler`; parses the date as YYYY-MM-DD.
func (*Date) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The time must be in the format YYYY-MM-DD.
func (Date) Value ¶
Value implements `driver.Valuer`; it marshals the value to a `time.Time` to be serialized into the database.
type NullDate ¶
NullDate is a `Date` that can be null.
func NullDateFromPtr ¶
NullDateFromPtr converts a `Date` pointer into a `NullDate`.
type TodayConfig ¶
TodayConfig helps customize the behavior of `Today()`.
type TodayOption ¶
type TodayOption func(*TodayConfig)
TodayOption defines a function that will be applied to a `Today()` config.
func OptTodayNow ¶
func OptTodayNow(now time.Time) TodayOption
OptTodayNow returns an option that sets the now provider on a `Today()` config to return a **constant** `now` value.
This is expected to be used in tests.
func OptTodayTimezone ¶
func OptTodayTimezone(tz *time.Location) TodayOption
OptTodayTimezone returns an option that sets the timezone on a `Today()` config.