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
diff options
context:
space:
mode:
Diffstat (limited to 'core/Date.php')
-rw-r--r--core/Date.php107
1 files changed, 55 insertions, 52 deletions
diff --git a/core/Date.php b/core/Date.php
index 0753514cf9..3fb96665e0 100644
--- a/core/Date.php
+++ b/core/Date.php
@@ -9,12 +9,15 @@
* @package Piwik
*/
+namespace Piwik;
+use Exception;
+
/**
* Date object widely used in Piwik.
*
* @package Piwik
*/
-class Piwik_Date
+class Date
{
/**
* The stored timestamp is always UTC based.
@@ -33,7 +36,7 @@ class Piwik_Date
const DATE_TIME_FORMAT = 'Y-m-d H:i:s';
/**
- * Builds a Piwik_Date object
+ * Builds a Date object
*
* @param int $timestamp
* @param string $timezone
@@ -42,21 +45,21 @@ class Piwik_Date
protected function __construct($timestamp, $timezone = 'UTC')
{
if (!is_int($timestamp)) {
- throw new Exception("Piwik_Date is expecting a unix timestamp");
+ throw new Exception("Date is expecting a unix timestamp");
}
$this->timezone = $timezone;
$this->timestamp = $timestamp;
}
/**
- * Returns a Piwik_Date objects.
+ * Returns a Date objects.
*
- * @param string|self $dateString 'today' 'yesterday' or any YYYY-MM-DD or timestamp
+ * @param string|\Piwik\Date $dateString 'today' 'yesterday' or any YYYY-MM-DD or timestamp
* @param string $timezone if specified, the dateString will be relative to this $timezone.
* For example, today in UTC+12 will be a timestamp in the future for UTC.
* This is different from using ->setTimezone()
* @throws Exception
- * @return Piwik_Date
+ * @return \Piwik\Date
*/
public static function factory($dateString, $timezone = null)
{
@@ -83,7 +86,7 @@ class Piwik_Date
) {
throw $invalidDateException;
} else {
- $date = new Piwik_Date($dateString);
+ $date = new Date($dateString);
}
$timestamp = $date->getTimestamp();
// can't be doing web analytics before the 1st website
@@ -96,7 +99,7 @@ class Piwik_Date
}
$timestamp = self::adjustForTimezone($timestamp, $timezone);
- return Piwik_Date::factory($timestamp);
+ return Date::factory($timestamp);
}
/**
@@ -117,7 +120,7 @@ class Piwik_Date
public function getDateStartUTC()
{
$dateStartUTC = gmdate('Y-m-d', $this->timestamp);
- $date = Piwik_Date::factory($dateStartUTC)->setTimezone($this->timezone);
+ $date = Date::factory($dateStartUTC)->setTimezone($this->timezone);
return $date->toString(self::DATE_TIME_FORMAT);
}
@@ -129,7 +132,7 @@ class Piwik_Date
public function getDateEndUTC()
{
$dateEndUTC = gmdate('Y-m-d 23:59:59', $this->timestamp);
- $date = Piwik_Date::factory($dateEndUTC)->setTimezone($this->timezone);
+ $date = Date::factory($dateEndUTC)->setTimezone($this->timezone);
return $date->toString(self::DATE_TIME_FORMAT);
}
@@ -139,11 +142,11 @@ class Piwik_Date
* Doesn't modify $this
*
* @param string $timezone 'UTC', 'Europe/London', ...
- * @return Piwik_Date
+ * @return \Piwik\Date
*/
public function setTimezone($timezone)
{
- return new Piwik_Date($this->timestamp, $timezone);
+ return new Date($this->timestamp, $timezone);
}
/**
@@ -238,10 +241,10 @@ class Piwik_Date
/**
* Returns true if the current date is older than the given $date
*
- * @param Piwik_Date $date
+ * @param \Piwik\Date $date
* @return bool
*/
- public function isLater(Piwik_Date $date)
+ public function isLater(Date $date)
{
return $this->getTimestamp() > $date->getTimestamp();
}
@@ -249,10 +252,10 @@ class Piwik_Date
/**
* Returns true if the current date is earlier than the given $date
*
- * @param Piwik_Date $date
+ * @param \Piwik\Date $date
* @return bool
*/
- public function isEarlier(Piwik_Date $date)
+ public function isEarlier(Date $date)
{
return $this->getTimestamp() < $date->getTimestamp();
}
@@ -284,10 +287,10 @@ class Piwik_Date
* Returns 0 if equal, -1 if current week is earlier or 1 if current week is later
* Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
*
- * @param Piwik_Date $date
+ * @param \Piwik\Date $date
* @return int 0 = equal, 1 = later, -1 = earlier
*/
- public function compareWeek(Piwik_Date $date)
+ public function compareWeek(Date $date)
{
$currentWeek = date('W', $this->getTimestamp());
$toCompareWeek = date('W', $date->getTimestamp());
@@ -305,10 +308,10 @@ class Piwik_Date
* Returns 0 if equal, -1 if current month is earlier or 1 if current month is later
* For example: 10.03.2000 -> 15.03.1950 -> 0
*
- * @param Piwik_Date $date Month to compare
+ * @param \Piwik\Date $date Month to compare
* @return int 0 = equal, 1 = later, -1 = earlier
*/
- public function compareMonth(Piwik_Date $date)
+ public function compareMonth(Date $date)
{
$currentMonth = date('n', $this->getTimestamp());
$toCompareMonth = date('n', $date->getTimestamp());
@@ -328,47 +331,47 @@ class Piwik_Date
*/
public function isToday()
{
- return $this->toString('Y-m-d') === Piwik_Date::factory('today', $this->timezone)->toString('Y-m-d');
+ return $this->toString('Y-m-d') === Date::factory('today', $this->timezone)->toString('Y-m-d');
}
/**
* Returns a date object set to now (same as today, except that the time is also set)
*
- * @return Piwik_Date
+ * @return \Piwik\Date
*/
public static function now()
{
- return new Piwik_Date(time());
+ return new Date(time());
}
/**
* Returns a date object set to today midnight
*
- * @return Piwik_Date
+ * @return \Piwik\Date
*/
public static function today()
{
- return new Piwik_Date(strtotime(date("Y-m-d 00:00:00")));
+ return new Date(strtotime(date("Y-m-d 00:00:00")));
}
/**
* Returns a date object set to yesterday midnight
*
- * @return Piwik_Date
+ * @return \Piwik\Date
*/
public static function yesterday()
{
- return new Piwik_Date(strtotime("yesterday"));
+ return new Date(strtotime("yesterday"));
}
/**
* Returns a date object set to yesterday same time of day
*
- * @return Piwik_Date
+ * @return \Piwik\Date
*/
public static function yesterdaySameTime()
{
- return new Piwik_Date(strtotime("yesterday " . date('H:i:s')));
+ return new Date(strtotime("yesterday " . date('H:i:s')));
}
/**
@@ -376,11 +379,11 @@ class Piwik_Date
* Doesn't modify $this
*
* @param string $time HH:MM:SS
- * @return Piwik_Date The new date with the time part set
+ * @return \Piwik\Date The new date with the time part set
*/
public function setTime($time)
{
- return new Piwik_Date(strtotime(date("Y-m-d", $this->timestamp) . " $time"), $this->timezone);
+ return new Date(strtotime(date("Y-m-d", $this->timestamp) . " $time"), $this->timezone);
}
/**
@@ -389,7 +392,7 @@ class Piwik_Date
* Doesn't modify $this
*
* @param int $day Day eg. 31
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function setDay($day)
{
@@ -402,7 +405,7 @@ class Piwik_Date
$day,
date('Y', $ts)
);
- return new Piwik_Date($result, $this->timezone);
+ return new Date($result, $this->timezone);
}
/**
@@ -411,7 +414,7 @@ class Piwik_Date
* Doesn't modify $this
*
* @param int $year 2010
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function setYear($year)
{
@@ -424,16 +427,16 @@ class Piwik_Date
date('j', $ts),
$year
);
- return new Piwik_Date($result, $this->timezone);
+ return new Date($result, $this->timezone);
}
/**
- * Subtracts days from the existing date object and returns a new Piwik_Date object
+ * Subtracts days from the existing date object and returns a new Date object
* Returned is the new date object
* Doesn't modify $this
*
* @param int $n
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function subDay($n)
{
@@ -441,16 +444,16 @@ class Piwik_Date
return clone $this;
}
$ts = strtotime("-$n day", $this->timestamp);
- return new Piwik_Date($ts, $this->timezone);
+ return new Date($ts, $this->timezone);
}
/**
- * Subtracts weeks from the existing date object and returns a new Piwik_Date object
+ * Subtracts weeks from the existing date object and returns a new Date object
* Returned is the new date object
* Doesn't modify $this
*
* @param int $n
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function subWeek($n)
{
@@ -463,7 +466,7 @@ class Piwik_Date
* Doesn't modify $this
*
* @param int $n
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function subMonth($n)
{
@@ -479,7 +482,7 @@ class Piwik_Date
1, // we set the day to 1
date('Y', $ts)
);
- return new Piwik_Date($result, $this->timezone);
+ return new Date($result, $this->timezone);
}
/**
@@ -488,7 +491,7 @@ class Piwik_Date
* Doesn't modify $this
*
* @param int $n
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function subYear($n)
{
@@ -504,7 +507,7 @@ class Piwik_Date
1, // we set the day to 1
date('Y', $ts) - $n
);
- return new Piwik_Date($result, $this->timezone);
+ return new Date($result, $this->timezone);
}
/**
@@ -539,12 +542,12 @@ class Piwik_Date
* Doesn't modify $this
*
* @param int $n Number of days to add
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function addDay($n)
{
$ts = strtotime("+$n day", $this->timestamp);
- return new Piwik_Date($ts, $this->timezone);
+ return new Date($ts, $this->timezone);
}
/**
@@ -553,12 +556,12 @@ class Piwik_Date
* Doesn't modify $this
*
* @param int $n Number of hours to add
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function addHour($n)
{
$ts = self::addHourTo($this->timestamp, $n);
- return new Piwik_Date($ts, $this->timezone);
+ return new Date($ts, $this->timezone);
}
/**
@@ -599,7 +602,7 @@ class Piwik_Date
* Doesn't modify $this
*
* @param int $n Number of hours to substract
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function subHour($n)
{
@@ -613,7 +616,7 @@ class Piwik_Date
*
* @param int $n
* @param string $period period to add (WEEK, DAY,...)
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function addPeriod($n, $period)
{
@@ -622,7 +625,7 @@ class Piwik_Date
} else {
$ts = strtotime("+$n $period", $this->timestamp);
}
- return new Piwik_Date($ts, $this->timezone);
+ return new Date($ts, $this->timezone);
}
/**
@@ -632,7 +635,7 @@ class Piwik_Date
*
* @param int $n
* @param string $period period to sub
- * @return Piwik_Date new date
+ * @return \Piwik\Date new date
*/
public function subPeriod($n, $period)
{