add test for timezone in instance from DateTime object
temporary fix for php 8.1 warning
This commit is contained in:
1 parent
84b7bf87a1
commit
4db4b98065
3 files changed
+22
-6
No files matched your search
@@ -31,10 +31,11 @@ trait Creator
|
||||
}
|
||||
|
||||
try {
|
||||
$tz = static::createTimeZone($timezone);
|
||||
parent::__construct($dt, $tz);
|
||||
if ($datetime instanceof DateTime) {
|
||||
parent::setTimezone($tz);
|
||||
parent::__construct($dt);
|
||||
parent::setTimezone($datetime->getTimezone());
|
||||
} else {
|
||||
parent::__construct($dt, static::createTimeZone($timezone));
|
||||
}
|
||||
self::loadMessages();
|
||||
} catch (Exception $exception) {
|
||||
@@ -130,7 +131,9 @@ trait Creator
|
||||
*/
|
||||
public function datetime()
|
||||
{
|
||||
return new DateTime(date('Y-m-d H:i:s', $this->getTimestamp()), $this->getTimezone());
|
||||
$dt = new DateTime(date('Y-m-d H:i:s', $this->getTimestamp()));
|
||||
$dt->setTimezone($this->getTimezone());
|
||||
return $dt;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Hekmatinasser\Verta\Traits;
|
||||
|
||||
use Hekmatinasser\Notowo\Notowo;
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
use ReturnTypeWillChange;
|
||||
|
||||
trait Formatting
|
||||
{
|
||||
@@ -325,7 +326,8 @@ trait Formatting
|
||||
* @param string $format
|
||||
* @return string
|
||||
*/
|
||||
public function format(string $format): string
|
||||
#[ReturnTypeWillChange]
|
||||
public function format($format)
|
||||
{
|
||||
return $this->date($this->strftime($format));
|
||||
}
|
||||
|
||||
+12
-1
@@ -3,8 +3,10 @@
|
||||
namespace Hekmatinasser\Verta\Tests;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Hekmatinasser\Verta\Facades\Verta as VertaFacade;
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CreatorTest extends TestCase
|
||||
@@ -55,7 +57,6 @@ class CreatorTest extends TestCase
|
||||
|
||||
$this->assertEquals('1397-10-11 10:20:11', $datetime);
|
||||
|
||||
|
||||
$datetime = (string) Verta::instance('2019-01-01 10:20:11');
|
||||
$this->assertEquals('1397-10-11 10:20:11', $datetime);
|
||||
|
||||
@@ -67,6 +68,16 @@ class CreatorTest extends TestCase
|
||||
$this->assertEquals('1397-10-11 10:20:11', $datetime);
|
||||
}
|
||||
|
||||
public function testInstanceFromDateTimeWithTimezone()
|
||||
{
|
||||
$now = new DateTime('now', new DateTimeZone('Asia/Tehran'));
|
||||
|
||||
$datetime = new Verta($now);
|
||||
|
||||
$this->assertEquals($now->getTimezone()->getName(), $datetime->getTimezone()->getName());
|
||||
$this->assertEquals($now->format('Y-m-d H:i:s'), $datetime->datetime()->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
public function testDatetime()
|
||||
{
|
||||
$datetime = verta('2019-01-01 10:20:11')->DateTime()->format('Y-m-d H:i:s');
|
||||
|
||||
Reference in new issue
Block a user