Comments (3)
I can see your problem, but I don't think MS SQL Server stores "timezone"
information anywhere in "datetime" or "datetime2" fields. So I don't see how
odbc driver can "recover" that information from somewhere. If you do store
"timezone" information somewhere (even by assuming that everything is in UTC or
any other specific timezone), then it becomes your responsibility to handle
that properly.
As to using time.Local when constructing dates, I had to come-up with a
reasonable default. I made it so MS SQL Server gettime returns my current date
and time. To demonstrate, this test:
# hg diff
diff -r 79ae99114308 mssql_test.go
--- a/mssql_test.go Fri Feb 07 12:39:51 2014 +1100
+++ b/mssql_test.go Wed Apr 02 10:32:02 2014 +1100
@@ -1218,3 +1218,18 @@
t.Fatal("comparison fails")
}
}
+
+func TestMSSQLALEX(t *testing.T) {
+ db, sc, err := mssqlConnect()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer closeDB(t, db, sc, sc)
+
+ var dt time.Time
+ err = db.QueryRow("select getdate()").Scan(&dt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Logf("%v", dt)
+}
and I see this output:
# go test -mssrv=aaa -msdb=golang -msuser=gopher -mspass=password -v -run=ALEX
=== RUN TestMSSQLALEX
--- PASS: TestMSSQLALEX (0.01 seconds)
mssql_test.go:1234: 2014-04-02 10:32:05.747 +1100 EST
PASS
ok code.google.com/p/odbc 0.010s
The time on this output is the same as my computer clock.
If you have alternative proposal, I am listening. Thank you.
Alex
Original comment by [email protected]
on 2 Apr 2014 at 1:12
- Changed state: WaitingForReply
from odbc.
Hi Alex, yes I agree that you've selected a sensible default. I think this is
more of an inconvenience in my specific situation than a real issue but it
would be nice to have a way to provide a default timezone to the connection so
that:
r := time.Date(int(t.Year), time.Month(t.Month), int(t.Day),
int(t.Hour), int(t.Minute), int(t.Second), int(t.Fraction),
conn.DefaultTimeZone)
But the connection doesn't get passed to the column so that is not simple.
So thanks for the reply, OK by me to close this issue. Thanks for this library!
Original comment by [email protected]
on 2 Apr 2014 at 1:15
from odbc.
You can do something like that
diff --git a/mssql_test.go b/mssql_test.go
--- a/mssql_test.go
+++ b/mssql_test.go
@@ -1218,3 +1218,35 @@
t.Fatal("comparison fails")
}
}
+
+type UTCTime time.Time
+
+// Scan implements the Scanner interface.
+func (utc *UTCTime) Scan(value interface{}) error {
+ t, istime := value.(time.Time)
+ if !istime {
+ return errors.New("Not a time.")
+ }
+ newt := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(),
t.Second(), t.Nanosecond(), time.UTC)
+ *utc = UTCTime(newt)
+ return nil
+}
+
+func (utc UTCTime) String() string {
+ return time.Time(utc).String()
+}
+
+func TestMSSQLALEX(t *testing.T) {
+ db, sc, err := mssqlConnect()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer closeDB(t, db, sc, sc)
+
+ var dt UTCTime
+ err = db.QueryRow("select getdate()").Scan(&dt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Logf("dt=%v", dt)
+}
to change timezone under the covers. Maybe this is more "convenient" for you.
Alternatively (since you use recent version of MS SQL Server), you can use
datetimeoffset field. I haven't used it myself, and code.google.com/p/odbc
package does not support it. I might implement it one day, but I don't have
time to do it now.
Alex
Original comment by [email protected]
on 4 Apr 2014 at 12:45
- Changed state: WontFix
from odbc.
Related Issues (20)
- Sorry to necro an old issue, but I am running into this myself and think I can probably spin up a PR for it. The MySQL package achieves this by having the database timezone/location provided in the DSN: HOT 1
- Incorrect date time value returned HOT 3
- Cannot connect tibero database with go lang HOT 8
- DB2 on Windows Server 2012 R2 connect but truncate data retreived HOT 1
- DB2 for i: Simple select with empty ResultSet locks table HOT 1
- Compile errors when building from Windows OS with GOOS=linux HOT 2
- an error of SQL syntax error HOT 1
- Panic when binding `varchar`/`string` types: slice bounds out of range [:668] with capacity 256 HOT 3
- memory leak using rows.next() HOT 7
- signal 13 received but handler not on signal stack HOT 4
- Low max message length HOT 3
- Add tags to the repo HOT 1
- Implement QueryerContext interface HOT 1
- Implement RowsColumnTypeDatabaseTypeName HOT 1
- error when build from linux HOT 2
- only half of unicode string is inserted.
- Issue with Unixodbc-2.3.7 for master and for_issue_88 branch HOT 1
- 向 varchar字段保存中文中会被截断 HOT 8
- Maintain releases HOT 5
- The package is returning a panic instead of an error HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from odbc.