Skip to content

Commit

Permalink
refactor: use current year starting from march
Browse files Browse the repository at this point in the history
  • Loading branch information
acifani committed Jan 10, 2025
1 parent 7b49a4c commit fbe82f5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pkg/api/ergast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ import (

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

var currentYear = time.Now().Year()
// getCurrentSeason returns the active F1 season year
func getCurrentSeason() int {
now := time.Now()
year := now.Year()

// F1 season typically starts in March
if now.Month() < time.March {
return year - 1
}
return year
}

func GetLatestRaceResult() (*RaceTable, error) {
result := RaceResultResponse{}
err := apiCall(fmt.Sprintf("/%d/last/results", currentYear), &result)
err := apiCall(fmt.Sprintf("/%d/last/results", getCurrentSeason()), &result)
if err != nil {
return nil, err
}
Expand All @@ -22,7 +32,7 @@ func GetLatestRaceResult() (*RaceTable, error) {

func GetCurrentDriverStandings() (*DriverStandingsTable, error) {
result := DriverStandingsResponse{}
err := apiCall(fmt.Sprintf("/%d/driverstandings", currentYear), &result)
err := apiCall(fmt.Sprintf("/%d/driverstandings", getCurrentSeason()), &result)
if err != nil {
return nil, err
}
Expand All @@ -31,7 +41,7 @@ func GetCurrentDriverStandings() (*DriverStandingsTable, error) {

func GetCurrentConstructorStandings() (*ConstructorStandingsTable, error) {
result := ConstructorStandingsResponse{}
err := apiCall(fmt.Sprintf("/%d/constructorstandings", currentYear), &result)
err := apiCall(fmt.Sprintf("/%d/constructorstandings", getCurrentSeason()), &result)
if err != nil {
return nil, err
}
Expand All @@ -40,7 +50,7 @@ func GetCurrentConstructorStandings() (*ConstructorStandingsTable, error) {

func GetCurrentSeasonSchedule() (*ScheduleTable, error) {
result := ScheduleResponse{}
err := apiCall("/current", &result)
err := apiCall(fmt.Sprintf("/%d", getCurrentSeason()), &result)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fbe82f5

Please sign in to comment.