本文整理汇总了Golang中github.com/pingcap/tidb/mysql.ParseTime函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseTime函数的具体用法?Golang ParseTime怎么用?Golang ParseTime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseTime函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestConvertToFloat64
func (s *testTypeConvertSuite) TestConvertToFloat64(c *check.C) {
testToFloat64(c, "0", float64(0))
testToFloat64(c, int(0), float64(0))
testToFloat64(c, int64(0), float64(0))
testToFloat64(c, uint64(0), float64(0))
// TODO: check this
//testToFloat64(c, float32(3.1), float64(3.1))
testToFloat64(c, float64(3.1), float64(3.1))
testToFloat64(c, mysql.Hex{Value: 100}, float64(100))
testToFloat64(c, mysql.Bit{Value: 100, Width: 8}, float64(100))
testToFloat64(c, mysql.Enum{Name: "a", Value: 1}, float64(1))
testToFloat64(c, mysql.Set{Name: "a", Value: 1}, float64(1))
t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
c.Assert(err, check.IsNil)
testToFloat64(c, t, float64(20111110111111.999999))
td, err := mysql.ParseDuration("11:11:11.999999", 6)
c.Assert(err, check.IsNil)
testToFloat64(c, td, float64(111111.999999))
ft := NewFieldType(mysql.TypeNewDecimal)
ft.Decimal = 5
v, err := Convert(3.1415926, ft)
c.Assert(err, check.IsNil)
testToFloat64(c, v, float64(3.14159))
_, err = ToFloat64(&invalidMockType{})
c.Assert(err, check.NotNil)
}
示例2: TestConvertToBool
func (s *testTypeConvertSuite) TestConvertToBool(c *check.C) {
testToBool(c, int(0), 0)
testToBool(c, int64(0), 0)
testToBool(c, uint64(0), 0)
testToBool(c, float32(0), 0)
testToBool(c, float64(0), 0)
testToBool(c, "", 0)
testToBool(c, "0", 0)
testToBool(c, []byte{}, 0)
testToBool(c, []byte("0"), 0)
testToBool(c, mysql.Hex{Value: 0}, 0)
testToBool(c, mysql.Bit{Value: 0, Width: 8}, 0)
testToBool(c, mysql.Enum{Name: "a", Value: 1}, 1)
testToBool(c, mysql.Set{Name: "a", Value: 1}, 1)
t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
c.Assert(err, check.IsNil)
testToBool(c, t, 1)
td, err := mysql.ParseDuration("11:11:11.999999", 6)
c.Assert(err, check.IsNil)
testToBool(c, td, 1)
ft := NewFieldType(mysql.TypeNewDecimal)
ft.Decimal = 5
v, err := Convert(3.1415926, ft)
c.Assert(err, check.IsNil)
testToBool(c, v, 1)
_, err = ToBool(&invalidMockType{})
c.Assert(err, check.NotNil)
}
示例3: TestConvertToString
func (s *testTypeConvertSuite) TestConvertToString(c *check.C) {
testToString(c, "0", "0")
testToString(c, true, "1")
testToString(c, "false", "false")
testToString(c, int(0), "0")
testToString(c, int64(0), "0")
testToString(c, uint64(0), "0")
testToString(c, float32(1.6), "1.6")
testToString(c, float64(-0.6), "-0.6")
testToString(c, []byte{1}, "\x01")
testToString(c, mysql.Hex{Value: 0x4D7953514C}, "MySQL")
testToString(c, mysql.Bit{Value: 0x41, Width: 8}, "A")
testToString(c, mysql.Enum{Name: "a", Value: 1}, "a")
testToString(c, mysql.Set{Name: "a", Value: 1}, "a")
t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
c.Assert(err, check.IsNil)
testToString(c, t, "2011-11-10 11:11:11.999999")
td, err := mysql.ParseDuration("11:11:11.999999", 6)
c.Assert(err, check.IsNil)
testToString(c, td, "11:11:11.999999")
ft := NewFieldType(mysql.TypeNewDecimal)
ft.Decimal = 5
v, err := Convert(3.1415926, ft)
c.Assert(err, check.IsNil)
testToString(c, v, "3.14159")
_, err = ToString(&invalidMockType{})
c.Assert(err, check.NotNil)
}
示例4: TestToBool
func (ts *testDatumSuite) TestToBool(c *C) {
testDatumToBool(c, int(0), 0)
testDatumToBool(c, int64(0), 0)
testDatumToBool(c, uint64(0), 0)
testDatumToBool(c, float32(0.1), 0)
testDatumToBool(c, float64(0.1), 0)
testDatumToBool(c, "", 0)
testDatumToBool(c, "0.1", 0)
testDatumToBool(c, []byte{}, 0)
testDatumToBool(c, []byte("0.1"), 0)
testDatumToBool(c, mysql.Hex{Value: 0}, 0)
testDatumToBool(c, mysql.Bit{Value: 0, Width: 8}, 0)
testDatumToBool(c, mysql.Enum{Name: "a", Value: 1}, 1)
testDatumToBool(c, mysql.Set{Name: "a", Value: 1}, 1)
t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
c.Assert(err, IsNil)
testDatumToBool(c, t, 1)
td, err := mysql.ParseDuration("11:11:11.999999", 6)
c.Assert(err, IsNil)
testDatumToBool(c, td, 1)
ft := NewFieldType(mysql.TypeNewDecimal)
ft.Decimal = 5
v, err := Convert(0.1415926, ft)
c.Assert(err, IsNil)
testDatumToBool(c, v, 0)
d := NewDatum(&invalidMockType{})
_, err = d.ToBool()
c.Assert(err, NotNil)
}
示例5: TestToInt64
func (ts *testTypeConvertSuite) TestToInt64(c *C) {
testDatumToInt64(c, "0", int64(0))
testDatumToInt64(c, int(0), int64(0))
testDatumToInt64(c, int64(0), int64(0))
testDatumToInt64(c, uint64(0), int64(0))
testDatumToInt64(c, float32(3.1), int64(3))
testDatumToInt64(c, float64(3.1), int64(3))
testDatumToInt64(c, mysql.Hex{Value: 100}, int64(100))
testDatumToInt64(c, mysql.Bit{Value: 100, Width: 8}, int64(100))
testDatumToInt64(c, mysql.Enum{Name: "a", Value: 1}, int64(1))
testDatumToInt64(c, mysql.Set{Name: "a", Value: 1}, int64(1))
t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 0)
c.Assert(err, IsNil)
testDatumToInt64(c, t, int64(20111110111112))
td, err := mysql.ParseDuration("11:11:11.999999", 6)
c.Assert(err, IsNil)
testDatumToInt64(c, td, int64(111112))
ft := NewFieldType(mysql.TypeNewDecimal)
ft.Decimal = 5
v, err := Convert(3.1415926, ft)
c.Assert(err, IsNil)
testDatumToInt64(c, v, int64(3))
}
示例6: setSnapshotTS
func (s *SessionVars) setSnapshotTS(sVal string) error {
if sVal == "" {
s.SnapshotTS = 0
return nil
}
t, err := mysql.ParseTime(sVal, mysql.TypeTimestamp, mysql.MaxFsp)
if err != nil {
return errors.Trace(err)
}
ts := (t.UnixNano() / int64(time.Millisecond)) << epochShiftBits
s.SnapshotTS = uint64(ts)
return nil
}
示例7: convertToMysqlTime
func (d *Datum) convertToMysqlTime(target *FieldType) (Datum, error) {
tp := target.Tp
fsp := mysql.DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
var ret Datum
switch d.k {
case KindMysqlTime:
t, err := d.GetMysqlTime().Convert(tp)
if err != nil {
ret.SetValue(t)
return ret, errors.Trace(err)
}
t, err = t.RoundFrac(fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
case KindMysqlDuration:
t, err := d.GetMysqlDuration().ConvertToTime(tp)
if err != nil {
ret.SetValue(t)
return ret, errors.Trace(err)
}
t, err = t.RoundFrac(fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
case KindString, KindBytes:
t, err := mysql.ParseTime(d.GetString(), tp, fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
case KindInt64:
t, err := mysql.ParseTimeFromNum(d.GetInt64(), tp, fsp)
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
default:
return invalidConv(d, tp)
}
return ret, nil
}
示例8: parseTime
func parseTime(c *C, s string) mysql.Time {
m, err := mysql.ParseTime(s, mysql.TypeDatetime, mysql.DefaultFsp)
c.Assert(err, IsNil)
return m
}
示例9: 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
}
示例10: TestConvertType
//.........这里部分代码省略.........
// TypeDouble
ft = NewFieldType(mysql.TypeDouble)
ft.Flen = 5
ft.Decimal = 2
v, err = Convert(999.9155, ft)
c.Assert(err, check.IsNil)
c.Assert(v, check.Equals, float64(999.92))
// For TypeString
ft = NewFieldType(mysql.TypeString)
ft.Flen = 3
v, err = Convert("12345", ft)
c.Assert(err, check.IsNil)
c.Assert(v, check.Equals, "123")
ft = NewFieldType(mysql.TypeString)
ft.Flen = 3
ft.Charset = charset.CharsetBin
v, err = Convert("12345", ft)
c.Assert(err, check.IsNil)
c.Assert(v, check.DeepEquals, []byte("123"))
// For TypeDuration
ft = NewFieldType(mysql.TypeDuration)
ft.Decimal = 3
v, err = Convert("10:11:12.123456", ft)
c.Assert(err, check.IsNil)
c.Assert(v.(mysql.Duration).String(), check.Equals, "10:11:12.123")
ft.Decimal = 1
vv, err := Convert(v, ft)
c.Assert(err, check.IsNil)
c.Assert(vv.(mysql.Duration).String(), check.Equals, "10:11:12.1")
vt, err := mysql.ParseTime("2010-10-10 10:11:11.12345", mysql.TypeTimestamp, 2)
c.Assert(vt.String(), check.Equals, "2010-10-10 10:11:11.12")
c.Assert(err, check.IsNil)
v, err = Convert(vt, ft)
c.Assert(err, check.IsNil)
c.Assert(v.(mysql.Duration).String(), check.Equals, "10:11:11.1")
// For mysql.TypeTimestamp, mysql.TypeDatetime, mysql.TypeDate
ft = NewFieldType(mysql.TypeTimestamp)
ft.Decimal = 3
v, err = Convert("2010-10-10 10:11:11.12345", ft)
c.Assert(err, check.IsNil)
c.Assert(v.(mysql.Time).String(), check.Equals, "2010-10-10 10:11:11.123")
ft.Decimal = 1
vv, err = Convert(v, ft)
c.Assert(err, check.IsNil)
c.Assert(vv.(mysql.Time).String(), check.Equals, "2010-10-10 10:11:11.1")
// For TypeLonglong
ft = NewFieldType(mysql.TypeLonglong)
v, err = Convert("100", ft)
c.Assert(err, check.IsNil)
c.Assert(v, check.Equals, int64(100))
ft = NewFieldType(mysql.TypeLonglong)
ft.Flag |= mysql.UnsignedFlag
v, err = Convert("100", ft)
c.Assert(err, check.IsNil)
c.Assert(v, check.Equals, uint64(100))
// For TypeBit
ft = NewFieldType(mysql.TypeBit)
ft.Flen = 8
v, err = Convert("100", ft)
示例11: Convert
// Convert converts the val with type tp.
func Convert(val interface{}, target *FieldType) (v interface{}, err error) {
tp := target.Tp
if val == nil {
return nil, nil
}
vdi, ok := val.(*DataItem)
if ok {
return Convert(vdi.Data, target)
}
switch tp { // TODO: implement mysql types convert when "CAST() AS" syntax are supported.
case mysql.TypeFloat:
x, err := ToFloat64(val)
if err != nil {
return invConv(val, tp)
}
// For float and following double type, we will only truncate it for float(M, D) format.
// If no D is set, we will handle it like origin float whether M is set or not.
if target.Flen != UnspecifiedLength && target.Decimal != UnspecifiedLength {
x, err = TruncateFloat(x, target.Flen, target.Decimal)
if err != nil {
return nil, errors.Trace(err)
}
}
return float32(x), nil
case mysql.TypeDouble:
x, err := ToFloat64(val)
if err != nil {
return invConv(val, tp)
}
if target.Flen != UnspecifiedLength && target.Decimal != UnspecifiedLength {
x, err = TruncateFloat(x, target.Flen, target.Decimal)
if err != nil {
return nil, errors.Trace(err)
}
}
return float64(x), nil
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob,
mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString:
x, err := ToString(val)
if err != nil {
return invConv(val, tp)
}
// TODO: consider target.Charset/Collate
x = truncateStr(x, target.Flen)
if target.Charset == charset.CharsetBin {
return []byte(x), nil
}
return x, nil
case mysql.TypeDuration:
fsp := mysql.DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
switch x := val.(type) {
case mysql.Duration:
return x.RoundFrac(fsp)
case mysql.Time:
t, err := x.ConvertToDuration()
if err != nil {
return nil, errors.Trace(err)
}
return t.RoundFrac(fsp)
case string:
return mysql.ParseDuration(x, fsp)
default:
return invConv(val, tp)
}
case mysql.TypeTimestamp, mysql.TypeDatetime, mysql.TypeDate:
fsp := mysql.DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
switch x := val.(type) {
case mysql.Time:
t, err := x.Convert(tp)
if err != nil {
return nil, errors.Trace(err)
}
return t.RoundFrac(fsp)
case mysql.Duration:
t, err := x.ConvertToTime(tp)
if err != nil {
return nil, errors.Trace(err)
}
return t.RoundFrac(fsp)
case string:
return mysql.ParseTime(x, tp, fsp)
case int64:
return mysql.ParseTimeFromNum(x, tp, fsp)
default:
return invConv(val, tp)
}
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
unsigned := mysql.HasUnsignedFlag(target.Flag)
if unsigned {
return convertToUint(val, target)
}
return convertToInt(val, target)
//.........这里部分代码省略.........
示例12: 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")
}
}
示例13: TestConvertToString
func (s *testTypeConvertSuite) TestConvertToString(c *C) {
defer testleak.AfterTest(c)()
testToString(c, "0", "0")
testToString(c, true, "1")
testToString(c, "false", "false")
testToString(c, int(0), "0")
testToString(c, int64(0), "0")
testToString(c, uint64(0), "0")
testToString(c, float32(1.6), "1.6")
testToString(c, float64(-0.6), "-0.6")
testToString(c, []byte{1}, "\x01")
testToString(c, mysql.Hex{Value: 0x4D7953514C}, "MySQL")
testToString(c, mysql.Bit{Value: 0x41, Width: 8}, "A")
testToString(c, mysql.Enum{Name: "a", Value: 1}, "a")
testToString(c, mysql.Set{Name: "a", Value: 1}, "a")
t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
c.Assert(err, IsNil)
testToString(c, t, "2011-11-10 11:11:11.999999")
td, err := mysql.ParseDuration("11:11:11.999999", 6)
c.Assert(err, IsNil)
testToString(c, td, "11:11:11.999999")
ft := NewFieldType(mysql.TypeNewDecimal)
ft.Decimal = 5
v, err := Convert(3.1415926, ft)
c.Assert(err, IsNil)
testToString(c, v, "3.14159")
_, err = ToString(&invalidMockType{})
c.Assert(err, NotNil)
// test truncate
cases := []struct {
flen int
charset string
input string
output string
}{
{5, "utf8", "你好,世界", "你好,世界"},
{5, "utf8mb4", "你好,世界", "你好,世界"},
{4, "utf8", "你好,世界", "你好,世"},
{4, "utf8mb4", "你好,世界", "你好,世"},
{15, "binary", "你好,世界", "你好,世界"},
{12, "binary", "你好,世界", "你好,世"},
{0, "binary", "你好,世界", ""},
}
for _, ca := range cases {
ft = NewFieldType(mysql.TypeVarchar)
ft.Flen = ca.flen
ft.Charset = ca.charset
inputDatum := NewStringDatum(ca.input)
outputDatum, err := inputDatum.ConvertTo(ft)
if ca.input != ca.output {
c.Assert(ErrDataTooLong.Equal(err), IsTrue)
} else {
c.Assert(err, IsNil)
}
c.Assert(outputDatum.GetString(), Equals, ca.output)
}
}
示例14: 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
}