Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeFormat: support quick access properties #302

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/StaticClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
trait StaticClass
{
/**
* @return never
* @throws \Error
* Class is static and cannot be instantiated.
*/
final public function __construct()
final private function __construct()
{
throw new \Error('Class ' . static::class . ' is static and cannot be instantiated.');
}


Expand Down
19 changes: 18 additions & 1 deletion src/Utils/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,27 @@

/**
* DateTime.
* @property-read int $timestamp
* @property-read int $year
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $microsecond
* @property-read int $millisecond
* @property-read string $date
* @property-read string $dateTime
* @property-read string $dateTimeTz
* @property-read string $dateTimeMicro
* @property-read string $dateTimeMicroTz
* @property-read float $timestampMicro
* @property-read int $dayOfWeek
*/
class DateTime extends \DateTime implements \JsonSerializable
{
use Nette\SmartObject;
use DateTimeFormat;

/** minute in seconds */
public const MINUTE = 60;
Expand Down Expand Up @@ -125,7 +142,7 @@ public function jsonSerialize(): string
*/
public function __toString(): string
{
return $this->format('Y-m-d H:i:s');
return $this->getDateTime();
}


Expand Down
110 changes: 110 additions & 0 deletions src/Utils/DateTimeFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Utils;

/**
* Date time format collection.
*/
trait DateTimeFormat
{
public function getYear(): int
{
return (int) $this->format('Y');
}


public function getMonth(): int
{
return (int) $this->format('n');
}


public function getDay(): int
{
return (int) $this->format('j');
}


public function getHour(): int
{
return (int) $this->format('G');
}


public function getMinute(): int
{
return (int) $this->format('i');
}


public function getSecond(): int
{
return (int) $this->format('s');
}


public function getMicrosecond(): int
{
return (int) $this->format('u');
}


public function getMillisecond(): int
{
return (int) $this->format('v');
}


public function getDate(): string
{
return $this->format('Y-m-d');
}


public function getDateTime(): string
{
return $this->format('Y-m-d H:i:s');
}


public function getDateTimeTz(): string
{
return $this->format('Y-m-d H:i:sO');
}


public function getDateTimeMicro(): string
{
$micro = rtrim($this->format('u'), '0');
if ($micro === '') {
$micro = '0';
}

return $this->format('Y-m-d H:i:s.') . $micro;
}


public function getDateTimeMicroTz(): string
{
return $this->getDateTimeMicro() . $this->format('O');
}


public function getTimestampMicro(): float
{
return (float) $this->format('U.u');
}


public function getDayOfWeek(): int
{
return (int) $this->format('N');
}
}
4 changes: 2 additions & 2 deletions src/Utils/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@

[, $operator, $size, $unit] = $matches;
$units = ['' => 1, 'k' => 1e3, 'm' => 1e6, 'g' => 1e9];
$size *= $units[strtolower($unit)];

Check failure on line 280 in src/Utils/Finder.php

View workflow job for this annotation

GitHub Actions / PHPStan

Binary operation "*=" between string and 1|1000.0|1000000.0|1000000000.0 results in an error.
$operator = $operator ?: '=';
}

Expand Down Expand Up @@ -337,7 +337,7 @@


/**
* @param array<\stdClass{pattern: string, mode: string, recursive: bool}> $searches
* @param array<object{pattern: string, mode: string, recursive: bool}> $searches
* @param string[] $subdirs
* @return \Generator<string, FileInfo>
*/
Expand Down Expand Up @@ -427,7 +427,7 @@
}


/** @return array<string, array<\stdClass{pattern: string, mode: string, recursive: bool}>> */
/** @return array<string, array<object{pattern: string, mode: string, recursive: bool}>> */
private function buildPlan(): array
{
$plan = $dirCache = [];
Expand Down
8 changes: 5 additions & 3 deletions src/Utils/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,11 @@ public static function findPrefix(array $strings): string
*/
public static function length(string $s): int
{
return function_exists('mb_strlen')
? mb_strlen($s, 'UTF-8')
: strlen(utf8_decode($s));
return match (true) {
extension_loaded('mbstring') => mb_strlen($s, 'UTF-8'),
extension_loaded('iconv') => iconv_strlen($s, 'UTF-8'),
default => strlen(@utf8_decode($s)), // deprecated
};
}


Expand Down
41 changes: 41 additions & 0 deletions tests/Utils/DateTimeFormat.properties.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Test: custom properties of Nette\Utils\DateTime.
*/

declare(strict_types=1);

use Nette\Utils\DateTime;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


date_default_timezone_set('Europe/Prague');

Assert::same(1978, DateTime::from('1978-01-23 11:40:00.86')->year);
Assert::same(1, DateTime::from('1978-01-23 11:40:00.86')->month);
Assert::same(23, DateTime::from('1978-01-23 11:40:00.86')->day);
Assert::same(11, DateTime::from('1978-01-23 11:40:00.86')->hour);
Assert::same(40, DateTime::from('1978-01-23 11:40:00.86')->minute);
Assert::same(0, DateTime::from('1978-01-23 11:40:00.86')->second);
Assert::same(863500, DateTime::from('1978-01-23 11:40:00.8635')->microsecond);
Assert::same(863, DateTime::from('1978-01-23 11:40:00.8635')->millisecond);
Assert::same('1978-01-23', DateTime::from('1978-01-23 11:40:00.86')->date);
Assert::same('1978-01-23 11:40:00', DateTime::from('1978-01-23 11:40:00.86')->dateTime);
Assert::same('1978-01-23 11:40:00+0100', DateTime::from('1978-01-23 11:40:00.86')->dateTimeTz);
Assert::same('1978-01-23 11:40:00.86', DateTime::from('1978-01-23 11:40:00.860000')->dateTimeMicro);
Assert::same('1978-01-23 11:40:00.0', DateTime::from('1978-01-23 11:40:00.000000')->dateTimeMicro);
Assert::same('1978-01-23 11:40:00.86+0100', DateTime::from('1978-01-23 11:40:00.860000')->dateTimeMicroTz);
Assert::same(254_400_000.86, DateTime::from('1978-01-23 11:40:00.860000')->timestampMicro);
Assert::same(254_400_000, DateTime::from('1978-01-23 11:40:00.860000')->timestamp);
Assert::same(1, DateTime::from('1978-01-23 11:40:00.860000')->dayOfWeek);

/**
* reverse engineering does not work, it is reason why keep zero for microsecond
* @see \Nette\Utils\DateTimeFormat::getDateTimeMicro()
*/
Assert::type(DateTime::class, DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00.0'));
Assert::false(DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00.'));
Assert::false(DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00'));
2 changes: 1 addition & 1 deletion tests/Utils/StaticClass.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestClass
Assert::exception(
fn() => new TestClass,
Error::class,
'Class TestClass is static and cannot be instantiated.',
'Call to private TestClass::__construct() from global scope',
);

Assert::exception(
Expand Down
Loading