本文整理匯總了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
}