简体   繁体   English

grails / mysql时区更改

[英]grails/mysql timezone change

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 服务器TZ由sys admin更改
  2. mysql must be restarted. mysql必须重启。
  3. every time based column in the database must have all values updated, using convert_tz or equivalent. 数据库中每次基于时间的列都必须使用convert_tz或等效方法更新所有值。 So either a mysql script must be written or a grails script that loads every row for each class, updating all the time fields. 因此,要么必须编写mysql脚本,要么必须编写grails脚本以加载每个类的每一行,从而更新所有时间字段。

Obviously the server should 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; Java在使用日期时不使用时区; it stores everything as UTC and only uses time zones when displaying dates. 它将所有内容存储为UTC,并且仅在显示日期时使用时区。 see the following link for a discussion of java date/time. 有关Java日期/时间的讨论,请参见以下链接。 http://www.odi.ch/prog/design/datetime.php http://www.odi.ch/prog/design/datetime.php

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. 首先,我使用Grails 2.5.1和PostgreSQL 9.4作为后端。

Second, Date fields in Groovy/Grails are stored as timestamp without time zone in PostgreSQL. 其次,Groovy / Grails中的Date字段在PostgreSQL中存储为timestamp without time zone So it seems to me the first answer above is not actually fully correct - the date is not stored in UTC. 因此在我看来,以上第一个答案实际上并不完全正确-日期未存储在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() . 第三,我的问题的具体情况是,我有很多通过BootStrap.groovynew 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). 而且因为这些是日期,而不是日期+时间,所以它们看起来都像是2005-11-03 00:00:00 PostgreSQL时间戳(没有时区)。

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); 第四,真正让我大跌眼镜的是,当我编辑我的一个GSP以便在日期格式字符串中包含时区时,日期格式字符串显示为PST(服务器所在的位置); and when I included timeZone="Asia/Kolkata" in the g:formatDate of the field in question, the time advanced by 12h30. 当我在相关字段的g:formatDate中添加timeZone="Asia/Kolkata"时,时间提前了12点30分。 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. 很明显,我的服务器运行在PST8PDT中,并且由于那不是PostgreSQL,所以我回到Spring作为改变事物的潜在场所。

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: 第五,在grails-app/conf/spring/resources.groovy阅读了一些关于设置语言环境的评论后,我决定尝试按照以下方法在此处设置语言环境和时区:

// 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 az" for all my date fields. 我还将g:format timezone="Asia/Kolkata" format="dd MMM, yyyy az"用于所有日期字段。 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". 这似乎可以解释PostgreSQL timestamp字段中正确时区和预期小时(即输入的小时)的所有数据,即使日期最初是在“错误的时区”输入的。

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. 第六, g:datePicker我读了许多有关使此“时区敏感”的文章,但是我发现它的日期被解释为与Spring使用的时区相同,因此在我的情况下,这正是我所需要的。 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. 相反,如果有人想在其语言环境中输入日期,并让Spring将它们即时转换为服务器的时区,我想那将需要一些额外的工作。

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. 我个人认为,如果g:datePicker接受timeZone作为参数并以与g:formatDate相同的方式使用它,那将真的很酷。

We had problems with time differences between using GORM and using groovy.sql.Sql (for quicker data import). 我们在使用GORM和使用groovy.sql.Sql之间存在时差问题(以便更快地导入数据)。

GORM was using the grails config timezone (UTC) that we set in the Bootstrap, but groovy sql was using the default system timezone (GMT). GORM使用的是我们在Bootstrap中设置的grails配置时区(UTC),但是groovy sql使用的是默认系统时区(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. 通过在$ JAVA_OPTS中设置时区可以解决此问题,尽管您可以将开关添加到grails opts或run-app命令中。

grails -Duser.timezone=UTC run-app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM