Skip to content

Commit

Permalink
Fix timezone issue when using magic date keywords (#17144)
Browse files Browse the repository at this point in the history
* Fix timezone issue when using magic date keywords

* improve readability

* use Date::factoryInTimezone
  • Loading branch information
sgiehl committed Jan 27, 2021
1 parent ca9a0a5 commit 43c920c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
5 changes: 2 additions & 3 deletions core/Period/Factory.php
Expand Up @@ -168,10 +168,9 @@ public static function makePeriodFromQueryParams($timezone, $period, $date)
} else {
if (!($date instanceof Date)) {
if (preg_match('/^(now|today|yesterday|yesterdaySameTime|last[ -]?(?:week|month|year))$/i', $date)) {
$date = Date::factory($date, $timezone);
} else {
$date = Date::factory($date);
$date = Date::factoryInTimezone($date, $timezone);
}
$date = Date::factory($date);
}
$oPeriod = Factory::build($period, $date);
}
Expand Down
35 changes: 22 additions & 13 deletions tests/PHPUnit/Integration/Period/FactoryTest.php
Expand Up @@ -16,7 +16,7 @@
use Piwik\Period\Range;
use Piwik\Period\Week;
use Piwik\Period\Year;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\TestCase\UnitTestCase;

class TestPeriod
{
Expand Down Expand Up @@ -61,31 +61,40 @@ public function findComponents($componentName, $expectedSubclass)
}
}

class FactoryTest extends IntegrationTestCase
/**
* @group PeriodFactoryTest
*/
class FactoryTest extends UnitTestCase
{
/**
* @dataProvider getTestDataForMakePeriodFromQueryParams
*/
public function test_makePeriodFromQueryParams_appliesTimezoneProperly($period, $date, $expectedLabel, $expectedRange)
public function test_makePeriodFromQueryParams_appliesTimezoneProperly($now, $timezone, $period, $date, $expectedLabel, $expectedRange)
{
Date::$now = strtotime('2020-12-24 03:37:00');
Date::$now = strtotime($now);

$factory = Period\Factory::makePeriodFromQueryParams('America/Chicago', $period, $date);
$factory = Period\Factory::makePeriodFromQueryParams($timezone, $period, $date);
$this->assertEquals($expectedLabel, $factory->getLabel());
$this->assertEquals($expectedRange, $factory->getRangeString());
}

public function getTestDataForMakePeriodFromQueryParams()
{
return [
['day', 'now', 'day', '2020-12-23,2020-12-23'],
['day', 'today', 'day', '2020-12-23,2020-12-23'],
['day', 'yesterday', 'day', '2020-12-22,2020-12-22'],
['day', 'yesterdaySameTime', 'day', '2020-12-22,2020-12-22'],
['day', 'last-week', 'day', '2020-12-16,2020-12-16'],
['day', 'last-month', 'day', '2020-11-23,2020-11-23'],
['day', 'last-year', 'day', '2019-12-23,2019-12-23'],
['day', '2020-12-23', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'now', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'today', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 16:37:00', 'America/Chicago', 'day', 'today', 'day', '2020-12-24,2020-12-24'],
['2020-12-24 22:37:00', 'UTC+5', 'day', 'today', 'day', '2020-12-25,2020-12-25'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'yesterday', 'day', '2020-12-22,2020-12-22'],
['2020-12-24 03:37:00', 'UTC+5', 'day', 'yesterday', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 16:37:00', 'UTC+12', 'day', 'yesterday', 'day', '2020-12-24,2020-12-24'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'yesterdaySameTime', 'day', '2020-12-22,2020-12-22'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-week', 'day', '2020-12-16,2020-12-16'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-month', 'day', '2020-11-23,2020-11-23'],
['2020-12-24 03:37:00', 'UTC', 'week', 'last-month', 'week', '2020-11-23,2020-11-29'],
['2020-12-23 03:37:00', 'America/Chicago', 'week', 'last-month', 'week', '2020-11-16,2020-11-22'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-year', 'day', '2019-12-23,2019-12-23'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', '2020-12-23', 'day', '2020-12-23,2020-12-23'],
];
}

Expand Down

0 comments on commit 43c920c

Please sign in to comment.