Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format of date or time is not correct #101

Open
xaququ opened this issue Dec 27, 2017 · 1 comment
Open

format of date or time is not correct #101

xaququ opened this issue Dec 27, 2017 · 1 comment

Comments

@xaququ
Copy link

xaququ commented Dec 27, 2017

--------correct with mysql driver--------------
c1=2017-12-27
c2=10:12:59
c3=2017-12-27 10:12:59
c4=2017-12-31 23:59:59
-------incorrect with odbc driver--------------
c1=2017-12-27T00:00:00+08:00
c2=0001-01-01T10:12:59+08:00
c3=2017-12-27T10:12:59+08:00
c4=2017-12-31T23:59:59+08:00

test case as following

package main

import (
"database/sql"
"fmt"

_ "github.com/alexbrainman/odbc"
_ "github.com/go-sql-driver/mysql"

)

func main() {
conn, err := sql.Open("odbc", "DSN=mysql;UID=root;PWD=mysql")
//conn, err := sql.Open("mysql", "root:mysql@/mysql")
conn.Exec("create table timeformat(c1 date,c2 time,c3 timestamp,c4 datetime)")
conn.Exec("insert into timeformat values('2017-12-27','10:12:59','2017-12-27 10:12:59','2017-12-31 23:59:59')")
rows, err := conn.Query("select * from timeformat")
if err != nil {
panic(err)
}
var c1, c2, c3, c4 string
for rows.Next() {
err = rows.Scan(&c1, &c2, &c3, &c4)
fmt.Printf("c1=%v\nc2=%v\nc3=%v\nc4=%v\n", c1, c2, c3, c4)
}
conn.Exec("drop table timeformat")
conn.Close()
}

@alexbrainman
Copy link
Owner

I don't think there is a problem here. For example, c1 database field is 'date'. And you have asked ODBC convert that date into string (by supplying string c1 variable to rows.Scan). And ODBC driver converted date into '2017-12-27T00:00:00+08:00'. Why do you think converting to '2017-12-27' string (what mysql driver did) is better than converting into '2017-12-27T00:00:00+08:00' string? If you want to control the conversion, you should use c1 of time.Time type and then convert it into whatever string you need.

Please let me know if I am missing something here. Thank you.

Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants