Documentation
¶
Index ¶
- Constants
- Variables
- func DecimalAdd(from1, from2, to *MyDecimal) error
- func DecimalBinSize(precision, frac int) int
- func DecimalDiv(from1, from2, to *MyDecimal, fracIncr int) error
- func DecimalMod(from1, from2, to *MyDecimal) error
- func DecimalMul(from1, from2, to *MyDecimal) error
- func DecimalPeak(b []byte) (int, error)
- func DecimalSub(from1, from2, to *MyDecimal) error
- func FormatIntWidthN(num, n int) string
- func GetMaxFloat(flen int, decimal int) float64
- func Round(f float64, dec int) float64
- func RoundFloat(f float64) float64
- func Truncate(f float64, dec int) float64
- type CoreTime
- func (t CoreTime) Day() int
- func (t CoreTime) GoTime(loc *gotime.Location) (gotime.Time, error)
- func (t CoreTime) Hour() int
- func (t CoreTime) IsLeapYear() bool
- func (t CoreTime) Microsecond() int
- func (t CoreTime) Minute() int
- func (t CoreTime) Month() int
- func (t CoreTime) Second() int
- func (t CoreTime) String() string
- func (t CoreTime) Week(mode int) int
- func (t CoreTime) Weekday() gotime.Weekday
- func (t CoreTime) Year() int
- func (t CoreTime) YearDay() int
- func (t CoreTime) YearWeek(mode int) (int, int)
- type Datum
- func (d *Datum) Collation() string
- func (d *Datum) Frac() int
- func (d *Datum) GetBytes() []byte
- func (d *Datum) GetFloat32() float32
- func (d *Datum) GetFloat64() float64
- func (d *Datum) GetInt64() int64
- func (d *Datum) GetInterface() interface{}
- func (d *Datum) GetMysqlDuration() Duration
- func (d *Datum) GetMysqlEnum() Enum
- func (d *Datum) GetMysqlTime() Time
- func (d *Datum) GetString() string
- func (d *Datum) GetUint64() uint64
- func (d *Datum) IsNull() bool
- func (d *Datum) Kind() byte
- func (d *Datum) Length() int
- func (d *Datum) SetBytes(b []byte)
- func (d *Datum) SetCollation(collation string)
- func (d *Datum) SetFloat32(f float32)
- func (d *Datum) SetFloat64(f float64)
- func (d *Datum) SetFrac(frac int)
- func (d *Datum) SetInt64(i int64)
- func (d *Datum) SetInterface(x interface{})
- func (d *Datum) SetLength(l int)
- func (d *Datum) SetMysqlDuration(b Duration)
- func (d *Datum) SetMysqlEnum(b Enum, collation string)
- func (d *Datum) SetMysqlTime(b Time)
- func (d *Datum) SetNull()
- func (d *Datum) SetString(s string, collation string)
- func (d *Datum) SetUint64(i uint64)
- type Duration
- type Enum
- type MyDecimal
- func (d *MyDecimal) Compare(to *MyDecimal) int
- func (d *MyDecimal) FromBin(bin []byte, precision, frac int) (binSize int, err error)
- func (d *MyDecimal) FromFloat64(f float64) error
- func (d *MyDecimal) FromInt(val int64) *MyDecimal
- func (d *MyDecimal) FromString(str []byte) error
- func (d *MyDecimal) FromUint(val uint64) *MyDecimal
- func (d *MyDecimal) GetDigitsFrac() int8
- func (d *MyDecimal) GetDigitsInt() int8
- func (d *MyDecimal) IsNegative() bool
- func (d *MyDecimal) IsZero() bool
- func (d *MyDecimal) PrecisionAndFrac() (precision, frac int)
- func (d *MyDecimal) Round(to *MyDecimal, frac int, roundMode RoundMode) (err error)
- func (d *MyDecimal) Shift(shift int) error
- func (d *MyDecimal) String() string
- func (d *MyDecimal) ToBin(precision, frac int) ([]byte, error)
- func (d *MyDecimal) ToFloat64() (float64, error)
- func (d *MyDecimal) ToHashKey() ([]byte, error)
- func (d *MyDecimal) ToInt() (int64, error)
- func (d *MyDecimal) ToString() (str []byte)
- func (d *MyDecimal) ToUint() (uint64, error)
- func (d *MyDecimal) WriteBin(precision, frac int, buf []byte) ([]byte, error)
- type RoundMode
- type Set
- type Time
- func (t Time) Clock() (hour int, minute int, second int)
- func (t Time) CoreTime() CoreTime
- func (t Time) DateFormat(layout string) (string, error)
- func (t *Time) FromPackedUint(packed uint64) error
- func (t Time) Fsp() int8
- func (t Time) InvalidZero() bool
- func (t Time) IsZero() bool
- func (t *Time) SetCoreTime(ct CoreTime)
- func (t *Time) SetFsp(fsp int8)
- func (t *Time) SetType(tp uint8)
- func (t Time) String() string
- func (t Time) ToPackedUint() (uint64, error)
- func (t Time) Type() uint8
Constants ¶
const ( KindNull byte = 0 KindInt64 byte = 1 KindUint64 byte = 2 KindFloat32 byte = 3 KindFloat64 byte = 4 KindString byte = 5 KindBytes byte = 6 KindMysqlDuration byte = 9 KindMysqlEnum byte = 10 KindMysqlTime byte = 13 KindInterface byte = 14 )
Kind constants.
const ( // UnspecifiedFsp is the unspecified fractional seconds part. UnspecifiedFsp = int8(-1) // DefaultFsp is the default digit of fractional seconds part. // MySQL use 0 as the default Fsp. DefaultFsp = int8(0) )
const ( TimeFormat = "2006-01-02 15:04:05" // TimeFSPFormat is time format with fractional seconds precision. TimeFSPFormat = "2006-01-02 15:04:05.000000" )
Time format without fractional seconds precision.
const ( // MinYear is the minimum for mysql year type. MinYear int16 = 1901 // MaxYear is the maximum for mysql year type. MaxYear int16 = 2155 // MaxDuration is the maximum for duration. MaxDuration int64 = 838*10000 + 59*100 + 59 // MinTime is the minimum for mysql time type. MinTime = -gotime.Duration(838*3600+59*60+59) * gotime.Second // MaxTime is the maximum for mysql time type. MaxTime = gotime.Duration(838*3600+59*60+59) * gotime.Second // ZeroDatetimeStr is the string representation of a zero datetime. ZeroDatetimeStr = "0000-00-00 00:00:00" // ZeroDateStr is the string representation of a zero date. ZeroDateStr = "0000-00-00" // TimeMaxHour is the max hour for mysql time type. TimeMaxHour = 838 // TimeMaxMinute is the max minute for mysql time type. TimeMaxMinute = 59 // TimeMaxSecond is the max second for mysql time type. TimeMaxSecond = 59 // TimeMaxValue is the maximum value for mysql time type. TimeMaxValue = TimeMaxHour*10000 + TimeMaxMinute*100 + TimeMaxSecond // TimeMaxValueSeconds is the maximum second value for mysql time type. TimeMaxValueSeconds = TimeMaxHour*3600 + TimeMaxMinute*60 + TimeMaxSecond )
const MyDecimalStructSize = 40
MyDecimalStructSize is the struct size of MyDecimal.
Variables ¶
var ( // MonthNames lists names of months, which are used in builtin time function `monthname`. MonthNames = []string{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", } )
var ZeroCoreTime = CoreTime(0)
ZeroCoreTime is the zero value for TimeInternal type.
var ( // ZeroCoreTime is the zero value for Time type. ZeroTime = Time{} )
Functions ¶
func DecimalAdd ¶
DecimalAdd adds two decimals, sets the result to 'to'. Note: DO NOT use `from1` or `from2` as `to` since the metadata of `to` may be changed during evaluating.
func DecimalBinSize ¶
DecimalBinSize returns the size of array to hold a binary representation of a decimal.
func DecimalDiv ¶
DecimalDiv does division of two decimals.
from1 - dividend from2 - divisor to - quotient fracIncr - increment of fraction
func DecimalMod ¶
DecimalMod does modulus of two decimals.
from1 - dividend
from2 - divisor
to - modulus
RETURN VALUE
E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
NOTES
see do_div_mod()
DESCRIPTION
the modulus R in R = M mod N
is defined as
0 <= |R| < |M|
sign R == sign M
R = M - k*N, where k is integer
thus, there's no requirement for M or N to be integers
func DecimalMul ¶
DecimalMul multiplies two decimals.
from1, from2 - factors
to - product
RETURN VALUE
E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;
NOTES
in this implementation, with wordSize=4 we have digitsPerWord=9,
and 63-digit number will take only 7 words (basically a 7-digit
"base 999999999" number). Thus there's no need in fast multiplication
algorithms, 7-digit numbers can be multiplied with a naive O(n*n)
method.
XXX if this library is to be used with huge numbers of thousands of
digits, fast multiplication must be implemented.
func DecimalPeak ¶
DecimalPeak returns the length of the encoded decimal.
func DecimalSub ¶
DecimalSub subs one decimal from another, sets the result to 'to'.
func FormatIntWidthN ¶
FormatIntWidthN uses to format int with width. Insufficient digits are filled by 0.
func GetMaxFloat ¶
nolint GetMaxFloat gets the max float for given flen and decimal.
func Round ¶
Round rounds the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero. nolint
func RoundFloat ¶
RoundFloat rounds float val to the nearest even integer value with float64 format, like MySQL Round function. RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html so rounding use "round to nearest even". e.g, 1.5 -> 2, -1.5 -> -2.
Types ¶
type CoreTime ¶
type CoreTime uint64
CoreTime is the internal struct type for Time.
func FromDate ¶
func FromDate(year int, month int, day int, hour int, minute int, second int, microsecond int) CoreTime
FromDate makes a internal time representation from the given date.
func (CoreTime) IsLeapYear ¶
IsLeapYear returns if it's leap year.
func (CoreTime) Microsecond ¶
Microsecond returns the microsecond value.
type Datum ¶
type Datum struct {
// contains filtered or unexported fields
}
Datum is a data box holds different kind of data. It has better performance and is easier to use than `interface{}`. nolint
func (*Datum) GetInterface ¶
func (d *Datum) GetInterface() interface{}
GetInterface gets interface value.
func (*Datum) GetMysqlDuration ¶
GetMysqlDuration gets Duration value
func (*Datum) SetCollation ¶
SetCollation sets the collation of the datum.
func (*Datum) SetInterface ¶
func (d *Datum) SetInterface(x interface{})
SetInterface sets interface to datum.
func (*Datum) SetMysqlDuration ¶
SetMysqlDuration sets Duration value
func (*Datum) SetMysqlEnum ¶
SetMysqlEnum sets Enum value
func (*Datum) SetMysqlTime ¶
SetMysqlTime sets types.Time value
type Duration ¶
type Duration struct {
gotime.Duration
// Fsp is short for Fractional Seconds Precision.
// See http://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html
Fsp int8
}
Duration is the type for MySQL TIME type.
type MyDecimal ¶
type MyDecimal struct {
// contains filtered or unexported fields
}
MyDecimal represents a decimal value.
func NewDecFromFloatForTest ¶
NewDecFromFloatForTest creates a MyDecimal from float, as it returns no error, it should only be used in test.
func NewDecFromInt ¶
NewDecFromInt creates a MyDecimal from int.
func NewDecFromStringForTest ¶
NewDecFromStringForTest creates a MyDecimal from string, as it returns no error, it should only be used in test.
func NewDecFromUint ¶
NewDecFromUint creates a MyDecimal from uint.
func NewMaxOrMinDec ¶
NewMaxOrMinDec returns the max or min value decimal for given precision and fraction.
func (*MyDecimal) FromFloat64 ¶
FromFloat64 creates a decimal from float64 value.
func (*MyDecimal) FromString ¶
FromString parses decimal from string.
func (*MyDecimal) GetDigitsFrac ¶
GetDigitsFrac returns the digitsFrac.
func (*MyDecimal) GetDigitsInt ¶
GetDigitsInt returns the digitsInt.
func (*MyDecimal) IsNegative ¶
IsNegative returns whether a decimal is negative.
func (*MyDecimal) PrecisionAndFrac ¶
PrecisionAndFrac returns the internal precision and frac number.
func (*MyDecimal) Round ¶
Round rounds the decimal to "frac" digits.
to - result buffer. d == to is allowed frac - to what position after fraction point to round. can be negative! roundMode - round to nearest even or truncate ModeHalfEven rounds normally. Truncate just truncates the decimal.
NOTES
scale can be negative ! one TRUNCATED error (line XXX below) isn't treated very logical :(
RETURN VALUE
eDecOK/eDecTruncated
func (*MyDecimal) Shift ¶
Shift shifts decimal digits in given number (with rounding if it need), shift > 0 means shift to left shift, shift < 0 means right shift. In fact it is multiplying on 10^shift.
RETURN
eDecOK OK eDecOverflow operation lead to overflow, number is untoched eDecTruncated number was rounded to fit into buffer
func (*MyDecimal) ToBin ¶
ToBin converts decimal to its binary fixed-length representation two representations of the same length can be compared with memcmp with the correct -1/0/+1 result
PARAMS
precision/frac - if precision is 0, internal value of the decimal will be used,
then the encoded value is not memory comparable.
NOTE
the buffer is assumed to be of the size DecimalBinSize(precision, frac)
RETURN VALUE
bin - binary value
errCode - eDecOK/eDecTruncate/eDecOverflow
DESCRIPTION
for storage decimal numbers are converted to the "binary" format.
This format has the following properties:
1. length of the binary representation depends on the {precision, frac}
as provided by the caller and NOT on the digitsInt/digitsFrac of the decimal to
convert.
2. binary representations of the same {precision, frac} can be compared
with memcmp - with the same result as DecimalCompare() of the original
decimals (not taking into account possible precision loss during
conversion).
This binary format is as follows:
1. First the number is converted to have a requested precision and frac.
2. Every full digitsPerWord digits of digitsInt part are stored in 4 bytes
as is
3. The first digitsInt % digitesPerWord digits are stored in the reduced
number of bytes (enough bytes to store this number of digits -
see dig2bytes)
4. same for frac - full word are stored as is,
the last frac % digitsPerWord digits - in the reduced number of bytes.
5. If the number is negative - every byte is inversed.
5. The very first bit of the resulting byte array is inverted (because
memcmp compares unsigned bytes, see property 2 above)
Example:
1234567890.1234
internally is represented as 3 words
1 234567890 123400000
(assuming we want a binary representation with precision=14, frac=4)
in hex it's
00-00-00-01 0D-FB-38-D2 07-5A-EF-40
now, middle word is full - it stores 9 decimal digits. It goes
into binary representation as is:
........... 0D-FB-38-D2 ............
First word has only one decimal digit. We can store one digit in
one byte, no need to waste four:
01 0D-FB-38-D2 ............
now, last word. It's 123400000. We can store 1234 in two bytes:
01 0D-FB-38-D2 04-D2
So, we've packed 12 bytes number in 7 bytes.
And now we invert the highest bit to get the final result:
81 0D FB 38 D2 04 D2
And for -1234567890.1234 it would be
7E F2 04 C7 2D FB 2D
func (*MyDecimal) ToHashKey ¶
ToHashKey removes the leading and trailing zeros and generates a hash key. Two Decimals dec0 and dec1 with different fraction will generate the same hash keys if dec0.Compare(dec1) == 0.
func (*MyDecimal) ToString ¶
ToString converts decimal to its printable string representation without rounding.
RETURN VALUE
str - result string
errCode - eDecOK/eDecTruncate/eDecOverflow
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
Time is the struct for handling datetime, timestamp and date.
func (Time) DateFormat ¶
DateFormat returns a textual representation of the time value formatted according to layout. See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
func (*Time) FromPackedUint ¶
FromPackedUint decodes Time from a packed uint64 value.
func (Time) InvalidZero ¶
InvalidZero returns a boolean indicating whether the month or day is zero.
func (Time) ToPackedUint ¶
ToPackedUint encodes Time to a packed uint64 value.
1 bit 0 17 bits year*13+month (year 0-9999, month 0-12) 5 bits day (0-31) 5 bits hour (0-23) 6 bits minute (0-59) 6 bits second (0-59) 24 bits microseconds (0-999999) Total: 64 bits = 8 bytes 0YYYYYYY.YYYYYYYY.YYdddddh.hhhhmmmm.mmssssss.ffffffff.ffffffff.ffffffff