Skip to content

Date and time improve #260

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
207 changes: 139 additions & 68 deletions src/Core/Base/Date.php

Large diffs are not rendered by default.

124 changes: 83 additions & 41 deletions src/Core/Base/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,86 @@

namespace OnPHP\Core\Base;

use DateTimeZone;
use OnPHP\Core\Exception\WrongArgumentException;
use OnPHP\Core\Exception\WrongStateException;

/* $Id$ */

/**
* Date and time container and utilities.
*
* @see Date
*
* @ingroup Base
**/
*/
class Timestamp extends Date
{
/**
* @param $timestamp
* @param DateTimeZone|null $zone
* @return Timestamp
**/
public static function create($timestamp, \DateTimeZone $zone=null)
* @throws WrongArgumentException
*/
public static function create($timestamp, DateTimeZone $zone = null): Timestamp
{
return new static($timestamp, $zone);
}

public static function now()
/**
* @return string
*/
public static function now(): string
{
return date(static::getFormat());
}

/**
* @return Timestamp
**/
public static function makeNow()
* @throws WrongArgumentException
*/
public static function makeNow(): Timestamp
{
return new static(time());
}

/**
* @return Timestamp
**/
public static function makeToday()
{
return new static(static::today());
}

public function __construct($dateTime, \DateTimeZone $zone=null)
* @param $dateTime
* @param DateTimeZone|null $zone
* @throws WrongArgumentException
*/
public function __construct($dateTime, DateTimeZone $zone = null)
{
parent::__construct($dateTime);

if($zone) {
if (null !== $zone) {
$this->dateTime->setTimezone($zone);
}

}

private function getDefaultTimeZone()
/**
* @return DateTimeZone
* @throws WrongStateException
*/
private function getDefaultTimeZone(): DateTimeZone
{
try {
$defaultTimeZoneName = date_default_timezone_get();
return new \DateTimeZone($defaultTimeZoneName);
} catch(\Exception $e) {
return new DateTimeZone($defaultTimeZoneName);
} catch(\Throwable $e) {
throw new WrongStateException(
"strange default time zone given - '{$defaultTimeZoneName}'!".
'Use date_default_timezone_set() for set valid default time zone.'
(
($defaultTimeZoneName ?? false)
? "strange default time zone given - '{$defaultTimeZoneName}'!"
: "default time zone not fetched!"
) . ' Use date_default_timezone_set() for set valid default time zone.'
);
}
}

public function toTime($timeDelimiter = ':', $secondDelimiter = '.')
/**
* @param string $timeDelimiter
* @param string $secondDelimiter
* @return string
*/
public function toTime(string $timeDelimiter = ':', string $secondDelimiter = '.'): string
{
return
$this->getHour()
Expand All @@ -87,33 +101,52 @@ public function toTime($timeDelimiter = ':', $secondDelimiter = '.')
.$this->getSecond();
}

/**
* @param string $dateDelimiter
* @param string $timeDelimiter
* @param string $secondDelimiter
* @return string
*/
public function toDateTime(
$dateDelimiter = '-',
$timeDelimiter = ':',
$secondDelimiter = '.'
)
string $dateDelimiter = '-',
string $timeDelimiter = ':',
string $secondDelimiter = '.'
): string
{
return
$this->toDate($dateDelimiter).' '
.$this->toTime($timeDelimiter, $secondDelimiter);
}

public function getHour()
/**
* @return string
*/
public function getHour(): string
{
return $this->dateTime->format('H');
}

public function getMinute()
/**
* @return string
*/
public function getMinute(): string
{
return $this->dateTime->format('i');
}

public function getSecond()
/**
* @return string
*/
public function getSecond(): string
{
return $this->dateTime->format('s');
}

public function equals(Timestamp $timestamp)
/**
* @param Timestamp $timestamp
* @return bool
*/
public function equals(Timestamp $timestamp): bool
{
return ($this->toDateTime() === $timestamp->toDateTime());
}
Expand All @@ -126,10 +159,14 @@ public function getDayStartStamp()
return parent::getDayStartStamp();
}

/**
* @return false|int
*/
public function getHourStartStamp()
{
if (!$this->getMinute() && !$this->getSecond())
if (!$this->getMinute() && !$this->getSecond()) {
return $this->dateTime->getTimestamp();
}

return
mktime(
Expand All @@ -144,8 +181,10 @@ public function getHourStartStamp()

/**
* ISO 8601 time string
**/
public function toIsoString($convertToUtc = true)
* @param bool $convertToUtc
* @return string
*/
public function toIsoString(bool $convertToUtc = true): string
{
if ($convertToUtc)
return date('Y-m-d\TH:i:s\Z', $this->dateTime->getTimestamp() - date('Z', $this->dateTime->getTimestamp()));
Expand All @@ -155,15 +194,18 @@ public function toIsoString($convertToUtc = true)

/**
* @return Timestamp
**/
public function toTimestamp()
* @throws WrongArgumentException
*/
public function toTimestamp(): Timestamp
{
return $this->spawn();
}

protected static function getFormat()
/**
* @return string
*/
protected static function getFormat(): string
{
return 'Y-m-d H:i:s';
}
}
?>
}
34 changes: 23 additions & 11 deletions src/Core/Base/TimestampTZ.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,39 @@

namespace OnPHP\Core\Base;

use DateTimeZone;
use OnPHP\Core\Exception\WrongArgumentException;

/**
* Timestamp with time zone
*
* @see Timestamp, Date
* @ingroup Base
*/
class TimestampTZ extends Timestamp
{
/**
* @static
* @return string
*/
protected static function getFormat()
protected static function getFormat(): string
{
return 'Y-m-d H:i:sO';
}

/**
* @return Timestamp
**/
public function toTimestamp($zone=null)
* @param null $zone
* @return Timestamp|TimestampTZ
* @throws WrongArgumentException
*/
public function toTimestamp($zone = null): Timestamp
{
if($zone) {

if (null === $zone) {
if(
!($zone instanceof \DateTimeZone)
!($zone instanceof DateTimeZone)
&& is_scalar($zone)
) {
$zone = new \DateTimeZone($zone);
$zone = new DateTimeZone($zone);
}

return new static($this->toStamp(), $zone);
Expand All @@ -44,7 +51,13 @@ public function toTimestamp($zone=null)
return parent::toTimestamp();
}

public static function compare(Date $left, Date $right)
/**
* @param Date $left
* @param Date $right
* @return int
* @throws WrongArgumentException
*/
public static function compare(Date $left, Date $right): int
{
Assert::isTrue(
(
Expand All @@ -55,5 +68,4 @@ public static function compare(Date $left, Date $right)

return parent::compare($left, $right);
}
}
?>
}
2 changes: 1 addition & 1 deletion src/Main/Base/CalendarDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class CalendarDay extends Date
/**
* @return CalendarDay
**/
public static function create($timestamp)
public static function create($timestamp): CalendarDay
{
return new self($timestamp);
}
Expand Down
Loading