Skip to content

Instantly share code, notes, and snippets.

@dmendiza
Created June 24, 2017 00:36
Show Gist options
  • Save dmendiza/4779c88f652a0c9d0bbbb1c84658398b to your computer and use it in GitHub Desktop.
Save dmendiza/4779c88f652a0c9d0bbbb1c84658398b to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
"time"
)
func main() {
startDate := time.Date(2017, 06, 21, 10, 19, 00, 0, time.UTC)
endDate := time.Date(2017, 06, 23, 11, 00, 00, 0, time.UTC)
fmt.Println(GetIndexes(startDate, endDate))
}
func GetIndexes(startDate, endDate time.Time) ([]string, error) {
if endDate.Before(startDate) {
return nil, errors.New("Invalid date range")
}
ret := make([]string, 0, 0)
for t := startDate; t.Before(endDate); t = t.AddDate(0, 0, 1) {
ret = append(ret,
fmt.Sprintf("logstash-%d.%02d.%02d", t.Year(), t.Month(), t.Day()))
}
return ret, nil
}
package main
import (
"testing"
"time"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestGetIndexes(c *C) {
startDate := time.Date(2017, 06, 21, 10, 19, 00, 0, time.UTC)
endDate := time.Date(2017, 06, 23, 11, 00, 00, 0, time.UTC)
expected := []string{"logstash-2017.06.21", "logstash-2017.06.22", "logstash-2017.06.23"}
indexes, err := GetIndexes(startDate, endDate)
c.Assert(err, IsNil)
c.Assert(indexes, DeepEquals, expected)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment