Skip to content

Commit

Permalink
Dropped PHP 7.4 support, fixed PHP 8.1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
foxycode committed Oct 8, 2022
1 parent a0f1dcb commit e36d66c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
extensions: iconv, json
coverage: none

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
strategy:
matrix:
php:
- 7.4
- 8.0
- 8.1

fail-fast: false

Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
extensions: iconv, json
coverage: none

Expand All @@ -72,7 +72,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
extensions: iconv, json

- name: Get Composer Cache Directory
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": "^7.4|^8.0"
"php": "^8.0"
},
"require-dev": {
"ext-iconv": "*",
Expand Down
49 changes: 23 additions & 26 deletions src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Sunfox\DateUtils;

use DateTime as NativeDateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;

class DateTime extends \DateTimeImmutable implements \JsonSerializable
class DateTime extends DateTimeImmutable implements \JsonSerializable
{
/** minute in seconds */
public const MINUTE = 60;
Expand All @@ -31,7 +34,7 @@ class DateTime extends \DateTimeImmutable implements \JsonSerializable
* @return static
* @throws \Exception if the date and time are not valid.
*/
public static function from($time)
public static function from(string|int|\DateTimeInterface|null $time): static
{
if ($time instanceof \DateTimeInterface) {
return new static($time->format('Y-m-d H:i:s.u'), $time->getTimezone());
Expand All @@ -40,7 +43,7 @@ public static function from($time)
if ($time <= self::YEAR) {
$time += time();
}
return (new static('@' . $time))->setTimezone(new \DateTimeZone(date_default_timezone_get()));
return (new static('@' . $time))->setTimezone(new DateTimeZone(date_default_timezone_get()));

} else { // textual or null
return new static((string) $time);
Expand All @@ -50,7 +53,7 @@ public static function from($time)
/**
* Creates DateTime object.
* @return static
* @throws \InvalidArgumentException if the date and time are not valid.
* @throws InvalidArgumentException if the date and time are not valid.
*/
public static function fromParts(
int $year,
Expand All @@ -59,7 +62,7 @@ public static function fromParts(
int $hour = 0,
int $minute = 0,
float $second = 0.0
) {
): static {
$s = sprintf('%04d-%02d-%02d %02d:%02d:%02.5F', $year, $month, $day, $hour, $minute, $second);
if (
!checkdate($month, $day, $year)
Expand All @@ -70,38 +73,32 @@ public static function fromParts(
|| $second < 0
|| $second >= 60
) {
throw new \InvalidArgumentException("Invalid date '$s'");
throw new InvalidArgumentException("Invalid date '$s'");
}
return new static($s);
}

/**
* Returns new DateTime object formatted according to the specified format.
* @param string $format The format the $time parameter should be in
* @param string $time
* @param string|\DateTimeZone $timezone (default timezone is used if null is passed)
* @param string $datetime
* @param DateTimeZone $timezone (default timezone is used if null is passed)
* @return static|false
*/
public static function createFromFormat($format, $time, $timezone = null)
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false
{
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');
$timezone = new DateTimeZone(date_default_timezone_get());
}

$date = parent::createFromFormat($format, $time, $timezone);
$date = parent::createFromFormat($format, $datetime, $timezone);
return $date ? static::from($date) : false;
}

/**
* Get first day of year as DateTime instance
*/
public static function firstDayOfYear(?DateTimeInterface $date = null): self
public static function firstDayOfYear(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
return static::from(sprintf('%04d-01-01', $date->format('Y')));
Expand All @@ -110,7 +107,7 @@ public static function firstDayOfYear(?DateTimeInterface $date = null): self
/**
* Get last day of year as DateTime instance
*/
public static function lastDayOfYear(?DateTimeInterface $date = null): self
public static function lastDayOfYear(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
return static::from(sprintf('%04d-12-31', $date->format('Y')));
Expand All @@ -119,7 +116,7 @@ public static function lastDayOfYear(?DateTimeInterface $date = null): self
/**
* Get first day of quarter as DateTime instance
*/
public static function firstDayOfQuarter(?DateTimeInterface $date = null): self
public static function firstDayOfQuarter(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
$quarter = (int) ceil((int) $date->format('n') / 3);
Expand All @@ -130,7 +127,7 @@ public static function firstDayOfQuarter(?DateTimeInterface $date = null): self
/**
* Get last day of quarter as DateTime instance
*/
public static function lastDayOfQuarter(?DateTimeInterface $date = null): self
public static function lastDayOfQuarter(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
$quarter = (int) ceil((int) $date->format('n') / 3);
Expand All @@ -141,7 +138,7 @@ public static function lastDayOfQuarter(?DateTimeInterface $date = null): self
/**
* Get first day of month as DateTime instance
*/
public static function firstDayOfMonth(?DateTimeInterface $date = null): self
public static function firstDayOfMonth(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
return static::from($date->format('Y-m-01'));
Expand All @@ -150,7 +147,7 @@ public static function firstDayOfMonth(?DateTimeInterface $date = null): self
/**
* Get last day of month as DateTime instance
*/
public static function lastDayOfMonth(?DateTimeInterface $date = null): self
public static function lastDayOfMonth(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
return static::from($date->format('Y-m-t'));
Expand All @@ -159,7 +156,7 @@ public static function lastDayOfMonth(?DateTimeInterface $date = null): self
/**
* Get first day of week as DateTime instance
*/
public static function firstDayOfWeek(?DateTimeInterface $date = null): self
public static function firstDayOfWeek(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
$dayOfWeek = (int) $date->format('N');
Expand All @@ -171,15 +168,15 @@ public static function firstDayOfWeek(?DateTimeInterface $date = null): self
return static::from($date->format('Y-m-d') . ' -' . ($dayOfWeek - 1) . ' day');
}

final public function __construct(string $datetime = 'now', ?\DateTimeZone $timezone = null)
final public function __construct(string $datetime = 'now', ?DateTimeZone $timezone = null)
{
parent::__construct($datetime, $timezone);
}

/**
* Get last day of week as DateTime instance
*/
public static function lastDayOfWeek(?DateTimeInterface $date = null): self
public static function lastDayOfWeek(?DateTimeInterface $date = null): static
{
$date = self::checkDate($date);
$dayOfWeek = (int) $date->format('N');
Expand Down
19 changes: 5 additions & 14 deletions src/SpentTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@

class SpentTime
{
/**
* @var int
*/
private $hours = 0;

/**
* @var int
*/
private $minutes = 0;

/**
* @var int
*/
private $seconds = 0;
private int $hours = 0;

private int $minutes = 0;

private int $seconds = 0;

public function __construct(?string $time = null)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

final class Time implements ITime
{
/**
* @var int
*/
private $seconds = 0;
private int $seconds = 0;

/**
* @throws InvalidArgumentException
Expand Down
19 changes: 9 additions & 10 deletions tests/Cases/DateTimeTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ final class DateTimeTest extends Tester\TestCase
Assert::same(254400000, DateTime::from(254400000)->getTimestamp());

Assert::same(time() + 60, (int) DateTime::from(60)->format('U'));
Assert::same('2050-08-13 11:40:00', (string) DateTime::from(2544000000));
Assert::same('2050-08-13 11:40:00', (string) (new DateTime)->setTimestamp(2544000000));
Assert::same(is_int(2544000000) ? 2544000000 : '2544000000', DateTime::from(2544000000)->getTimestamp()); // 64 bit
Assert::same(2544000000, DateTime::from(2544000000)->getTimestamp());
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
Assert::same('2050-08-13 12:40:00', (string)DateTime::from(2544000000));
Assert::same('2050-08-13 12:40:00', (string)(new DateTime)->setTimestamp(2544000000));
} else {
Assert::same('2050-08-13 11:40:00', (string)DateTime::from(2544000000));
Assert::same('2050-08-13 11:40:00', (string)(new DateTime)->setTimestamp(2544000000));
}

Assert::same('1978-05-05 00:00:00', (string) DateTime::from('1978-05-05'));

Expand Down Expand Up @@ -74,13 +79,7 @@ final class DateTimeTest extends Tester\TestCase
Assert::same('2050-08-13 11:40:00.123450', DateTime::createFromFormat('Y-m-d H:i:s.u', '2050-08-13 11:40:00.12345')->format('Y-m-d H:i:s.u'));

Assert::same('Europe/Prague', DateTime::createFromFormat('Y', '2050')->getTimezone()->getName());
Assert::same('Europe/Bratislava', DateTime::createFromFormat('Y', '2050', 'Europe/Bratislava')->getTimezone()->getName());

Assert::error(
fn() => DateTime::createFromFormat('Y-m-d H:i:s', '2050-08-13 11:40:00', 5),
\InvalidArgumentException::class,
'Invalid timezone given'
);
Assert::same('Europe/Bratislava', DateTime::createFromFormat('Y', '2050', new \DateTimeZone('Europe/Bratislava'))->getTimezone()->getName());

Assert::false(DateTime::createFromFormat('Y-m-d', '2014-10'));
}
Expand Down

0 comments on commit e36d66c

Please sign in to comment.