本文整理匯總了Golang中github.com/pingcap/tidb/mysql.Time.Time方法的典型用法代碼示例。如果您正苦於以下問題:Golang Time.Time方法的具體用法?Golang Time.Time怎麽用?Golang Time.Time使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pingcap/tidb/mysql.Time
的用法示例。
在下文中一共展示了Time.Time方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: dumpBinaryDateTime
func dumpBinaryDateTime(t mysql.Time, loc *time.Location) (data []byte) {
if t.Type == mysql.TypeTimestamp && loc != nil {
t.Time = t.In(loc)
}
year, mon, day := t.Year(), t.Month(), t.Day()
if t.IsZero() {
year, mon, day = 1, time.January, 1
}
switch t.Type {
case mysql.TypeTimestamp, mysql.TypeDatetime:
data = append(data, 11)
data = append(data, dumpUint16(uint16(year))...)
data = append(data, byte(mon), byte(day), byte(t.Hour()), byte(t.Minute()), byte(t.Second()))
data = append(data, dumpUint32(uint32((t.Nanosecond() / 1000)))...)
case mysql.TypeDate, mysql.TypeNewDate:
data = append(data, 4)
data = append(data, dumpUint16(uint16(year))...) //year
data = append(data, byte(mon), byte(day))
}
return
}
示例2: getTimeValue
func getTimeValue(ctx context.Context, v interface{}, tp byte, fsp int) (interface{}, error) {
value := mysql.Time{
Type: tp,
Fsp: fsp,
}
defaultTime, err := getSystemTimestamp(ctx)
if err != nil {
return nil, errors.Trace(err)
}
switch x := v.(type) {
case string:
if x == CurrentTimestamp {
value.Time = defaultTime
} else if x == ZeroTimestamp {
value, _ = mysql.ParseTimeFromNum(0, tp, fsp)
} else {
value, err = mysql.ParseTime(x, tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
}
case Value:
x.Val = types.RawData(x.Val)
switch xval := x.Val.(type) {
case string:
value, err = mysql.ParseTime(xval, tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
case int64:
value, err = mysql.ParseTimeFromNum(int64(xval), tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
case nil:
return nil, nil
default:
return nil, errors.Trace(errDefaultValue)
}
case *Ident:
if x.Equal(CurrentTimeExpr) {
return CurrentTimestamp, nil
}
return nil, errors.Trace(errDefaultValue)
case *UnaryOperation:
// support some expression, like `-1`
m := map[interface{}]interface{}{}
v := Eval(x, nil, m)
ft := types.NewFieldType(mysql.TypeLonglong)
xval, err := types.Convert(v, ft)
if err != nil {
return nil, errors.Trace(err)
}
value, err = mysql.ParseTimeFromNum(xval.(int64), tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
default:
return nil, nil
}
return value, nil
}
示例3: getTimeValue
func getTimeValue(ctx context.Context, v interface{}, tp byte, fsp int) (d types.Datum, err error) {
value := mysql.Time{
Type: tp,
Fsp: fsp,
}
defaultTime, err := getSystemTimestamp(ctx)
if err != nil {
return d, errors.Trace(err)
}
switch x := v.(type) {
case string:
upperX := strings.ToUpper(x)
if upperX == CurrentTimestamp {
value.Time = defaultTime
} else if upperX == ZeroTimestamp {
value, _ = mysql.ParseTimeFromNum(0, tp, fsp)
} else {
value, err = mysql.ParseTime(x, tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
}
case *ast.ValueExpr:
switch x.Kind() {
case types.KindString:
value, err = mysql.ParseTime(x.GetString(), tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
case types.KindInt64:
value, err = mysql.ParseTimeFromNum(x.GetInt64(), tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
case types.KindNull:
return d, nil
default:
return d, errors.Trace(errDefaultValue)
}
case *ast.FuncCallExpr:
if x.FnName.L == currentTimestampL {
d.SetString(CurrentTimestamp)
return d, nil
}
return d, errors.Trace(errDefaultValue)
case *ast.UnaryOperationExpr:
// support some expression, like `-1`
v, err := Eval(ctx, x)
if err != nil {
return d, errors.Trace(err)
}
ft := types.NewFieldType(mysql.TypeLonglong)
xval, err := v.ConvertTo(ft)
if err != nil {
return d, errors.Trace(err)
}
value, err = mysql.ParseTimeFromNum(xval.GetInt64(), tp, fsp)
if err != nil {
return d, errors.Trace(err)
}
default:
return d, nil
}
d.SetMysqlTime(value)
return d, nil
}