From d2ca2b496aa4f752a6befc4769ee737589712bcc Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Mon, 22 Aug 2016 21:08:13 +0100 Subject: [PATCH] Add days_in_month function. --- promql/functions.go | 17 +++++++++++++++++ promql/testdata/functions.test | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/promql/functions.go b/promql/functions.go index 857a7862d..98bc3ad54 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -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 === func funcDayOfMonth(ev *evaluator, args Expressions) model.Value { vector := ev.evalVector(args[0]) @@ -966,6 +977,12 @@ var functions = map[string]*Function{ ReturnType: model.ValScalar, Call: funcCountScalar, }, + "days_in_month": { + Name: "days_in_month", + ArgTypes: []model.ValueType{model.ValVector}, + ReturnType: model.ValVector, + Call: funcDaysInMonth, + }, "day_of_month": { Name: "day_of_month", ArgTypes: []model.ValueType{model.ValVector}, diff --git a/promql/testdata/functions.test b/promql/testdata/functions.test index 4d49f6f30..6b1b578c3 100644 --- a/promql/testdata/functions.test +++ b/promql/testdata/functions.test @@ -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. eval instant at 0m month_of_year(vector(1456790400)) + day_of_month(vector(1456790400)) / 100 {} 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