Add info on database timezone configuration during application init
All checks were successful
pedestrian-simulator / build (push) Successful in 1m0s

This commit is contained in:
2026-01-11 20:50:08 -07:00
parent c94f2d8bb5
commit 123f3432ee
2 changed files with 22 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"time"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@@ -23,7 +24,8 @@ func InitDB() {
return return
} }
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&loc=Local", user, pass, host, port, name) log.Printf("[DB] Using timezone: %s (Local=%s)", os.Getenv("TZ"), time.Local.String())
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&loc=Local&time_zone='Local'", user, pass, host, port, name)
var err error var err error
db, err = sql.Open("mysql", dsn) db, err = sql.Open("mysql", dsn)
if err != nil { if err != nil {
@@ -35,6 +37,21 @@ func InitDB() {
} }
log.Println("Connected to MariaDB successfully") log.Println("Connected to MariaDB successfully")
// Verify and enforce timezones
var dbNow, dbTZ string
err = db.QueryRow("SELECT NOW(), @@session.time_zone").Scan(&dbNow, &dbTZ)
if err == nil {
log.Printf("[DB] Before setting: NOW()=%s, session.time_zone=%s", dbNow, dbTZ)
}
if _, err := db.Exec("SET time_zone = SYSTEM"); err != nil {
log.Printf("[DB] Warning: Failed to set session time_zone to SYSTEM: %v", err)
} else {
db.QueryRow("SELECT NOW(), @@session.time_zone").Scan(&dbNow, &dbTZ)
log.Printf("[DB] After setting: NOW()=%s, session.time_zone=%s", dbNow, dbTZ)
}
createTables() createTables()
} }

View File

@@ -93,6 +93,10 @@ func (sm *StepManager) SaveTripState() {
} }
func (sm *StepManager) saveTripStateLocked() { func (sm *StepManager) saveTripStateLocked() {
fmt.Printf("[StepManager] Saving trip state for %s: StartTime=%v (%s), LastSync=%v (%s)\n",
sm.userID, sm.tripState.StartTime, sm.tripState.StartTime.Location(),
sm.lastSyncTime, sm.lastSyncTime.Location())
_, err := db.Exec(` _, err := db.Exec(`
INSERT INTO trips (user_id, start_date, start_time, start_day_initial_steps, previous_total_steps, target_total_steps, last_sync_time, next_sync_time) INSERT INTO trips (user_id, start_date, start_time, start_day_initial_steps, previous_total_steps, target_total_steps, last_sync_time, next_sync_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)