Add days_in_month function.

This commit is contained in:
Brian Brazil 2016-08-22 21:08:13 +01:00
parent 0ed31c8c47
commit d2ca2b496a
2 changed files with 25 additions and 0 deletions

View file

@ -860,6 +860,17 @@ func funcVector(ev *evaluator, args Expressions) model.Value {
} }
} }
// === days_in_month(v vector) scalar ===
func funcDaysInMonth(ev *evaluator, args Expressions) model.Value {
vector := ev.evalVector(args[0])
for _, el := range vector {
el.Metric.Del(model.MetricNameLabel)
t := time.Unix(int64(el.Value), 0)
el.Value = model.SampleValue(32 - time.Date(t.Year(), t.Month(), 32, 0, 0, 0, 0, time.UTC).Day())
}
return vector
}
// === day_of_month(v vector) scalar === // === day_of_month(v vector) scalar ===
func funcDayOfMonth(ev *evaluator, args Expressions) model.Value { func funcDayOfMonth(ev *evaluator, args Expressions) model.Value {
vector := ev.evalVector(args[0]) vector := ev.evalVector(args[0])
@ -966,6 +977,12 @@ var functions = map[string]*Function{
ReturnType: model.ValScalar, ReturnType: model.ValScalar,
Call: funcCountScalar, Call: funcCountScalar,
}, },
"days_in_month": {
Name: "days_in_month",
ArgTypes: []model.ValueType{model.ValVector},
ReturnType: model.ValVector,
Call: funcDaysInMonth,
},
"day_of_month": { "day_of_month": {
Name: "day_of_month", Name: "day_of_month",
ArgTypes: []model.ValueType{model.ValVector}, ArgTypes: []model.ValueType{model.ValVector},

View file

@ -398,3 +398,11 @@ eval instant at 0m month_of_year(vector(1456790399)) + day_of_month(vector(14567
# 2016-03-01 00:00:00 March 1st in leap year. # 2016-03-01 00:00:00 March 1st in leap year.
eval instant at 0m month_of_year(vector(1456790400)) + day_of_month(vector(1456790400)) / 100 eval instant at 0m month_of_year(vector(1456790400)) + day_of_month(vector(1456790400)) / 100
{} 3.01 {} 3.01
# Febuary 1st 2016 in leap year.
eval instant at 0m days_in_month(vector(1454284800))
{} 29
# Febuary 1st 2017 not in leap year.
eval instant at 0m days_in_month(vector(1485907200))
{} 28