func TestCollect(t *testing.T) { startdate, _ := epochdate.NewFromDate(2001, 2, 10) enddate, _ := epochdate.NewFromDate(2001, 2, 11) c := NewSDayCollect("minute") c.AddImport("c_count", "c_count") c.Init(startdate, enddate) d1value := map[string]interface{}{ "_hr": map[string]interface{}{ "h_18": map[string]interface{}{ "mn": map[string]interface{}{ "m_15": map[string]interface{}{ "c_count": float64(10), }, }, }, }, } c.ValueDay(startdate, d1value) for _, v := range c.BuildResult() { if v["c_count"].(float64) > 0 { log.Printf("%+v", v) } } }
// Calculate the next period until the passed date, or the end date if nil. // includelast determines if the last item is included. func (d *Generator) nextUntilInternal(date *epochdate.Date, includelast bool) (havenext bool, isfinished bool) { if d.isfirst { // initialize on first run d.initializeGeneration() } if date == nil { // use default end date date = &d.currentEndDate } if d.isFinishedInternal(*date) { d.isfirst = false return false, true } if !d.isfirst { switch d.Group { case WEEK: // advance 7 days if d.isforward { d.CurrentDate += 7 } else { d.CurrentDate -= 7 } case MONTH: // advance day 1 of month year, month, _ := d.CurrentDate.Date() if d.isforward { d.CurrentDate, _ = epochdate.NewFromDate(year, month+1, 1) } else { d.CurrentDate, _ = epochdate.NewFromDate(year, month-1, 1) } default: // advance 1 day if d.isforward { d.CurrentDate++ } else { d.CurrentDate-- } } } else { d.isfirst = false } if !includelast && d.isFinishedInternal(*date) { return false, false } return true, false }
// returns the first day from the date, depending on the group func (d Generator) FirstFromDate(date epochdate.Date) epochdate.Date { switch d.Group { case WEEK: // first date is the previous monday of the week wd := date.UTC().Weekday() if wd > d.FirstDayOfWeek { return date - epochdate.Date(wd) + epochdate.Date(d.FirstDayOfWeek) } else if wd < d.FirstDayOfWeek { return date - 6 + epochdate.Date(wd) } case MONTH: // first day of the month year, month, _ := date.Date() ret, _ := epochdate.NewFromDate(year, month, 1) return ret } return date }