开发者

grails/mysql timezone change

开发者 https://www.devze.com 2023-01-10 02:52 出处:网络
Whats the best way to accomplish changing the timezone of an app?The way I see it the following must occur:

Whats the best way to accomplish changing the timezone of an app? The way I see it the following must occur:

  1. Server TZ is changed by sys admin
  2. mysql must be restarted.
  3. every time based column in the database must have all values updated, using convert_tz or equivalent. So either a mysql script must be written or a grails script that loads every row for each class, updating all the time fields.

Obviously the server sho开发者_高级运维uld be taken down while this is happening, and backups must be in place incase of an error.

Is there a better/easier way to do this?


Java does not use time zones when using Dates; it stores everything as UTC and only uses time zones when displaying dates. see the following link for a discussion of java date/time. http://www.odi.ch/prog/design/datetime.php

  • If you're using the Date, Time, or DateTime column types in MySQL, time zone does not matter.
  • If you’re using the TIMESTAMP column type, time zones may matter since the TIMESTAMP is stored as a UTC but has conversion done when both retrieving and storing the values. For a discussion of MySQL time zone behavior see http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html .
  • If you’re worried about synchronizing objects across multiple servers in different time zones things get more complicated, see the following thread for a discussion of this. http://www.pubbs.net/201006/grails/2500-grails-user-how-to-get-gorm-to-store-dates-as-timestamp-in-utc-by-default-without-a-custom-hibernate-mapping-or-joda-time-plu.html


I know this is an old question but I think it's also pretty timeless... at least, I have stumbled upon it a fair number of times recently... so I thought I would contribute my solution.

First, I am using Grails 2.5.1 and PostgreSQL 9.4 as the backend.

Second, Date fields in Groovy/Grails are stored as timestamp without time zone in PostgreSQL. So it seems to me the first answer above is not actually fully correct - the date is not stored in UTC. This observation got me thinking... along the lines of "well if the database doesn't know what the timezone is, who does"? And the first answer that came to mind was "maybe it's Spring".

Third, the specifics of my problem is that I have a lot of dates that I bootstrapped into the database via BootStrap.groovy and new ThisClass().save(). And because these were dates, not dates + times, they all look like 2005-11-03 00:00:00 as PostgreSQL timestamps (without timezones).

Fourth, what really made the penny drop was when I edited one of my GSPs to include the timezone in the date format string, which showed up as PST (where my server is); and when I included timeZone="Asia/Kolkata" in the g:formatDate of the field in question, the time advanced by 12h30. So pretty clearly my server was running in PST8PDT and since that wasn't PostgreSQL I came back to Spring as the potential place to change things.

Fifth, after reading a few comments about setting the locale in grails-app/conf/spring/resources.groovy I decided to try setting the locale and timezone there, as per:

// Place your Spring DSL code here
beans = {
    // from http://stackoverflow.com/questions/1569446/grails-how-to-change-the-current-locale
    localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
        defaultLocale = new Locale("en","IN")
        java.util.Locale.setDefault(defaultLocale)
        println "configure spring/resources.groovy defaultLocale $defaultLocale"
        defaultTimeZone = TimeZone.getTimeZone("Asia/Kolkata")
        java.util.TimeZone.setDefault(defaultTimeZone)
        println "configure spring/resources.groovy defaultTimeZone $defaultTimeZone"
    }
}

I also used g:format timezone="Asia/Kolkata" format="dd MMM, yyyy a z" for all my date fields. And that seems to interpret all data in PostgreSQL timestamp fields in the correct timezone and at the anticipated hour (ie the hour that was entered), even though the dates were first entered "in the wrong time zone".

Sixth, g:datePicker - I read a number of posts about making this "time zone sensitive", but I found that its dates are interpreted as in the timezone used by Spring and so in my case, this is exactly what I need. Conversely, if someone wanted to enter dates in their locale and have Spring convert them on the fly to the server's time zone, I guess that would require some extra effort.

Personally I think it would be really cool if g:datePicker accepted timeZone as a parameter and used it in the same way g:formatDate does.


We had problems with time differences between using GORM and using groovy.sql.Sql (for quicker data import).

GORM was using the grails config timezone (UTC) that we set in the Bootstrap, but groovy sql was using the default system timezone (GMT).

The problem was solved by setting the timezone in the $JAVA_OPTS, although you could add the switch to grails opts or to the run-app command.

grails -Duser.timezone=UTC run-app

0

精彩评论

暂无评论...
验证码 换一张
取 消