added timezonemanagment to timezone fix

This commit is contained in:
Jannover5000r 2026-04-13 07:58:48 +00:00
parent 3bf0158260
commit a62e91b30b
3 changed files with 30 additions and 7 deletions

7
.exampleEnv Normal file
View file

@ -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()]

View file

@ -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 ## 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 # 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

28
main.go
View file

@ -13,6 +13,7 @@ import (
"strings" "strings"
"syscall" "syscall"
"time" "time"
_ "time/tzdata"
Untis "untislogger/Untis" Untis "untislogger/Untis"
@ -34,6 +35,8 @@ type NamedTimetableEntry struct {
ActivityType string `json:"activityType"` ActivityType string `json:"activityType"`
} }
var location *time.Location
// lessonKey creates a stable key for a lesson (date + time + subject) // lessonKey creates a stable key for a lesson (date + time + subject)
func lessonKey(e NamedTimetableEntry) string { func lessonKey(e NamedTimetableEntry) string {
subject := "unknown" subject := "unknown"
@ -98,6 +101,20 @@ func init() {
} else { } else {
log.Println("No Discord webhook provided, Discord notifications will be disabled") 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() { func main() {
@ -304,7 +321,7 @@ func scheduleTimetableUpdate() {
updateAllUsers() updateAllUsers()
// Ticker for periodic updates // 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() { go func() {
for range hourTicker.C { for range hourTicker.C {
updateAllUsers() updateAllUsers()
@ -313,8 +330,7 @@ func scheduleTimetableUpdate() {
// Ticker for scheduled lesson notifications // Ticker for scheduled lesson notifications
startMinuteTicker(func() { startMinuteTicker(func() {
now := time.Now() now := getTime()
now = now.Add(1 * time.Hour)
if isScheduledTime(now) { if isScheduledTime(now) {
log.Println("Scheduled time reached, running notifications...") log.Println("Scheduled time reached, running notifications...")
accounts, _ := BotStart.LoadAndDecryptAccounts() accounts, _ := BotStart.LoadAndDecryptAccounts()
@ -329,7 +345,7 @@ func scheduleTimetableUpdate() {
} }
func startMinuteTicker(f func()) { func startMinuteTicker(f func()) {
now := time.Now() now := getTime()
next := now.Truncate(time.Minute).Add(time.Minute) next := now.Truncate(time.Minute).Add(time.Minute)
time.Sleep(time.Until(next)) time.Sleep(time.Until(next))
ticker := time.NewTicker(1 * time.Minute) ticker := time.NewTicker(1 * time.Minute)
@ -358,9 +374,7 @@ func Run(userID string) {
roomByStartTime, _ := MapTimeToRoom(timetableFile) roomByStartTime, _ := MapTimeToRoom(timetableFile)
subjectByStartTime, _ := MapTimeToSubject(timetableFile) subjectByStartTime, _ := MapTimeToSubject(timetableFile)
nowOld := time.Now() now := getTime().Format("15:04")
nowOld = nowOld.Add(1 * time.Hour)
now := nowOld.Format("15:04")
nextTime, room, foundRoom := NextRoomForTime(roomByStartTime, now) nextTime, room, foundRoom := NextRoomForTime(roomByStartTime, now)
if !foundRoom { if !foundRoom {