timetable-mgr/src/cif/helpers.go

54 lines
1.4 KiB
Go
Raw Normal View History

package cif
import (
"errors"
"time"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
2024-03-30 01:09:12 +00:00
// Fetches the day string for the provided date.
func getDayString(t time.Time) string {
london, err := time.LoadLocation("Europe/London")
if err != nil {
log.Msg.Error("Unable to load time zone info", zap.Error(err))
}
2024-03-30 01:09:12 +00:00
timeNow := t.In(london)
day := timeNow.Weekday()
dayStrings := [...]string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"}
return dayStrings[day]
}
// Simply returns the correct URL for either a 'daily' or 'full' update.
func getUpdateUrl(updateType string) (string, error) {
if updateType == "daily" {
2024-03-30 01:09:12 +00:00
return dailyUpdateUrl, nil
} else if updateType == "full" {
return fullUpdateUrl, nil
}
2024-03-28 22:47:08 +00:00
err := errors.New("invalid update type provided, must be one of 'daily' or 'full'")
return "", err
}
2024-03-30 01:09:12 +00:00
// Takes a time.Time as input and returns True if it is
// the same day as now, or false if it is not the same day as now
func isSameToday(t time.Time) bool {
2024-04-04 13:58:18 +01:00
test := t.In(time.UTC)
2024-03-30 01:09:12 +00:00
today := time.Now().In(time.UTC)
2024-04-04 13:58:18 +01:00
return test.Year() == today.Year() && test.Month() == today.Month() && test.Day() == today.Day()
2024-03-30 01:09:12 +00:00
}
// Returns how many days ago `t` was compared to today
func howManyDaysAgo(t time.Time) int {
today := time.Now().In(time.UTC).Truncate(24 * time.Hour)
input := t.In(time.UTC).Truncate(24 * time.Hour)
diff := today.Sub(input)
days := int(diff.Hours() / 24)
return days
}