2

完成更改应用程序时区的最佳方法是什么?我看到它的方式必须发生以下情况:

  1. 服务器 TZ 由系统管理员更改
  2. mysql 必须重新启动。
  3. 数据库中每次基于时间的列都必须使用 convert_tz 或等效方法更新所有值。因此,要么必须编写 mysql 脚本,要么必须编写一个 grails 脚本,为每个类加载每一行,更新所有时间字段。

显然,在发生这种情况时应该关闭服务器,并且备份必须到位以防出现错误。

有没有更好/更简单的方法来做到这一点?

4

3 回答 3

3

Java 在使用日期时不使用时区;它将所有内容存储为 UTC,并且仅在显示日期时使用时区。有关 java 日期/时间的讨论,请参见以下链接。 http://www.odi.ch/prog/design/datetime.php

于 2010-08-02T19:13:23.997 回答
2

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.

于 2016-02-11T21:15:42.013 回答
1

我们在使用 GORM 和使用 groovy.sql.Sql(为了更快地导入数据)之间存在时间差异问题。

GORM 使用的是我们在 Bootstrap 中设置的 grails 配置时区 (UTC),但 groovy sql 使用的是默认系统时区 (GMT)。

通过在 $JAVA_OPTS 中设置时区解决了这个问题,尽管您可以将开关添加到 grails opts 或 run-app 命令。

grails -Duser.timezone=UTC run-app

于 2012-09-11T08:56:47.340 回答