Code Monkey home page Code Monkey logo

Comments (3)

GoogleCodeExporter avatar GoogleCodeExporter commented on May 26, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 26, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 26, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.