dates

package
v1.58.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 10 Imported by: 52

Documentation

Index

Constants

View Source
const (
	DateOnlyLayouts = LayoutType(dateSeq)
	TimeOnlyLayouts = LayoutType(timeSeq)
	DateTimeLayouts = LayoutType(dateSeq | timeSeq | dateTimeSeq)
)

formatting mode constants

View Source
const (
	ISO8601Date = "YYYY-MM-DD"
)

Variables

View Source
var ZeroDate = Date{}

ZeroDate is our uninitialized date value

View Source
var ZeroDateTime = time.Time{}

ZeroDateTime is our uninitialized datetime value

View Source
var ZeroTimeOfDay = TimeOfDay{}

ZeroTimeOfDay is our uninitialized time of day value

Functions

func DayToUTCRange

func DayToUTCRange(d time.Time, tz *time.Location) (time.Time, time.Time)

DayToUTCRange returns the UTC time range of the given day

func DaysBetween

func DaysBetween(date1 time.Time, date2 time.Time) int

DaysBetween returns the number of calendar days (an int) between the two dates. Note that if these are in different timezones then the local calendar day is used for each and the difference is calculated from that.

func Format added in v1.9.0

func Format(t time.Time, layout string, locale i18n.Locale, type_ LayoutType) (string, error)

Format formats a date/time value using a layout string.

If type is DateOnlyLayouts or DateTimeLayouts, the following sequences are accepted:

`YY`        - last two digits of year 0-99
`YYYY`      - four digits of your 0000-9999
`M`         - month 1-12
`MM`        - month 01-12
`MMM`       - month Jan-Dec (localized using given locale)
`MMMM`      - month January-December (localized using given locale)
`D`         - day of month 1-31
`DD`        - day of month, zero padded 0-31
`EEE`       - day of week Mon-Sun (localized using given locale)
`EEEE`      - day of week Monday-Sunday (localized using given locale)

If type is TimeOnlyLayouts or DateTimeLayouts, the following sequences are accepted:

`h`         - hour of the day 1-12
`hh`        - hour of the day 01-12
`t`         - twenty four hour of the day 0-23
`tt`        - twenty four hour of the day 00-23
`m`         - minute 0-59
`mm`        - minute 00-59
`s`         - second 0-59
`ss`        - second 00-59
`fff`       - milliseconds
`ffffff`    - microseconds
`fffffffff` - nanoseconds
`aa`        - am or pm (localized using given locale)
`AA`        - AM or PM (localized using given locale)

If type is DateTimeLayouts, the following sequences are accepted:

`Z`         - hour and minute offset from UTC, or Z for UTC
`ZZZ`       - hour and minute offset from UTC

The following chars are allowed and ignored: ' ', ':', ',', 'T', '-', '_', '/'

func FormatISO

func FormatISO(date time.Time) string

DateTimeToISO converts the passed in time.Time to a string in full ISO8601 format

func MonthsBetween

func MonthsBetween(date1 time.Time, date2 time.Time) int

MonthsBetween returns the number of calendar months (an int) between the two dates. Note that if these are in different timezones then the local calendar day is used for each and the difference is calculated from that.

func Now

func Now() time.Time

Now returns the time now.. according to the current now function which can be switched out for testing.

func ParseDateTime added in v1.9.0

func ParseDateTime(layout string, value string, tz *time.Location) (time.Time, error)

ParseDate parses the given string into a datetime

func SetNowFunc added in v1.57.1

func SetNowFunc(source NowFunc)

SetNowFunc sets the current now function

func Since added in v1.22.4

func Since(t time.Time) time.Duration

Since returns the time elapsed since t

func ValidateFormat added in v1.9.0

func ValidateFormat(layout string, type_ LayoutType, mode LayoutMode) error

ValidateFormat parses a formatting layout string to validate it

Types

type Date

type Date struct {
	Year  int
	Month time.Month
	Day   int
}

Date is a local gregorian calendar date

func ExtractDate

func ExtractDate(dt time.Time) Date

ExtractDate extracts the date from the give datetime

func NewDate

func NewDate(year, month, day int) Date

NewDate creates a new date

func ParseDate

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

ParseDate parses the given string into a date

func (Date) Combine

func (d Date) Combine(tod TimeOfDay, tz *time.Location) time.Time

Combine combines this date and a time to make a datetime

func (Date) Compare

func (d Date) Compare(other Date) int

Compare compares this time of day to another

func (Date) Equal

func (d Date) Equal(other Date) bool

Equal determines equality for this type

func (Date) Format

func (d Date) Format(layout string, loc i18n.Locale) (string, error)

Format formats this date as a string using the given layout

func (*Date) Scan added in v1.20.0

func (d *Date) Scan(value any) error

Scan scans from the db value

func (Date) String

func (d Date) String() string

String returns the ISO8601 representation

func (Date) Value added in v1.20.0

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

Value returns the value used in db writes

func (Date) WeekNum

func (d Date) WeekNum() int

WeekNum returns the number of the week (1-54)

func (Date) Weekday

func (d Date) Weekday() time.Weekday

Weekday returns the day of the week

func (Date) YearDay

func (d Date) YearDay() int

YearDay returns the day of the year (1-366)

type LayoutMode added in v1.9.0

type LayoutMode int

LayoutMode describes what a layout is being used for

const (
	FormattingMode LayoutMode = 1
	ParsingMode    LayoutMode = 2
)

formatting mode constants

func (LayoutMode) String added in v1.9.0

func (m LayoutMode) String() string

String converts a layout mode to a string - used for error messages

type LayoutType added in v1.9.0

type LayoutType uint

LayoutType describes what layout sequences are permitted in a formatting operation

func (LayoutType) Includes added in v1.9.0

func (t LayoutType) Includes(seqType int) bool

Includes returns whether the given sequence type is included in this layout type

func (LayoutType) String added in v1.9.0

func (t LayoutType) String() string

String converts a layout type to a string - used for error messages

type NowFunc added in v1.57.1

type NowFunc func() time.Time

NowFunc is a function that can provide a now time

func NewFixedNow added in v1.57.1

func NewFixedNow(now time.Time) NowFunc

NewFixedNow creates a new fixed now func

func NewSequentialNow added in v1.57.1

func NewSequentialNow(start time.Time, step time.Duration) NowFunc

NewSequentialNow creates a new sequential time func

type TimeOfDay

type TimeOfDay struct {
	Hour   int
	Minute int
	Second int
	Nanos  int
}

TimeOfDay represents a local time of day value

func ExtractTimeOfDay

func ExtractTimeOfDay(dt time.Time) TimeOfDay

ExtractTimeOfDay extracts the time of day from the give datetime

func NewTimeOfDay

func NewTimeOfDay(hour, minute, second, nanos int) TimeOfDay

NewTimeOfDay creates a new time of day

func ParseTimeOfDay

func ParseTimeOfDay(layout string, value string) (TimeOfDay, error)

ParseTimeOfDay parses the given string into a time of day

func (TimeOfDay) Combine

func (t TimeOfDay) Combine(date Date, tz *time.Location) time.Time

Combine combines this time and a date to make a datetime

func (TimeOfDay) Compare

func (t TimeOfDay) Compare(other TimeOfDay) int

Compare compares this time of day to another

func (TimeOfDay) Equal

func (t TimeOfDay) Equal(other TimeOfDay) bool

Equal determines equality for this type

func (TimeOfDay) Format

func (t TimeOfDay) Format(layout string, loc i18n.Locale) (string, error)

Format formats this time of day as a string

func (TimeOfDay) String

func (t TimeOfDay) String() string

String returns the ISO8601 representation

type Translation added in v1.9.0

type Translation struct {
	Days        []string `json:"days"`
	ShortDays   []string `json:"short_days"`
	Months      []string `json:"months"`
	ShortMonths []string `json:"short_months"`
	AmPm        []string `json:"am_pm"`
}

func GetTranslation added in v1.9.0

func GetTranslation(loc i18n.Locale) *Translation

GetTranslation gets the best match translation for the given locale

Jump to

Keyboard shortcuts

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