datetime

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidDateFormat indicates date format is invalid.
	ErrInvalidDateFormat = errors.New("invalid date format")
	// ErrInvalidDateTimeFormat indicates datetime format is invalid.
	ErrInvalidDateTimeFormat = errors.New("invalid datetime format")
	// ErrInvalidTimeFormat indicates time format is invalid.
	ErrInvalidTimeFormat = errors.New("invalid time format")
	// ErrFailedScan indicates scan target type/value is invalid.
	ErrFailedScan = errors.New("failed to scan value")
	// ErrUnsupportedDestType indicates dest type is unsupported.
	ErrUnsupportedDestType = errors.New("unsupported destination type")
	// ErrInvalidJSONFormat indicates invalid JSON length/quotes for datetime types.
	ErrInvalidJSONFormat = errors.New("invalid JSON format: expected quoted value of specific length")
)

Functions

This section is empty.

Types

type Date

type Date time.Time

Date represents a date value (without time) with database and JSON support. It uses the standard time.DateOnly format (2006-01-02).

func DateOf

func DateOf(t time.Time) Date

DateOf returns the date of the given time (time components are zeroed).

func NowDate

func NowDate() Date

NowDate returns the current date in the local timezone.

func ParseDate

func ParseDate(value string, pattern ...string) (Date, error)

ParseDate parses a date string and returns a Date. First tries with the provided pattern, then falls back to cast.ToTime as a backup.

func (Date) AddDays

func (d Date) AddDays(days int) Date

AddDays adds the specified number of days to the date.

func (Date) AddMonths

func (d Date) AddMonths(months int) Date

AddMonths adds the specified number of months to the date.

func (Date) AddYears

func (d Date) AddYears(years int) Date

AddYears adds the specified number of years to the date.

func (Date) After

func (d Date) After(other Date) bool

After reports whether the date d is after other.

func (Date) Before

func (d Date) Before(other Date) bool

Before reports whether the date d is before other.

func (Date) BeginOfDay

func (d Date) BeginOfDay() Date

BeginOfDay returns the beginning of the day for d (same as the date itself).

func (Date) BeginOfMonth

func (d Date) BeginOfMonth() Date

BeginOfMonth returns the beginning of the month for d.

func (Date) BeginOfQuarter

func (d Date) BeginOfQuarter() Date

BeginOfQuarter returns the beginning of the quarter for d.

func (Date) BeginOfWeek

func (d Date) BeginOfWeek() Date

BeginOfWeek returns the beginning of the week (Sunday) for d.

func (Date) BeginOfYear

func (d Date) BeginOfYear() Date

BeginOfYear returns the beginning of the year for d.

func (Date) Between

func (d Date) Between(start, end Date) bool

Between reports whether d is between start and end.

func (Date) Day

func (d Date) Day() int

Day returns the day of the month specified by d.

func (Date) EndOfDay

func (d Date) EndOfDay() Date

EndOfDay returns the end of the day for d (same as the date itself).

func (Date) EndOfMonth

func (d Date) EndOfMonth() Date

EndOfMonth returns the end of the month for d.

func (Date) EndOfQuarter

func (d Date) EndOfQuarter() Date

EndOfQuarter returns the end of the quarter for d.

func (Date) EndOfWeek

func (d Date) EndOfWeek() Date

EndOfWeek returns the end of the week (Saturday) for d.

func (Date) EndOfYear

func (d Date) EndOfYear() Date

EndOfYear returns the end of the year for d.

func (Date) Equal

func (d Date) Equal(other Date) bool

Equal compares two Date values for equality.

func (Date) Format

func (d Date) Format(layout string) string

Format returns the string representation using the provided layout.

func (Date) Friday

func (d Date) Friday() Date

Friday returns the Friday of the week containing d.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether d represents the zero time instant, January 1, year 1.

func (Date) Location

func (d Date) Location() *time.Location

Location returns the time zone information associated with d.

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for JSON serialization.

func (Date) MarshalText

func (d Date) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Date) Monday

func (d Date) Monday() Date

Monday returns the Monday of the week containing d.

func (Date) Month

func (d Date) Month() time.Month

Month returns the month of the year specified by d.

func (Date) Saturday

func (d Date) Saturday() Date

Saturday returns the Saturday of the week containing d.

func (*Date) Scan

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

Scan implements the sql.Scanner interface for database compatibility.

func (Date) String

func (d Date) String() string

String returns the string representation using the standard DateOnly layout.

func (Date) Sunday

func (d Date) Sunday() Date

Sunday returns the Sunday of the week containing d.

func (Date) Thursday

func (d Date) Thursday() Date

Thursday returns the Thursday of the week containing d.

func (Date) Tuesday

func (d Date) Tuesday() Date

Tuesday returns the Tuesday of the week containing d.

func (Date) Unix

func (d Date) Unix() int64

Unix returns d as a Unix time, the number of seconds elapsed since January 1, 1970 UTC.

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(bs []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for JSON deserialization.

func (*Date) UnmarshalText

func (d *Date) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Date) Unwrap

func (d Date) Unwrap() time.Time

Unwrap returns the underlying time.Time value.

func (Date) Value

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

Value implements the driver.Valuer interface for database compatibility.

func (Date) Wednesday

func (d Date) Wednesday() Date

Wednesday returns the Wednesday of the week containing d.

func (Date) Weekday

func (d Date) Weekday() time.Weekday

Weekday returns the day of the week specified by d.

func (Date) Year

func (d Date) Year() int

Year returns the year in which d occurs.

func (Date) YearDay

func (d Date) YearDay() int

YearDay returns the day of the year specified by d, in the range [1,365] for non-leap years, and [1,366] in leap years.

type DateTime

type DateTime time.Time

DateTime represents a date and time value with database and JSON support. It uses the standard time.DateTime format (2006-01-02 15:04:05).

func FromUnix

func FromUnix(sec, nsec int64) DateTime

FromUnix returns the DateTime corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC.

func FromUnixMicro

func FromUnixMicro(usec int64) DateTime

FromUnixMicro returns the DateTime corresponding to the given Unix time, usec microseconds since January 1, 1970 UTC.

func FromUnixMilli

func FromUnixMilli(msec int64) DateTime

FromUnixMilli returns the DateTime corresponding to the given Unix time, msec milliseconds since January 1, 1970 UTC.

func Now

func Now() DateTime

Now returns the current date time in the local timezone.

func Of

func Of(t time.Time) DateTime

Of converts a time.Time to DateTime.

func Parse

func Parse(value string, pattern ...string) (DateTime, error)

Parse parses a date time string and returns a DateTime. First tries with the provided pattern, then falls back to cast.ToTime as a backup.

func (DateTime) Add

func (dt DateTime) Add(d time.Duration) DateTime

Add returns the datetime dt+d.

func (DateTime) AddDate

func (dt DateTime) AddDate(years, months, days int) DateTime

AddDate returns the datetime corresponding to adding the given number of years, months, and days to dt.

func (DateTime) AddDays

func (dt DateTime) AddDays(days int) DateTime

AddDays adds the specified number of days to the datetime.

func (DateTime) AddHours

func (dt DateTime) AddHours(hours int) DateTime

AddHours adds the specified number of hours to the datetime.

func (DateTime) AddMinutes

func (dt DateTime) AddMinutes(minutes int) DateTime

AddMinutes adds the specified number of minutes to the datetime.

func (DateTime) AddMonths

func (dt DateTime) AddMonths(months int) DateTime

AddMonths adds the specified number of months to the datetime.

func (DateTime) AddSeconds

func (dt DateTime) AddSeconds(seconds int) DateTime

AddSeconds adds the specified number of seconds to the datetime.

func (DateTime) AddYears

func (dt DateTime) AddYears(years int) DateTime

AddYears adds the specified number of years to the datetime.

func (DateTime) After

func (dt DateTime) After(other DateTime) bool

After reports whether the datetime dt is after other.

func (DateTime) Before

func (dt DateTime) Before(other DateTime) bool

Before reports whether the datetime dt is before other.

func (DateTime) BeginOfDay

func (dt DateTime) BeginOfDay() DateTime

BeginOfDay returns the beginning of the day for dt.

func (DateTime) BeginOfHour

func (dt DateTime) BeginOfHour() DateTime

BeginOfHour returns the beginning of the hour for dt.

func (DateTime) BeginOfMinute

func (dt DateTime) BeginOfMinute() DateTime

BeginOfMinute returns the beginning of the minute for dt.

func (DateTime) BeginOfMonth

func (dt DateTime) BeginOfMonth() DateTime

BeginOfMonth returns the beginning of the month for dt.

func (DateTime) BeginOfQuarter

func (dt DateTime) BeginOfQuarter() DateTime

BeginOfQuarter returns the beginning of the quarter for dt.

func (DateTime) BeginOfWeek

func (dt DateTime) BeginOfWeek() DateTime

BeginOfWeek returns the beginning of the week (Sunday) for dt.

func (DateTime) BeginOfYear

func (dt DateTime) BeginOfYear() DateTime

BeginOfYear returns the beginning of the year for dt.

func (DateTime) Between

func (dt DateTime) Between(start, end DateTime) bool

Between reports whether dt is between start and end.

func (DateTime) Day

func (dt DateTime) Day() int

Day returns the day of the month specified by dt.

func (DateTime) EndOfDay

func (dt DateTime) EndOfDay() DateTime

EndOfDay returns the end of the day for dt.

func (DateTime) EndOfHour

func (dt DateTime) EndOfHour() DateTime

EndOfHour returns the end of the hour for dt.

func (DateTime) EndOfMinute

func (dt DateTime) EndOfMinute() DateTime

EndOfMinute returns the end of the minute for dt.

func (DateTime) EndOfMonth

func (dt DateTime) EndOfMonth() DateTime

EndOfMonth returns the end of the month for dt.

func (DateTime) EndOfQuarter

func (dt DateTime) EndOfQuarter() DateTime

EndOfQuarter returns the end of the quarter for dt.

func (DateTime) EndOfWeek

func (dt DateTime) EndOfWeek() DateTime

EndOfWeek returns the end of the week (Saturday) for dt.

func (DateTime) EndOfYear

func (dt DateTime) EndOfYear() DateTime

EndOfYear returns the end of the year for dt.

func (DateTime) Equal

func (dt DateTime) Equal(other DateTime) bool

Equal compares two DateTime values for equality.

func (DateTime) Format

func (dt DateTime) Format(layout string) string

Format returns the string representation using the provided layout.

func (DateTime) Friday

func (dt DateTime) Friday() DateTime

Friday returns the Friday of the week containing dt.

func (DateTime) Hour

func (dt DateTime) Hour() int

Hour returns the hour within the day specified by dt, in the range [0, 23].

func (DateTime) IsZero

func (dt DateTime) IsZero() bool

IsZero reports whether dt represents the zero time instant, January 1, year 1, 00:00:00 UTC.

func (DateTime) Location

func (dt DateTime) Location() *time.Location

Location returns the time zone information associated with dt.

func (DateTime) MarshalJSON

func (dt DateTime) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for JSON serialization.

func (DateTime) MarshalText

func (dt DateTime) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (DateTime) Minute

func (dt DateTime) Minute() int

Minute returns the minute offset within the hour specified by dt, in the range [0, 59].

func (DateTime) Monday

func (dt DateTime) Monday() DateTime

Monday returns the Monday of the week containing dt.

func (DateTime) Month

func (dt DateTime) Month() time.Month

Month returns the month of the year specified by dt.

func (DateTime) Nanosecond

func (dt DateTime) Nanosecond() int

Nanosecond returns the nanosecond offset within the second specified by dt, in the range [0, 999999999].

func (DateTime) Saturday

func (dt DateTime) Saturday() DateTime

Saturday returns the Saturday of the week containing dt.

func (*DateTime) Scan

func (dt *DateTime) Scan(src any) error

Scan implements the sql.Scanner interface for database compatibility.

func (DateTime) Second

func (dt DateTime) Second() int

Second returns the second offset within the minute specified by dt, in the range [0, 59].

func (DateTime) String

func (dt DateTime) String() string

String returns the string representation using the standard DateTime layout.

func (DateTime) Sunday

func (dt DateTime) Sunday() DateTime

Sunday returns the Sunday of the week containing dt.

func (DateTime) Thursday

func (dt DateTime) Thursday() DateTime

Thursday returns the Thursday of the week containing dt.

func (DateTime) Tuesday

func (dt DateTime) Tuesday() DateTime

Tuesday returns the Tuesday of the week containing dt.

func (DateTime) Unix

func (dt DateTime) Unix() int64

Unix returns dt as a Unix time, the number of seconds elapsed since January 1, 1970 UTC.

func (DateTime) UnixMicro

func (dt DateTime) UnixMicro() int64

UnixMicro returns dt as a Unix time, the number of microseconds elapsed since January 1, 1970 UTC.

func (DateTime) UnixMilli

func (dt DateTime) UnixMilli() int64

UnixMilli returns dt as a Unix time, the number of milliseconds elapsed since January 1, 1970 UTC.

func (DateTime) UnixNano

func (dt DateTime) UnixNano() int64

UnixNano returns dt as a Unix time, the number of nanoseconds elapsed since January 1, 1970 UTC.

func (*DateTime) UnmarshalJSON

func (dt *DateTime) UnmarshalJSON(bs []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for JSON deserialization.

func (*DateTime) UnmarshalText

func (dt *DateTime) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (DateTime) Unwrap

func (dt DateTime) Unwrap() time.Time

Unwrap returns the underlying time.Time value.

func (DateTime) Value

func (dt DateTime) Value() (driver.Value, error)

Value implements the driver.Valuer interface for database compatibility.

func (DateTime) Wednesday

func (dt DateTime) Wednesday() DateTime

Wednesday returns the Wednesday of the week containing dt.

func (DateTime) Weekday

func (dt DateTime) Weekday() time.Weekday

Weekday returns the day of the week specified by dt.

func (DateTime) Year

func (dt DateTime) Year() int

Year returns the year in which dt occurs.

func (DateTime) YearDay

func (dt DateTime) YearDay() int

YearDay returns the day of the year specified by dt, in the range [1,365] for non-leap years, and [1,366] in leap years.

type Time

type Time time.Time

Time represents a time value (without date) with database and JSON support. It uses the standard time.TimeOnly format (15:04:05).

func NowTime

func NowTime() Time

NowTime returns the current time in the local timezone.

func ParseTime

func ParseTime(value string, pattern ...string) (Time, error)

ParseTime parses a time string and returns a Time. First tries with the provided pattern, then falls back to cast.ToTime as a backup.

func TimeOf

func TimeOf(t time.Time) Time

TimeOf returns the time of the given time (date components are set to epoch).

func (Time) Add

func (t Time) Add(d time.Duration) Time

Add returns the time t+d.

func (Time) AddHours

func (t Time) AddHours(hours int) Time

AddHours adds the specified number of hours to the time.

func (Time) AddMicroseconds

func (t Time) AddMicroseconds(microseconds int64) Time

AddMicroseconds adds the specified number of microseconds to the time.

func (Time) AddMilliseconds

func (t Time) AddMilliseconds(milliseconds int64) Time

AddMilliseconds adds the specified number of milliseconds to the time.

func (Time) AddMinutes

func (t Time) AddMinutes(minutes int) Time

AddMinutes adds the specified number of minutes to the time.

func (Time) AddNanoseconds

func (t Time) AddNanoseconds(nanoseconds int64) Time

AddNanoseconds adds the specified number of nanoseconds to the time.

func (Time) AddSeconds

func (t Time) AddSeconds(seconds int) Time

AddSeconds adds the specified number of seconds to the time.

func (Time) After

func (t Time) After(other Time) bool

After reports whether the time t is after other.

func (Time) Before

func (t Time) Before(other Time) bool

Before reports whether the time t is before other.

func (Time) BeginOfHour

func (t Time) BeginOfHour() Time

BeginOfHour returns the beginning of the hour for t.

func (Time) BeginOfMinute

func (t Time) BeginOfMinute() Time

BeginOfMinute returns the beginning of the minute for t.

func (Time) Between

func (t Time) Between(start, end Time) bool

Between reports whether t is between start and end.

func (Time) EndOfHour

func (t Time) EndOfHour() Time

EndOfHour returns the end of the hour for t.

func (Time) EndOfMinute

func (t Time) EndOfMinute() Time

EndOfMinute returns the end of the minute for t.

func (Time) Equal

func (t Time) Equal(other Time) bool

Equal compares two Time values for equality.

func (Time) Format

func (t Time) Format(layout string) string

Format returns the string representation using the provided layout.

func (Time) Hour

func (t Time) Hour() int

Hour returns the hour within the day specified by t, in the range [0, 23].

func (Time) IsZero

func (t Time) IsZero() bool

IsZero reports whether t represents the zero time instant.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for JSON serialization.

func (Time) MarshalText

func (t Time) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Time) Minute

func (t Time) Minute() int

Minute returns the minute offset within the hour specified by t, in the range [0, 59].

func (Time) Nanosecond

func (t Time) Nanosecond() int

Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].

func (*Time) Scan

func (t *Time) Scan(src any) error

Scan implements the sql.Scanner interface for database compatibility.

func (Time) Second

func (t Time) Second() int

Second returns the second offset within the minute specified by t, in the range [0, 59].

func (Time) String

func (t Time) String() string

String returns the string representation using the standard TimeOnly layout.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(bs []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for JSON deserialization.

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Time) Unwrap

func (t Time) Unwrap() time.Time

Unwrap returns the underlying time.Time value.

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements the driver.Valuer interface for database compatibility.

Jump to

Keyboard shortcuts

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