From 37d4d58b570a75d31b9426037ae6ec5325cf2976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Thu, 29 Apr 2021 21:08:47 +0200 Subject: [PATCH] fix: make wakatime summary endpoint date range inclusive (resolve #192) --- models/compat/wakatime/v1/summaries.go | 1 + routes/compat/wakatime/v1/summaries.go | 9 +++++++++ utils/date.go | 12 ++++++++++++ utils/date_test.go | 22 ++++++++++++++++++++++ version.txt | 2 +- 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/models/compat/wakatime/v1/summaries.go b/models/compat/wakatime/v1/summaries.go index 5342bbc..b61e8a0 100644 --- a/models/compat/wakatime/v1/summaries.go +++ b/models/compat/wakatime/v1/summaries.go @@ -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{} diff --git a/routes/compat/wakatime/v1/summaries.go b/routes/compat/wakatime/v1/summaries.go index b4eab3d..66ab03f 100644 --- a/routes/compat/wakatime/v1/summaries.go +++ b/routes/compat/wakatime/v1/summaries.go @@ -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 } diff --git a/utils/date.go b/utils/date.go index c512caa..d026120 100644 --- a/utils/date.go +++ b/utils/date.go @@ -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)) } diff --git a/utils/date_test.go b/utils/date_test.go index f7e2002..4952528 100644 --- a/utils/date_test.go +++ b/utils/date_test.go @@ -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) diff --git a/version.txt b/version.txt index 25691b4..3cd33cd 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.26.8 +1.26.9