[英]Symfony + JMS Serializer: How to set default Timezone?
I am working on a Symfony webapp project and have a problem with the timezone used by the JMS Serializer. 我正在开发一个Symfony webapp项目,并且JMS序列化器使用的时区有问题。
Short version: 简洁版本:
How can I set a global default timezone that is used all over my symfony project, not matter if the project is used in the browser or on the command line? 如何设置在我的symfony项目中使用的全局默认时区,而不管该项目是在浏览器中还是在命令行中使用?
(very) Long Version: (非常)长版:
date.timezone = "America/Chicago"
date.timezone = "America/Chicago"
UTC
as default timezone. UTC
作为默认时区。 To achieve this I simply added ini_set("date.timezone", "UTC");
为此,我只添加了
ini_set("date.timezone", "UTC");
to the app.php
and app_dev.php
FrontControllers. 到
app.php
和app_dev.php
FrontControllers。 Since every access to the app is routed through these files, this solutions works quite well. 由于对应用程序的每次访问都通过这些文件进行路由,因此该解决方案效果很好。
However there is one big problem with this solution: It only works when the app is being used through these FrontControllers / in the browser. 但是,此解决方案存在一个大问题:仅当通过这些FrontControllers /在浏览器中使用该应用程序时,它才有效。
When ever I use the console or any other commandline access to the project the Frontcontrollers are not uses and thus the timezone setting is not applied. 每当我使用控制台或任何其他命令行访问项目时,都不会使用Frontcontrollers,因此不会应用时区设置。
This is a problem for example when clearing and warming up the cache: 例如,在清除和预热缓存时,这是一个问题:
php app/console cache:clear --env=dev
Since php
is used directly on the commandline the global php.ini
is used. 由于
php
直接在命令行上使用,因此使用全局php.ini
。 Thus the global date.timezone = "America/Chicago"
is applied. 因此,将应用全局
date.timezone = "America/Chicago"
。
It seems that the default timezone is stored in appDevDebugProjectContainer.xml
cache file and additionally the timezone is cached for the JMS Serializer as well in: 似乎默认时区存储在
appDevDebugProjectContainer.xml
缓存文件中,此外,该时区也为JMS序列化程序缓存在以下位置:
$ cd ...../app/cache/dev
$ grep -rnw .'/' -e "America/Chicago"
./appDevDebugProjectContainer.xml:4125: <argument>America/Chicago</argument>
./appDevDebugProjectContainer.php:3083: return $this->services['jms_serializer.datetime_handler'] = new \JMS\Serializer\Handler\DateHandler('Y-m-d\\TH:i:sO', 'America/Chicago', true);
Thus every time an object is (de)serialized using JMS the wrong timezone is applied to all dates. 因此,每次使用JMS对对象进行序列化时,都会将错误的时区应用于所有日期。 This is even the case when the app is running the browser (using the FrontControllers with changed default timezone).
甚至在应用程序运行浏览器(使用具有更改的默认时区的FrontController)时也是如此。
Of course the problem is the same for all other console commands, since they all use the global php.ini. 当然,所有其他控制台命令的问题都相同,因为它们都使用全局php.ini。 How can I solve this?
我该如何解决?
Possible Solutions: 可能的解决方案:
America/Chicago
as default time zone America/Chicago
作为默认时区 php
calls : The Frontontrollers would still set the time zone for all "browser" calls to the app. php
调用 :Frontontrollers仍然会为应用程序的所有“浏览器”调用设置时区。 Additionally I would create one php.ini for the project that is being uses as parameter when using php on the commandline: Code: 码:
// in ./.bashrc
alias phpx='php -c /path/to/project/php.ini'
// use phpx instead of php
phpx app/console cache:clear --env=dev
This solution would work but I do not like it: 此解决方案可以工作,但我不喜欢它:
Conclusion: 结论:
Is there any option that will set the default timezone globally for the complete Symfony project? 是否有任何选项可以为整个Symfony项目全局设置默认时区? So far I only found an option that was used in Symfony 1.x but no equivalent solution for Symfony 2.x
到目前为止,我只找到了Symfony 1.x中使用的选项,但没有Symfony 2.x的等效解决方案
Any idea? 任何想法?
Well, sometimes an answer is so obvious that one cannot see it. 好吧,有时候答案很明显,以至于看不到它。 At least not before writing a long question here... :-)
至少在这里写一个长问题之前不是... :-)
Right after I send the question I found the answer: The default timezone should be applied globally, all over Symfony, no matter how it is being used (browser, console, etc.)? 在我发送问题之后,我立即找到了答案:无论使用哪种方式(浏览器,控制台等),默认时区都应该在整个Symfony中全局应用。 So, which part of Symfony is beeing use every time and every where?
那么,Symfony的哪一部分在任何时间,任何地方都在使用? The Kernel!!
内核!
I simply modified the AppKernel and it works fine: 我只是修改了AppKernel,它可以正常工作:
class AppKernel extends Kernel
{
public function __construct($environment, $debug)
{
// Two is better than one...
ini_set("date.timezone", "UTC");
date_default_timezone_set('UTC');
parent::__construct($environment, $debug);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.