Documentation ¶
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() 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() 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 Date) LastMonday() Date
- 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(lang ...language.Code) (Date, error)
- func (date Date) NormalizedEqual(other Date) bool
- 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) (err 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) String() string
- 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) 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() 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() 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 (n NullableDate) LastMonday() NullableDate
- func (n NullableDate) MarshalJSON() ([]byte, 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(lang ...language.Code) (NullableDate, error)
- func (n NullableDate) NormalizedEqual(other NullableDate) bool
- func (n NullableDate) NormalizedOrNull(lang ...language.Code) NullableDate
- func (n NullableDate) NormalizedOrUnchanged(lang ...language.Code) NullableDate
- func (n *NullableDate) Scan(value any) (err 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) String() string
- func (n NullableDate) StringOr(nullString string) string
- func (n NullableDate) Sub(other NullableDate) time.Duration
- 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 Parser
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 = "" )
Variables ¶
var ParseTimeDefaultLayouts = []string{ time.RFC3339Nano, time.RFC3339, "2006-01-02 15:04:05", }
Functions ¶
Types ¶
type Date ¶
type Date string
Date represents a the day of calender date Date implements the database/sql.Scanner and database/sql/driver.Valuer interfaces, and will treat an empty string or the zero dates "0000-00-00" and "0001-01-01" (see IsZero) as SQL NULL.
func Normalize ¶
Normalize returns str as normalized Date or an error. The first given lang argument is used as language hint.
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 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 month of the period, the returned until Date is the last day of the month of the period. Exmaples: 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-07-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) AfterTime ¶
AfterTime returns if midnight of the date in location of the passed time is after the time.
func (Date) AfterToday ¶
func (Date) AfterTodayInUTC ¶
func (Date) BeforeTime ¶
BeforeTime returns if midnight of the date in location of the passed time is before the time.
func (Date) BeforeToday ¶
func (Date) BeforeTodayInUTC ¶
func (Date) BeginningOfMonth ¶
func (Date) BeginningOfQuarter ¶
func (Date) BeginningOfWeek ¶
func (Date) BeginningOfYear ¶
func (Date) BetweenExcl ¶
BetweenExcl returns if date is between and exlusive after and until.
func (Date) Compare ¶
Compare compares the date with the passed other Date. If the date is before the other, it returns -1; if the date is after the other, it returns +1; if they're the same, it returns 0.
func (Date) EndOfMonth ¶
func (Date) EndOfQuarter ¶
func (Date) EqualOrAfter ¶
EqualOrAfter returns if the date is equal or after the passed other one.
func (Date) EqualOrBefore ¶
EqualOrBefore returns if the date is equal or before the passed other one.
func (Date) Format ¶
Format returns date.MidnightUTC().Format(layout), or an empty string if date or layout are an empty string.
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) IsTodayInUTC ¶
func (Date) IsZero ¶
IsZero returns true when the date is any of ["", "0000-00-00", "0001-01-01"] "0001-01-01" is treated as zero because it's the zero value of time.Time. "0000-00-00" may be the zero value of other date implementations.
func (Date) LastMonday ¶
func (Date) Midnight ¶
Midnight returns the midnight (00:00) time.Time of the date in the local time zone, or a zero time.Time value if the date is not valid.
func (Date) MidnightInLocation ¶
MidnightInLocation returns the midnight (00:00) time.Time of the date in the given location, or a zero time.Time value if the date is not valid.
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 not valid.
func (Date) NextSunday ¶
func (Date) Normalized ¶
Normalized returns the date in normalized form, or an error if the format can't be detected. The first given lang argument is used as language hint.
func (Date) NormalizedEqual ¶
NormalizedEqual returns if two dates are equal in normalized form.
func (Date) NormalizedOrNull ¶
func (date Date) NormalizedOrNull(lang ...language.Code) NullableDate
func (Date) NormalizedOrUnchanged ¶
func (Date) Nullable ¶
func (date Date) Nullable() NullableDate
Nullable returns the date as NullableDate
func (*Date) ScanString ¶
ScanString tries to parse and assign the passed source string as value of the implementing type.
If validate is true, the source string is checked for validity before it is assigned to the type.
If validate is false and the source string can still be assigned in some non-normalized way it will be assigned without returning an error.
func (*Date) ScanStringWithLang ¶
func (Date) String ¶
String returns the normalized date if possible, else it will be returned unchanged as string. String implements the fmt.Stringer interface.
func (Date) ValidAndNormalized ¶
func (Date) Validate ¶
Validate returns an error if the date is not in a valid, normalizeable format.
func (Date) WithinIncl ¶
WithinIncl returns if date is within and inclusive from and until.
type Format ¶
type Format struct { Layout string `json:"layout"` NilString string `json:"nilString"` ZeroString string `json:"zeroString"` }
func (*Format) AssignString ¶
type NullableDate ¶
type NullableDate string
NullableDate is identical to Date, except that IsZero() is considered valid by the Valid() and Validate() methods. NullableDate implements the database/sql.Scanner and database/sql/driver.Valuer interfaces, and will treat an empty/zero Date string as SQL NULL value. The main difference between Date and NullableDate is: Date("").Valid() == false NullableDate("").Valid() == true
var Null NullableDate
Null is an empty string and will be treatet 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.
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
func (NullableDate) AddDate ¶
func (n NullableDate) AddDate(years int, months int, days int) NullableDate
func (NullableDate) AddDays ¶
func (n NullableDate) AddDays(days int) NullableDate
func (NullableDate) AddMonths ¶
func (n NullableDate) AddMonths(months int) NullableDate
func (NullableDate) AddYears ¶
func (n NullableDate) AddYears(years int) NullableDate
func (NullableDate) After ¶
func (n NullableDate) After(other NullableDate) bool
After returns if the date is after the passed other one. Returns false if any of the dates is null.
func (NullableDate) AfterTime ¶
func (n NullableDate) AfterTime(other time.Time) bool
AfterTime returns if midnight of the date in location of the passed time is after the time. Returns false if the date is null.
func (NullableDate) AfterToday ¶
func (n NullableDate) AfterToday() bool
func (NullableDate) AfterTodayInUTC ¶
func (n NullableDate) AfterTodayInUTC() bool
func (NullableDate) Before ¶
func (n NullableDate) Before(other NullableDate) bool
Before returns if the date is before the passed other one. A null date is always before any other date.
func (NullableDate) BeforeTime ¶
func (n NullableDate) BeforeTime(other time.Time) bool
BeforeTime returns if midnight of the date in location of the passed time is before the time. Returns true if the date is null.
func (NullableDate) BeforeToday ¶
func (n NullableDate) BeforeToday() bool
Null is always before today.
func (NullableDate) BeforeTodayInUTC ¶
func (n NullableDate) BeforeTodayInUTC() bool
Null is always before today.
func (NullableDate) BeginningOfMonth ¶
func (n NullableDate) BeginningOfMonth() NullableDate
func (NullableDate) BeginningOfQuarter ¶
func (n NullableDate) BeginningOfQuarter() NullableDate
func (NullableDate) BeginningOfWeek ¶
func (n NullableDate) BeginningOfWeek() NullableDate
func (NullableDate) BeginningOfYear ¶
func (n NullableDate) BeginningOfYear() NullableDate
func (NullableDate) Compare ¶
func (n NullableDate) Compare(other NullableDate) int
Compare compares n with the passed other NullableDate. If n is before the other, it returns -1; if n is after the other, it returns +1; if they're the same, it returns 0. A null date is always before a non-null date.
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 within the month of the date or zero if the date is null.
func (NullableDate) EndOfMonth ¶
func (n NullableDate) EndOfMonth() NullableDate
func (NullableDate) EndOfQuarter ¶
func (n NullableDate) EndOfQuarter() NullableDate
func (NullableDate) EndOfWeek ¶
func (n NullableDate) EndOfWeek() NullableDate
func (NullableDate) EndOfYear ¶
func (n NullableDate) EndOfYear() NullableDate
func (NullableDate) EqualOrAfter ¶
func (n NullableDate) EqualOrAfter(other NullableDate) bool
EqualOrAfter returns if the date is equal or after the passed other one.
func (NullableDate) EqualOrBefore ¶
func (n NullableDate) EqualOrBefore(other NullableDate) bool
EqualOrBefore returns if the date is equal or before the passed other one.
func (NullableDate) Format ¶
func (n NullableDate) Format(layout string) string
Format returns n.MidnightUTC().Format(layout), or an empty string if n is Null or layout is an empty string.
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. IsNull implements the nullable.Nullable interface.
func (NullableDate) IsToday ¶
func (n NullableDate) IsToday() bool
func (NullableDate) IsTodayInUTC ¶
func (n NullableDate) IsTodayInUTC() bool
func (NullableDate) IsZero ¶
func (n NullableDate) IsZero() bool
IsZero returns true when the date is any of ["", "0000-00-00", "0001-01-01", "null", "NULL"] "0001-01-01" is treated as zero because it's the zero value of time.Time.
func (NullableDate) LastMonday ¶
func (n NullableDate) LastMonday() NullableDate
func (NullableDate) MarshalJSON ¶
func (n NullableDate) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler by returning the JSON null value for an empty (null) string.
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
MidnightTime returns the midnight (00:00) nullable.Time of the date in the given location, or a null nullable.Time value if the date is not valid.
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 not valid.
func (NullableDate) Month ¶
func (n NullableDate) Month() time.Month
Month of the date or zero if the date is null.
func (NullableDate) NextSunday ¶
func (n NullableDate) NextSunday() NullableDate
func (NullableDate) Normalized ¶
func (n NullableDate) Normalized(lang ...language.Code) (NullableDate, error)
Normalized returns the date in normalized form, or an error if the format can't be detected. The first given lang argument is used as language hint. if n.IsZero() then Null, nil will be returned.
func (NullableDate) NormalizedEqual ¶
func (n NullableDate) NormalizedEqual(other NullableDate) bool
NormalizedEqual returns if two dates are equal in normalized form.
func (NullableDate) NormalizedOrNull ¶
func (n NullableDate) NormalizedOrNull(lang ...language.Code) NullableDate
func (NullableDate) NormalizedOrUnchanged ¶
func (n NullableDate) NormalizedOrUnchanged(lang ...language.Code) NullableDate
func (*NullableDate) Scan ¶
func (n *NullableDate) Scan(value any) (err error)
Scan implements the database/sql.Scanner interface.
func (*NullableDate) ScanString ¶
func (n *NullableDate) ScanString(source string, validate bool) error
ScanString tries to parse and assign the passed source string as value of the implementing type.
If validate is true, the source string is checked for validity before it is assigned to the type.
If validate is false and the source string can still be assigned in some non-normalized way it will be assigned without returning an error.
func (*NullableDate) ScanStringWithLang ¶
func (*NullableDate) Set ¶
func (n *NullableDate) Set(d Date)
Set sets an Date for this NullableDate
func (*NullableDate) SetNull ¶
func (n *NullableDate) SetNull()
SetNull sets the NullableDate to null
func (NullableDate) String ¶
func (n NullableDate) String() string
String returns the normalized date if possible, else it will be returned unchanged as string. String implements the fmt.Stringer interface.
func (NullableDate) StringOr ¶
func (n NullableDate) StringOr(nullString string) string
StringOr returns the NullableDate as string or the passed nullString if the NullableDate is null.
func (NullableDate) Sub ¶
func (n NullableDate) Sub(other NullableDate) time.Duration
func (NullableDate) Valid ¶
func (n NullableDate) Valid() bool
Valid returns if the format of the date is correct, see Format n.IsZero() is valid
func (NullableDate) ValidAndNormalized ¶
func (n NullableDate) ValidAndNormalized() bool
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, normalizeable format. n.IsZero() is valid
func (NullableDate) Value ¶
func (n NullableDate) Value() (driver.Value, error)
Value implements the driver database/sql/driver.Valuer interface.
func (NullableDate) Weekday ¶
func (n NullableDate) Weekday() time.Weekday
Weekday returns the date's day of the week or zero if the date is null.
func (NullableDate) Year ¶
func (n NullableDate) Year() int
Year of the date or zero if the date is null.
func (NullableDate) YearMonthDay ¶
func (n NullableDate) YearMonthDay() (year int, month time.Month, day int)
YearMonthDay returns the year, month, day components of the Date. Zero values will be returned when the date is not valid or null.