Temp timezone fix #11

Merged
Jannover5000r merged 22 commits from temp-timezoneFix into main 2026-08-23 00:02:29 +02:00
2 changed files with 20 additions and 14 deletions
Showing only changes of commit c9a5dd2ff5 - Show all commits

View file

@ -12,6 +12,7 @@ import (
"io" "io"
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@ -108,6 +109,12 @@ func decrypt(encrypted string) (string, error) {
func saveAccount(userID, username, password string) error { func saveAccount(userID, username, password string) error {
var accounts []Account var accounts []Account
// Ensure the accounts directory exists
accountsDir := filepath.Dir(accountsFile)
if err := os.MkdirAll(accountsDir, 0755); err != nil {
return fmt.Errorf("failed to create accounts directory: %v", err)
}
// Load existing accounts if file exists // Load existing accounts if file exists
if data, err := os.ReadFile(accountsFile); err == nil && len(data) > 0 { if data, err := os.ReadFile(accountsFile); err == nil && len(data) > 0 {
_ = json.Unmarshal(data, &accounts) _ = json.Unmarshal(data, &accounts)

27
main.go
View file

@ -101,7 +101,6 @@ func init() {
} }
func main() { func main() {
godotenv.Load(".env") godotenv.Load(".env")
// Untis.Main() //starting API calls function| happens in schedule func // Untis.Main() //starting API calls function| happens in schedule func
// Run() // Run()
@ -316,7 +315,7 @@ func Run(userID string) {
nowOld := time.Now() nowOld := time.Now()
nowOld = nowOld.Add(1 * time.Hour) nowOld = nowOld.Add(1 * time.Hour)
now := nowOld.Format("15:04") now := nowOld.Format("15:04")
nextTime, room, foundRoom := NextRoomForTime(roomByStartTime, now) nextTime, room, foundRoom := NextRoomForTime(roomByStartTime, now)
if !foundRoom { if !foundRoom {
return // No upcoming lessons return // No upcoming lessons
@ -355,10 +354,10 @@ func NextRoomForTime(roomByStartTime map[string]string, current string) (string,
if err != nil { if err != nil {
return "", "", false return "", "", false
} }
// Calculate 30 minutes from now // Calculate 30 minutes from now
thirtyMinutesLater := now.Add(30 * time.Minute) thirtyMinutesLater := now.Add(30 * time.Minute)
var times []time.Time var times []time.Time
timeToStr := make(map[time.Time]string) timeToStr := make(map[time.Time]string)
for t := range roomByStartTime { for t := range roomByStartTime {
@ -372,11 +371,11 @@ func NextRoomForTime(roomByStartTime map[string]string, current string) (string,
timeToStr[parsed] = t timeToStr[parsed] = t
} }
} }
if len(times) == 0 { if len(times) == 0 {
return "", "", false return "", "", false
} }
sort.Slice(times, func(i, j int) bool { return times[i].Before(times[j]) }) sort.Slice(times, func(i, j int) bool { return times[i].Before(times[j]) })
// Return the first (earliest) room within the 30-minute window // Return the first (earliest) room within the 30-minute window
return timeToStr[times[0]], roomByStartTime[timeToStr[times[0]]], true return timeToStr[times[0]], roomByStartTime[timeToStr[times[0]]], true
@ -388,10 +387,10 @@ func NextSubjectForTime(subjectByStartTime map[string]string, current string) (s
if err != nil { if err != nil {
return "", false return "", false
} }
// Calculate 30 minutes from now // Calculate 30 minutes from now
thirtyMinutesLater := now.Add(30 * time.Minute) thirtyMinutesLater := now.Add(30 * time.Minute)
var times []time.Time var times []time.Time
timeToStr := make(map[time.Time]string) timeToStr := make(map[time.Time]string)
for t := range subjectByStartTime { for t := range subjectByStartTime {
@ -405,11 +404,11 @@ func NextSubjectForTime(subjectByStartTime map[string]string, current string) (s
timeToStr[parsed] = t timeToStr[parsed] = t
} }
} }
if len(times) == 0 { if len(times) == 0 {
return "", false return "", false
} }
sort.Slice(times, func(i, j int) bool { return times[i].Before(times[j]) }) sort.Slice(times, func(i, j int) bool { return times[i].Before(times[j]) })
// Return the first (earliest) subject within the 30-minute window // Return the first (earliest) subject within the 30-minute window
return subjectByStartTime[timeToStr[times[0]]], true return subjectByStartTime[timeToStr[times[0]]], true
@ -421,10 +420,10 @@ func NextCodeForTime(codeByStartTime map[string]string, current string) (string,
if err != nil { if err != nil {
return "", false return "", false
} }
// Calculate 30 minutes from now // Calculate 30 minutes from now
thirtyMinutesLater := now.Add(30 * time.Minute) thirtyMinutesLater := now.Add(30 * time.Minute)
var times []time.Time var times []time.Time
timeToStr := make(map[time.Time]string) timeToStr := make(map[time.Time]string)
for t := range codeByStartTime { for t := range codeByStartTime {
@ -438,11 +437,11 @@ func NextCodeForTime(codeByStartTime map[string]string, current string) (string,
timeToStr[parsed] = t timeToStr[parsed] = t
} }
} }
if len(times) == 0 { if len(times) == 0 {
return "", false return "", false
} }
sort.Slice(times, func(i, j int) bool { return times[i].Before(times[j]) }) sort.Slice(times, func(i, j int) bool { return times[i].Before(times[j]) })
// Return the first (earliest) code within the 30-minute window // Return the first (earliest) code within the 30-minute window
return codeByStartTime[timeToStr[times[0]]], true return codeByStartTime[timeToStr[times[0]]], true