[英]How to set timezone to UTC in Play Framework 2.0 for both production and tests?
我们希望我们的Play Framework 2.0 Scala应用程序能够在应用程序服务器和MySQL数据库服务器中处理UTC中的所有日期和时间信息。
诀窍是:
这样做有标准的最佳做法吗? 我们希望测试以UTC -Duser.timezone=GMT
运行,而不必在所有命令行上传递-Duser.timezone=GMT
。 同样用于play start
具有play start
服务器。
这比我们预期的要容易。
首先,在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"
其次,在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"))
这两个变化将共同处理测试 ( play test
)和开发 ( play start
)模式。
对于制作 ( play dist
),必须在发布之前设置属性。 例如,通过:
start
脚本以添加export _JAVA_OPTIONS=-Duser.timezone=GMT
-Duser.timezone=GMT
调用start
脚本 System.setProperty("user.timezone", "GMT")
后在现有JVM中启动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.