简体   繁体   English

如何在Play Framework 2.0中为生产和测试设置UTC时区?

[英]How to set timezone to UTC in Play Framework 2.0 for both production and tests?

We'd like our Play Framework 2.0 Scala applications to handle all date and time information in UTC, both in the app servers and in MySQL database servers. 我们希望我们的Play Framework 2.0 Scala应用程序能够在应用程序服务器和MySQL数据库服务器中处理UTC中的所有日期和时间信息。

The trick is: 诀窍是:

  • Without changing deployment environment 无需改变部署环境
  • Without changing CI (testing) environment 无需更改CI(测试)环境
  • Without changing local (dev) environment 不改变本地(dev)环境

Is there a standard best practice to do this? 这样做有标准的最佳做法吗? We want the tests to run in UTC, without having to pass -Duser.timezone=GMT on all commandlines. 我们希望测试以UTC -Duser.timezone=GMT运行,而不必在所有命令行上传递-Duser.timezone=GMT Ditto for bringing up servers with play start . 同样用于play start具有play start服务器。

This was easier than we'd expected. 这比我们预期的要容易。

First, in application.conf , configure the JDBC URL with the parameters as described on another StackOverflow question : 首先,在application.conf ,使用另一个StackOverflow问题中描述的参数配置JDBC URL:

# Set MySQL Connector/J to use UTC server connection and time conversions
#   see https://stackoverflow.com/questions/10488529/gettimestamp-does-timezone-converstion-twice-in-mysql-jdbc-connector
db.default.url="jdbc:mysql://localhost/database?useGmtMillisForDatetimes=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&useTimezone=true&serverTimezone=UTC"

Second, in Build.scala , set the Java system property and the default: 其次,在Build.scala ,设置Java系统属性和默认值:

// Setting this property here forces the JVM to run in UTC time, 
// both for test (`play test`) and development (`play start`) modes, 
// but not for production (`play dist`).
System.setProperty("user.timezone", "GMT")
TimeZone.setDefault(TimeZone.getTimeZone("GMT"))

These two changes together will handle both test ( play test ) and development ( play start ) modes. 这两个变化将共同处理测试play test )和开发play start )模式。

For production ( play dist ), one must still set the property before launch. 对于制作play dist ),必须在发布之前设置属性。 For example, by: 例如,通过:

  1. Editing the generated start script to add export _JAVA_OPTIONS=-Duser.timezone=GMT 编辑生成的start脚本以添加export _JAVA_OPTIONS=-Duser.timezone=GMT
  2. Invoking the start script with -Duser.timezone=GMT 使用-Duser.timezone=GMT调用start脚本
  3. Launching within an existing JVM after calling System.setProperty("user.timezone", "GMT") 调用System.setProperty("user.timezone", "GMT")后在现有JVM中启动

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

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