本文整理汇总了Golang中github.com/araddon/dateparse.ParseAny函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseAny函数的具体用法?Golang ParseAny怎么用?Golang ParseAny使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseAny函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TimeExtractFunc
func TimeExtractFunc(ctx expr.EvalContext, items ...value.Value) (value.StringValue, bool) {
switch len(items) {
case 0:
// if we have no "items", return time associated with ctx
// This is an alias of now()
t := ctx.Ts()
if !t.IsZero() {
return value.NewStringValue(t.String()), true
}
return value.EmptyStringValue, false
case 1:
// if only 1 item, convert item to time
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.EmptyStringValue, false
}
t, err := dateparse.ParseAny(dateStr)
if err != nil {
return value.EmptyStringValue, false
}
return value.NewStringValue(t.String()), true
case 2:
// if we have 2 items, the first is the time string
// and the second is the format string.
// Use leekchan/timeutil package
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.EmptyStringValue, false
}
formatStr, ok := value.ToString(items[1].Rv())
if !ok {
return value.EmptyStringValue, false
}
t, err := dateparse.ParseAny(dateStr)
if err != nil {
return value.EmptyStringValue, false
}
formatted := timeutil.Strftime(&t, formatStr)
return value.NewStringValue(formatted), true
default:
return value.EmptyStringValue, false
}
}
示例2: Cast
func Cast(valType ValueType, val Value) (Value, error) {
switch valType {
case ByteSliceType:
return NewByteSliceValue([]byte(val.ToString())), nil
case TimeType:
switch valt := val.(type) {
case StringValue:
if t, err := dateparse.ParseAny(valt.Val()); err == nil {
return NewTimeValue(t), nil
} else {
return nil, err
}
case TimeValue:
return valt, nil
}
case StringType:
sv := val.ToString()
return NewStringValue(sv), nil
case IntType:
iv, ok := ToInt64(val.Rv())
if ok {
return NewIntValue(iv), nil
}
return nil, ErrConversion
}
return nil, ErrConvestionNotSupported
}
示例3: Yy
// Get year in integer from field, must be able to convert to date
//
// yy() => 15, true // assuming it is 2015
// yy("2014-03-01") => 14, true
//
func Yy(ctx expr.EvalContext, items ...value.Value) (value.IntValue, bool) {
yy := 0
if len(items) == 0 {
if !ctx.Ts().IsZero() {
yy = ctx.Ts().Year()
} else {
// Do we want to use Now()?
}
} else if len(items) == 1 {
//u.Debugf("has 1 items? %#v", items[0].Rv())
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.NewIntValue(0), false
}
//u.Debugf("v=%v %v", dateStr, items[0].Rv())
if t, err := dateparse.ParseAny(dateStr); err != nil {
return value.NewIntValue(0), false
} else {
yy = t.Year()
}
} else {
return value.NewIntValue(0), false
}
if yy >= 2000 {
yy = yy - 2000
} else if yy >= 1900 {
yy = yy - 1900
}
//u.Debugf("yy = %v", yy)
return value.NewIntValue(int64(yy)), true
}
示例4: TestStaticDataSource
func TestStaticDataSource(t *testing.T) {
static := NewStaticDataSource("users", 0, nil, []string{"user_id", "name", "email", "created", "roles"})
created, _ := dateparse.ParseAny("2015/07/04")
static.Put(nil, &datasource.KeyInt{123}, []driver.Value{123, "aaron", "[email protected]", created.In(time.UTC), []string{"admin"}})
assert.Tf(t, static.Length() == 1, "has 1 rows after Put()")
row, _ := static.Get(123)
assert.Tf(t, row != nil, "Should find row with Get() part of Seeker interface")
di, ok := row.Body().(*datasource.SqlDriverMessageMap)
assert.Tf(t, ok, "Must be []driver.Value type: %T", row.Body())
vals := di.Values()
assert.Tf(t, len(vals) == 5, "want 5 cols in user but got %v", len(vals))
assert.Tf(t, vals[0].(int) == 123, "want user_id=123 but got %v", vals[0])
assert.Tf(t, vals[2].(string) == "[email protected]", "want [email protected] but got %v", vals[2])
static.Put(nil, &datasource.KeyInt{123}, []driver.Value{123, "aaron", "[email protected]", created.In(time.UTC), []string{"root", "admin"}})
assert.Tf(t, static.Length() == 1, "has 1 rows after Put()")
row, _ = static.Get(123)
assert.Tf(t, row != nil, "Should find row with Get() part of Seeker interface")
vals2 := row.Body().(*datasource.SqlDriverMessageMap).Values()
assert.Tf(t, vals2[2].(string) == "[email protected]", "want [email protected] but got %v", vals2[2])
assert.Equal(t, []string{"root", "admin"}, vals2[4], "Roles should match updated vals")
assert.Equal(t, created, vals2[3], "created date should match updated vals")
}
示例5: ToDate
// todate: convert to Date
//
// todate(field) uses araddon\dateparse util to recognize formats
//
// todate("01/02/2006", field ) uses golang date parse rules
// first parameter is the layout/format
//
//
func ToDate(ctx expr.EvalContext, items ...value.Value) (value.TimeValue, bool) {
if len(items) == 1 {
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.TimeZeroValue, false
}
//u.Infof("v=%v %v ", v, item.Rv())
if t, err := dateparse.ParseAny(dateStr); err == nil {
return value.NewTimeValue(t), true
}
} else if len(items) == 2 {
dateStr, ok := value.ToString(items[1].Rv())
if !ok {
return value.TimeZeroValue, false
}
formatStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.TimeZeroValue, false
}
//u.Infof("hello layout=%v time=%v", formatStr, dateStr)
if t, err := time.Parse(formatStr, dateStr); err == nil {
return value.NewTimeValue(t), true
}
}
return value.TimeZeroValue, false
}
示例6: Yy
// Get year in integer from date
func Yy(ctx expr.EvalContext, items ...value.Value) (value.IntValue, bool) {
yy := 0
if len(items) == 0 {
if !ctx.Ts().IsZero() {
yy = ctx.Ts().Year()
}
} else if len(items) == 1 {
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.NewIntValue(0), false
}
//u.Infof("v=%v %v ", v, item.Rv())
if t, err := dateparse.ParseAny(dateStr); err != nil {
return value.NewIntValue(0), false
} else {
yy = t.Year()
}
} else {
return value.NewIntValue(0), false
}
if yy >= 2000 {
yy = yy - 2000
} else if yy >= 1900 {
yy = yy - 1900
}
//u.Infof("%v yy = %v", item, yy)
return value.NewIntValue(int64(yy)), true
}
示例7: TestMemDb
func TestMemDb(t *testing.T) {
created, _ := dateparse.ParseAny("2015/07/04")
db, err := NewMemDb("users", []string{"user_id", "name", "email", "created", "roles"})
assert.Tf(t, err == nil, "wanted no error got %v", err)
c, err := db.Open("users")
assert.Tf(t, err == nil, "wanted no error got %v", err)
dc, ok := c.(schema.ConnAll)
assert.T(t, ok)
dc.Put(nil, &datasource.KeyInt{123}, []driver.Value{123, "aaron", "[email protected]", created.In(time.UTC), []string{"admin"}})
row, err := dc.Get(123)
assert.T(t, err == nil)
assert.Tf(t, row != nil, "Should find row with Get() part of Seeker interface")
di, ok := row.(*datasource.SqlDriverMessage)
assert.Tf(t, ok, "Must be []driver.Value type: %T", row)
vals := di.Vals
assert.Tf(t, len(vals) == 5, "want 5 cols in user but got %v", len(vals))
assert.Tf(t, vals[0].(int) == 123, "want user_id=123 but got %v", vals[0])
assert.Tf(t, vals[2].(string) == "[email protected]", "want [email protected] but got %v", vals[2])
dc.Put(nil, &datasource.KeyInt{123}, []driver.Value{123, "aaron", "[email protected]", created.In(time.UTC), []string{"root", "admin"}})
row, _ = dc.Get(123)
assert.Tf(t, row != nil, "Should find row with Get() part of Seeker interface")
vals2 := row.Body().([]driver.Value)
assert.Tf(t, vals2[2].(string) == "[email protected]", "want [email protected] but got %v", vals2[2])
assert.Equal(t, []string{"root", "admin"}, vals2[4], "Roles should match updated vals")
assert.Equal(t, created, vals2[3], "created date should match updated vals")
}
示例8: guessValueType
func guessValueType(val string) value.ValueType {
if _, err := strconv.ParseInt(val, 10, 64); err == nil {
return value.IntType
} else if _, err := strconv.ParseBool(val); err == nil {
return value.IntType
} else if _, err := strconv.ParseFloat(val, 64); err == nil {
return value.NumberType
} else if _, err := dateparse.ParseAny(val); err == nil {
return value.TimeType
}
return value.StringType
}
示例9: ToTimestamp
// totimestamp: convert to date, then to unix Seconds
//
func ToTimestamp(ctx expr.EvalContext, item value.Value) (value.IntValue, bool) {
dateStr, ok := value.ToString(item.Rv())
if !ok {
return value.NewIntValue(0), false
}
if t, err := dateparse.ParseAny(dateStr); err == nil {
//u.Infof("v=%v %v unix=%v", item, item.Rv(), t.Unix())
return value.NewIntValue(int64(t.Unix())), true
}
return value.NewIntValue(0), false
}
示例10: ToDate
// todate
func ToDate(ctx expr.EvalContext, item value.Value) (value.TimeValue, bool) {
dateStr, ok := value.ToString(item.Rv())
if !ok {
return value.TimeZeroValue, false
}
//u.Infof("v=%v %v ", v, item.Rv())
if t, err := dateparse.ParseAny(dateStr); err == nil {
return value.NewTimeValue(t), true
}
return value.TimeZeroValue, false
}
示例11: Scan
func (m *TimeValue) Scan(src interface{}) error {
//u.Debugf("scan: '%v'", src)
var t time.Time
switch val := src.(type) {
case string:
//u.Infof("trying to scan string: '%v'", val)
t2, err := dateparse.ParseAny(val)
if err == nil {
*m = TimeValue(t2)
return nil
}
//u.Infof("%v %v", t2, err)
err = json.Unmarshal([]byte(val), &t)
if err == nil {
*m = TimeValue(t)
} else {
u.Warnf("error for %q err=%v", val, err)
return err
}
case []byte:
t2, err := dateparse.ParseAny(string(val))
if err == nil {
*m = TimeValue(t2)
return nil
}
err = json.Unmarshal(val, &t)
if err == nil {
*m = TimeValue(t)
} else {
return err
}
case nil:
return nil
default:
u.Warnf("unknown type: %T", m)
return errors.New("Incompatible type for TimeValue")
}
return nil
}
示例12: HourOfDay
// hour of day [0-23]
func HourOfDay(ctx expr.EvalContext, items ...value.Value) (value.IntValue, bool) {
if len(items) == 0 {
if !ctx.Ts().IsZero() {
return value.NewIntValue(int64(ctx.Ts().Hour())), true
}
} else if len(items) == 1 {
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.NewIntValue(0), false
}
//u.Infof("v=%v %v ", v, items[0].Rv())
if t, err := dateparse.ParseAny(dateStr); err == nil {
return value.NewIntValue(int64(t.Hour())), true
}
}
return value.NewIntValue(0), false
}
示例13: YyMm
// Get yymm in 4 digits from argument if supplied, else uses message context ts
//
func YyMm(ctx expr.EvalContext, items ...value.Value) (value.StringValue, bool) {
if len(items) == 0 {
if !ctx.Ts().IsZero() {
t := ctx.Ts()
return value.NewStringValue(t.Format(yymmTimeLayout)), true
}
} else if len(items) == 1 {
dateStr, ok := value.ToString(items[0].Rv())
if !ok {
return value.EmptyStringValue, false
}
//u.Infof("v=%v %v ", v, items[0].Rv())
if t, err := dateparse.ParseAny(dateStr); err == nil {
return value.NewStringValue(t.Format(yymmTimeLayout)), true
}
}
return value.EmptyStringValue, false
}
示例14: Yy
func Yy(ctx expr.EvalContext, item value.Value) (value.IntValue, bool) {
//u.Info("yy: %T", item)
val, ok := value.ToString(item.Rv())
if !ok || val == "" {
return value.NewIntValue(0), false
}
//u.Infof("v=%v %v ", val, item.Rv())
if t, err := dateparse.ParseAny(val); err == nil {
yy := t.Year()
if yy >= 2000 {
yy = yy - 2000
} else if yy >= 1900 {
yy = yy - 1900
}
//u.Infof("Yy = %v yy = %v", item, yy)
return value.NewIntValue(int64(yy)), true
}
return value.NewIntValue(0), false
}
示例15: TestStructWrapper
func TestStructWrapper(t *testing.T) {
t1, _ := dateparse.ParseAny("12/18/2015")
tr := true
user := &User{
Name: "Yoda",
Created: t1,
Updated: &t1,
Authenticated: true,
HasSession: &tr,
Roles: []string{"admin", "api"},
BankAmount: 55.5,
}
readers := []expr.ContextReader{
datasource.NewContextWrapper(user),
datasource.NewContextSimpleNative(map[string]interface{}{
"str1": "str1",
"int1": 1,
"t1": t1,
"Name": "notyoda",
}),
}
nc := datasource.NewNestedContextReader(readers, time.Now())
expected := value.NewMapValue(map[string]interface{}{
"str1": "str1",
"int1": 1,
"Name": "Yoda",
"Authenticated": true,
"bankamount": 55.5,
"FullName": "Yoda, Jedi",
"Roles": []string{"admin", "api"},
})
for k, v := range expected.Val() {
//u.Infof("k:%v v:%#v", k, v)
checkval(t, nc, k, v)
}
}