forked from h4kuna/fio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added DateTime class to avoid autoloader issues with old nette/nette
- Loading branch information
Showing
3 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace h4kuna\Fio\Utils; | ||
|
||
use InvalidArgumentException; | ||
use function date_default_timezone_get; | ||
use function is_float; | ||
use function is_numeric; | ||
use function is_string; | ||
use function time; | ||
|
||
/** | ||
* DateTime. | ||
* | ||
* @see https://github.com/nette/utils/blob/v2.3.11/src/Utils/DateTime.php | ||
*/ | ||
class DateTime extends \DateTime | ||
{ | ||
/** minute in seconds */ | ||
const MINUTE = 60; | ||
|
||
/** hour in seconds */ | ||
const HOUR = 3600; | ||
|
||
/** day in seconds */ | ||
const DAY = 86400; | ||
|
||
/** week in seconds */ | ||
const WEEK = 604800; | ||
|
||
/** average month in seconds */ | ||
const MONTH = 2629800; | ||
|
||
/** average year in seconds */ | ||
const YEAR = 31557600; | ||
|
||
|
||
/** | ||
* DateTime object factory. | ||
* @param string|int|\DateTime | ||
* @return static | ||
*/ | ||
public static function from($time) | ||
{ | ||
if ($time instanceof \DateTime || $time instanceof \DateTimeInterface) { | ||
return new static($time->format('Y-m-d H:i:s'), $time->getTimezone()); | ||
|
||
} elseif (is_numeric($time)) { | ||
if ($time <= self::YEAR) { | ||
$time += time(); | ||
} | ||
$tmp = new static('@' . $time); | ||
return $tmp->setTimeZone(new \DateTimeZone(date_default_timezone_get())); | ||
|
||
} else { // textual or NULL | ||
return new static($time); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->format('Y-m-d H:i:s'); | ||
} | ||
|
||
|
||
/** | ||
* @param string | ||
* @return static | ||
*/ | ||
public function modifyClone($modify = '') | ||
{ | ||
$dolly = clone $this; | ||
return $modify ? $dolly->modify($modify) : $dolly; | ||
} | ||
|
||
|
||
/** | ||
* @param int | ||
* @return static | ||
*/ | ||
public function setTimestamp($timestamp) | ||
{ | ||
$zone = $this->getTimezone(); | ||
$this->__construct('@' . $timestamp); | ||
return $this->setTimeZone($zone); | ||
} | ||
|
||
|
||
/** | ||
* @return int|string | ||
*/ | ||
public function getTimestamp() | ||
{ | ||
$ts = $this->format('U'); | ||
return is_float($tmp = $ts * 1) ? $ts : $tmp; | ||
} | ||
|
||
|
||
/** | ||
* Returns new DateTime object formatted according to the specified format. | ||
* @param string The format the $time parameter should be in | ||
* @param string String representing the time | ||
* @param string|\DateTimeZone desired timezone (default timezone is used if NULL is passed) | ||
* @return static|FALSE | ||
*/ | ||
public static function createFromFormat($format, $time, $timezone = NULL) | ||
{ | ||
if ($timezone === NULL) { | ||
$timezone = new \DateTimeZone(date_default_timezone_get()); | ||
|
||
} elseif (is_string($timezone)) { | ||
$timezone = new \DateTimeZone($timezone); | ||
|
||
} elseif (!$timezone instanceof \DateTimeZone) { | ||
throw new InvalidArgumentException('Invalid timezone given'); | ||
} | ||
|
||
$date = parent::createFromFormat($format, $time, $timezone); | ||
return $date ? static::from($date) : FALSE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters