簡體   English   中英

如何在Play Framework 2.0中為生產和測試設置UTC時區?

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

我們希望我們的Play Framework 2.0 Scala應用程序能夠在應用程序服務器和MySQL數據庫服務器中處理UTC中的所有日期和時間信息。

訣竅是:

  • 無需改變部署環境
  • 無需更改CI(測試)環境
  • 不改變本地(dev)環境

這樣做有標准的最佳做法嗎? 我們希望測試以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 ),必須在發布之前設置屬性。 例如,通過:

  1. 編輯生成的start腳本以添加export _JAVA_OPTIONS=-Duser.timezone=GMT
  2. 使用-Duser.timezone=GMT調用start腳本
  3. 調用System.setProperty("user.timezone", "GMT")后在現有JVM中啟動

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM