From 0d026a269f32f92e6c1ca18470943f375ebc6caa Mon Sep 17 00:00:00 2001 From: cchampou Date: Sat, 4 Nov 2023 09:15:07 +0100 Subject: [PATCH] fix --- duolingo/users.go | 3 ++- utils/date.go | 4 ++++ utils/date_test.go | 13 +++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/duolingo/users.go b/duolingo/users.go index 6ee783f..48c0971 100644 --- a/duolingo/users.go +++ b/duolingo/users.go @@ -1,6 +1,7 @@ package duolingo import ( + "duolingo/utils" "encoding/json" "net/http" "strconv" @@ -24,7 +25,7 @@ type XPSummaries struct { } func getXPGains(userId int) int { - req, newRequestErr := http.NewRequest("GET", BaseURL+"/users/"+strconv.Itoa(userId)+"/xp_summaries?startDate=2023-11-01", nil) + req, newRequestErr := http.NewRequest("GET", BaseURL+"/users/"+strconv.Itoa(userId)+"/xp_summaries?startDate="+utils.FormatBeginningOfMonth(utils.GetBeginningOfMonth()), nil) if newRequestErr != nil { println("Error creating request") return 0 diff --git a/utils/date.go b/utils/date.go index 53a77b4..1445182 100644 --- a/utils/date.go +++ b/utils/date.go @@ -8,3 +8,7 @@ func GetBeginningOfMonth() time.Time { now := time.Now() return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC) } + +func FormatBeginningOfMonth(date time.Time) string { + return date.Format("2006-01-02") +} diff --git a/utils/date_test.go b/utils/date_test.go index 4c2d102..fbe7549 100644 --- a/utils/date_test.go +++ b/utils/date_test.go @@ -1,10 +1,12 @@ package utils -import "testing" +import ( + "testing" + "time" +) func TestGetBeginningOfMonth(t *testing.T) { date := GetBeginningOfMonth() - println(date.String()) if date.Day() != 1 { t.Fatalf("Expected day to be 1, got %d", date.Day()) } @@ -21,3 +23,10 @@ func TestGetBeginningOfMonth(t *testing.T) { t.Fatalf("Expected nanosecond to be 0, got %d", date.Nanosecond()) } } + +func TestFormatBeginningOfMonth(t *testing.T) { + date := FormatBeginningOfMonth(time.Date(2021, 10, 1, 0, 0, 0, 0, time.UTC)) + if date != "2021-10-01" { + t.Fatalf("Expected date to be 2021-10-01, got %s", date) + } +}