本文整理汇总了Golang中github.com/pingcap/tidb/mysql.Time.RoundFrac方法的典型用法代码示例。如果您正苦于以下问题:Golang Time.RoundFrac方法的具体用法?Golang Time.RoundFrac怎么用?Golang Time.RoundFrac使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pingcap/tidb/mysql.Time
的用法示例。
在下文中一共展示了Time.RoundFrac方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: builtinNow
func builtinNow(args []types.Datum, _ context.Context) (d types.Datum, err error) {
// TODO: if NOW is used in stored function or trigger, NOW will return the beginning time
// of the execution.
fsp := 0
if len(args) == 1 && args[0].Kind() != types.KindNull {
if fsp, err = checkFsp(args[0]); err != nil {
d.SetNull()
return d, errors.Trace(err)
}
}
t := mysql.Time{
Time: time.Now(),
Type: mysql.TypeDatetime,
// set unspecified for later round
Fsp: mysql.UnspecifiedFsp,
}
tr, err := t.RoundFrac(int(fsp))
if err != nil {
d.SetNull()
return d, errors.Trace(err)
}
d.SetMysqlTime(tr)
return d, nil
}
示例2: builtinNow
func builtinNow(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) {
// TODO: if NOW is used in stored function or trigger, NOW will return the beginning time
// of the execution.
fsp := 0
if len(args) == 1 {
var err error
if fsp, err = checkFsp(args[0]); err != nil {
return nil, errors.Trace(err)
}
}
t := mysql.Time{
Time: time.Now(),
Type: mysql.TypeDatetime,
// set unspecified for later round
Fsp: mysql.UnspecifiedFsp,
}
return t.RoundFrac(int(fsp))
}
示例3: Cast
// Cast casts val to certain types and does not return error.
func Cast(val interface{}, target *FieldType) (v interface{}) {
tp := target.Tp
switch tp {
case mysql.TypeString:
x, _ := ToString(val)
// TODO: consider target.Charset/Collate
x = truncateStr(x, target.Flen)
if target.Charset == charset.CharsetBin {
return []byte(x)
}
return x
case mysql.TypeDuration:
var dur mysql.Duration
fsp := mysql.DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
switch x := val.(type) {
case mysql.Duration:
dur, _ = x.RoundFrac(fsp)
case mysql.Time:
dur, _ = x.ConvertToDuration()
dur, _ = dur.RoundFrac(fsp)
case string:
dur, _ = mysql.ParseDuration(x, fsp)
case *DataItem:
return Cast(x.Data, target)
}
return dur
case mysql.TypeDatetime, mysql.TypeDate:
fsp := mysql.DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
var t mysql.Time
t.Type = tp
switch x := val.(type) {
case mysql.Time:
t, _ = x.Convert(tp)
t, _ = t.RoundFrac(fsp)
case mysql.Duration:
t, _ = x.ConvertToTime(tp)
t, _ = t.RoundFrac(fsp)
case string:
t, _ = mysql.ParseTime(x, tp, fsp)
case int64:
t, _ = mysql.ParseTimeFromNum(x, tp, fsp)
case *DataItem:
return Cast(x.Data, target)
}
return t
case mysql.TypeLonglong:
if mysql.HasUnsignedFlag(target.Flag) {
v, _ = ToUint64(val)
} else {
v, _ = ToInt64(val)
}
return
case mysql.TypeNewDecimal:
x, _ := ToDecimal(val)
if target.Decimal != UnspecifiedLength {
x = x.Round(int32(target.Decimal))
}
// TODO: check Flen
return x
default:
panic("should never happen")
}
}