1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: make wakatime summary endpoint date range inclusive (resolve #192)

This commit is contained in:
Ferdinand Mütsch 2021-04-29 21:08:47 +02:00
parent 331ace3c1e
commit 37d4d58b57
5 changed files with 45 additions and 1 deletions

View File

@ -58,6 +58,7 @@ type SummariesRange struct {
}
func NewSummariesFrom(summaries []*models.Summary, filters *models.Filters) *SummariesViewModel {
// TODO: implement filtering (https://github.com/muety/wakapi/issues/58)
data := make([]*SummariesData, len(summaries))
minDate, maxDate := time.Now().Add(1*time.Second), time.Time{}

View File

@ -110,6 +110,12 @@ func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary
}
}
// wakatime iterprets end date as "inclusive", wakapi usually as "exclusive"
// i.e. for wakatime, an interval 2021-04-29 - 2021-04-29 is actually 2021-04-29 - 2021-04-30,
// while for wakapi it would be empty
// see https://github.com/muety/wakapi/issues/192
end = utils.EndOfDay(end).Add(-1 * time.Second)
overallParams := &models.SummaryParams{
From: start,
To: end,
@ -125,6 +131,9 @@ func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary
if err != nil {
return nil, err, http.StatusInternalServerError
}
// wakatime returns requested instead of actual summary range
summary.FromTime = models.CustomTime(interval[0])
summary.ToTime = models.CustomTime(interval[1].Add(-1 * time.Second))
summaries[i] = summary
}

View File

@ -13,6 +13,18 @@ func StartOfToday(tz *time.Location) time.Time {
return StartOfDay(FloorDate(time.Now().In(tz)))
}
func EndOfDay(date time.Time) time.Time {
floored := FloorDate(date)
if floored == date {
date = date.Add(1 * time.Second)
}
return CeilDate(date)
}
func EndOfToday(tz *time.Location) time.Time {
return EndOfDay(time.Now().In(tz))
}
func StartOfThisWeek(tz *time.Location) time.Time {
return StartOfWeek(time.Now().In(tz))
}

View File

@ -66,6 +66,28 @@ func TestDate_StartOfDay(t *testing.T) {
assert.Equal(t, tzCet, StartOfDay(d4).Location())
}
func TestDate_EndOfDay(t *testing.T) {
d1, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-25 20:25:00", tzLocal)
d2, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-25 20:25:00", tzUtc)
d3, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-25 20:25:00", tzPst)
d4, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-25 20:25:00", tzCet)
t1, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-26 00:00:00", tzLocal)
t2, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-26 00:00:00", tzUtc)
t3, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-26 00:00:00", tzPst)
t4, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-26 00:00:00", tzCet)
assert.Equal(t, t1, EndOfDay(d1))
assert.Equal(t, t2, EndOfDay(d2))
assert.Equal(t, t3, EndOfDay(d3))
assert.Equal(t, t4, EndOfDay(d4))
assert.Equal(t, tzLocal, EndOfDay(d1).Location())
assert.Equal(t, tzUtc, EndOfDay(d2).Location())
assert.Equal(t, tzPst, EndOfDay(d3).Location())
assert.Equal(t, tzCet, EndOfDay(d4).Location())
}
func TestDate_StartOfWeek(t *testing.T) {
d1, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-25 20:25:00", tzLocal)
d2, _ := time.ParseInLocation(config.SimpleDateTimeFormat, "2021-04-25 20:25:00", tzUtc)

View File

@ -1 +1 @@
1.26.8
1.26.9