From 9357f7381b1a648781e199d1ff9a51809984344b Mon Sep 17 00:00:00 2001 From: Jannover5000r <90698896+Jannover5000r@users.noreply.github.com> Date: Mon, 24 Aug 2026 02:36:41 +0200 Subject: [PATCH] Fixed timezone management timezones now get loaded by a function in /Untis Should make fetching happen at 0 am --- Untis/Timetable.go | 11 +++++++---- Untis/Untis.go | 11 +++++++++++ main.go | 27 +++++++++++++-------------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Untis/Timetable.go b/Untis/Timetable.go index 898cca4..e954edb 100644 --- a/Untis/Timetable.go +++ b/Untis/Timetable.go @@ -8,7 +8,6 @@ import ( "log" "net/http" "os" - "time" ) type TimetableResponse struct { @@ -100,7 +99,7 @@ func Timetable(cookies []*http.Cookie, userID string) { return } - today := time.Now().Format("20060102") + today := GetTime().Format("20060102") g := getTimetable{"2023-05-06 15:44:22.215292", "getTimetable", params{today, today, loginResult.PersonID, loginResult.PersonType}, "2.0"} timetablesJson, err := json.Marshal(g) if err != nil { @@ -148,7 +147,7 @@ func Timetable(cookies []*http.Cookie, userID string) { if userID != "" { timetableFile = "timetable_" + userID + ".json" } - if err := os.WriteFile(timetableFile, data, 0644); err != nil { + if err := os.WriteFile(timetableFile, data, 0o644); err != nil { log.Printf("Error writing timetable file: %v", err) return } @@ -172,6 +171,7 @@ func LoadIDMap(path string) (map[int]string, error) { } return m, nil } + func LoadTimetable(path string) ([]TimetableEntry, error) { data, err := os.ReadFile(path) if err != nil { @@ -183,11 +183,13 @@ func LoadTimetable(path string) ([]TimetableEntry, error) { } return entries, nil } + func formatTime(t int) string { h := t / 100 m := t % 100 return fmt.Sprintf("%02d:%02d", h, m) } + func formatDate(date int) string { s := fmt.Sprintf("%08d", date) // ensures leading zeros year := s[:4] @@ -195,6 +197,7 @@ func formatDate(date int) string { day := s[6:8] return fmt.Sprintf("%s-%s-%s", day, month, year) } + func setTimetable(userID string) { subjects, _ := LoadIDMap("subjects.json") rooms, _ := LoadIDMap("rooms.json") @@ -242,7 +245,7 @@ func setTimetable(userID string) { if userID != "" { timetableFilledFile = "timetableFilled_" + userID + ".json" } - if err := os.WriteFile(timetableFilledFile, data, 0644); err != nil { + if err := os.WriteFile(timetableFilledFile, data, 0o644); err != nil { log.Printf("Error writing timetableFilled file: %v", err) return } diff --git a/Untis/Untis.go b/Untis/Untis.go index 01f6324..5df1b28 100644 --- a/Untis/Untis.go +++ b/Untis/Untis.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "time" "github.com/joho/godotenv" ) @@ -32,6 +33,16 @@ type LoginResponse struct { var Url = os.Getenv("URL") +var location *time.Location + +func init() { + locEnv := os.Getenv("LOCATION_ENV") + if locEnv == "" { + locEnv = "UTC" + } + location, _ = time.LoadLocation(locEnv) +} + // var Password = os.Getenv("UNTIS_PASSWORD") // var USERS = os.Getenv("UNTIS_USER") func Main(user, password, userID string) { diff --git a/main.go b/main.go index fd60b0c..3a4b742 100644 --- a/main.go +++ b/main.go @@ -102,19 +102,15 @@ func init() { log.Println("No Discord webhook provided, Discord notifications will be disabled") } locEnv := os.Getenv("LOCATION_ENV") - - var locErr error - - location, locErr = time.LoadLocation(locEnv) - - if locErr != nil { - log.Fatalf("Failed to load timezone: %v", locErr) + var locerr error + Untis.Location, locerr = time.LoadLocation(locEnv) + if err != nil { + log.Fatalf("Failed to load Timezone: %v", locerr) } - log.Printf("Timezone set to: %s", location) } func getTime() time.Time { - return time.Now().In(location) + return Untis.GetTime() } func main() { @@ -208,9 +204,15 @@ func scheduleTimetableUpdate() { prevTimeslotMap := make(map[string]NamedTimetableEntry) // Get current date to filter out new day changes - today := time.Now().Format("02-01-2006") + today := getTime().Format("02-01-2006") // Get current time to filter any changes to Timetable before 3am so the new day changes stop - now := time.Now() + // + now := getTime() + // If before 3am stop and update userdata + if now.Hour() < 3 { + prevData[acc.UserID] = data + continue + } for _, e := range currentEntries { key := lessonKey(e) @@ -229,9 +231,6 @@ func scheduleTimetableUpdate() { if curr.Date != today { continue } - if now.Hour() < 3 { - continue - } if prev, exists := prevMap[key]; exists { // Existing lesson — check for changes