@@ -461,13 +461,62 @@ public function testFormatWithDefaultTimezoneIntl()
461
461
{
462
462
$ this ->skipIfIntlExtensionIsNotLoaded ();
463
463
$ this ->skipIfICUVersionIsTooOld ();
464
+
465
+ $ formatter = new \IntlDateFormatter ('en ' , StubIntlDateFormatter::MEDIUM , StubIntlDateFormatter::SHORT );
466
+ $ formatter ->setPattern ('yyyy-MM-dd HH:mm:ss ' );
467
+
468
+ $ this ->assertEquals (
469
+ $ this ->createDateTime (0 )->format ('Y-m-d H:i:s ' ),
470
+ $ formatter ->format (0 )
471
+ );
472
+ }
473
+
474
+ public function testFormatWithDefaultTimezoneStubShouldUseTheTzEnvironmentVariableWhenAvailable ()
475
+ {
476
+ $ tz = getenv ('TZ ' );
477
+ putenv ('TZ=Europe/London ' );
478
+
479
+ $ formatter = new StubIntlDateFormatter ('en ' , StubIntlDateFormatter::MEDIUM , StubIntlDateFormatter::SHORT );
480
+ $ formatter ->setPattern ('yyyy-MM-dd HH:mm:ss ' );
481
+
482
+ $ this ->assertEquals (
483
+ $ this ->createDateTime (0 )->format ('Y-m-d H:i:s ' ),
484
+ $ formatter ->format (0 )
485
+ );
486
+
487
+ $ this ->assertEquals ('Europe/London ' , getenv ('TZ ' ));
488
+
489
+ // Restores TZ.
490
+ putenv ('TZ= ' .$ tz );
491
+ }
492
+
493
+ /**
494
+ * It seems IntlDateFormatter caches the timezone id when not explicitely set via constructor or by the
495
+ * setTimeZoneId() method. Since testFormatWithDefaultTimezoneIntl() runs using the default environment
496
+ * time zone, this test would use it too if not running in a separated process.
497
+ *
498
+ * @runInSeparateProcess
499
+ */
500
+ public function testFormatWithDefaultTimezoneIntlShouldUseTheTzEnvironmentVariableWhenAvailable ()
501
+ {
502
+ $ this ->skipIfIntlExtensionIsNotLoaded ();
503
+ $ this ->skipIfICUVersionIsTooOld ();
504
+
505
+ $ tz = getenv ('TZ ' );
506
+ putenv ('TZ=Europe/Paris ' );
507
+
464
508
$ formatter = new \IntlDateFormatter ('en ' , StubIntlDateFormatter::MEDIUM , StubIntlDateFormatter::SHORT );
465
509
$ formatter ->setPattern ('yyyy-MM-dd HH:mm:ss ' );
466
510
511
+ $ this ->assertEquals ('Europe/Paris ' , getenv ('TZ ' ));
512
+
467
513
$ this ->assertEquals (
468
514
$ this ->createDateTime (0 )->format ('Y-m-d H:i:s ' ),
469
515
$ formatter ->format (0 )
470
516
);
517
+
518
+ // Restores TZ.
519
+ putenv ('TZ= ' .$ tz );
471
520
}
472
521
473
522
/**
@@ -992,11 +1041,13 @@ protected function createIntlFormatter($pattern = null)
992
1041
return new \IntlDateFormatter ('en ' , \IntlDateFormatter::MEDIUM , \IntlDateFormatter::SHORT , 'UTC ' , \IntlDateFormatter::GREGORIAN , $ pattern );
993
1042
}
994
1043
995
- protected function createDateTime ($ timestamp = null , $ timeZone = null )
1044
+ protected function createDateTime ($ timestamp = null )
996
1045
{
1046
+ $ timeZone = getenv ('TZ ' ) ?: 'UTC ' ;
1047
+
997
1048
$ dateTime = new \DateTime ();
998
1049
$ dateTime ->setTimestamp (null === $ timestamp ? time () : $ timestamp );
999
- $ dateTime ->setTimeZone (new \DateTimeZone (null === $ timeZone ? date_default_timezone_get () : $ timeZone ));
1050
+ $ dateTime ->setTimeZone (new \DateTimeZone ($ timeZone ));
1000
1051
1001
1052
return $ dateTime ;
1002
1053
}
0 commit comments