如何在生产和测试中将Play Framework 2.0中的时区设置为UTC? [英] How to set timezone to UTC in Play Framework 2.0 for both production and tests?

查看:117
本文介绍了如何在生产和测试中将Play Framework 2.0中的时区设置为UTC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望我们的Play Framework 2.0 Scala应用程序能够在UTC中处理应用程序服务器和MySQL数据库服务器中的所有日期和时间信息.

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.

诀窍是:

  • 不更改部署环境
  • 在不更改CI(测试)环境的情况下
  • 不更改本地(dev)环境

是否有标准的最佳做法来做到这一点?我们希望测试以UTC运行,而不必在所有命令行上均通过-Duser.timezone=GMT.同上用play start启动服务器.

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. Ditto for bringing up servers with play start.

推荐答案

这比我们预期的要容易.

This was easier than we'd expected.

首先,在application.conf中,使用参数

First, in application.conf, configure the JDBC URL with the parameters as described on another StackOverflow question:

# 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"

第二,在Build.scala中,设置Java系统属性和默认值:

Second, in Build.scala, set the Java system property and the default:

// 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"))

这两个更改将一起处理测试(play test)和开发(play start)模式.

These two changes together will handle both test (play test) and development (play start) modes.

对于生产(play dist),仍然必须在启动之前设置属性.例如,通过:

For production (play dist), one must still set the property before launch. For example, by:

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

这篇关于如何在生产和测试中将Play Framework 2.0中的时区设置为UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆