Skip to content

Commit

Permalink
Merge pull request #1 from JanBerktold/more_intervals
Browse files Browse the repository at this point in the history
Add Intervals TenMinutes, ThirtyMinutes, Biweekly, Bimonthly & Quarter
  • Loading branch information
sent-hil committed Aug 28, 2015
2 parents d83bc27 + 606d9f4 commit e75caf7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
1 change: 0 additions & 1 deletion Readme.md
Expand Up @@ -171,4 +171,3 @@ users, err := client.EventUsers("dodge rock", time.Now(), Hour)
* Total count of users metric
* Add method to undo an event
* Move to lua scripts wherever possible
* Add more intervals: biweekly, bimonthly, quarter
35 changes: 35 additions & 0 deletions interval.go
@@ -1,6 +1,7 @@
package bitesized

import (
"math"
"time"

"github.com/jinzhu/now"
Expand All @@ -13,28 +14,62 @@ type Interval int

const (
All Interval = iota
TenMinutes
ThirtyMinutes
Hour
Day
Biweekly
Week
Bimonthly
Month
Quarter
Year
)

func handleMinuteInterval(t time.Time, n *now.Now, cycleLength int, keyName string) string {
layout := keyName + ":2006-01-02-15:04"
offset := t.Sub(n.BeginningOfHour())
cycle := int(math.Floor(offset.Minutes() / float64(cycleLength)))
return n.BeginningOfHour().Add(time.Duration(cycle*cycleLength) * time.Minute).Format(layout)
}

func nearestInterval(t time.Time, interval Interval) string {
n := now.New(t.UTC())

switch interval {
case All:
return "all"
case TenMinutes:
return handleMinuteInterval(t, n, 10, "ten_minutes")
case ThirtyMinutes:
return handleMinuteInterval(t, n, 30, "thirty_minutes")
case Day:
layout := "day:2006-01-02"
return n.BeginningOfDay().Format(layout)
case Biweekly:
layout := "biweekly:2006-01-02"
date := n.BeginningOfWeek()
if offset := t.Sub(n.BeginningOfWeek()); offset.Hours() > 84 {
date = date.Add(84 * time.Hour)
}
return date.Format(layout)
case Week:
layout := "week:2006-01-02"
return n.BeginningOfWeek().Format(layout)
case Bimonthly:
layout := "bimonthly:2006-01-02"
monthMiddle := n.EndOfMonth().Sub(n.BeginningOfMonth()) / 2
date := n.BeginningOfMonth()
if offset := t.Sub(n.BeginningOfMonth()); offset > monthMiddle {
date = date.Add(monthMiddle)
}
return date.Format(layout)
case Month:
layout := "month:2006-01"
return n.BeginningOfMonth().Format(layout)
case Quarter:
layout := "quarter:2006-01"
return n.BeginningOfQuarter().Format(layout)
case Year:
layout := "year:2006"
return n.BeginningOfYear().Format(layout)
Expand Down
42 changes: 41 additions & 1 deletion interval_test.go
Expand Up @@ -7,7 +7,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
)

var randomTime = time.Date(1981, time.June, 12, 01, 1, 0, 0, time.UTC)
var randomTime = time.Date(1981, time.June, 12, 01, 42, 0, 0, time.UTC)

func TestNearestInterval(t *testing.T) {
Convey("It should empty for 'All'", t, func() {
Expand Down Expand Up @@ -39,6 +39,46 @@ func TestNearestInterval(t *testing.T) {
n := nearestInterval(randomTime, Year)
So(n, ShouldEqual, "year:1981")
})

Convey("It should find nearest quarter", t, func() {
n := nearestInterval(randomTime, Quarter)
So(n, ShouldEqual, "quarter:1981-04")
})

Convey("It should find nearest 10 minute cycle", t, func() {
n := nearestInterval(randomTime, TenMinutes)
So(n, ShouldEqual, "ten_minutes:1981-06-12-01:40")
})

Convey("It should find nearest 30 minute cycle", t, func() {
n := nearestInterval(randomTime, ThirtyMinutes)
So(n, ShouldEqual, "thirty_minutes:1981-06-12-01:30")
})

Convey("It should find nearest biweekly date (first part)", t, func() {
testingTime := time.Date(1981, time.June, 12, 01, 42, 0, 0, time.UTC)
n := nearestInterval(testingTime, Biweekly)
So(n, ShouldEqual, "biweekly:1981-06-10")
})

Convey("It should find nearest biweekly date (second part)", t, func() {
testingTime := time.Date(1981, time.June, 16, 01, 42, 0, 0, time.UTC)
n := nearestInterval(testingTime, Biweekly)
So(n, ShouldEqual, "biweekly:1981-06-14")
})

Convey("It should find nearest bimonthly date (first part)", t, func() {
testingTime := time.Date(1981, time.June, 12, 01, 42, 0, 0, time.UTC)
n := nearestInterval(testingTime, Bimonthly)
So(n, ShouldEqual, "bimonthly:1981-06-01")
})

Convey("It should find nearest bimonthly date (second part)", t, func() {
testingTime := time.Date(1981, time.June, 28, 01, 42, 0, 0, time.UTC)
n := nearestInterval(testingTime, Bimonthly)
So(n, ShouldEqual, "bimonthly:1981-06-15")
})

}

func TestGetDuration(t *testing.T) {
Expand Down

0 comments on commit e75caf7

Please sign in to comment.