From a62e91b30bdcd1b50718c3cd128945ca1ec30661 Mon Sep 17 00:00:00 2001 From: Jannover5000r Date: Mon, 13 Apr 2026 07:58:48 +0000 Subject: [PATCH] added timezonemanagment to timezone fix --- .exampleEnv | 7 +++++++ README.md | 2 ++ main.go | 28 +++++++++++++++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 .exampleEnv diff --git a/.exampleEnv b/.exampleEnv new file mode 100644 index 0000000..a58fcf8 --- /dev/null +++ b/.exampleEnv @@ -0,0 +1,7 @@ +DISCORD_WEBHOOK_URL=[Paste Webhook] +UNTIS_PASSWORD=[Default user passwd] +UNTIS_USER=[Default user username] +DISCORD_BOT_TOKEN=[Paste Bot token] +ENC_KEY=[Generate enc key and paste] +URL=[Untis url, create from tutorial in readme] +LOCATION_ENV=Europe/Berlin [change to your timezone, needs to be usable by go's time package via time.LoadLocation()] diff --git a/README.md b/README.md index 96cd7fe..085dd0c 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,6 @@ ## In case your school is using a different timing for the Lessons than mine, you can change the times where you will be notified with the next room and Lesson for the day in **Line 71 in the main.go** file. The bot may not send you a message if there is no lesson after the specified time so that you can leave empty lessons as is for the days +## For your timezone setup an ENV called "LOCATION_ENV" with your timezone as explained in the example env file. In case your school is using a different timing for the Lessons than mine, you can change the times where you will be notified with the next room and Lesson for the day in **Line 71 in the main.go** file. The bot may not send you a message if there is no lesson after the specified time so that you can leave empty lessons as is for the days + # I am neither a representative of Untis, Untis Baden-Württemberg GmbH nor a Developer in their team. This project is based on their API and my code and is not affiliated with them diff --git a/main.go b/main.go index a09fb47..1e43345 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "strings" "syscall" "time" + _ "time/tzdata" Untis "untislogger/Untis" @@ -34,6 +35,8 @@ type NamedTimetableEntry struct { ActivityType string `json:"activityType"` } +var location *time.Location + // lessonKey creates a stable key for a lesson (date + time + subject) func lessonKey(e NamedTimetableEntry) string { subject := "unknown" @@ -98,6 +101,20 @@ func init() { } else { 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) + } + log.Printf("Timezone set to: %s", location) +} + +func getTime() time.Time { + return time.Now().In(location) } func main() { @@ -304,7 +321,7 @@ func scheduleTimetableUpdate() { updateAllUsers() // Ticker for periodic updates - hourTicker := time.NewTicker(1 * time.Minute) // For testing, 1 minute. Change to 1 * time.Hour for production. + hourTicker := time.NewTicker(30 * time.Minute) go func() { for range hourTicker.C { updateAllUsers() @@ -313,8 +330,7 @@ func scheduleTimetableUpdate() { // Ticker for scheduled lesson notifications startMinuteTicker(func() { - now := time.Now() - now = now.Add(1 * time.Hour) + now := getTime() if isScheduledTime(now) { log.Println("Scheduled time reached, running notifications...") accounts, _ := BotStart.LoadAndDecryptAccounts() @@ -329,7 +345,7 @@ func scheduleTimetableUpdate() { } func startMinuteTicker(f func()) { - now := time.Now() + now := getTime() next := now.Truncate(time.Minute).Add(time.Minute) time.Sleep(time.Until(next)) ticker := time.NewTicker(1 * time.Minute) @@ -358,9 +374,7 @@ func Run(userID string) { roomByStartTime, _ := MapTimeToRoom(timetableFile) subjectByStartTime, _ := MapTimeToSubject(timetableFile) - nowOld := time.Now() - nowOld = nowOld.Add(1 * time.Hour) - now := nowOld.Format("15:04") + now := getTime().Format("15:04") nextTime, room, foundRoom := NextRoomForTime(roomByStartTime, now) if !foundRoom {