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

Add optional time zone information for DATETIME database colums. #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add optional time zone information for DATETIME database colums. #80

wants to merge 2 commits into from

Conversation

seehuhn
Copy link

@seehuhn seehuhn commented Aug 27, 2013

MySQL DATETIME columns store times without information about the time zone the time should be interpreted in. This commit adds the posibility to specify which time zone is used for columns accessed via a mymysql database connection. If a timezone is specified, times are converted to the correct time zone before being sent to the database, and are interpreted as being in this time zone when returned from the database. If no time zone is set, all times are assumed to be in the current local time zone.

There are two methods for specifying the time zone. The new global variable mysql.DefaultTimeZone can be set to set the timezone for all database connections opened afterwards. A per-connection time zone can be chosen by setting the new native.Conn.TimeZone field.

MySQL DATETIME columns store times without information about the time
zone the time should be interpreted in.  This commit adds the
posibility to specify which time zone is used for columns accessed via
a mymysql database connection.  If a timezone is specified, times are
converted to the correct time zone before being sent to the database,
and are interpreted as being in this time zone when returned from the
database.  If no time zone is set, all times are assumed to be in the
current local time zone.

There are two methods for specifying the time zone.  The new global
variable mysql.DefaultTimeZone can be set to set the timezone for all
database connections opened afterwards.  A per-connection time zone
can be chosen by setting the new native.Conn.TimeZone field.
@jszwedko
Copy link

We ran into this issue as well (the application writing data to the database we are reading from is in UTC, but the driver reads it as time.Local); would it be possible to get this reviewed and merged in @ziutek ?

@ziutek
Copy link
Owner

ziutek commented Mar 20, 2014

Your timezone information is lost when your write data to the database (there is no any TZ info in MySQL).

Another problem is that MySQL supports text queries, that can contain date. Such date can't be handled by any driver.

There is no general solution for this problem, see: #77

The best way to reliably store date and time in MySQL database is store the Unix timestamp (see Time.Unix() function) and optionaly TZ info in separate column. Using MySQL DATETIME can always cause problems in special cases.

@jszwedko
Copy link

I agree that a better solution is to write the dates as unix timestamps, but we are working against a legacy system's database where we cannot make such a change.

The driver cannot inherently know the timezone the date fields are in, but I think a reasonable compromise is to be able to inform the driver what timezone to interpret the datetimes as (which is what this PR does). For example, we know that all of our datetime fields are UTC so it would be nice to be able to read them from the database as such and not need to make the conversion after the fact.

I also feel like time.Local is a bad default because this can change depending on where the application is being ran.

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

Successfully merging this pull request may close these issues.

3 participants