-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DateTimeFormat: support shortcut properties
- Loading branch information
Showing
3 changed files
with
169 additions
and
1 deletion.
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,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'); | ||
} | ||
} |
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,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')); |