Documentation
¶
Overview ¶
Package date provides calendar dates as ISO 8601 strings (YYYY-MM-DD) with lenient parsing of the many ways people write dates.
Normalize and Date.Normalized turn input like "25.12.2023", "Dec 25, 2023" or "1. Dezember 2016" into "2023-12-25". A numeric date like "05/06/2024", where both orders are valid and no ordinal suffix like "1st" marks the day, is read by its separators: day first with dots like in Germany ("05.06.2024") and with any other separator but slashes, also with an English language hint. Slashes are written in both orders, so for them a language hint decides: English (language.EN, "en-US") reads month first, every other language day first. Without a hint slashes are read month first like in the US. SQL Scan and Value read them that way, because they fully normalize and have no hint. JSON and XML only trim surrounding whitespace, so a slash date stays as written. Two-digit years below 45 are read as 20xx, all others as 19xx.
The package includes:
- Date and NullableDate with SQL Scan and Value methods; only NullableDate encodes null as JSON null
- YearMonth, YearQuarter, NullableYearMonth and NullableYearQuarter for reporting periods. They trim on every path, and a year-quarter is also uppercased. A zero period is kept.
- PeriodRange for years, half years, quarters, months and ISO weeks
- date arithmetic, comparison and conversion to time.Time
- Finder to find dates in text
- Parser, Formatter and Format for the strfmt package
Methods other than reading and writing normalize their input, so a valid but non-normalized Date like "25.12.2023" works everywhere except in == comparisons. SQL Scan stores a valid date normalized and a zero date like "0001-01-01" as empty, and Value writes the normalized date, SQL NULL for an empty or zero date, and returns an error for an invalid date. JSON and XML only trim surrounding whitespace, so "25.12.2023" and a zero date stay as written. fmt prints a date as stored.
See the README.md of this package for the full API, the serialization rules and the gotchas.
Index ¶
- Constants
- Variables
- func ParseTime(str string, layouts ...string) (time.Time, error)
- func StringIsDate(str string, lang ...language.Code) bool
- type Date
- func FromUntilFromYearAndMonths(year, months string) (fromDate, untilDate Date, err error)
- func Must(str string) Date
- func Normalize(str string, lang ...language.Code) (Date, error)
- func Of(year int, month time.Month, day int) Date
- func OfNowInUTC() Date
- func OfTime(t time.Time) Date
- func OfToday() Date
- func OfTodayIn(loc *time.Location) Date
- func OfTomorrow() Date
- func OfYesterday() Date
- func Parse(layout, value string) (Date, error)
- func PeriodRange(period string) (from, until Date, err error)
- func YearRange(year int) (from, until Date)
- func YearWeekMonday(year, week int) (monday Date)
- func YearWeekRange(year, week int) (monday, sunday Date)
- func (date Date) Add(d time.Duration) Date
- func (date Date) AddDate(years int, months int, days int) Date
- func (date Date) AddDays(days int) Date
- func (date Date) AddMonths(months int) Date
- func (date Date) AddYears(years int) Date
- func (date Date) After(other Date) bool
- func (date Date) AfterTime(other time.Time) bool
- func (date Date) AfterToday() bool
- func (date Date) AfterTodayInUTC() bool
- func (date Date) Before(other Date) bool
- func (date Date) BeforeTime(other time.Time) bool
- func (date Date) BeforeToday() bool
- func (date Date) BeforeTodayInUTC() bool
- func (date Date) BeginningOfMonth() Date
- func (date Date) BeginningOfQuarter() Date
- func (date Date) BeginningOfWeek(startDay time.Weekday) Date
- func (date Date) BeginningOfYear() Date
- func (date Date) BetweenExcl(after, before Date) bool
- func (date Date) Compare(other Date) int
- func (date Date) Day() int
- func (date Date) EndOfMonth() Date
- func (date Date) EndOfQuarter() Date
- func (date Date) EndOfWeek(weekday time.Weekday) Date
- func (date Date) EndOfYear() Date
- func (date Date) EqualOrAfter(other Date) bool
- func (date Date) EqualOrBefore(other Date) bool
- func (date Date) Format(layout string) string
- func (date Date) ISOWeek() (year, week int)
- func (date Date) IsToday() bool
- func (date Date) IsTodayInUTC() bool
- func (date Date) IsZero() bool
- func (Date) JSONSchema() *jsonschema.Schema
- func (date Date) LastMonday() Date
- func (date Date) MarshalJSON() ([]byte, error)
- func (date Date) MarshalText() ([]byte, error)
- func (date *Date) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (date Date) Midnight() time.Time
- func (date Date) MidnightInLocation(loc *time.Location) time.Time
- func (date Date) MidnightUTC() time.Time
- func (date Date) Month() time.Month
- func (date Date) NextSunday() Date
- func (date Date) Normalized() (Date, error)
- func (date Date) NormalizedEqual(other Date) bool
- func (date Date) NormalizedLang(lang language.Code) (Date, error)
- func (date Date) NormalizedOrNull(lang ...language.Code) NullableDate
- func (date Date) NormalizedOrUnchanged(lang ...language.Code) Date
- func (date Date) Nullable() NullableDate
- func (date *Date) Scan(value any) error
- func (date *Date) ScanString(source string, validate bool) error
- func (date *Date) ScanStringWithLang(source string, lang language.Code) (wasNormalized bool, monthMustBeFirst bool, err error)
- func (date Date) Sub(other Date) time.Duration
- func (date Date) Time(hour, minute, second int, location *time.Location) time.Time
- func (date Date) TimeLocal(hour, minute, second int) time.Time
- func (date Date) TimeUTC(hour, minute, second int) time.Time
- func (date *Date) UnmarshalJSON(data []byte) error
- func (date *Date) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (date *Date) UnmarshalXMLAttr(attr xml.Attr) error
- func (date Date) Valid() bool
- func (date Date) ValidAndNormalized() bool
- func (date Date) Validate() error
- func (date Date) Value() (driver.Value, error)
- func (date Date) Weekday() time.Weekday
- func (date Date) WithinIncl(from, until Date) bool
- func (date Date) Year() int
- func (date Date) YearMonthDay() (year int, month time.Month, day int)
- type Finder
- type Format
- type Formatter
- type NullableDate
- func (n NullableDate) Add(d time.Duration) NullableDate
- func (n NullableDate) AddDate(years int, months int, days int) NullableDate
- func (n NullableDate) AddDays(days int) NullableDate
- func (n NullableDate) AddMonths(months int) NullableDate
- func (n NullableDate) AddYears(years int) NullableDate
- func (n NullableDate) After(other NullableDate) bool
- func (n NullableDate) AfterTime(other time.Time) bool
- func (n NullableDate) AfterToday() bool
- func (n NullableDate) AfterTodayInUTC() bool
- func (n NullableDate) Before(other NullableDate) bool
- func (n NullableDate) BeforeTime(other time.Time) bool
- func (n NullableDate) BeforeToday() bool
- func (n NullableDate) BeforeTodayInUTC() bool
- func (n NullableDate) BeginningOfMonth() NullableDate
- func (n NullableDate) BeginningOfQuarter() NullableDate
- func (n NullableDate) BeginningOfWeek(startDay time.Weekday) NullableDate
- func (n NullableDate) BeginningOfYear() NullableDate
- func (n NullableDate) Compare(other NullableDate) int
- func (n NullableDate) Date() Date
- func (n NullableDate) DateOr(useIfNull Date) Date
- func (n NullableDate) Day() int
- func (n NullableDate) EndOfMonth() NullableDate
- func (n NullableDate) EndOfQuarter() NullableDate
- func (n NullableDate) EndOfWeek(weekday time.Weekday) NullableDate
- func (n NullableDate) EndOfYear() NullableDate
- func (n NullableDate) EqualOrAfter(other NullableDate) bool
- func (n NullableDate) EqualOrBefore(other NullableDate) bool
- func (n NullableDate) Format(layout string) string
- func (n NullableDate) Get() Date
- func (n NullableDate) GetOr(defaultDate Date) Date
- func (n NullableDate) ISOWeek() (year, week int)
- func (n NullableDate) IsNotNull() bool
- func (n NullableDate) IsNull() bool
- func (n NullableDate) IsToday() bool
- func (n NullableDate) IsTodayInUTC() bool
- func (n NullableDate) IsZero() bool
- func (NullableDate) JSONSchema() *jsonschema.Schema
- func (n NullableDate) LastMonday() NullableDate
- func (n NullableDate) MarshalJSON() ([]byte, error)
- func (n NullableDate) MarshalText() ([]byte, error)
- func (n *NullableDate) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (n NullableDate) Midnight() nullable.Time
- func (n NullableDate) MidnightInLocation(loc *time.Location) nullable.Time
- func (n NullableDate) MidnightUTC() nullable.Time
- func (n NullableDate) Month() time.Month
- func (n NullableDate) NextSunday() NullableDate
- func (n NullableDate) Normalized() (NullableDate, error)
- func (n NullableDate) NormalizedEqual(other NullableDate) bool
- func (n NullableDate) NormalizedLang(lang language.Code) (NullableDate, error)
- func (n NullableDate) NormalizedOrNull(lang ...language.Code) NullableDate
- func (n NullableDate) NormalizedOrUnchanged(lang ...language.Code) NullableDate
- func (n *NullableDate) Scan(value any) error
- func (n *NullableDate) ScanString(source string, validate bool) error
- func (n *NullableDate) ScanStringWithLang(source string, lang language.Code) (wasNormalized bool, monthMustBeFirst bool, err error)
- func (n *NullableDate) Set(d Date)
- func (n *NullableDate) SetNull()
- func (n NullableDate) StringOr(nullString string) string
- func (n NullableDate) Sub(other NullableDate) time.Duration
- func (n *NullableDate) UnmarshalJSON(data []byte) error
- func (n *NullableDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (n *NullableDate) UnmarshalXMLAttr(attr xml.Attr) error
- func (n NullableDate) Valid() bool
- func (n NullableDate) ValidAndNormalized() bool
- func (n NullableDate) ValidAndNotNull() bool
- func (n NullableDate) Validate() error
- func (n NullableDate) Value() (driver.Value, error)
- func (n NullableDate) Weekday() time.Weekday
- func (n NullableDate) Year() int
- func (n NullableDate) YearMonthDay() (year int, month time.Month, day int)
- type NullableYearMonth
- func (ym NullableYearMonth) AddMonths(months int) NullableYearMonth
- func (ym NullableYearMonth) AddYears(years int) NullableYearMonth
- func (ym NullableYearMonth) Compare(other NullableYearMonth) int
- func (ym NullableYearMonth) ContainsDate(date Date) bool
- func (ym NullableYearMonth) ContainsTime(t time.Time) bool
- func (ym NullableYearMonth) Date(day int) NullableDate
- func (ym NullableYearMonth) DateRange() (fromDate, untilDate NullableDate)
- func (ym NullableYearMonth) Format(layout string) string
- func (ym NullableYearMonth) Get() YearMonth
- func (ym NullableYearMonth) GetOr(defaultYearMonth YearMonth) YearMonth
- func (ym NullableYearMonth) IsNotNull() bool
- func (ym NullableYearMonth) IsNull() bool
- func (ym NullableYearMonth) IsZero() bool
- func (NullableYearMonth) JSONSchema() *jsonschema.Schema
- func (ym NullableYearMonth) MarshalJSON() ([]byte, error)
- func (ym NullableYearMonth) MarshalText() ([]byte, error)
- func (ym *NullableYearMonth) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (ym NullableYearMonth) Month() time.Month
- func (ym NullableYearMonth) Normalized() (NullableYearMonth, error)
- func (ym *NullableYearMonth) Scan(value any) error
- func (ym *NullableYearMonth) ScanString(source string, validate bool) error
- func (ym *NullableYearMonth) Set(yearMonth YearMonth)
- func (ym *NullableYearMonth) SetNull()
- func (ym NullableYearMonth) String() string
- func (ym NullableYearMonth) StringOr(nullString string) string
- func (ym *NullableYearMonth) UnmarshalJSON(data []byte) error
- func (ym *NullableYearMonth) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (ym *NullableYearMonth) UnmarshalXMLAttr(attr xml.Attr) error
- func (ym NullableYearMonth) Valid() bool
- func (ym NullableYearMonth) ValidAndNormalized() bool
- func (ym NullableYearMonth) ValidAndNotNull() bool
- func (ym NullableYearMonth) Validate() error
- func (ym NullableYearMonth) Value() (driver.Value, error)
- func (ym NullableYearMonth) Year() int
- type NullableYearQuarter
- func (yq NullableYearQuarter) AddQuarters(quarters int) NullableYearQuarter
- func (yq NullableYearQuarter) AddYears(years int) NullableYearQuarter
- func (yq NullableYearQuarter) Compare(other NullableYearQuarter) int
- func (yq NullableYearQuarter) ContainsDate(date Date) bool
- func (yq NullableYearQuarter) ContainsTime(t time.Time) bool
- func (yq NullableYearQuarter) ContainsYearMonth(ym YearMonth) bool
- func (yq NullableYearQuarter) Date(monthInQuarter, day int) NullableDate
- func (yq NullableYearQuarter) DateRange() (fromDate, untilDate NullableDate)
- func (yq NullableYearQuarter) FirstMonth() time.Month
- func (yq NullableYearQuarter) Get() YearQuarter
- func (yq NullableYearQuarter) GetOr(defaultYearQuarter YearQuarter) YearQuarter
- func (yq NullableYearQuarter) IsNotNull() bool
- func (yq NullableYearQuarter) IsNull() bool
- func (yq NullableYearQuarter) IsZero() bool
- func (NullableYearQuarter) JSONSchema() *jsonschema.Schema
- func (yq NullableYearQuarter) MarshalJSON() ([]byte, error)
- func (yq NullableYearQuarter) MarshalText() ([]byte, error)
- func (yq *NullableYearQuarter) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (yq NullableYearQuarter) Normalized() (NullableYearQuarter, error)
- func (yq NullableYearQuarter) Quarter() int
- func (yq *NullableYearQuarter) Scan(value any) error
- func (yq *NullableYearQuarter) ScanString(source string, validate bool) error
- func (yq *NullableYearQuarter) Set(yearQuarter YearQuarter)
- func (yq *NullableYearQuarter) SetNull()
- func (yq NullableYearQuarter) String() string
- func (yq NullableYearQuarter) StringOr(nullString string) string
- func (yq *NullableYearQuarter) UnmarshalJSON(data []byte) error
- func (yq *NullableYearQuarter) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (yq *NullableYearQuarter) UnmarshalXMLAttr(attr xml.Attr) error
- func (yq NullableYearQuarter) Valid() bool
- func (yq NullableYearQuarter) ValidAndNormalized() bool
- func (yq NullableYearQuarter) ValidAndNotNull() bool
- func (yq NullableYearQuarter) Validate() error
- func (yq NullableYearQuarter) Value() (driver.Value, error)
- func (yq NullableYearQuarter) Year() int
- type Parser
- type YearMonth
- func (ym YearMonth) AddMonths(months int) YearMonth
- func (ym YearMonth) AddYears(years int) YearMonth
- func (ym YearMonth) Compare(other YearMonth) int
- func (ym YearMonth) ContainsDate(date Date) bool
- func (ym YearMonth) ContainsTime(t time.Time) bool
- func (ym YearMonth) Date(day int) Date
- func (ym YearMonth) DateRange() (fromDate, untilDate Date)
- func (ym YearMonth) Format(layout string) string
- func (ym YearMonth) IsZero() bool
- func (YearMonth) JSONSchema() *jsonschema.Schema
- func (ym YearMonth) MarshalJSON() ([]byte, error)
- func (ym YearMonth) MarshalText() ([]byte, error)
- func (ym *YearMonth) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (ym YearMonth) Month() time.Month
- func (ym YearMonth) Normalized() (YearMonth, error)
- func (ym YearMonth) Nullable() NullableYearMonth
- func (ym *YearMonth) Scan(value any) error
- func (ym *YearMonth) ScanString(source string, validate bool) error
- func (ym YearMonth) String() string
- func (ym *YearMonth) UnmarshalJSON(data []byte) error
- func (ym *YearMonth) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (ym *YearMonth) UnmarshalXMLAttr(attr xml.Attr) error
- func (ym YearMonth) Valid() bool
- func (ym YearMonth) ValidAndNormalized() bool
- func (ym YearMonth) Validate() error
- func (ym YearMonth) Value() (driver.Value, error)
- func (ym YearMonth) Year() int
- type YearQuarter
- func (yq YearQuarter) AddQuarters(quarters int) YearQuarter
- func (yq YearQuarter) AddYears(years int) YearQuarter
- func (yq YearQuarter) Compare(other YearQuarter) int
- func (yq YearQuarter) ContainsDate(date Date) bool
- func (yq YearQuarter) ContainsTime(t time.Time) bool
- func (yq YearQuarter) ContainsYearMonth(ym YearMonth) bool
- func (yq YearQuarter) Date(monthInQuarter, day int) Date
- func (yq YearQuarter) DateRange() (fromDate, untilDate Date)
- func (yq YearQuarter) FirstMonth() time.Month
- func (yq YearQuarter) IsZero() bool
- func (YearQuarter) JSONSchema() *jsonschema.Schema
- func (yq YearQuarter) MarshalJSON() ([]byte, error)
- func (yq YearQuarter) MarshalText() ([]byte, error)
- func (yq *YearQuarter) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (yq YearQuarter) Normalized() (YearQuarter, error)
- func (yq YearQuarter) Nullable() NullableYearQuarter
- func (yq YearQuarter) Quarter() int
- func (yq *YearQuarter) Scan(value any) error
- func (yq *YearQuarter) ScanString(source string, validate bool) error
- func (yq YearQuarter) String() string
- func (yq *YearQuarter) UnmarshalJSON(data []byte) error
- func (yq *YearQuarter) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (yq *YearQuarter) UnmarshalXMLAttr(attr xml.Attr) error
- func (yq YearQuarter) Valid() bool
- func (yq YearQuarter) ValidAndNormalized() bool
- func (yq YearQuarter) Validate() error
- func (yq YearQuarter) Value() (driver.Value, error)
- func (yq YearQuarter) Year() int
Constants ¶
const ( // Layout used for the Date type, compatible with time.Time.Format() Layout = time.DateOnly // Regular expression for the Layout Regex = `\d{4}-\d{2}-\d{2}` Length = 10 // len("2006-01-02") // MinLength is the minimum length of a valid date MinLength = 6 // Invalid holds an empty string Date. // See NullableDate for where an empty string is a valid value. Invalid Date = "" )
const YearMonthLayout = "2006-01"
YearMonthLayout is the layout string for formatting YearMonth values (YYYY-MM). Compatible with time.Time.Format().
Variables ¶
var ParseTimeDefaultLayouts = []string{ time.RFC3339Nano, time.RFC3339, "2006-01-02 15:04:05", }
ParseTimeDefaultLayouts contains the default time layouts used for parsing.
Functions ¶
Types ¶
type Date ¶
type Date string
Date represents a calendar date in ISO 8601 format (YYYY-MM-DD). See NullableDate for a nullable version of this type.
A Date is zero, see IsZero, if it is empty, whitespace only, or one of the zero dates "0000-00-00" and "0001-01-01" in any spelling, which have no practical use as a date. A zero Date is not valid.
Scan and Value fully normalize, because a database date is not free text: a valid date is stored as YYYY-MM-DD, a zero date (see IsZero) is "", Value writes SQL NULL for a zero date and returns an error for a date that can't be normalized, and a time.Time from the driver is its date. JSON, XML, MarshalText and ScanString without validate only trim surrounding whitespace, including a zero date, and do not parse another spelling into YYYY-MM-DD. ScanString with validate stores the normalized date or returns the error of Normalized. fmt prints the date as stored. Date has no UnmarshalText, so encoding/json reads and writes the keys of a map[Date]V as given. See "Reading and writing values" in the README of the module.
func FromUntilFromYearAndMonths ¶
FromUntilFromYearAndMonths returns the first and last date of a month range within a given year. The year argument must be a 4-digit year string (e.g. "2024"). The months argument is either a single month number ("3") or a dash-separated range ("3-9"); an empty months string defaults to the full year ("1-12"). Returns empty dates without an error when year is empty.
func Normalize ¶
Normalize returns str as a Date in the canonical YYYY-MM-DD form, or an error if the input cannot be parsed.
A numeric date with the year last is ambiguous when both of its first numbers are valid months, like "05/06/2024". The separators decide: dots are read day first like in Germany, "05.06.2024" is 2024-06-05, and so are other separators, like "05-06-2024", with any language hint. Slashes are written month first in the US and day first in Britain, so for slashes the first lang argument, if present, is a hint: language.EN reads month first, every other language day first, so "05/06/2024" with language.DE is 2024-06-05. Without a hint slashes are read month first like in the US, "05/06/2024" is 2024-05-06. An English ordinal suffix marks the day and wins over the separators and the hint: "1st 02 2003" is 2003-02-01 also with language.EN.
Accepted input shapes (case-insensitive, lenient whitespace):
- ISO 8601: "2006-01-02", "2006/01/02", "2006.01.02", "2006 01 02"
- ISO 8601 with time suffix, the date part is taken: "2006-01-02T15:04:05Z07:00", "30.11.2023 00:00:00"
- dd.mm.yyyy / dd.mm.yy: "25.12.1975", "25.12.75"
- mm/dd/yyyy / mm/dd/yy: "12/25/1975", "12/25/75"
- "1st of 02/2003", "4th of dec. 2020"
- Month-first English / German: "jan. 24 2012", "1. Dezember 2016"
- Day-first English / German: "7 oct 1970", "1st of dec. 2020"
- Month-first with comma: "Oct 7, 1970", "May 17, 2012", "September 17th, 2012"
- Short year with leading apostrophe: "Oct 7, '70"
- "yyyy-Mon-dd": "2013-Feb-03"
- "dd-Mon-yyyy" / "dd-Mon-yy": "07-Feb-2004", "07-Feb-04"
- With weekday prefix (dropped): "Mon Jan 02 2006", "Fri, 03 Jul 2015", "Wednesday, 28 Feb 2018"
- CJK separators: "2014年04月08日"
- French, Italian and Spanish month names and abbreviations: "2 janv. 2019", "23/gen/2019", "12 enero 2020", "2 févr. 2019"
2-digit years map to 2000..2044 (< 45) or 1900..1999 (>= 45).
Explicitly NOT supported (use a different parser, e.g. time.Parse):
- Year-only or year-month-only strings ("2014", "201412", "20140601", "2014-04", "2014.05")
- Single-digit day-month-year combinations under 5 total digits ("8/8/71") — too ambiguous to disambiguate reliably
- RubyDate / RFC1123 with embedded time and timezone offset ("Mon Jan 02 15:04:05 -0700 2006")
- Locale-specific written forms beyond the abbreviations above (e.g. Arabic numerals, full Russian months)
func Of ¶
Of returns a normalized Date for the given year, month, and day. The month, day values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.
func OfNowInUTC ¶
func OfNowInUTC() Date
OfNowInUTC returns the date of the current time in the UTC timezone.
func OfTime ¶
OfTime returns the date part of the passed time.Time or an empty string if t.IsZero(). To get the date in a certain time zone, pass the time.Time with a location set to the time zone.
func OfTomorrow ¶
func OfTomorrow() Date
OfTomorrow returns the date of tomorrow in the local timezone.
func OfYesterday ¶
func OfYesterday() Date
OfYesterday returns the date of yesterday in the local timezone.
func PeriodRange ¶
PeriodRange returns the dates [from, until] for a period defined in one of the following formats:
- period of a ISO 8601 week of a year: YYYY-Wnn
- period of a month of a year: YYYY-MM
- period of a quarter of a year: YYYY-Qn
- period of a half year: YYYY-Hn
- period of full year: YYYY
The returned from Date is the first day of the period, the returned until Date is the last day of the period. For a week these are the Monday and the Sunday of the ISO 8601 week, which can fall into the previous or next year.
Examples:
- Period of June 2018: PeriodRange("2018-06") == Date("2018-06-01"), Date("2018-06-30"), nil
- Period of Q3 2018: PeriodRange("2018-Q3") == Date("2018-07-01"), Date("2018-09-30"), nil
- Period of second half of 2018: PeriodRange("2018-H2") == Date("2018-07-01"), Date("2018-12-31"), nil
- Period of year 2018: PeriodRange("2018") == Date("2018-01-01"), Date("2018-12-31"), nil
- Period of week 1 2019: PeriodRange("2019-W01") == Date("2018-12-31"), Date("2019-01-06"), nil
func YearRange ¶
YearRange returns the date range from first of January to 31st of December of a year.
func YearWeekMonday ¶
YearWeekMonday returns the date of Monday of an ISO 8601 week.
func YearWeekRange ¶
YearWeekRange returns the dates of Monday and Sunday of an ISO 8601 week.
func (Date) AddDate ¶
AddDate returns a new date with the specified years, months, and days added. The month and day values may be outside their usual ranges and will be normalized.
func (Date) After ¶
After returns true if the date is after the other date. An invalid date (including "") has no position in time, so After returns false if either date is not valid, like NormalizedEqual returns false for an invalid date.
func (Date) AfterTime ¶
AfterTime returns true if midnight of the date (in the location of the passed time) is after the time. Returns false if the date is not valid, see After.
func (Date) AfterToday ¶
AfterToday returns true if the date is after today in local time.
func (Date) AfterTodayInUTC ¶
AfterTodayInUTC returns true if the date is after today in UTC time.
func (Date) Before ¶
Before returns true if the date is before the other date. Returns false if either date is not valid, see After.
func (Date) BeforeTime ¶
BeforeTime returns true if midnight of the date (in the location of the passed time) is before the time. Returns false if the date is not valid, see After.
func (Date) BeforeToday ¶
BeforeToday returns true if the date is before today in local time.
func (Date) BeforeTodayInUTC ¶
BeforeTodayInUTC returns true if the date is before today in UTC time.
func (Date) BeginningOfMonth ¶
BeginningOfMonth returns the date of the first day of the month containing this date.
func (Date) BeginningOfQuarter ¶
BeginningOfQuarter returns the date of the first day of the quarter containing this date.
func (Date) BeginningOfWeek ¶
BeginningOfWeek returns the date of the first day of the week containing this date. The startDay parameter specifies which day is considered the start of the week. For example, time.Monday for ISO 8601 weeks, or time.Sunday for US-style weeks.
func (Date) BeginningOfYear ¶
BeginningOfYear returns the date of the first day of the year containing this date.
func (Date) BetweenExcl ¶
BetweenExcl returns true if the date is strictly between (exclusive) after and before. Returns false if any of the dates is not valid, see After.
func (Date) Compare ¶
Compare compares the date with another Date. Returns -1 if date is before other, +1 if after, 0 if equal. An invalid date compares like the empty string: equal to any other invalid date and before every valid date, so Compare is a consistent sort order for slices.SortFunc also for invalid dates.
func (Date) Day ¶
Day returns the day of the month component of the date. Returns 0 if the date is not valid.
func (Date) EndOfMonth ¶
EndOfMonth returns the date of the last day of the month containing this date.
func (Date) EndOfQuarter ¶
EndOfQuarter returns the date of the last day of the quarter containing this date.
func (Date) EndOfWeek ¶
EndOfWeek returns the date of the last day of the week containing this date. The weekday parameter specifies which day is considered the start of the week. The end of the week is calculated as 6 days after the beginning. For example, if weekday is time.Monday, the end will be Sunday.
func (Date) EndOfYear ¶
EndOfYear returns the date of the last day of the year containing this date.
func (Date) EqualOrAfter ¶
EqualOrAfter returns true if the date is equal to or after the other date.
func (Date) EqualOrBefore ¶
EqualOrBefore returns true if the date is equal to or before the other date.
func (Date) Format ¶
Format formats the date using the given layout string (see time.Time.Format). Returns an empty string if the date is zero (see IsZero) or layout is empty. If layout equals Layout constant, returns the normalized date without a time conversion. A date that is not valid is returned unchanged for every layout instead of formatting the zero time.Time like "01.01.0001".
func (Date) ISOWeek ¶
ISOWeek returns the ISO 8601 year and week number in which the date 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. Returns zeros if the date is not valid.
func (Date) IsToday ¶
IsToday returns true if the date is today in local time. A valid date that is not in the normalized form is normalized first.
func (Date) IsTodayInUTC ¶
IsTodayInUTC returns true if the date is today in UTC time. A valid date that is not in the normalized form is normalized first.
func (Date) IsZero ¶
IsZero returns true if the date is empty or whitespace only, or one of the zero dates "0000-00-00" and "0001-01-01" in any spelling, like "01.01.0001", "0001-01-01T00:00:00Z", "00.00.0000" or "0000-00-00 00:00:00". "0001-01-01" is the zero value of time.Time, "0000-00-00" may be the zero value of other date implementations, like MySQL. A zero date has no practical use as a date and is not a valid Date. SQL Scan, JSON and XML store it as "" and Value writes SQL NULL. A time.Time on that calendar day is stored as "" too.
func (Date) JSONSchema ¶
func (Date) JSONSchema() *jsonschema.Schema
JSONSchema returns the JSON schema definition for the Date type. Implements the jsonschema.JSONSchemaProvider interface.
func (Date) LastMonday ¶
LastMonday returns the date of the Monday on or before this date.
func (Date) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler by writing the date with surrounding whitespace trimmed. A zero date is written as "", like Value writes SQL NULL. A blank date is "".
func (Date) MarshalText ¶
MarshalText implements encoding.TextMarshaler like MarshalJSON, so that encoding/xml and strfmt write the trimmed date. encoding/json writes map keys of string types as they are and doesn't call MarshalText for them.
func (*Date) MarshalXMLAttr ¶
MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.
func (Date) Midnight ¶
Midnight returns the midnight (00:00) time.Time of the date in the local time zone. Returns a zero time.Time if the date is not valid.
func (Date) MidnightInLocation ¶
MidnightInLocation returns the midnight (00:00) time.Time of the date in the given location. Returns a zero time.Time if the date is zero or not valid. A valid date that is not in the normalized form is normalized first.
func (Date) MidnightUTC ¶
MidnightUTC returns the midnight (00:00) time.Time of the date in UTC, or a zero time.Time value if the date is zero or not valid. A valid date that is not in the normalized form is normalized first.
func (Date) Month ¶
Month returns the month component of the date. Returns 0 if the date is not valid.
func (Date) NextSunday ¶
NextSunday returns the date of the Sunday on or after this date.
func (Date) Normalized ¶
Normalized returns the date in normalized form, or an error if the format can't be detected or the date is zero, see IsZero. Returns the Date unchanged in case of an error. Normalized implements types.Normalizable[Date], use NormalizedLang to pass a language hint.
func (Date) NormalizedEqual ¶
NormalizedEqual returns true if two dates are equal after normalization. An invalid date (including "") has no normalized value and is not equal to any date, not even to an identical invalid string.
func (Date) NormalizedLang ¶
NormalizedLang returns the date in normalized form like Normalized, with lang as hint for the order of day and month in an ambiguous numeric date with slashes: language.EN reads "05/06/2024" month first and every other language day first. Other separators, like in "05.06.2024", are read day first with any hint. An empty lang or one that can't be normalized gives no hint, then slashes are read month first, see Normalize. Returns the Date unchanged in case of an error.
func (Date) NormalizedOrNull ¶
func (date Date) NormalizedOrNull(lang ...language.Code) NullableDate
NormalizedOrNull returns the normalized date as NullableDate if possible, otherwise returns Null. The first given lang argument is used as language hint for parsing.
func (Date) NormalizedOrUnchanged ¶
NormalizedOrUnchanged returns the normalized date if possible, otherwise returns the date unchanged. The first given lang argument is used as language hint for parsing.
func (Date) Nullable ¶
func (date Date) Nullable() NullableDate
Nullable returns the date as a NullableDate. Returns Null if the date is zero, otherwise returns the date as NullableDate.
func (*Date) Scan ¶
Scan implements the database/sql.Scanner interface. A valid date from a string or []byte is stored normalized, SQL NULL and a zero date (see IsZero) as "", and a date that can't be normalized unchanged without an error, so that one invalid value doesn't fail a whole query; Validate reports it. A time.Time is stored as OfTime, which is already YYYY-MM-DD or "" for the zero time, without parsing it again.
func (*Date) ScanString ¶
ScanString implements the strfmt.Scannable interface. Without validate it trims surrounding whitespace, including a zero date. With validate it stores the normalized date or returns the error of Normalized, also for a blank or zero source.
func (*Date) ScanStringWithLang ¶
func (date *Date) ScanStringWithLang(source string, lang language.Code) (wasNormalized bool, monthMustBeFirst bool, err error)
ScanStringWithLang parses and assigns the source string as a Date using the given language hint. Returns wasNormalized to indicate if the source was already in normalized form, monthMustBeFirst to indicate if the date format requires month-first ordering, and an error if parsing fails, also for a blank source. A zero date in any spelling, see IsZero, is assigned as "" like by Scan. monthMustBeFirst is only true if month first is the only valid reading, like for "12/25/2023", not for an ambiguous date like "05/06/2024" that is read month first because of its slashes or the hint.
func (Date) Time ¶
Time returns a time.Time for the date at the specified hour, minute, and second in the given location. Returns a zero time.Time if the date is zero. An invalid date is not detected and results in a meaningless time before year 1 (from time.Date(0, 0, 0, ...)), use Valid to check the date first.
func (Date) TimeLocal ¶
TimeLocal returns a time.Time for the date at the specified hour, minute, and second in local time. Returns a zero time.Time if the date is zero, see Time for invalid dates.
func (Date) TimeUTC ¶
TimeUTC returns a time.Time for the date at the specified hour, minute, and second in UTC. Returns a zero time.Time if the date is zero, see Time for invalid dates.
func (*Date) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler. A JSON string is stored with surrounding whitespace trimmed. A zero date is stored as "", like Scan. JSON null leaves the Date unchanged like encoding/json does for a string type, and any other JSON value is an error.
func (*Date) UnmarshalXML ¶
UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like UnmarshalJSON stores it.
func (*Date) UnmarshalXMLAttr ¶
UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like UnmarshalJSON stores it.
func (Date) ValidAndNormalized ¶
ValidAndNormalized returns true if the date is both valid and already in normalized form (YYYY-MM-DD).
func (Date) Validate ¶
Validate returns an error if the date is not in a valid, normalizable format.
func (Date) Value ¶
Value implements the database/sql/driver.Valuer interface. It returns the normalized date, SQL NULL for a zero date (see IsZero), or the error of Normalized for a date that can't be normalized.
func (Date) Weekday ¶
Weekday returns the day of the week for this date. Returns 0 (Sunday) if the date is not valid.
func (Date) WithinIncl ¶
WithinIncl returns true if the date is within the inclusive range [from, until]. Returns false if any of the dates is not valid, see After.
type Finder ¶
Finder scans arbitrary text for substrings that can be parsed as dates. Use NewFinder to create one, optionally with a language hint to resolve ambiguous day/month orderings.
func NewFinder ¶
NewFinder returns a Finder that uses the first element of lang as a language hint when parsing date candidates. The hint is used to disambiguate ambiguous dd/mm vs mm/dd orderings with slashes like Normalize: language.EN reads "05/06/2024" month first, every other language day first, and without a hint it is read month first. "05.06.2024" with dots or any other separator is read day first with every hint.
func (*Finder) FindAllIndex ¶
FindAllIndex returns the byte-index pairs [start, end) of every date found in str, trying spans of up to three whitespace-delimited words at a time. A span is a date if Normalize accepts it, so an ISO 8601 date-time like "2023-12-25T10:00:00Z" is found as a whole. Only the Italian and Spanish month abbreviations that are common words in other languages, like "set" and "ago", need language.IT or language.ES as LangHint, so that text like "Game set 10 2020" has no date. At most n results are returned; pass n < 0 to return all matches. The returned slice is nil when no dates are found.
type Format ¶
type Format struct {
Layout string `json:"layout"` // The time layout string for formatting
NilString string `json:"nilString"` // String to use for nil values
ZeroString string `json:"zeroString"` // String to use for zero values
}
Format provides date formatting and parsing capabilities with customizable layouts.
func (*Format) AssignString ¶
AssignString assigns a string value to various date-related types using reflection.
func (*Format) FormatString ¶
FormatString formats a value as a date string using the configured layout.
type Formatter ¶
type Formatter string
Formatter is a date layout string (compatible with time.Time.Format) that can format Date values and parse date strings. It implements the strfmt.Parser interface (but not strfmt.Formatter) so it can be used with generic parsing pipelines.
type NullableDate ¶
type NullableDate string
NullableDate is identical to Date, except that a zero date (see Date.IsZero) is null and considered valid by the Valid() and Validate() methods. The main difference between Date and NullableDate is: Date("").Valid() == false NullableDate("").Valid() == true
NullableDate reads and writes like Date, except that null is SQL NULL and JSON null. See "Reading and writing values" in the README of the module.
const Null NullableDate = ""
Null is an empty string and will be treated as SQL NULL. date.Null.IsZero() == true
func MustNullable ¶
func MustNullable(str string) NullableDate
MustNullable returns str as normalized NullableDate or panics if str is not neither a valid Date nor Null ("").
func NormalizeNullable ¶
func NormalizeNullable(str string, lang ...language.Code) (NullableDate, error)
NormalizeNullable returns str as normalized NullableDate or an error. The first given lang argument is used as language hint for parsing.
func OfTimePtr ¶
func OfTimePtr(t *time.Time) NullableDate
OfTimePtr returns the date part of the passed time.Time or Null (an empty string) if t is nil or t.IsZero().
func (NullableDate) Add ¶
func (n NullableDate) Add(d time.Duration) NullableDate
Add returns a new date with the specified duration added. Returns Null if the NullableDate is null.
func (NullableDate) AddDate ¶
func (n NullableDate) AddDate(years int, months int, days int) NullableDate
AddDate returns a new date with the specified years, months, and days added. Returns Null if the NullableDate is null.
func (NullableDate) AddDays ¶
func (n NullableDate) AddDays(days int) NullableDate
AddDays returns a new date with the specified number of days added. Returns Null if the NullableDate is null.
func (NullableDate) AddMonths ¶
func (n NullableDate) AddMonths(months int) NullableDate
AddMonths returns a new date with the specified number of months added. Returns Null if the NullableDate is null.
func (NullableDate) AddYears ¶
func (n NullableDate) AddYears(years int) NullableDate
AddYears returns a new date with the specified number of years added. Returns Null if the NullableDate is null.
func (NullableDate) After ¶
func (n NullableDate) After(other NullableDate) bool
After returns true if the date is after the other date. A null date is always before any non-null date, so any non-null date is after a null date and a null date is never after any date. Returns false if either date is not null and not valid, see Date.After.
func (NullableDate) AfterTime ¶
func (n NullableDate) AfterTime(other time.Time) bool
AfterTime returns true if midnight of the date (in the location of the passed time) is after the time. Returns false if the date is null or not valid, see Date.After.
func (NullableDate) AfterToday ¶
func (n NullableDate) AfterToday() bool
AfterToday returns true if the date is after today in local time. Returns false if the date is null.
func (NullableDate) AfterTodayInUTC ¶
func (n NullableDate) AfterTodayInUTC() bool
AfterTodayInUTC returns true if the date is after today in UTC time. Returns false if the date is null.
func (NullableDate) Before ¶
func (n NullableDate) Before(other NullableDate) bool
Before returns true if the date is before the other date. A null date is always before any non-null date. Returns false if either date is not null and not valid, see Date.After.
func (NullableDate) BeforeTime ¶
func (n NullableDate) BeforeTime(other time.Time) bool
BeforeTime returns true if midnight of the date (in the location of the passed time) is before the time. Returns true if the date is null and the time is not zero, and false if the date is not valid, see Date.After.
func (NullableDate) BeforeToday ¶
func (n NullableDate) BeforeToday() bool
BeforeToday returns true if the date is before today in local time. A null date is always before today.
func (NullableDate) BeforeTodayInUTC ¶
func (n NullableDate) BeforeTodayInUTC() bool
BeforeTodayInUTC returns true if the date is before today in UTC time. A null date is always before today.
func (NullableDate) BeginningOfMonth ¶
func (n NullableDate) BeginningOfMonth() NullableDate
BeginningOfMonth returns the date of the first day of the month containing this date. Returns Null if the NullableDate is null.
func (NullableDate) BeginningOfQuarter ¶
func (n NullableDate) BeginningOfQuarter() NullableDate
BeginningOfQuarter returns the date of the first day of the quarter containing this date. Returns Null if the NullableDate is null.
func (NullableDate) BeginningOfWeek ¶
func (n NullableDate) BeginningOfWeek(startDay time.Weekday) NullableDate
BeginningOfWeek returns the date of the first day of the week containing this date. The startDay parameter specifies which day is considered the start of the week. Returns Null if the NullableDate is null.
func (NullableDate) BeginningOfYear ¶
func (n NullableDate) BeginningOfYear() NullableDate
BeginningOfYear returns the date of the first day of the year containing this date. Returns Null if the NullableDate is null.
func (NullableDate) Compare ¶
func (n NullableDate) Compare(other NullableDate) int
Compare compares the date with another NullableDate. Returns -1 if n is before other, +1 if after, 0 if equal. A null date is always before a non-null date. An invalid date compares like Null, see Date.Compare.
func (NullableDate) Date ¶
func (n NullableDate) Date() Date
Date returns the NullableDate as Date without checking if it's null. See also Get which panics on null.
func (NullableDate) DateOr ¶
func (n NullableDate) DateOr(useIfNull Date) Date
DateOr returns the NullableDate as Date if it is not null, else useIfNull will be returned.
func (NullableDate) Day ¶
func (n NullableDate) Day() int
Day returns the day of the month component of the date. Returns 0 if the date is null.
func (NullableDate) EndOfMonth ¶
func (n NullableDate) EndOfMonth() NullableDate
EndOfMonth returns the date of the last day of the month containing this date. Returns Null if the NullableDate is null.
func (NullableDate) EndOfQuarter ¶
func (n NullableDate) EndOfQuarter() NullableDate
EndOfQuarter returns the date of the last day of the quarter containing this date. Returns Null if the NullableDate is null.
func (NullableDate) EndOfWeek ¶
func (n NullableDate) EndOfWeek(weekday time.Weekday) NullableDate
EndOfWeek returns the date of the last day of the week containing this date. The weekday parameter specifies which day is considered the start of the week. Returns Null if the NullableDate is null.
func (NullableDate) EndOfYear ¶
func (n NullableDate) EndOfYear() NullableDate
EndOfYear returns the date of the last day of the year containing this date. Returns Null if the NullableDate is null.
func (NullableDate) EqualOrAfter ¶
func (n NullableDate) EqualOrAfter(other NullableDate) bool
EqualOrAfter returns true if the date is equal to or after the other date.
func (NullableDate) EqualOrBefore ¶
func (n NullableDate) EqualOrBefore(other NullableDate) bool
EqualOrBefore returns true if the date is equal to or before the other date.
func (NullableDate) Format ¶
func (n NullableDate) Format(layout string) string
Format returns the date formatted like Date.Format, or an empty string if n is null (see IsNull) or layout is an empty string. A date that is not valid is returned unchanged.
func (NullableDate) Get ¶
func (n NullableDate) Get() Date
Get returns the non-nullable Date value or panics if the NullableDate is null. Note: check with IsNull before using Get!
func (NullableDate) GetOr ¶
func (n NullableDate) GetOr(defaultDate Date) Date
GetOr returns the non-nullable Date value or the passed defaultDate if the NullableDate is null.
func (NullableDate) ISOWeek ¶
func (n NullableDate) ISOWeek() (year, week int)
ISOWeek returns the ISO 8601 year and week number in which the date 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. Returns zeros if the date is not valid or null.
func (NullableDate) IsNotNull ¶
func (n NullableDate) IsNotNull() bool
IsNotNull returns true if the NullableDate is not null.
func (NullableDate) IsNull ¶
func (n NullableDate) IsNull() bool
IsNull returns true if the NullableDate is null or zero, see Date.IsZero, which includes a string of only the runes strutil.TrimSpace removes like for country.NullableCode, and a zero date in another format like "01.01.0001". IsNull implements the nullable.Nullable interface.
func (NullableDate) IsToday ¶
func (n NullableDate) IsToday() bool
IsToday returns true if the date is today in local time. Returns false if the date is null.
func (NullableDate) IsTodayInUTC ¶
func (n NullableDate) IsTodayInUTC() bool
IsTodayInUTC returns true if the date is today in UTC time. Returns false if the date is null.
func (NullableDate) IsZero ¶
func (n NullableDate) IsZero() bool
IsZero returns true if the date is zero like Date.IsZero, which is the same as IsNull.
func (NullableDate) JSONSchema ¶
func (NullableDate) JSONSchema() *jsonschema.Schema
JSONSchema returns the JSON schema definition for the NullableDate type. Implements the jsonschema.JSONSchemaProvider interface.
func (NullableDate) LastMonday ¶
func (n NullableDate) LastMonday() NullableDate
LastMonday returns the date of the Monday on or before this date. Returns Null if the NullableDate is null.
func (NullableDate) MarshalJSON ¶
func (n NullableDate) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler by writing JSON null if the NullableDate is blank, else the date with surrounding whitespace trimmed. A zero date is written as JSON null, like Value writes SQL NULL.
func (NullableDate) MarshalText ¶
func (n NullableDate) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler like MarshalJSON, but with "" for null, so that encoding/xml and strfmt write the trimmed date. encoding/json writes map keys of string types as they are and doesn't call MarshalText for them.
func (*NullableDate) MarshalXMLAttr ¶
MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.
func (NullableDate) Midnight ¶
func (n NullableDate) Midnight() nullable.Time
Midnight returns the midnight (00:00) nullable.Time of the date in the local time zone, or a null nullable.Time value if the date is not valid.
func (NullableDate) MidnightInLocation ¶
func (n NullableDate) MidnightInLocation(loc *time.Location) nullable.Time
MidnightInLocation returns the midnight (00:00) nullable.Time of the date in the given location. Returns a null nullable.Time if the date is not valid or null. A valid date that is not in the normalized form is normalized first.
func (NullableDate) MidnightUTC ¶
func (n NullableDate) MidnightUTC() nullable.Time
MidnightUTC returns the midnight (00:00) nullable.Time of the date in UTC, or a null nullable.Time value if the date is null or not valid. A valid date that is not in the normalized form is normalized first.
func (NullableDate) Month ¶
func (n NullableDate) Month() time.Month
Month returns the month component of the date. Returns 0 if the date is null.
func (NullableDate) NextSunday ¶
func (n NullableDate) NextSunday() NullableDate
NextSunday returns the date of the Sunday on or after this date. Returns Null if the NullableDate is null.
func (NullableDate) Normalized ¶
func (n NullableDate) Normalized() (NullableDate, error)
Normalized returns the date in normalized form, or an error if the format can't be detected. If n.IsNull() then Null, nil will be returned. Returns the NullableDate unchanged in case of an error. Normalized implements types.Normalizable[NullableDate], use NormalizedLang to pass a language hint.
func (NullableDate) NormalizedEqual ¶
func (n NullableDate) NormalizedEqual(other NullableDate) bool
NormalizedEqual returns true if two dates are equal after normalization. Null and the zero dates are equal to each other. An invalid date has no normalized value and is not equal to any date, not even to Null or an identical invalid string.
func (NullableDate) NormalizedLang ¶
func (n NullableDate) NormalizedLang(lang language.Code) (NullableDate, error)
NormalizedLang returns the date in normalized form like Normalized, with lang as hint for the order of day and month like Date.NormalizedLang.
func (NullableDate) NormalizedOrNull ¶
func (n NullableDate) NormalizedOrNull(lang ...language.Code) NullableDate
NormalizedOrNull returns the normalized date as NullableDate if possible, otherwise returns Null. The first given lang argument is used as language hint for parsing.
func (NullableDate) NormalizedOrUnchanged ¶
func (n NullableDate) NormalizedOrUnchanged(lang ...language.Code) NullableDate
NormalizedOrUnchanged returns the normalized date if possible, otherwise returns the date unchanged. The first given lang argument is used as language hint for parsing.
func (*NullableDate) Scan ¶
func (n *NullableDate) Scan(value any) error
Scan implements the database/sql.Scanner interface. A valid date from a string or []byte is stored normalized, SQL NULL and a zero date (see IsNull) as Null, and a date that can't be normalized unchanged without an error, so that one invalid value doesn't fail a whole query; Validate reports it. A time.Time is stored as OfTime, which is already YYYY-MM-DD or Null for the zero time, without parsing it again.
func (*NullableDate) ScanString ¶
func (n *NullableDate) ScanString(source string, validate bool) error
ScanString implements the strfmt.Scannable interface. The strings "NULL", "null" and "nil" set the NullableDate to null. Without validate it stores source with surrounding whitespace trimmed, including a zero date. With validate it stores the normalized date or returns the error of Normalized.
func (*NullableDate) ScanStringWithLang ¶
func (n *NullableDate) ScanStringWithLang(source string, lang language.Code) (wasNormalized bool, monthMustBeFirst bool, err error)
ScanStringWithLang parses and assigns the source string as a NullableDate using the given language hint. Returns wasNormalized to indicate if the source was already in normalized form, monthMustBeFirst to indicate if the date format requires month-first ordering like Date.ScanStringWithLang, and an error if parsing fails. Null values (empty, whitespace only or zero) are treated as Null.
func (*NullableDate) Set ¶
func (n *NullableDate) Set(d Date)
Set sets a Date for this NullableDate.
func (*NullableDate) SetNull ¶
func (n *NullableDate) SetNull()
SetNull sets the NullableDate to null.
func (NullableDate) StringOr ¶
func (n NullableDate) StringOr(nullString string) string
StringOr returns the NullableDate as stored, or the passed nullString if the NullableDate is null.
func (NullableDate) Sub ¶
func (n NullableDate) Sub(other NullableDate) time.Duration
Sub returns the duration between the date and the other date (n - other). Returns 0 if either date is null or both dates are equal.
func (*NullableDate) UnmarshalJSON ¶
func (n *NullableDate) UnmarshalJSON(data []byte) error
UnmarshalJSON implements encoding/json.Unmarshaler. A JSON string is stored with surrounding whitespace trimmed, including a zero date, unlike Scan. JSON null sets the NullableDate to null, so that decoding null into a field that holds a value resets it, and any other JSON value is an error.
func (*NullableDate) UnmarshalXML ¶
func (n *NullableDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like UnmarshalJSON stores it.
func (*NullableDate) UnmarshalXMLAttr ¶
func (n *NullableDate) UnmarshalXMLAttr(attr xml.Attr) error
UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like UnmarshalJSON stores it.
func (NullableDate) Valid ¶
func (n NullableDate) Valid() bool
Valid returns if the format of the date is correct, see Format. A null date is valid, see IsNull.
func (NullableDate) ValidAndNormalized ¶
func (n NullableDate) ValidAndNormalized() bool
ValidAndNormalized returns if the date is valid and already normalized.
func (NullableDate) ValidAndNotNull ¶
func (n NullableDate) ValidAndNotNull() bool
ValidAndNotNull returns if the date is valid and not Null or Zero.
func (NullableDate) Validate ¶
func (n NullableDate) Validate() error
Validate returns an error if the date is not in a valid, normalizable format. A null date is valid, see IsNull.
func (NullableDate) Value ¶
func (n NullableDate) Value() (driver.Value, error)
Value implements the database/sql/driver.Valuer interface. It returns the normalized date, SQL NULL if the NullableDate is null, or the error of Normalized for a date that can't be normalized.
func (NullableDate) Weekday ¶
func (n NullableDate) Weekday() time.Weekday
Weekday returns the day of the week for this date. Returns 0 (Sunday) if the date is null.
func (NullableDate) Year ¶
func (n NullableDate) Year() int
Year returns the year component of the date. Returns 0 if the date is null.
func (NullableDate) YearMonthDay ¶
func (n NullableDate) YearMonthDay() (year int, month time.Month, day int)
YearMonthDay returns the year, month, and day components of the date. Returns zero values if the date is not valid or null.
type NullableYearMonth ¶
type NullableYearMonth string
NullableYearMonth is a year-month string in YYYY-MM format that can also represent a NULL value as an empty string.
const YearMonthNull NullableYearMonth = ""
YearMonthNull is the null value for NullableYearMonth (empty string). Value writes it as SQL NULL and MarshalJSON as JSON null.
func (NullableYearMonth) AddMonths ¶
func (ym NullableYearMonth) AddMonths(months int) NullableYearMonth
AddMonths returns a new NullableYearMonth with the specified number of months added. Returns null if the NullableYearMonth is null and an invalid NullableYearMonth unchanged. Handles month overflow by adjusting the year accordingly.
func (NullableYearMonth) AddYears ¶
func (ym NullableYearMonth) AddYears(years int) NullableYearMonth
AddYears returns a new NullableYearMonth with the specified number of years added. Returns null if the NullableYearMonth is null and an invalid NullableYearMonth unchanged.
func (NullableYearMonth) Compare ¶
func (ym NullableYearMonth) Compare(other NullableYearMonth) int
Compare compares the normalized year-month with another normalized NullableYearMonth, an invalid one as it is. Null is before every year-month. Returns -1 if ym is before other, +1 if after, 0 if equal.
func (NullableYearMonth) ContainsDate ¶
func (ym NullableYearMonth) ContainsDate(date Date) bool
ContainsDate returns true if the given date falls within this year-month. Returns false if the NullableYearMonth is null.
func (NullableYearMonth) ContainsTime ¶
func (ym NullableYearMonth) ContainsTime(t time.Time) bool
ContainsTime returns true if the given time falls within this year-month. Returns false if the NullableYearMonth is null.
func (NullableYearMonth) Date ¶
func (ym NullableYearMonth) Date(day int) NullableDate
Date returns a NullableDate for the given day within this year-month. Returns Null if the NullableYearMonth is null. The day value is formatted as-is without validation.
func (NullableYearMonth) DateRange ¶
func (ym NullableYearMonth) DateRange() (fromDate, untilDate NullableDate)
DateRange returns the first and last NullableDate of the month. Returns Null dates if the NullableYearMonth is null or not valid. Returns the date range [fromDate, untilDate] representing the entire month.
func (NullableYearMonth) Format ¶
func (ym NullableYearMonth) Format(layout string) string
Format formats the year-month using the given layout string (see time.Time.Format). Returns an empty string if ym or layout are empty, or if ym is null. If layout equals YearMonthLayout constant, returns the normalized year-month, or an invalid one unchanged. For other layouts, converts to Date using the 1st day of the month and delegates to Date.Format.
func (NullableYearMonth) Get ¶
func (ym NullableYearMonth) Get() YearMonth
Get returns the non-nullable YearMonth value or panics if the NullableYearMonth is null. Note: check with IsNull before using Get!
func (NullableYearMonth) GetOr ¶
func (ym NullableYearMonth) GetOr(defaultYearMonth YearMonth) YearMonth
GetOr returns the non-nullable YearMonth value or the passed defaultYearMonth if the NullableYearMonth is null.
func (NullableYearMonth) IsNotNull ¶
func (ym NullableYearMonth) IsNotNull() bool
IsNotNull returns true if the NullableYearMonth is not null, see IsNull.
func (NullableYearMonth) IsNull ¶
func (ym NullableYearMonth) IsNull() bool
IsNull returns true if the NullableYearMonth is empty or whitespace only. A zero period such as "0001-01" is kept and is not null, so Get returns it and callers that skip null values do not drop a stored period. IsNull implements the nullable.Nullable interface.
func (NullableYearMonth) IsZero ¶
func (ym NullableYearMonth) IsZero() bool
IsZero returns true if the year-month is zero like YearMonth.IsZero. A zero period such as "0001-01" is not null: reading and writing keep that text, and IsNull is false for it.
func (NullableYearMonth) JSONSchema ¶
func (NullableYearMonth) JSONSchema() *jsonschema.Schema
JSONSchema returns the JSON schema definition for the NullableYearMonth type. Implements the jsonschema.JSONSchemaProvider interface.
func (NullableYearMonth) MarshalJSON ¶
func (ym NullableYearMonth) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler by writing JSON null if the NullableYearMonth is blank, else the trimmed year-month. A zero year-month is written as that text.
func (NullableYearMonth) MarshalText ¶
func (ym NullableYearMonth) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler like MarshalJSON, but with "" for null, so that encoding/xml and strfmt write the trimmed year-month, including a zero period. encoding/json writes map keys of string types as they are and doesn't call MarshalText for them.
func (*NullableYearMonth) MarshalXMLAttr ¶
MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.
func (NullableYearMonth) Month ¶
func (ym NullableYearMonth) Month() time.Month
Month returns the month component of the year-month. Returns 0 if the year-month is null or not valid.
func (NullableYearMonth) Normalized ¶
func (ym NullableYearMonth) Normalized() (NullableYearMonth, error)
Normalized returns YearMonthNull for a null year-month, see IsNull, else the year-month normalized like YearMonth.Normalized, or unchanged together with an error if it can't be normalized.
func (*NullableYearMonth) Scan ¶
func (ym *NullableYearMonth) Scan(value any) error
Scan implements the database/sql.Scanner interface. Surrounding whitespace is trimmed. A time.Time is stored as its year-month, and the zero time.Time as YearMonthNull. SQL NULL and a blank string are YearMonthNull. A zero year-month such as "0001-01" is kept. An invalid year-month is kept after the trim, without an error.
func (*NullableYearMonth) ScanString ¶
func (ym *NullableYearMonth) ScanString(source string, validate bool) error
ScanString implements the strfmt.Scannable interface. The strings "NULL", "null" and "nil" set the NullableYearMonth to null. Without validate it stores source like Scan, with validate it stores the normalized year-month or returns the error of Normalized.
func (*NullableYearMonth) Set ¶
func (ym *NullableYearMonth) Set(yearMonth YearMonth)
Set sets a YearMonth for this NullableYearMonth.
func (*NullableYearMonth) SetNull ¶
func (ym *NullableYearMonth) SetNull()
SetNull sets the NullableYearMonth to null.
func (NullableYearMonth) String ¶
func (ym NullableYearMonth) String() string
String returns the year-month as a string in YYYY-MM format. String implements the fmt.Stringer interface.
func (NullableYearMonth) StringOr ¶
func (ym NullableYearMonth) StringOr(nullString string) string
StringOr returns the NullableYearMonth as string or the passed nullString if the NullableYearMonth is null.
func (*NullableYearMonth) UnmarshalJSON ¶
func (ym *NullableYearMonth) UnmarshalJSON(data []byte) error
UnmarshalJSON implements encoding/json.Unmarshaler. A JSON string is stored like Scan stores it, JSON null sets the NullableYearMonth to null, so that decoding null into a field that holds a value resets it, and any other JSON value is an error.
func (*NullableYearMonth) UnmarshalXML ¶
func (ym *NullableYearMonth) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like Scan stores it.
func (*NullableYearMonth) UnmarshalXMLAttr ¶
func (ym *NullableYearMonth) UnmarshalXMLAttr(attr xml.Attr) error
UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like Scan stores it.
func (NullableYearMonth) Valid ¶
func (ym NullableYearMonth) Valid() bool
Valid returns true if the year-month is null or can be normalized, see YearMonth.Validate.
func (NullableYearMonth) ValidAndNormalized ¶
func (ym NullableYearMonth) ValidAndNormalized() bool
ValidAndNormalized returns true if the year-month is valid and already normalized, or if it is the empty YearMonthNull. A whitespace-only or zero value is null and valid, but not normalized.
func (NullableYearMonth) ValidAndNotNull ¶
func (ym NullableYearMonth) ValidAndNotNull() bool
ValidAndNotNull returns if the year-month is valid and not null, see IsNull.
func (NullableYearMonth) Validate ¶
func (ym NullableYearMonth) Validate() error
Validate returns nil if the year-month is null or can be normalized, see YearMonth.Validate.
func (NullableYearMonth) Value ¶
func (ym NullableYearMonth) Value() (driver.Value, error)
Value implements the database/sql/driver.Valuer interface. It returns the trimmed year-month, or SQL NULL for a blank one. A zero year-month such as "0001-01" is written as that text. An invalid year-month is written after the trim, not rejected.
func (NullableYearMonth) Year ¶
func (ym NullableYearMonth) Year() int
Year returns the year component of the year-month. Returns 0 if the year-month is null or not valid.
type NullableYearQuarter ¶
type NullableYearQuarter string
NullableYearQuarter is a year-quarter string in YYYY-Q# format (e.g. "2024-Q3") that can also represent a NULL value as an empty string.
const YearQuarterNull NullableYearQuarter = ""
YearQuarterNull is the null value for NullableYearQuarter (empty string). Value writes it as SQL NULL and MarshalJSON as JSON null.
func (NullableYearQuarter) AddQuarters ¶
func (yq NullableYearQuarter) AddQuarters(quarters int) NullableYearQuarter
AddQuarters returns a new NullableYearQuarter with the specified number of quarters added. Returns null if the NullableYearQuarter is null and an invalid NullableYearQuarter unchanged. Handles quarter overflow by adjusting the year accordingly.
func (NullableYearQuarter) AddYears ¶
func (yq NullableYearQuarter) AddYears(years int) NullableYearQuarter
AddYears returns a new NullableYearQuarter with the specified number of years added. Returns null if the NullableYearQuarter is null and an invalid NullableYearQuarter unchanged.
func (NullableYearQuarter) Compare ¶
func (yq NullableYearQuarter) Compare(other NullableYearQuarter) int
Compare compares the normalized year-quarter with another normalized NullableYearQuarter, an invalid one as it is. Null is before every year-quarter. Returns -1 if yq is before other, +1 if after, 0 if equal.
func (NullableYearQuarter) ContainsDate ¶
func (yq NullableYearQuarter) ContainsDate(date Date) bool
ContainsDate returns true if the given date falls within this year-quarter. Returns false if the NullableYearQuarter is null.
func (NullableYearQuarter) ContainsTime ¶
func (yq NullableYearQuarter) ContainsTime(t time.Time) bool
ContainsTime returns true if the given time falls within this year-quarter. Returns false if the NullableYearQuarter is null.
func (NullableYearQuarter) ContainsYearMonth ¶
func (yq NullableYearQuarter) ContainsYearMonth(ym YearMonth) bool
ContainsYearMonth returns true if the given year-month falls within this year-quarter. Returns false if the NullableYearQuarter is null.
func (NullableYearQuarter) Date ¶
func (yq NullableYearQuarter) Date(monthInQuarter, day int) NullableDate
Date returns a NullableDate for the given month (1-3 within quarter) and day. Returns Null if the NullableYearQuarter is null or not valid.
func (NullableYearQuarter) DateRange ¶
func (yq NullableYearQuarter) DateRange() (fromDate, untilDate NullableDate)
DateRange returns the first and last NullableDate of the quarter. Returns Null dates if the NullableYearQuarter is null or not valid.
func (NullableYearQuarter) FirstMonth ¶
func (yq NullableYearQuarter) FirstMonth() time.Month
FirstMonth returns the first month of the quarter. Returns 0 if the year-quarter is null or not valid.
func (NullableYearQuarter) Get ¶
func (yq NullableYearQuarter) Get() YearQuarter
Get returns the non-nullable YearQuarter value or panics if the NullableYearQuarter is null. Note: check with IsNull before using Get!
func (NullableYearQuarter) GetOr ¶
func (yq NullableYearQuarter) GetOr(defaultYearQuarter YearQuarter) YearQuarter
GetOr returns the non-nullable YearQuarter value or the passed defaultYearQuarter if the NullableYearQuarter is null.
func (NullableYearQuarter) IsNotNull ¶
func (yq NullableYearQuarter) IsNotNull() bool
IsNotNull returns true if the NullableYearQuarter is not null, see IsNull.
func (NullableYearQuarter) IsNull ¶
func (yq NullableYearQuarter) IsNull() bool
IsNull returns true if the NullableYearQuarter is empty or whitespace only. A zero period such as "0001-Q1" is kept and is not null, so Get returns it. IsNull implements the nullable.Nullable interface.
func (NullableYearQuarter) IsZero ¶
func (yq NullableYearQuarter) IsZero() bool
IsZero returns true if the year-quarter is zero like YearQuarter.IsZero. A zero period such as "0001-Q1" is not null: reading and writing keep that text, and IsNull is false for it.
func (NullableYearQuarter) JSONSchema ¶
func (NullableYearQuarter) JSONSchema() *jsonschema.Schema
JSONSchema returns the JSON schema definition for the NullableYearQuarter type. Implements the jsonschema.JSONSchemaProvider interface.
func (NullableYearQuarter) MarshalJSON ¶
func (yq NullableYearQuarter) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler by writing JSON null if the NullableYearQuarter is blank, else the trimmed and uppercased year-quarter. A zero year-quarter is written as that text.
func (NullableYearQuarter) MarshalText ¶
func (yq NullableYearQuarter) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler like MarshalJSON, but with "" for null, so that encoding/xml and strfmt write the trimmed and uppercased year-quarter. encoding/json writes map keys of string types as they are and doesn't call MarshalText for them.
func (*NullableYearQuarter) MarshalXMLAttr ¶
MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.
func (NullableYearQuarter) Normalized ¶
func (yq NullableYearQuarter) Normalized() (NullableYearQuarter, error)
Normalized returns YearQuarterNull for a null year-quarter, see IsNull, else the year-quarter normalized like YearQuarter.Normalized, or unchanged together with an error if it can't be normalized.
func (NullableYearQuarter) Quarter ¶
func (yq NullableYearQuarter) Quarter() int
Quarter returns the quarter component of the year-quarter (1-4). Returns 0 if the year-quarter is null or not valid.
func (*NullableYearQuarter) Scan ¶
func (yq *NullableYearQuarter) Scan(value any) error
Scan implements the database/sql.Scanner interface. Surrounding whitespace is trimmed and letters are uppercased. A time.Time is stored as its year-quarter, and the zero time.Time as YearQuarterNull. SQL NULL and a blank string are YearQuarterNull. A zero year-quarter such as "0001-Q1" is kept. An invalid year-quarter is kept after that prettification, without an error.
func (*NullableYearQuarter) ScanString ¶
func (yq *NullableYearQuarter) ScanString(source string, validate bool) error
ScanString implements the strfmt.Scannable interface. The strings "NULL", "null" and "nil" set the NullableYearQuarter to null. Without validate it stores source like Scan, with validate it stores the normalized year-quarter or returns the error of Normalized.
func (*NullableYearQuarter) Set ¶
func (yq *NullableYearQuarter) Set(yearQuarter YearQuarter)
Set sets a YearQuarter for this NullableYearQuarter.
func (*NullableYearQuarter) SetNull ¶
func (yq *NullableYearQuarter) SetNull()
SetNull sets the NullableYearQuarter to null.
func (NullableYearQuarter) String ¶
func (yq NullableYearQuarter) String() string
String returns the year-quarter as a string in YYYY-Q# format. String implements the fmt.Stringer interface.
func (NullableYearQuarter) StringOr ¶
func (yq NullableYearQuarter) StringOr(nullString string) string
StringOr returns the NullableYearQuarter as string or the passed nullString if the NullableYearQuarter is null.
func (*NullableYearQuarter) UnmarshalJSON ¶
func (yq *NullableYearQuarter) UnmarshalJSON(data []byte) error
UnmarshalJSON implements encoding/json.Unmarshaler. A JSON string is stored like Scan stores it, JSON null sets the NullableYearQuarter to null, so that decoding null into a field that holds a value resets it, and any other JSON value is an error.
func (*NullableYearQuarter) UnmarshalXML ¶
func (yq *NullableYearQuarter) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like Scan stores it.
func (*NullableYearQuarter) UnmarshalXMLAttr ¶
func (yq *NullableYearQuarter) UnmarshalXMLAttr(attr xml.Attr) error
UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like Scan stores it.
func (NullableYearQuarter) Valid ¶
func (yq NullableYearQuarter) Valid() bool
Valid returns true if the year-quarter is null or can be normalized, see YearQuarter.Validate.
func (NullableYearQuarter) ValidAndNormalized ¶
func (yq NullableYearQuarter) ValidAndNormalized() bool
ValidAndNormalized returns true if the year-quarter is valid and already normalized, or if it is the empty YearQuarterNull. A whitespace-only or zero value is null and valid, but not normalized.
func (NullableYearQuarter) ValidAndNotNull ¶
func (yq NullableYearQuarter) ValidAndNotNull() bool
ValidAndNotNull returns if the year-quarter is valid and not null, see IsNull.
func (NullableYearQuarter) Validate ¶
func (yq NullableYearQuarter) Validate() error
Validate returns nil if the year-quarter is null or can be normalized, see YearQuarter.Validate.
func (NullableYearQuarter) Value ¶
func (yq NullableYearQuarter) Value() (driver.Value, error)
Value implements the database/sql/driver.Valuer interface. It returns the trimmed and uppercased year-quarter, or SQL NULL for a blank one. A zero year-quarter such as "0001-Q1" is written as that text. An invalid year-quarter is written after that prettification, not rejected.
func (NullableYearQuarter) Year ¶
func (yq NullableYearQuarter) Year() int
Year returns the year component of the year-quarter. Returns 0 if the year-quarter is null or not valid.
type YearMonth ¶
type YearMonth string
YearMonth represents a calendar year and month in ISO 8601 format (YYYY-MM). See NullableYearMonth for a nullable version of this type.
Scan, UnmarshalJSON, UnmarshalXML, MarshalJSON, MarshalText, Value and ScanString without validate trim surrounding whitespace. A zero year-month such as "0001-01" is kept. They do not reject an invalid year-month. A time.Time from the driver is still its year-month, and the zero time.Time is "". Value writes SQL NULL only for a blank year-month. ScanString with validate stores the normalized year-month or returns the error of Normalized. fmt prints the year-month as stored. See "Reading and writing values" in the README of the module.
func YearMonthFrom ¶
YearMonthFrom creates a YearMonth from the given year and month values. Returns the year-month in normalized YYYY-MM format.
func YearMonthOfTime ¶
YearMonthOfTime returns the year-month part of the passed time.Time. Returns an empty string if t.IsZero().
func YearMonthOfToday ¶
func YearMonthOfToday() YearMonth
YearMonthOfToday returns the year-month of today in the local timezone.
func (YearMonth) AddMonths ¶
AddMonths returns a new year-month with the specified number of months added. Handles month overflow by adjusting the year accordingly. Returns the year-month unchanged if it is not valid. A result outside the years 1 to 9999, like "10000-01" for "9999-12" plus one month, is not valid, so that Value returns an error for it like AddYears.
func (YearMonth) AddYears ¶
AddYears returns a new year-month with the specified number of years added. Returns the year-month unchanged if it is not valid. A result outside the years 1 to 9999, like "10000-12", is not valid. Value writes that text after trimming. It does not return an error.
func (YearMonth) Compare ¶
Compare compares the normalized year-month with another normalized YearMonth, a zero one (see IsZero) as "" like Date.Compare does, and an invalid one as it is. Returns -1 if ym is before other, +1 if after, 0 if equal.
func (YearMonth) ContainsDate ¶
ContainsDate returns true if the given date falls within this year-month.
func (YearMonth) ContainsTime ¶
ContainsTime returns true if the given time falls within this year-month.
func (YearMonth) Date ¶
Date returns a Date for the given day within this year-month. The year-month is normalized if it is valid, the day value is formatted as-is without validation.
func (YearMonth) DateRange ¶
DateRange returns the first and last date of the month. Returns the date range [fromDate, untilDate] representing the entire month where untilDate is the last day of the month. Returns empty dates if the year-month is not valid.
func (YearMonth) Format ¶
Format formats the year-month using the given layout string (see time.Time.Format). Returns an empty string if ym is zero (see IsZero) or layout is empty, like Date.Format. If layout equals YearMonthLayout constant, returns the normalized year-month, or an invalid one unchanged. For other layouts, converts to Date using the 1st day of the month and delegates to Date.Format.
func (YearMonth) IsZero ¶
IsZero returns true when the year-month is any of ["", "0000-00", "0001-01"], also with surrounding whitespace. "0001-01" is treated as zero because "0001-01-01" is the zero value of time.Time. "0000-00" may be the zero value of other date implementations. A zero year-month is not a valid YearMonth. Reading and Value keep the trimmed text, including "0001-01". That text is not null, so Get returns it.
func (YearMonth) JSONSchema ¶
func (YearMonth) JSONSchema() *jsonschema.Schema
JSONSchema returns the JSON schema definition for the YearMonth type. Implements the jsonschema.JSONSchemaProvider interface.
func (YearMonth) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler by writing the trimmed year-month, or "" for a blank one. A zero year-month is written as that text.
func (YearMonth) MarshalText ¶
MarshalText implements encoding.TextMarshaler like MarshalJSON, so that encoding/xml and strfmt write the trimmed year-month. encoding/json writes map keys of string types as they are and doesn't call MarshalText for them.
func (*YearMonth) MarshalXMLAttr ¶
MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.
func (YearMonth) Month ¶
Month returns the month component of the year-month. Returns 0 if the year-month is not valid.
func (YearMonth) Normalized ¶
Normalized returns the year-month with the surrounding whitespace trimmed, or the YearMonth unchanged together with an error if that is not in valid YYYY-MM format with a year from 1 to 9999 like Date, which has no year 0, and a month from 1 to 12, or if it is the zero year-month "0001-01", see IsZero.
func (YearMonth) Nullable ¶
func (ym YearMonth) Nullable() NullableYearMonth
Nullable returns the year-month as a NullableYearMonth.
func (*YearMonth) Scan ¶
Scan implements the database/sql.Scanner interface. Surrounding whitespace is trimmed. A time.Time is stored as its year-month in the location of the time, and the zero time.Time as "". SQL NULL and a blank string are "". A zero year-month such as "0001-01" is kept. An invalid year-month is kept after the trim, without an error.
func (*YearMonth) ScanString ¶
ScanString implements the strfmt.Scannable interface. Without validate it stores source like Scan, with validate it stores the normalized year-month or returns the error of Normalized, also for a zero source.
func (YearMonth) String ¶
String returns the year-month as a string in YYYY-MM format. String implements the fmt.Stringer interface.
func (*YearMonth) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler. A JSON string is stored like Scan stores it, JSON null leaves the YearMonth unchanged like encoding/json does for a string type, and any other JSON value is an error.
func (*YearMonth) UnmarshalXML ¶
UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like Scan stores it.
func (*YearMonth) UnmarshalXMLAttr ¶
UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like Scan stores it.
func (YearMonth) ValidAndNormalized ¶
ValidAndNormalized returns true if the year-month is valid and already normalized.
func (YearMonth) Validate ¶
Validate returns the error of Normalized, so a year-month with surrounding whitespace is valid.
type YearQuarter ¶
type YearQuarter string
YearQuarter represents a calendar year and quarter in format YYYY-Q# (e.g., "2023-Q1"). See NullableYearQuarter for a nullable version of this type.
Scan, UnmarshalJSON, UnmarshalXML, MarshalJSON, MarshalText, Value and ScanString without validate trim surrounding whitespace and uppercase the letters, so "2024-q1" becomes "2024-Q1". A zero year-quarter such as "0001-Q1" is kept. They do not reject an invalid year-quarter. A time.Time from the driver is still its year-quarter, and the zero time.Time is "". Value writes SQL NULL only for a blank year-quarter. ScanString with validate stores the normalized year-quarter or returns the error of Normalized. fmt prints the year-quarter as stored. See "Reading and writing values" in the README of the module.
func YearQuarterFrom ¶
func YearQuarterFrom(year int, quarter int) YearQuarter
YearQuarterFrom creates a YearQuarter from the given year and quarter values. Quarter must be 1-4. Returns the year-quarter in normalized YYYY-Q# format.
func YearQuarterOfTime ¶
func YearQuarterOfTime(t time.Time) YearQuarter
YearQuarterOfTime returns the year-quarter part of the passed time.Time. Returns an empty string if t.IsZero().
func YearQuarterOfToday ¶
func YearQuarterOfToday() YearQuarter
YearQuarterOfToday returns the year-quarter of today in the local timezone.
func (YearQuarter) AddQuarters ¶
func (yq YearQuarter) AddQuarters(quarters int) YearQuarter
AddQuarters returns a new year-quarter with the specified number of quarters added. Handles quarter overflow by adjusting the year accordingly. Returns the year-quarter unchanged if it is not valid. A result outside the years 1 to 9999, like "10000-Q1" for "9999-Q4" plus one quarter, is not valid like for AddYears.
func (YearQuarter) AddYears ¶
func (yq YearQuarter) AddYears(years int) YearQuarter
AddYears returns a new year-quarter with the specified number of years added. Returns the year-quarter unchanged if it is not valid. A result outside the years 1 to 9999, like "10000-Q4", is not valid. Value writes that text after trimming and uppercasing. It does not return an error.
func (YearQuarter) Compare ¶
func (yq YearQuarter) Compare(other YearQuarter) int
Compare compares the normalized year-quarter with another normalized YearQuarter, a zero one (see IsZero) as "" like Date.Compare does, and an invalid one as it is. Returns -1 if yq is before other, +1 if after, 0 if equal.
func (YearQuarter) ContainsDate ¶
func (yq YearQuarter) ContainsDate(date Date) bool
ContainsDate returns true if the given date falls within this year-quarter.
func (YearQuarter) ContainsTime ¶
func (yq YearQuarter) ContainsTime(t time.Time) bool
ContainsTime returns true if the given time falls within this year-quarter.
func (YearQuarter) ContainsYearMonth ¶
func (yq YearQuarter) ContainsYearMonth(ym YearMonth) bool
ContainsYearMonth returns true if the given year-month falls within this year-quarter.
func (YearQuarter) Date ¶
func (yq YearQuarter) Date(monthInQuarter, day int) Date
Date returns a Date for the given month (1-3 within quarter) and day. The month parameter is 1-3 representing the month within the quarter. The values are formatted as-is without validation.
func (YearQuarter) DateRange ¶
func (yq YearQuarter) DateRange() (fromDate, untilDate Date)
DateRange returns the first and last date of the quarter. Returns the date range [fromDate, untilDate] representing the entire quarter.
func (YearQuarter) FirstMonth ¶
func (yq YearQuarter) FirstMonth() time.Month
FirstMonth returns the first month of the quarter (1, 4, 7, or 10). Returns 0 if the year-quarter is not valid.
func (YearQuarter) IsZero ¶
func (yq YearQuarter) IsZero() bool
IsZero returns true when the year-quarter is any of ["", "0000-Q0", "0001-Q1"], also with surrounding whitespace or a lower case "q". "0001-Q1" is treated as zero because "0001-01-01" is the zero value of time.Time. "0000-Q0" may be the zero value of other date implementations. A zero year-quarter is not a valid YearQuarter. Reading and Value keep the trimmed and uppercased text, including "0001-Q1". That text is not null, so Get returns it.
func (YearQuarter) JSONSchema ¶
func (YearQuarter) JSONSchema() *jsonschema.Schema
JSONSchema returns the JSON schema definition for the YearQuarter type. Implements the jsonschema.JSONSchemaProvider interface.
func (YearQuarter) MarshalJSON ¶
func (yq YearQuarter) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler by writing the trimmed and uppercased year-quarter, or "" for a blank one. A zero year-quarter is written as that text.
func (YearQuarter) MarshalText ¶
func (yq YearQuarter) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler like MarshalJSON, so that encoding/xml and strfmt write the trimmed and uppercased year-quarter. encoding/json writes map keys of string types as they are and doesn't call MarshalText for them.
func (*YearQuarter) MarshalXMLAttr ¶
MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.
func (YearQuarter) Normalized ¶
func (yq YearQuarter) Normalized() (YearQuarter, error)
Normalized returns the year-quarter with the surrounding whitespace trimmed and an upper case "Q", or the YearQuarter unchanged together with an error if that is not in valid YYYY-Q# format with a year from 1 to 9999 like Date, which has no year 0, and a quarter from 1 to 4, or if it is the zero year-quarter "0001-Q1", see IsZero.
func (YearQuarter) Nullable ¶
func (yq YearQuarter) Nullable() NullableYearQuarter
Nullable returns the year-quarter as a NullableYearQuarter.
func (YearQuarter) Quarter ¶
func (yq YearQuarter) Quarter() int
Quarter returns the quarter component of the year-quarter (1-4). Returns 0 if the year-quarter is not valid.
func (*YearQuarter) Scan ¶
func (yq *YearQuarter) Scan(value any) error
Scan implements the database/sql.Scanner interface. Surrounding whitespace is trimmed and letters are uppercased. A time.Time is stored as its year-quarter in the location of the time, and the zero time.Time as "". SQL NULL and a blank string are "". A zero year-quarter such as "0001-Q1" is kept. An invalid year-quarter is kept after that prettification, without an error.
func (*YearQuarter) ScanString ¶
func (yq *YearQuarter) ScanString(source string, validate bool) error
ScanString implements the strfmt.Scannable interface. Without validate it stores source like Scan, with validate it stores the normalized year-quarter or returns the error of Normalized, also for a zero source.
func (YearQuarter) String ¶
func (yq YearQuarter) String() string
String returns the year-quarter as a string in YYYY-Q# format. String implements the fmt.Stringer interface.
func (*YearQuarter) UnmarshalJSON ¶
func (yq *YearQuarter) UnmarshalJSON(data []byte) error
UnmarshalJSON implements encoding/json.Unmarshaler. A JSON string is stored like Scan stores it, JSON null leaves the YearQuarter unchanged like encoding/json does for a string type, and any other JSON value is an error.
func (*YearQuarter) UnmarshalXML ¶
func (yq *YearQuarter) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like Scan stores it.
func (*YearQuarter) UnmarshalXMLAttr ¶
func (yq *YearQuarter) UnmarshalXMLAttr(attr xml.Attr) error
UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like Scan stores it.
func (YearQuarter) Valid ¶
func (yq YearQuarter) Valid() bool
Valid returns true if the year-quarter can be normalized, see Validate.
func (YearQuarter) ValidAndNormalized ¶
func (yq YearQuarter) ValidAndNormalized() bool
ValidAndNormalized returns true if the year-quarter is valid and already normalized.
func (YearQuarter) Validate ¶
func (yq YearQuarter) Validate() error
Validate returns the error of Normalized, so "2023-q1" and a year-quarter with surrounding whitespace are valid.
func (YearQuarter) Value ¶
func (yq YearQuarter) Value() (driver.Value, error)
Value implements the database/sql/driver.Valuer interface. It returns the trimmed and uppercased year-quarter, or SQL NULL for a blank one. A zero year-quarter such as "0001-Q1" is written as that text. An invalid year-quarter is written after that prettification, not rejected.
func (YearQuarter) Year ¶
func (yq YearQuarter) Year() int
Year returns the year component of the year-quarter. Returns 0 if the year-quarter is not valid.