DSN for MySQL Location/Timezone In Golang

Muhammad Tri Wibowo
1 min readJul 31, 2019

--

Yeah, few moment ago, i got problem in storing time to MySQL, not an syntax error but time that stored is not as expected. My expectation is time that stored is in “Asia/Jakarta”, but in MySQL stored as time in UTC timezone.

i’ve already set timezone in my ubuntu server

sudo timedatectl set-timezone Asia/Jakarta

and i’m using golang as my programming language, and i’ve already set time in “Asia/Jakarta”,

timezoneLocString := “Asia/Jakarta”time.LoadLocation(timezoneLocString)

and also i’ve already set my MySQL timezone

SET GLOBAL time_zone = 'Asia/Jakarta';

but still, stored as UTC timezone.

so, i check my persistence library, i’m using GORM and MySQL driver Library, i check source code, https://github.com/go-sql-driver/mysql/blob/master/connection.go#L231

v := v.In(mc.cfg.Loc)

i got that mysql driver not read from system or mysql server timezone, but read it from “DSN” config.

so, i try modifying my DSN config

from

user:pass@tcp(127.0.0.1:3306)/dbname?parseTime=True&charset=utf8&autocommit=false

to

user:pass@tcp(127.0.0.1:3306)/dbname?parseTime=True&loc=Asia%2FJakarta&charset=utf8&autocommit=false

and voila, it’s working now as expected.

--

--

Responses (3)