datetime

package
v0.15.10 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: Apache-2.0, MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPrecision = 6
	MaxHours         = 838
)
View Source
const (
	// Unary interval types
	IntervalNone        IntervalType = 0
	IntervalMicrosecond IntervalType = 1 << 0
	IntervalSecond      IntervalType = 1 << 1
	IntervalMinute      IntervalType = 1 << 2
	IntervalHour        IntervalType = 1 << 3
	IntervalDay         IntervalType = 1 << 4
	IntervalMonth       IntervalType = 1 << 5
	IntervalYear        IntervalType = 1 << 6

	// IntervalWeek and IntervalQuarter are an exception for unary interval types,
	// which are not unique temporal elements but instead a modifier on a unary element
	// - WEEK is just a count of DAYS multiplied by 7
	// - QUARTER is just a count of MONTHS multiplied by 3
	IntervalWeek    = IntervalDay | intervalMulti
	IntervalQuarter = IntervalMonth | intervalMulti

	// Compound interval types
	IntervalSecondMicrosecond = IntervalSecond | IntervalMicrosecond
	IntervalMinuteMicrosecond = IntervalMinute | IntervalSecond | IntervalMicrosecond
	IntervalMinuteSecond      = IntervalMinute | IntervalSecond
	IntervalHourMicrosecond   = IntervalHour | IntervalMinute | IntervalSecond | IntervalMicrosecond
	IntervalHourSecond        = IntervalHour | IntervalMinute | IntervalSecond
	IntervalHourMinute        = IntervalHour | IntervalMinute
	IntervalDayMicrosecond    = IntervalDay | IntervalHour | IntervalMinute | IntervalSecond | IntervalMicrosecond
	IntervalDaySecond         = IntervalDay | IntervalHour | IntervalMinute | IntervalSecond
	IntervalDayMinute         = IntervalDay | IntervalHour | IntervalMinute
	IntervalDayHour           = IntervalDay | IntervalHour
	IntervalYearMonth         = IntervalYear | IntervalMonth
)

IntervalType constants.

View Source
const DefaultWeekMode = 0

Variables

View Source
var DateTime_YYMMDDhhmmss = &Strftime{
	pattern: "YYMMDDhhmmss",
	compiled: []Spec{
		fmtYearShort{},
		fmtMonth{true},
		fmtDay{},
		fmtHour24{true},
		fmtMin{true},
		fmtSecond{true, true},
	},
}
View Source
var DateTime_YYYYMMDDhhmmss = &Strftime{
	pattern: "YYYYMMDDhhmmss",
	compiled: []Spec{
		fmtYearLong{},
		fmtMonth{true},
		fmtDay{true},
		fmtHour24{true},
		fmtMin{true},
		fmtSecond{true, true},
	},
}
View Source
var DateTime_YYYY_MM_DD_hh_mm_ss = &Strftime{
	pattern: "YYYY-MM-DD hh:mm:ss",
	compiled: []Spec{
		fmtYearLong{},
		fmtSeparator('-'),
		fmtMonth{true},
		fmtSeparator('-'),
		fmtDay{true},
		fmtTimeSeparator{},
		fmtHour24{true},
		fmtSeparator(':'),
		fmtMin{true},
		fmtSeparator(':'),
		fmtSecond{true, true},
	},
}
View Source
var DateTime_YYYY_M_D_h_m_s = &Strftime{
	pattern: "YYYY-M-D h:m:s",
	compiled: []Spec{
		fmtYearLong{},
		fmtSeparator('-'),
		fmtMonth{false},
		fmtSeparator('-'),
		fmtDay{false},
		fmtTimeSeparator{},
		fmtHour24{false},
		fmtSeparator(':'),
		fmtMin{false},
		fmtSeparator(':'),
		fmtSecond{false, true},
	},
}
View Source
var DateTime_YY_M_D_h_m_s = &Strftime{
	pattern: "YY-M-D h:m:s",
	compiled: []Spec{
		fmtYearShort{},
		fmtSeparator('-'),
		fmtMonth{false},
		fmtSeparator('-'),
		fmtDay{false},
		fmtTimeSeparator{},
		fmtHour24{false},
		fmtSeparator(':'),
		fmtMin{false},
		fmtSeparator(':'),
		fmtSecond{false, true},
	},
}
View Source
var Date_YYMMDD = &Strftime{
	pattern: "YYMMDD",
	compiled: []Spec{
		fmtYearShort{},
		fmtMonth{true},
		fmtDay{true},
	},
}
View Source
var Date_YYYYMMDD = &Strftime{
	pattern: "YYYYMMDD",
	compiled: []Spec{
		fmtYearLong{},
		fmtMonth{true},
		fmtDay{true},
	},
}
View Source
var Date_YYYY_MM_DD = &Strftime{
	pattern: "YYYY-MM-DD",
	compiled: []Spec{
		fmtYearLong{},
		fmtSeparator('-'),
		fmtMonth{true},
		fmtSeparator('-'),
		fmtDay{true},
	},
}
View Source
var Date_YYYY_M_D = &Strftime{
	pattern: "YYYY-M-D",
	compiled: []Spec{
		fmtYearLong{},
		fmtSeparator('-'),
		fmtMonth{false},
		fmtSeparator('-'),
		fmtDay{false},
	},
}
View Source
var Date_YY_M_D = &Strftime{
	pattern: "YY-M-D",
	compiled: []Spec{
		fmtYearShort{},
		fmtSeparator('-'),
		fmtMonth{false},
		fmtSeparator('-'),
		fmtDay{false},
	},
}
View Source
var DefaultMySQLStrftime = map[byte]Spec{
	'a': fmtWeekdayNameShort{},
	'b': fmtMonthNameShort{},
	'c': fmtMonth{false},
	'D': fmtMonthDaySuffix{},
	'd': fmtDay{true},
	'e': fmtDay{false},
	'f': fmtMicroseconds{},
	'H': fmtHour24{true},
	'h': fmtHour12{true},
	'I': fmtHour12{true},
	'i': fmtMin{true},
	'j': fmtZeroYearDay{},
	'k': fmtHour24{false},
	'l': fmtHour12{false},
	'M': fmtMonthName{},
	'm': fmtMonth{true},
	'p': fmtAMorPM{},
	'r': fmtFullTime12{},
	'S': fmtSecond{true, false},
	's': fmtSecond{true, false},
	'T': fmtFullTime24{},
	'U': fmtWeek0{},
	'u': fmtWeek1{},
	'V': fmtWeek2{},
	'v': fmtWeek3{},
	'W': fmtWeekdayName{},
	'w': fmtWeekday{},
	'X': fmtYearForWeek2{},
	'x': fmtYearForWeek3{},
	'Y': fmtYearLong{},
	'y': fmtYearShort{},
}
View Source
var Time_hh_mm_ss = &Strftime{
	pattern: "hh:mm:ss",
	compiled: []Spec{
		fmtHour24{true},
		fmtSeparator(':'),
		fmtMin{true},
		fmtSeparator(':'),
		fmtSecond{true, true},
	},
}
View Source
var Time_hhmmss = &Strftime{
	pattern: "hhmmss",
	compiled: []Spec{
		fmtHour24{true},
		fmtMin{true},
		fmtSecond{true, true},
	},
}

Functions

func Format

func Format(p string, t DateTime, prec uint8) ([]byte, error)

Format takes the format `p` and the time `t` to produce the format date/time. Note that this function re-compiles the pattern every time it is called.

If you know beforehand that you will be reusing the pattern within your application, consider creating a `Strftime` object and reusing it.

func MysqlDayNumber

func MysqlDayNumber(year, month, day int) int

MysqlDayNumber converts a date into an absolute day number. This is an algorithm that has been reverse engineered from MySQL; the tables used as a reference can be found in `testdata/year_to_daynr.json`. It is worth noting that this absolute day number does not match the day count that traditional datetime systems use (e.g. the daycount algorithm in the Go standard library). It is often off by one, possibly because of incorrect leap year handling, but the inverse of the algorithm in mysqlDateFromDayNumber takes this into account. Hence, the results of this function can ONLY be passed to mysqlDateFromDayNumber; using a day number with one of Go's datetime APIs will return incorrect results. This API should only be used when performing datetime calculations (addition and subtraction), so that the results match MySQL's. All other date handling operations must use our helpers based on Go's standard library.

func ParseTime

func ParseTime(in string, prec int) (t Time, l int, state TimeState)

func ParseTimeZone

func ParseTimeZone(tz string) (*time.Location, error)

func SizeFromString

func SizeFromString(s string) int32

SizeAndScaleFromString

Types

type Date

type Date struct {
	// contains filtered or unexported fields
}

func DateFromDayNumber

func DateFromDayNumber(daynr int) Date

DateFromDayNumber converts an absolute day number into a Date. Returns zero date if day number exceeds 3652499 or is less than 366.

func NewDateFromStd

func NewDateFromStd(t time.Time) Date

func ParseDate

func ParseDate(s string) (Date, bool)

func ParseDateDecimal

func ParseDateDecimal(d decimal.Decimal) (Date, bool)

func ParseDateFloat

func ParseDateFloat(f float64) (Date, bool)

func ParseDateInt64

func ParseDateInt64(i int64) (d Date, ok bool)

func (Date) AddInterval

func (d Date) AddInterval(itv *Interval) (Date, bool)

func (Date) Compare

func (d Date) Compare(d2 Date) int

func (Date) Day

func (d Date) Day() int

func (Date) Format

func (d Date) Format() []byte

func (Date) FormatInt64

func (d Date) FormatInt64() int64

func (Date) Hash

func (d Date) Hash(h *vthash.Hasher)

func (Date) ISOWeek

func (d Date) ISOWeek() (int, int)

func (Date) IsZero

func (d Date) IsZero() bool

func (Date) MondayWeek

func (d Date) MondayWeek() (int, int)

MondayWeek returns the year and week number of the current date, when week numbers are defined by starting on the first Monday of the year.

func (Date) Month

func (d Date) Month() int

func (Date) Quarter

func (d Date) Quarter() int

func (Date) Sunday4DayWeek

func (d Date) Sunday4DayWeek() (int, int)

Sunday4DayWeek returns the year and week number of the current date, when week numbers are defined by starting on the Sunday where week 1 is defined as having at least 4 days in the new year.

func (Date) SundayWeek

func (d Date) SundayWeek() (int, int)

SundayWeek returns the year and week number of the current date, when week numbers are defined by starting on the first Sunday of the year.

func (Date) ToStdTime

func (d Date) ToStdTime(loc *time.Location) (out time.Time)

func (Date) Week

func (d Date) Week(mode int) int

func (Date) Weekday

func (d Date) Weekday() time.Weekday

func (Date) Year

func (d Date) Year() int

func (Date) YearWeek

func (d Date) YearWeek(mode int) int

func (Date) Yearday

func (d Date) Yearday() int

type DateTime

type DateTime struct {
	Date Date
	Time Time
}

func NewDateTimeFromStd

func NewDateTimeFromStd(t time.Time) DateTime

func ParseDateTime

func ParseDateTime(s string, l int) (DateTime, int, bool)

func ParseDateTimeDecimal

func ParseDateTimeDecimal(d decimal.Decimal, l int32, prec int) (DateTime, int, bool)

func ParseDateTimeFloat

func ParseDateTimeFloat(f float64, prec int) (DateTime, int, bool)

func ParseDateTimeInt64

func ParseDateTimeInt64(i int64) (dt DateTime, ok bool)

func (DateTime) AddInterval

func (dt DateTime) AddInterval(itv *Interval, prec uint8, stradd bool) (DateTime, uint8, bool)

func (DateTime) Compare

func (dt DateTime) Compare(dt2 DateTime) int

func (DateTime) Format

func (dt DateTime) Format(prec uint8) []byte

func (DateTime) FormatDecimal

func (dt DateTime) FormatDecimal() decimal.Decimal

func (DateTime) FormatFloat64

func (dt DateTime) FormatFloat64() float64

func (DateTime) FormatInt64

func (dt DateTime) FormatInt64() int64

func (DateTime) Hash

func (dt DateTime) Hash(h *vthash.Hasher)

func (DateTime) IsZero

func (dt DateTime) IsZero() bool

func (DateTime) Round

func (dt DateTime) Round(p int) (r DateTime)

func (DateTime) ToSeconds

func (dt DateTime) ToSeconds() int64

func (DateTime) ToStdTime

func (dt DateTime) ToStdTime(now time.Time) time.Time

func (DateTime) WeightString

func (dt DateTime) WeightString(dst []byte) []byte

type Interval

type Interval struct {
	// contains filtered or unexported fields
}

func ParseInterval

func ParseInterval(itv string, unit IntervalType, negate bool) *Interval

func ParseIntervalDecimal

func ParseIntervalDecimal(dec decimal.Decimal, mysqlPrec int32, unit IntervalType, negate bool) *Interval

func ParseIntervalFloat

func ParseIntervalFloat(f float64, unit IntervalType, negate bool) *Interval

func ParseIntervalInt64

func ParseIntervalInt64(itv int64, unit IntervalType, negate bool) *Interval

func (*Interval) Unit

func (itv *Interval) Unit() IntervalType

type IntervalType

type IntervalType uint8

IntervalType represents the temporal elements contained in an Interval. Intervals in MySQL can contain more than one temporal element. We define their types as a bitset to let us efficiently query the temporal elements that form each interval. There are two kinds of IntervalTypes: unary and compound. Unary interval types contain a single temporal element (e.g. SECONDS, or DAYS) and hence contain only one bit set. Compount interval types are the logical combination of several unary interval types.

func (IntervalType) HasDateParts

func (itv IntervalType) HasDateParts() bool

func (IntervalType) HasDayParts

func (itv IntervalType) HasDayParts() bool

func (IntervalType) HasMonthParts

func (itv IntervalType) HasMonthParts() bool

func (IntervalType) HasTimeParts

func (itv IntervalType) HasTimeParts() bool

func (IntervalType) NeedsPrecision

func (itv IntervalType) NeedsPrecision() bool

func (IntervalType) PartCount

func (itv IntervalType) PartCount() int

func (IntervalType) ToString

func (itv IntervalType) ToString() string

ToString returns the type as a string

type Spec

type Spec interface {
	// contains filtered or unexported methods
}

type Strftime

type Strftime struct {
	// contains filtered or unexported fields
}

Strftime is the object that represents a compiled strftime pattern

func New

func New(p string) (*Strftime, error)

New creates a new Strftime object. If the compilation fails, then an error is returned in the second argument.

func (*Strftime) AppendFormat

func (f *Strftime) AppendFormat(dst []byte, t DateTime, prec uint8) []byte

func (*Strftime) Format

func (f *Strftime) Format(dt DateTime, prec uint8) []byte

func (*Strftime) FormatNumeric

func (f *Strftime) FormatNumeric(t DateTime) (n int64)

func (*Strftime) FormatString

func (f *Strftime) FormatString(t DateTime, prec uint8) string

func (*Strftime) Parse

func (f *Strftime) Parse(s string, prec int) (DateTime, int, bool)

func (*Strftime) Pattern

func (f *Strftime) Pattern() string

Pattern returns the original pattern string

type Time

type Time struct {
	// contains filtered or unexported fields
}

func NewTimeFromSeconds

func NewTimeFromSeconds(seconds decimal.Decimal) Time

func NewTimeFromStd

func NewTimeFromStd(t time.Time) Time

func ParseTimeDecimal

func ParseTimeDecimal(d decimal.Decimal, l int32, prec int) (Time, int, bool)

func ParseTimeFloat

func ParseTimeFloat(f float64, prec int) (Time, int, bool)

func ParseTimeInt64

func ParseTimeInt64(i int64) (t Time, ok bool)

func (Time) AddInterval

func (t Time) AddInterval(itv *Interval, stradd bool) (Time, uint8, bool)

func (Time) AppendFormat

func (t Time) AppendFormat(b []byte, prec uint8) []byte

func (Time) Compare

func (t Time) Compare(t2 Time) int

func (Time) Format

func (t Time) Format(prec uint8) []byte

func (Time) FormatDecimal

func (t Time) FormatDecimal() decimal.Decimal

func (Time) FormatFloat64

func (t Time) FormatFloat64() (n float64)

func (Time) FormatInt64

func (t Time) FormatInt64() (n int64)

func (Time) Hash

func (t Time) Hash(h *vthash.Hasher)

func (Time) Hour

func (t Time) Hour() int

func (Time) IsZero

func (t Time) IsZero() bool

func (Time) Minute

func (t Time) Minute() int

func (Time) Nanosecond

func (t Time) Nanosecond() int

func (Time) Neg

func (t Time) Neg() bool

func (Time) Round

func (t Time) Round(p int) (r Time)

func (Time) RoundForJSON

func (t Time) RoundForJSON() Time

RoundForJSON rounds the time to the nearest 32nd hour. This is some really weird behavior that MySQL does when it casts a JSON time back to a MySQL TIME value. We just mimic the behavior here.

func (Time) Second

func (t Time) Second() int

func (Time) ToDateTime

func (t Time) ToDateTime(now time.Time) (out DateTime)

func (Time) ToDuration

func (t Time) ToDuration() time.Duration

func (Time) ToSeconds

func (t Time) ToSeconds() int64

func (Time) ToStdTime

func (t Time) ToStdTime(now time.Time) (out time.Time)

type TimeState

type TimeState uint8
const (
	// TimeOK indicates that the parsed value is valid and complete.
	TimeOK TimeState = iota
	// TimePartial indicates that the parsed value has a partially parsed value
	// but it is not fully complete and valid. There could be additional stray
	// data in the input, or it has an overflow.
	TimePartial
	// TimeInvalid indicates that the parsed value is invalid and no partial
	// TIME value could be extracted from the input.
	TimeInvalid
)

Jump to

Keyboard shortcuts

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