Documentation ¶
Index ¶
- Constants
- Variables
- func Format(p string, t DateTime, prec uint8) ([]byte, error)
- func MysqlDayNumber(year, month, day int) int
- func ParseTime(in string, prec int) (t Time, l int, state TimeState)
- func ParseTimeZone(tz string) (*time.Location, error)
- func SizeFromString(s string) int32
- type Date
- func (d Date) AddInterval(itv *Interval) (Date, bool)
- func (d Date) Compare(d2 Date) int
- func (d Date) Day() int
- func (d Date) Format() []byte
- func (d Date) FormatInt64() int64
- func (d Date) Hash(h *vthash.Hasher)
- func (d Date) ISOWeek() (int, int)
- func (d Date) IsZero() bool
- func (d Date) MondayWeek() (int, int)
- func (d Date) Month() int
- func (d Date) Quarter() int
- func (d Date) Sunday4DayWeek() (int, int)
- func (d Date) SundayWeek() (int, int)
- func (d Date) ToStdTime(loc *time.Location) (out time.Time)
- func (d Date) Week(mode int) int
- func (d Date) Weekday() time.Weekday
- func (d Date) Year() int
- func (d Date) YearWeek(mode int) int
- func (d Date) Yearday() int
- type DateTime
- func NewDateTimeFromStd(t time.Time) DateTime
- func ParseDateTime(s string, l int) (DateTime, int, bool)
- func ParseDateTimeDecimal(d decimal.Decimal, l int32, prec int) (DateTime, int, bool)
- func ParseDateTimeFloat(f float64, prec int) (DateTime, int, bool)
- func ParseDateTimeInt64(i int64) (dt DateTime, ok bool)
- func (dt DateTime) AddInterval(itv *Interval, prec uint8, stradd bool) (DateTime, uint8, bool)
- func (dt DateTime) Compare(dt2 DateTime) int
- func (dt DateTime) Format(prec uint8) []byte
- func (dt DateTime) FormatDecimal() decimal.Decimal
- func (dt DateTime) FormatFloat64() float64
- func (dt DateTime) FormatInt64() int64
- func (dt DateTime) Hash(h *vthash.Hasher)
- func (dt DateTime) IsZero() bool
- func (dt DateTime) Round(p int) (r DateTime)
- func (dt DateTime) ToSeconds() int64
- func (dt DateTime) ToStdTime(now time.Time) time.Time
- func (dt DateTime) WeightString(dst []byte) []byte
- type Interval
- func ParseInterval(itv string, unit IntervalType, negate bool) *Interval
- func ParseIntervalDecimal(dec decimal.Decimal, mysqlPrec int32, unit IntervalType, negate bool) *Interval
- func ParseIntervalFloat(f float64, unit IntervalType, negate bool) *Interval
- func ParseIntervalInt64(itv int64, unit IntervalType, negate bool) *Interval
- type IntervalType
- func (itv IntervalType) HasDateParts() bool
- func (itv IntervalType) HasDayParts() bool
- func (itv IntervalType) HasMonthParts() bool
- func (itv IntervalType) HasTimeParts() bool
- func (itv IntervalType) NeedsPrecision() bool
- func (itv IntervalType) PartCount() int
- func (itv IntervalType) ToString() string
- type Spec
- type Strftime
- func (f *Strftime) AppendFormat(dst []byte, t DateTime, prec uint8) []byte
- func (f *Strftime) Format(dt DateTime, prec uint8) []byte
- func (f *Strftime) FormatNumeric(t DateTime) (n int64)
- func (f *Strftime) FormatString(t DateTime, prec uint8) string
- func (f *Strftime) Parse(s string, prec int) (DateTime, int, bool)
- func (f *Strftime) Pattern() string
- type Time
- func (t Time) AddInterval(itv *Interval, stradd bool) (Time, uint8, bool)
- func (t Time) AppendFormat(b []byte, prec uint8) []byte
- func (t Time) Compare(t2 Time) int
- func (t Time) Format(prec uint8) []byte
- func (t Time) FormatDecimal() decimal.Decimal
- func (t Time) FormatFloat64() (n float64)
- func (t Time) FormatInt64() (n int64)
- func (t Time) Hash(h *vthash.Hasher)
- func (t Time) Hour() int
- func (t Time) IsZero() bool
- func (t Time) Minute() int
- func (t Time) Nanosecond() int
- func (t Time) Neg() bool
- func (t Time) Round(p int) (r Time)
- func (t Time) RoundForJSON() Time
- func (t Time) Second() int
- func (t Time) ToDateTime(now time.Time) (out DateTime)
- func (t Time) ToDuration() time.Duration
- func (t Time) ToSeconds() int64
- func (t Time) ToStdTime(now time.Time) (out time.Time)
- type TimeState
Constants ¶
const ( DefaultPrecision = 6 MaxHours = 838 )
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.
const DefaultWeekMode = 0
Variables ¶
var DateTime_YYMMDDhhmmss = &Strftime{ pattern: "YYMMDDhhmmss", compiled: []Spec{ fmtYearShort{}, fmtMonth{true}, fmtDay{}, fmtHour24{true}, fmtMin{true}, fmtSecond{true, true}, }, }
var DateTime_YYYYMMDDhhmmss = &Strftime{ pattern: "YYYYMMDDhhmmss", compiled: []Spec{ fmtYearLong{}, fmtMonth{true}, fmtDay{true}, fmtHour24{true}, fmtMin{true}, fmtSecond{true, true}, }, }
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}, }, }
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}, }, }
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}, }, }
var Date_YYMMDD = &Strftime{ pattern: "YYMMDD", compiled: []Spec{ fmtYearShort{}, fmtMonth{true}, fmtDay{true}, }, }
var Date_YYYYMMDD = &Strftime{ pattern: "YYYYMMDD", compiled: []Spec{ fmtYearLong{}, fmtMonth{true}, fmtDay{true}, }, }
var Date_YYYY_MM_DD = &Strftime{ pattern: "YYYY-MM-DD", compiled: []Spec{ fmtYearLong{}, fmtSeparator('-'), fmtMonth{true}, fmtSeparator('-'), fmtDay{true}, }, }
var Date_YYYY_M_D = &Strftime{ pattern: "YYYY-M-D", compiled: []Spec{ fmtYearLong{}, fmtSeparator('-'), fmtMonth{false}, fmtSeparator('-'), fmtDay{false}, }, }
var Date_YY_M_D = &Strftime{ pattern: "YY-M-D", compiled: []Spec{ fmtYearShort{}, fmtSeparator('-'), fmtMonth{false}, fmtSeparator('-'), fmtDay{false}, }, }
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{}, }
Functions ¶
func Format ¶
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 ¶ added in v0.19.0
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.
Types ¶
type Date ¶
type Date struct {
// contains filtered or unexported fields
}
func DateFromDayNumber ¶ added in v0.19.0
DateFromDayNumber converts an absolute day number into a Date. Returns zero date if day number exceeds 3652499 or is less than 366.
func NewDateFromStd ¶ added in v0.18.0
func ParseDateFloat ¶
func ParseDateInt64 ¶
func (Date) FormatInt64 ¶
func (Date) MondayWeek ¶
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) Sunday4DayWeek ¶
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 ¶
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.
type DateTime ¶
func NewDateTimeFromStd ¶ added in v0.18.0
func ParseDateTimeDecimal ¶
func ParseDateTimeInt64 ¶
func (DateTime) AddInterval ¶ added in v0.18.0
func (DateTime) FormatDecimal ¶
func (DateTime) FormatFloat64 ¶
func (DateTime) FormatInt64 ¶
func (DateTime) WeightString ¶ added in v0.18.0
type Interval ¶ added in v0.18.0
type Interval struct {
// contains filtered or unexported fields
}
func ParseInterval ¶ added in v0.18.0
func ParseInterval(itv string, unit IntervalType, negate bool) *Interval
func ParseIntervalDecimal ¶ added in v0.18.0
func ParseIntervalFloat ¶ added in v0.18.0
func ParseIntervalFloat(f float64, unit IntervalType, negate bool) *Interval
func ParseIntervalInt64 ¶ added in v0.18.0
func ParseIntervalInt64(itv int64, unit IntervalType, negate bool) *Interval
func (*Interval) Unit ¶ added in v0.18.0
func (itv *Interval) Unit() IntervalType
type IntervalType ¶ added in v0.18.0
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 ¶ added in v0.18.0
func (itv IntervalType) HasDateParts() bool
func (IntervalType) HasDayParts ¶ added in v0.18.0
func (itv IntervalType) HasDayParts() bool
func (IntervalType) HasMonthParts ¶ added in v0.18.0
func (itv IntervalType) HasMonthParts() bool
func (IntervalType) HasTimeParts ¶ added in v0.18.0
func (itv IntervalType) HasTimeParts() bool
func (IntervalType) NeedsPrecision ¶ added in v0.18.0
func (itv IntervalType) NeedsPrecision() bool
func (IntervalType) PartCount ¶ added in v0.18.0
func (itv IntervalType) PartCount() int
func (IntervalType) ToString ¶ added in v0.18.0
func (itv IntervalType) ToString() string
ToString returns the type as a string
type Strftime ¶
type Strftime struct {
// contains filtered or unexported fields
}
Strftime is the object that represents a compiled strftime pattern
func New ¶
New creates a new Strftime object. If the compilation fails, then an error is returned in the second argument.
func (*Strftime) AppendFormat ¶
func (*Strftime) FormatNumeric ¶
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
func NewTimeFromSeconds ¶ added in v0.20.0
func NewTimeFromStd ¶ added in v0.18.0
func ParseTimeDecimal ¶
func ParseTimeInt64 ¶
func (Time) AddInterval ¶ added in v0.18.0
func (Time) FormatDecimal ¶
func (Time) FormatFloat64 ¶
func (Time) FormatInt64 ¶
func (Time) Nanosecond ¶
func (Time) RoundForJSON ¶
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) ToDuration ¶
type TimeState ¶ added in v0.20.0
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 )