本文整理汇总了Golang中common.ParseTimeDuration函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseTimeDuration函数的具体用法?Golang ParseTimeDuration怎么用?Golang ParseTimeDuration使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseTimeDuration函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ParseAndValidate
func (self *ShardConfiguration) ParseAndValidate(defaultShardDuration time.Duration) error {
var err error
if self.Split == 0 {
self.Split = 1
}
if self.SplitRandom == "" {
self.hasRandomSplit = false
} else {
self.hasRandomSplit = true
self.splitRandomRegex, err = regexp.Compile(self.SplitRandom)
if err != nil {
return err
}
}
if self.Duration == "" {
self.parsedDuration = defaultShardDuration
return nil
}
val, err := common.ParseTimeDuration(self.Duration)
if err != nil {
return err
}
self.parsedDuration = time.Duration(val)
return nil
}
示例2: parseTime
// parse time expressions, e.g. now() - 1d
func parseTime(value *Value) (int64, error) {
if value.Type != ValueExpression {
if value.IsFunctionCall() && value.Name == "now" {
return time.Now().UnixNano(), nil
}
if value.IsFunctionCall() {
return 0, fmt.Errorf("Invalid use of function %s", value.Name)
}
if value.Type == ValueString {
t, err := parseTimeString(value.Name)
return t.UnixNano(), err
}
return common.ParseTimeDuration(value.Name)
}
leftValue, err := parseTime(value.Elems[0])
if err != nil {
return 0, err
}
rightValue, err := parseTime(value.Elems[1])
if err != nil {
return 0, err
}
switch value.Name {
case "+":
return leftValue + rightValue, nil
case "-":
return leftValue - rightValue, nil
default:
return 0, fmt.Errorf("Cannot use '%s' in a time expression", value.Name)
}
}
示例3: GetGroupByTime
func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
for _, groupBy := range self.Elems {
if groupBy.IsFunctionCall() {
// TODO: check the number of arguments and return an error
if len(groupBy.Elems) != 1 {
return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
}
// TODO: check the function name
// TODO: error checking
arg := groupBy.Elems[0].Name
durationInt, err := common.ParseTimeDuration(arg)
if err != nil {
return nil, common.NewQueryError(common.InvalidArgument, fmt.Sprintf("invalid argument %s to the time function", arg))
}
duration := time.Duration(durationInt)
return &duration, nil
}
}
return nil, nil
}
示例4: GetGroupByTime
func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
for _, groupBy := range self.Elems {
if groupBy.IsFunctionCall() && strings.ToLower(groupBy.Name) == "time" {
// TODO: check the number of arguments and return an error
if len(groupBy.Elems) != 1 {
return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
}
if groupBy.Elems[0].Type != ValueDuration {
log.Debug("Get a time function without a duration argument %v", groupBy.Elems[0].Type)
}
arg := groupBy.Elems[0].Name
durationInt, err := common.ParseTimeDuration(arg)
if err != nil {
return nil, common.NewQueryError(common.InvalidArgument, fmt.Sprintf("invalid argument %s to the time function", arg))
}
duration := time.Duration(durationInt)
return &duration, nil
}
}
return nil, nil
}