pass timezone env var to dsn string
All checks were successful
pedestrian-simulator / build (push) Successful in 58s
All checks were successful
pedestrian-simulator / build (push) Successful in 58s
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -25,7 +26,12 @@ func InitDB() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DB] Using timezone: %s (Local=%s)", os.Getenv("TZ"), time.Local.String())
|
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)
|
// Default to UTC if TZ is empty, otherwise use the encoded TZ name
|
||||||
|
tzParam := "UTC"
|
||||||
|
if os.Getenv("TZ") != "" {
|
||||||
|
tzParam = url.QueryEscape("'" + os.Getenv("TZ") + "'")
|
||||||
|
}
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&loc=Local&time_zone=%s", user, pass, host, port, name, tzParam)
|
||||||
var err error
|
var err error
|
||||||
db, err = sql.Open("mysql", dsn)
|
db, err = sql.Open("mysql", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user