Skip to content

Commit

Permalink
DateTimeFormat: support shortcut properties
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Sep 13, 2023
1 parent 71017d2 commit d333b05
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
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');
}
}
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'));

0 comments on commit d333b05

Please sign in to comment.