Skip to content

Commit fbe82f5

Browse files
committed
refactor: use current year starting from march
1 parent 7b49a4c commit fbe82f5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

pkg/api/ergast.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@ import (
99

1010
const baseURL = "https://api.jolpi.ca/ergast/f1"
1111

12-
var currentYear = time.Now().Year()
12+
// getCurrentSeason returns the active F1 season year
13+
func getCurrentSeason() int {
14+
now := time.Now()
15+
year := now.Year()
16+
17+
// F1 season typically starts in March
18+
if now.Month() < time.March {
19+
return year - 1
20+
}
21+
return year
22+
}
1323

1424
func GetLatestRaceResult() (*RaceTable, error) {
1525
result := RaceResultResponse{}
16-
err := apiCall(fmt.Sprintf("/%d/last/results", currentYear), &result)
26+
err := apiCall(fmt.Sprintf("/%d/last/results", getCurrentSeason()), &result)
1727
if err != nil {
1828
return nil, err
1929
}
@@ -22,7 +32,7 @@ func GetLatestRaceResult() (*RaceTable, error) {
2232

2333
func GetCurrentDriverStandings() (*DriverStandingsTable, error) {
2434
result := DriverStandingsResponse{}
25-
err := apiCall(fmt.Sprintf("/%d/driverstandings", currentYear), &result)
35+
err := apiCall(fmt.Sprintf("/%d/driverstandings", getCurrentSeason()), &result)
2636
if err != nil {
2737
return nil, err
2838
}
@@ -31,7 +41,7 @@ func GetCurrentDriverStandings() (*DriverStandingsTable, error) {
3141

3242
func GetCurrentConstructorStandings() (*ConstructorStandingsTable, error) {
3343
result := ConstructorStandingsResponse{}
34-
err := apiCall(fmt.Sprintf("/%d/constructorstandings", currentYear), &result)
44+
err := apiCall(fmt.Sprintf("/%d/constructorstandings", getCurrentSeason()), &result)
3545
if err != nil {
3646
return nil, err
3747
}
@@ -40,7 +50,7 @@ func GetCurrentConstructorStandings() (*ConstructorStandingsTable, error) {
4050

4151
func GetCurrentSeasonSchedule() (*ScheduleTable, error) {
4252
result := ScheduleResponse{}
43-
err := apiCall("/current", &result)
53+
err := apiCall(fmt.Sprintf("/%d", getCurrentSeason()), &result)
4454
if err != nil {
4555
return nil, err
4656
}

0 commit comments

Comments
 (0)