Skip to content

Commit

Permalink
added DateTime class to avoid autoloader issues with old nette/nette
Browse files Browse the repository at this point in the history
  • Loading branch information
Dukecz committed Mar 24, 2020
1 parent 589fec4 commit 941c699
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Request/Pay/Payment/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
h4kuna\Fio\Account,
h4kuna\Fio\Utils,
Iterator,
Nette\Utils\DateTime;
h4kuna\Fio\Utils\DateTime;

/**
* @author Milan Matějček
Expand Down
126 changes: 126 additions & 0 deletions src/Utils/DateTime.php
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;
}

}
3 changes: 1 addition & 2 deletions src/Utils/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace h4kuna\Fio\Utils;

use h4kuna\Fio\Account,
Nette\Utils\DateTime;
use h4kuna\Fio\Account;

/**
* @author Milan Matějček
Expand Down

0 comments on commit 941c699

Please sign in to comment.