2024-03-27 23:08:40 +00:00
|
|
|
package cif
|
|
|
|
|
|
|
|
import (
|
2024-03-27 23:35:03 +00:00
|
|
|
"errors"
|
2024-03-27 23:08:40 +00:00
|
|
|
"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 {
|
2024-03-27 23:08:40 +00:00
|
|
|
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)
|
2024-03-27 23:08:40 +00:00
|
|
|
day := timeNow.Weekday()
|
|
|
|
|
|
|
|
dayStrings := [...]string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"}
|
|
|
|
|
|
|
|
return dayStrings[day]
|
|
|
|
}
|
2024-03-27 23:35:03 +00:00
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Simply returns the correct URL for either a 'daily' or 'full' update.
|
2024-03-27 23:35:03 +00:00
|
|
|
func getUpdateUrl(updateType string) (string, error) {
|
|
|
|
if updateType == "daily" {
|
2024-03-30 01:09:12 +00:00
|
|
|
return dailyUpdateUrl, nil
|
2024-03-27 23:35:03 +00:00
|
|
|
} 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'")
|
2024-03-27 23:35:03 +00:00
|
|
|
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 {
|
2024-04-08 21:08:07 +01:00
|
|
|
log.Msg.Debug("Calculating how many days ago", zap.Time("Input time", t))
|
|
|
|
// Truncate both times to midnight in UTC timezone
|
|
|
|
today := time.Now().UTC().Truncate(24 * time.Hour)
|
|
|
|
input := t.UTC().Truncate(24 * time.Hour)
|
2024-03-30 01:09:12 +00:00
|
|
|
|
|
|
|
diff := today.Sub(input)
|
2024-04-08 21:08:07 +01:00
|
|
|
days := int(diff / (24 * time.Hour))
|
2024-03-30 01:09:12 +00:00
|
|
|
return days
|
|
|
|
}
|
2024-04-04 22:39:09 +01:00
|
|
|
|
|
|
|
// Generates a slice of time.Time values representing which days files need downloading
|
|
|
|
func generateUpdateDays(days int) []time.Time {
|
|
|
|
var updateDays []time.Time
|
|
|
|
|
|
|
|
for i := 0; i < days; i++ {
|
|
|
|
day := time.Now().Add(-time.Duration(i) * 24 * time.Hour)
|
|
|
|
updateDays = append(updateDays, day)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse slice to ensure chronological order
|
|
|
|
for i, j := 0, len(updateDays)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
updateDays[i], updateDays[j] = updateDays[j], updateDays[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateDays
|
|
|
|
}
|
2024-04-05 21:42:00 +01:00
|
|
|
|
|
|
|
// Parses CIF Schedule Start/End Dates (YYYY-MM-DD) into time.Time types (00:00:00 for start, 23:59:59 for end)
|
2024-04-07 20:59:41 +01:00
|
|
|
func ParseCifDate(input *string, startOrEnd string) time.Time {
|
2024-04-05 21:42:00 +01:00
|
|
|
layout := "2006-01-02" // Layout of input
|
2024-04-07 20:59:41 +01:00
|
|
|
t, err := time.ParseInLocation(layout, *input, londonTimezone)
|
2024-04-05 21:42:00 +01:00
|
|
|
if err != nil {
|
2024-04-07 20:59:41 +01:00
|
|
|
log.Msg.Error("Error parsing date string", zap.String("date string", *input), zap.Error(err))
|
2024-04-05 21:42:00 +01:00
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if startOrEnd == "start" {
|
|
|
|
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, londonTimezone)
|
|
|
|
} else if startOrEnd == "end" {
|
|
|
|
t = time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, londonTimezone)
|
|
|
|
} else {
|
2024-04-07 20:59:41 +01:00
|
|
|
log.Msg.Error("Error parsing date string", zap.String("date string", *input), zap.Error(err))
|
2024-04-05 21:42:00 +01:00
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses CIF days_run field and converts to array of day strings
|
2024-04-07 20:59:41 +01:00
|
|
|
func parseDaysRun(daysBinary *string) []string {
|
2024-04-05 21:42:00 +01:00
|
|
|
shortDays := []string{"m", "t", "w", "th", "f", "s", "su"}
|
|
|
|
var result []string
|
2024-04-07 20:59:41 +01:00
|
|
|
for i, digit := range *daysBinary {
|
2024-04-05 21:42:00 +01:00
|
|
|
if digit == '1' {
|
|
|
|
result = append(result, shortDays[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|