date

package
v0.0.0-...-f3f6071 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 14 Imported by: 4

Documentation

Index

Constants

View Source
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

View Source
var ParseTimeDefaultLayouts = []string{
	time.RFC3339Nano,
	time.RFC3339,
	"2006-01-02 15:04:05",
}

Functions

func ParseTime

func ParseTime(str string, layouts ...string) (time.Time, error)

ParseTime is a non Date related helper function that parses the passed string as time.Time. It uses time.Parse with the passed layouts and returns the first valid parsed time. If no layouts are passed, then ParseTimeDefaultLayouts will be used.

func StringIsDate

func StringIsDate(str string, lang ...language.Code) bool

StringIsDate returns if a string can be parsed as Date. The first given lang argument is used as language hint.

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 FromUntilFromYearAndMonths

func FromUntilFromYearAndMonths(year, months string) (fromDate, untilDate Date, err error)

func Must

func Must(str string) Date

Must returns str as normalized Date or panics if str is not a valid Date.

func Normalize

func Normalize(str string, lang ...language.Code) (Date, error)

Normalize returns str as normalized Date or an error. The first given lang argument is used as language hint.

func Of

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

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

func OfTime(t time.Time) Date

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 OfToday

func OfToday() Date

OfToday returns the date of today in the local timezone.

func OfTodayIn

func OfTodayIn(loc *time.Location) Date

OfTodayUTC returns the date of today in the timezone of the passed location.

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 Parse

func Parse(layout, value string) (Date, error)

Parse returns the date part from time.Parse(layout, value)

func PeriodRange

func PeriodRange(period string) (from, until Date, err error)

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

func YearRange(year int) (from, until Date)

YearRange returns the date range from first of January to 31st of December of a year.

func YearWeekMonday

func YearWeekMonday(year, week int) (monday Date)

YearWeekMonday returns the date of Monday of an ISO 8601 week.

func YearWeekRange

func YearWeekRange(year, week int) (monday, sunday Date)

YearWeekRange returns the dates of Monday and Sunday of an ISO 8601 week.

func (Date) Add

func (date Date) Add(d time.Duration) Date

func (Date) AddDate

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

func (Date) AddDays

func (date Date) AddDays(days int) Date

func (Date) AddMonths

func (date Date) AddMonths(months int) Date

func (Date) AddYears

func (date Date) AddYears(years int) Date

func (Date) After

func (date Date) After(other Date) bool

After returns if the date is after the passed other one.

func (Date) AfterTime

func (date Date) AfterTime(other time.Time) bool

AfterTime returns if midnight of the date in location of the passed time is after the time.

func (Date) AfterToday

func (date Date) AfterToday() bool

func (Date) AfterTodayInUTC

func (date Date) AfterTodayInUTC() bool

func (Date) Before

func (date Date) Before(other Date) bool

Before returns if the date is before the passed other one.

func (Date) BeforeTime

func (date Date) BeforeTime(other time.Time) bool

BeforeTime returns if midnight of the date in location of the passed time is before the time.

func (Date) BeforeToday

func (date Date) BeforeToday() bool

func (Date) BeforeTodayInUTC

func (date Date) BeforeTodayInUTC() bool

func (Date) BeginningOfMonth

func (date Date) BeginningOfMonth() Date

func (Date) BeginningOfQuarter

func (date Date) BeginningOfQuarter() Date

func (Date) BeginningOfWeek

func (date Date) BeginningOfWeek() Date

func (Date) BeginningOfYear

func (date Date) BeginningOfYear() Date

func (Date) BetweenExcl

func (date Date) BetweenExcl(after, before Date) bool

BetweenExcl returns if date is between and exlusive after and until.

func (Date) Compare

func (date Date) Compare(other Date) int

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) Day

func (date Date) Day() int

Day within the month of the date

func (Date) EndOfMonth

func (date Date) EndOfMonth() Date

func (Date) EndOfQuarter

func (date Date) EndOfQuarter() Date

func (Date) EndOfWeek

func (date Date) EndOfWeek() Date

func (Date) EndOfYear

func (date Date) EndOfYear() Date

func (Date) EqualOrAfter

func (date Date) EqualOrAfter(other Date) bool

EqualOrAfter returns if the date is equal or after the passed other one.

func (Date) EqualOrBefore

func (date Date) EqualOrBefore(other Date) bool

EqualOrBefore returns if the date is equal or before the passed other one.

func (Date) Format

func (date Date) Format(layout string) string

Format returns date.MidnightUTC().Format(layout), or an empty string if date or layout are an empty string.

func (Date) ISOWeek

func (date Date) 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.

func (Date) IsToday

func (date Date) IsToday() bool

func (Date) IsTodayInUTC

func (date Date) IsTodayInUTC() bool

func (Date) IsZero

func (date Date) IsZero() bool

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 Date) LastMonday() Date

func (Date) Midnight

func (date Date) Midnight() time.Time

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

func (date Date) MidnightInLocation(loc *time.Location) time.Time

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

func (date Date) MidnightUTC() time.Time

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) Month

func (date Date) Month() time.Month

Month of the date

func (Date) NextSunday

func (date Date) NextSunday() Date

func (Date) Normalized

func (date Date) Normalized(lang ...language.Code) (Date, 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.

func (Date) NormalizedEqual

func (date Date) NormalizedEqual(other Date) bool

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 Date) NormalizedOrUnchanged(lang ...language.Code) Date

func (Date) Nullable

func (date Date) Nullable() NullableDate

Nullable returns the date as NullableDate

func (*Date) Scan

func (date *Date) Scan(value any) (err error)

Scan implements the database/sql.Scanner interface.

func (*Date) ScanString

func (date *Date) 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 (*Date) ScanStringWithLang

func (date *Date) ScanStringWithLang(source string, lang language.Code) (wasNormalized bool, monthMustBeFirst bool, err error)

func (Date) String

func (date Date) String() string

String returns the normalized date if possible, else it will be returned unchanged as string. String implements the fmt.Stringer interface.

func (Date) Sub

func (date Date) Sub(other Date) time.Duration

func (Date) Time

func (date Date) Time(hour, minute, second int, location *time.Location) time.Time

func (Date) TimeLocal

func (date Date) TimeLocal(hour, minute, second int) time.Time

func (Date) TimeUTC

func (date Date) TimeUTC(hour, minute, second int) time.Time

func (Date) Valid

func (date Date) Valid() bool

Valid returns if the format of the date is correct, see Format

func (Date) ValidAndNormalized

func (date Date) ValidAndNormalized() bool

func (Date) Validate

func (date Date) Validate() error

Validate returns an error if the date is not in a valid, normalizeable format.

func (Date) Value

func (date Date) Value() (driver.Value, error)

Value implements the driver database/sql/driver.Valuer interface.

func (Date) Weekday

func (date Date) Weekday() time.Weekday

Weekday returns the date's day of the week.

func (Date) WithinIncl

func (date Date) WithinIncl(from, until Date) bool

WithinIncl returns if date is within and inclusive from and until.

func (Date) Year

func (date Date) Year() int

Year of the date

func (Date) YearMonthDay

func (date Date) 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.

type Finder

type Finder struct {
	LangHint language.Code
}

func NewFinder

func NewFinder(lang ...language.Code) *Finder

func (*Finder) FindAllIndex

func (df *Finder) FindAllIndex(str []byte, n int) (indices [][]int)

type Format

type Format struct {
	Layout     string `json:"layout"`
	NilString  string `json:"nilString"`
	ZeroString string `json:"zeroString"`
}

func (*Format) AssignString

func (f *Format) AssignString(dest reflect.Value, source string) error

func (*Format) Format

func (f *Format) Format(date Date) string

func (*Format) FormatString

func (f *Format) FormatString(val reflect.Value) (string, error)

func (*Format) Parse

func (f *Format) Parse(str string, langHints ...language.Code) (normalized string, err error)

Parse implements the strfmt.Parser interface

type Formatter

type Formatter string

func (Formatter) Format

func (f Formatter) Format(date Date) string

func (Formatter) Parse

func (f Formatter) Parse(str string, langHints ...language.Code) (normalized string, err error)

Parse implements the strfmt.Parser interface

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 (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 (n *NullableDate) ScanStringWithLang(source string, lang language.Code) (wasNormalized bool, monthMustBeFirst bool, err error)

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.

type Parser

type Parser struct{}

Parser implements the strfmt.Parser interface for dates.

func (Parser) Parse

func (Parser) Parse(str string, langHints ...language.Code) (normalized string, err error)

Jump to

Keyboard shortcuts

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