Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cchampou committed Nov 4, 2023
1 parent 5dcae9d commit 0d026a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion duolingo/users.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duolingo

import (
"duolingo/utils"
"encoding/json"
"net/http"
"strconv"
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions utils/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
13 changes: 11 additions & 2 deletions utils/date_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
Expand All @@ -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)
}
}

0 comments on commit 0d026a2

Please sign in to comment.