Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go MySql driver doesn't set time correctly

Tags:

mysql

go

I have been developing a micro service that interact mysql for a while in golang, and i love this talented language. Anyway have a problem and do not know where is the problem, in my code, in mysql driver else in mysql. So my machine timezone utc+3, i am sharing some result may be it helps

//created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
mysql> select now();
"2016-11-07 22:43:02", //that is correct.

in go

fmt.PrintLn(time.Now().Local())
"2016-11-07 22:51:02" //that is correct too

But when i added the entity into db, mysql workbench shows me wrong datetime.

"2016-11-07 19:51:02" // 

Go code:

func (this *AppLogHandler) addLog(_log *AppLog) (int64, error){
    fmt.Println("addLog")
    db:= this.Context.DB
    stmt, err := db.Prepare("INSERT tbl_logs SET user_id=?,ip_proxy=?, ip_original=?, end_point=?, http_method=?, message=?, status=?, created_date=?")
    if(err != nil){
        log.Println(err)
        return -1, err
    }
    defer stmt.Close()
    res, err := stmt.Exec(&_log.UserID, &_log.IPProxy, &_log.IPOriginal, &_log.Endpoint, &_log.HttpMethod, &_log.Message, &_log.Status, &_log.CreatedDate)
    if(err != nil){
        log.Println(err)
        return -1, err
    }
    return res.LastInsertId()
}

/// some code here
    app_log := AppLog{}
    app_log.IPProxy = r.RemoteAddr
    app_log.IPOriginal = r.Header.Get("X-Forwarded-For")
    app_log.CreatedDate = time.Now().Local()
    app_log.UserID = user_id
    app_log.Endpoint = r.URL.Path
    app_log.HttpMethod = r.Method
    fmt.Println(app_log.CreatedDate)
    return this.addLog(&app_log)

So guys i need your helps. I couldn't solve the problem for hours.

mysql=>  Ver 14.14 Distrib 5.7.15, for osx10.11 (x86_64) using  EditLine wrapper
go => 1.7
mysql driver => 1.2, https://github.com/go-sql-driver/mysql/
like image 766
RockOnGom Avatar asked Nov 07 '16 19:11

RockOnGom


People also ask

How do I set UTC time in MySQL?

Option 2: Edit the MySQL Configuration File Scroll down to the [mysqld] section, and find the default-time-zone = "+00:00" line. Change the +00:00 value to the GMT value for the time zone you want. Save the file and exit. In the example below we set the MySQL Server time zone to +08:00 (GMT +8).

How do I change the default time zone in MySQL?

To explicitly specify the system time zone for MySQL Server at startup, set the TZ environment variable before you start mysqld. If you start the server using mysqld_safe, its --timezone option provides another way to set the system time zone. The permissible values for TZ and --timezone are system dependent.

Does MySQL store datetime as UTC?

MySQL Column Types Range: 1970-01-01 00:00:01 to 2038-01-19 03:14:07 for TIMESTAMP ; 1000-01-01 to 9999-12-31 for DATETIME . Time zones: TIMESTAMP values will be converted to UTC for storage and from UTC for retrieval, which can lead to reading different values.

What timezone does MySQL use?

By default, the time zone for a MySQL DB instance is Universal Time Coordinated (UTC). You can set the time zone for your DB instance to the local time zone for your application instead.


1 Answers

The mysql driver has a configuration parameter for the default time zone, which you can set to time.Local (the default is time.UTC). When you are saving the value, it first converts the time stamp to the UTC time zone and then sends it off to the database.

As has been already stated in the comments, a much more robust approach would be to accept the default Loc, and standardize on UTC in the database. This greatly simplifies anything having to do with date math further down the line, and doesn't make assumptions about the time zone of the person viewing the data if you just convert it from UTC to local when displaying the value.

like image 193
nothingmuch Avatar answered Sep 19 '22 13:09

nothingmuch