Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timezone issue when using magic date keywords #17144

Merged
merged 3 commits into from Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions core/Period/Factory.php
Expand Up @@ -168,10 +168,13 @@ 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);
// most magic keywords will automatically set the time to 00:00:00
// we need to set the time manually, so the timezone can be applied correctly
$timestampWithCurrentTime = Date::factory($date)->setTime(date('H:i:s', Date::getNowTimestamp()))->getTimestamp();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think about it, I think this is what the Date::factoryInTimezone method is meant to achieve. Can you check if this can be used instead? It's meant to retain the time of day before converting to the timezone. If it works, then the code will be simpler and we won't need to do something like this everywhere. Otherwise, feel free to merge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good hint. Seems I missed that method for some reason. Using it does the trick as well. At least the tests are still passing.

// apply the timezone and convert it to a string date
$date = Date::factory($timestampWithCurrentTime, $timezone)->toString('Y-m-d');
}
$date = Date::factory($date);
sgiehl marked this conversation as resolved.
Show resolved Hide resolved
}
$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