[英]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.