diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2018-12-14 04:29:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-14 04:29:27 +0300 |
commit | ef323275e351ab26370ba67aa5ca62b85035cb45 (patch) | |
tree | 088b6ede4170ebf545cc90b8c729b0b448de4196 /core/Date.php | |
parent | 24b26c6370b6792b6770a8eefcd59bcd9a92f81a (diff) |
For special dates in evolution graphs, calculate date & timezone together, to get proper result. (#13799)
* Update TimezonesTest to be more useful.
* Update TimezonesTest which will not pass until https://github.com/matomo-org/matomo/issues/13829
* Non-intrusively fix evolution graph timezone end date issue using new Date method.
* Make sure Date::factoryInTimezone only works w/ special word times.
Diffstat (limited to 'core/Date.php')
-rw-r--r-- | core/Date.php | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/core/Date.php b/core/Date.php index d7b2053910..3d1751591a 100644 --- a/core/Date.php +++ b/core/Date.php @@ -55,6 +55,9 @@ class Date const DATE_FORMAT_YEAR = DateTimeFormatProvider::DATE_FORMAT_YEAR; const TIME_FORMAT = DateTimeFormatProvider::TIME_FORMAT; + // for tests + public static $now = null; + /** * Max days for months (non-leap-year). See {@link addPeriod()} implementation. * @@ -164,6 +167,52 @@ class Date } /** + * Returns Date w/ UTC timestamp of time $dateString/$timezone. + * (Only applies to special strings, like 'now','today','yesterday','yesterdaySameTime'. + * + * @param $dateString + * @param $timezone + * @return Date + * @ignore + */ + public static function factoryInTimezone($dateString, $timezone) + { + if ($dateString == 'now') { + return self::nowInTimezone($timezone); + } else if ($dateString == 'today') { + return self::todayInTimezone($timezone); + } else if ($dateString == 'yesterday') { + return self::yesterdayInTimezone($timezone); + } else if ($dateString == 'yesterdaySameTime') { + return self::yesterdaySameTimeInTimezone($timezone); + } else { + throw new \Exception("Date::factoryInTimezone() should not be used with $dateString."); + } + } + + private static function nowInTimezone($timezone) + { + $now = self::getNowTimestamp(); + $now = self::adjustForTimezone($now, $timezone); + return new Date($now); + } + + private static function todayInTimezone($timezone) + { + return self::nowInTimezone($timezone)->getStartOfDay(); + } + + private static function yesterdayInTimezone($timezone) + { + return self::todayInTimezone($timezone)->subDay(1); + } + + private static function yesterdaySameTimeInTimezone($timezone) + { + return self::nowInTimezone($timezone)->subDay(1); + } + + /** * Returns the current timestamp as a string with the following format: `'YYYY-MM-DD HH:MM:SS'`. * * @return string @@ -495,7 +544,7 @@ class Date */ public static function now() { - return new Date(time()); + return new Date(self::getNowTimestamp()); } /** @@ -1002,4 +1051,9 @@ class Date $message = Piwik::translate('General_ExceptionInvalidDateFormat', array("YYYY-MM-DD, or 'today' or 'yesterday'", "strtotime", "http://php.net/strtotime")); return new Exception($message . ": $dateString"); } + + private static function getNowTimestamp() + { + return isset(self::$now) ? self::$now : time(); + } } |