Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorStefan Giehl <stefan@matomo.org>2020-12-10 05:18:07 +0300
committerGitHub <noreply@github.com>2020-12-10 05:18:07 +0300
commit7cde69f763f2cbf2b9a27c8a7dcf7bcaa950c3bd (patch)
treeda719a778d96e167ee4ba55c89a529c1c3bb0dce /core
parent06918eea4d2ca01298200348b40f57817433b931 (diff)
Allow using last (week|month|year) as date param (#16830)
* Allow using last (week|month|year) as date param * Adds some unit tests * improve tests * Make it possible to use last (week|month|year) for api requests * improve test
Diffstat (limited to 'core')
-rw-r--r--core/Date.php59
-rw-r--r--core/Period/Factory.php11
-rw-r--r--core/Period/Range.php4
3 files changed, 63 insertions, 11 deletions
diff --git a/core/Date.php b/core/Date.php
index 4b67eeb9cb..328467ab67 100644
--- a/core/Date.php
+++ b/core/Date.php
@@ -135,6 +135,12 @@ class Date
$date = self::yesterday();
} elseif ($dateString === 'yesterdaySameTime') {
$date = self::yesterdaySameTime();
+ } else if (preg_match('/last[ -]?week/i', urldecode($dateString))) {
+ $date = self::lastWeek();
+ } else if (preg_match('/last[ -]?month/i', urldecode($dateString))) {
+ $date = self::lastMonth();
+ } else if (preg_match('/last[ -]?year/i', urldecode($dateString))) {
+ $date = self::lastYear();
} elseif (!is_int($dateString)
&& (
// strtotime returns the timestamp for April 1st for a date like 2011-04-01,today
@@ -187,6 +193,12 @@ class Date
return self::yesterdayInTimezone($timezone);
} else if ($dateString === 'yesterdaySameTime') {
return self::yesterdaySameTimeInTimezone($timezone);
+ } else if (preg_match('/last[ -]?week/i', urldecode($dateString))) {
+ return self::lastWeekInTimezone($timezone);
+ } else if (preg_match('/last[ -]?month/i', urldecode($dateString))) {
+ return self::lastMonthInTimezone($timezone);
+ } else if (preg_match('/last[ -]?year/i', urldecode($dateString))) {
+ return self::lastYearInTimezone($timezone);
} else {
throw new \Exception("Date::factoryInTimezone() should not be used with $dateString.");
}
@@ -214,6 +226,21 @@ class Date
return self::nowInTimezone($timezone)->subDay(1);
}
+ private static function lastWeekInTimezone($timezone)
+ {
+ return new Date(strtotime('-1week', self::todayInTimezone($timezone)->getTimestamp()));
+ }
+
+ private static function lastMonthInTimezone($timezone)
+ {
+ return new Date(strtotime('-1month', self::todayInTimezone($timezone)->getTimestamp()));
+ }
+
+ private static function lastYearInTimezone($timezone)
+ {
+ return new Date(strtotime('-1year', self::todayInTimezone($timezone)->getTimestamp()));
+ }
+
/**
* Returns the current timestamp as a string with the following format: `'YYYY-MM-DD HH:MM:SS'`.
*
@@ -586,7 +613,37 @@ class Date
*/
public static function yesterdaySameTime()
{
- return new Date(strtotime("yesterday " . date('H:i:s'), self::getNowTimestamp()));
+ return new Date(strtotime("yesterday " . date('H:i:s', self::getNowTimestamp()), self::getNowTimestamp()));
+ }
+
+ /**
+ * Returns a date object set to the day a week ago at midnight in UTC.
+ *
+ * @return \Piwik\Date
+ */
+ public static function lastWeek()
+ {
+ return new Date(strtotime("-1week 00:00:00", self::getNowTimestamp()));
+ }
+
+ /**
+ * Returns a date object set to the day a month ago at midnight in UTC.
+ *
+ * @return \Piwik\Date
+ */
+ public static function lastMonth()
+ {
+ return new Date(strtotime("-1month 00:00:00", self::getNowTimestamp()));
+ }
+
+ /**
+ * Returns a date object set to the day a year ago at midnight in UTC.
+ *
+ * @return \Piwik\Date
+ */
+ public static function lastYear()
+ {
+ return new Date(strtotime("-1year 00:00:00", self::getNowTimestamp()));
}
/**
diff --git a/core/Period/Factory.php b/core/Period/Factory.php
index 052e7c0dfb..75b4e35214 100644
--- a/core/Period/Factory.php
+++ b/core/Period/Factory.php
@@ -74,7 +74,7 @@ abstract class Factory
self::checkPeriodIsEnabled($period);
if (is_string($date)) {
- list($period, $date) = self::convertRangeToDateIfNeeded($period, $date);
+ [$period, $date] = self::convertRangeToDateIfNeeded($period, $date);
if (Period::isMultiplePeriod($date, $period)
|| $period == 'range'
) {
@@ -160,19 +160,14 @@ abstract class Factory
$timezone = 'UTC';
}
- list($period, $date) = self::convertRangeToDateIfNeeded($period, $date);
+ [$period, $date] = self::convertRangeToDateIfNeeded($period, $date);
if ($period == 'range') {
self::checkPeriodIsEnabled('range');
$oPeriod = new Range('range', $date, $timezone, Date::factory('today', $timezone));
} else {
if (!($date instanceof Date)) {
- if ($date == 'now' || $date == 'today') {
- $date = date('Y-m-d', Date::factory('now', $timezone)->getTimestamp());
- } elseif ($date == 'yesterday' || $date == 'yesterdaySameTime') {
- $date = date('Y-m-d', Date::factory('now', $timezone)->subDay(1)->getTimestamp());
- }
- $date = Date::factory($date);
+ $date = Date::factory($date, $timezone);
}
$oPeriod = Factory::build($period, $date);
}
diff --git a/core/Period/Range.php b/core/Period/Range.php
index 0f466912a6..1a4e381dc6 100644
--- a/core/Period/Range.php
+++ b/core/Period/Range.php
@@ -220,7 +220,7 @@ class Range extends Period
parent::generate();
- if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) {
+ if (preg_match('/^(last|previous)([0-9]*)$/', $this->strDate, $regs)) {
$lastN = $regs[2];
$lastOrPrevious = $regs[1];
if (!is_null($this->defaultEndDate)) {
@@ -290,7 +290,7 @@ class Range extends Period
*/
public static function parseDateRange($dateString)
{
- $matched = preg_match('/^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}),(([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})|today|now|yesterday)$/D', trim($dateString), $regs);
+ $matched = preg_match('/^((?:[0-9]{4}-[0-9]{1,2}-[0-9]{1,2})|last[ -]?(?:week|month|year)),((?:[0-9]{4}-[0-9]{1,2}-[0-9]{1,2})|today|now|yesterday|last[ -]?(?:week|month|year))$/Di', trim($dateString), $regs);
if (empty($matched)) {
return false;