diff --git a/docs/en/index.html b/docs/en/index.html index 6753d1a..d939e17 100644 --- a/docs/en/index.html +++ b/docs/en/index.html @@ -118,6 +118,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
$c = \Carbon::now(); return verta($c); // 1395-12-09 15:05:56 +// or +\Illuminate\Support\Carbon::now()->toJalali(); // 1395-12-09 15:05:56 +// or with laravel helper +now()->toJalali(); // 1395-12-09 15:05:56 +// or in models +return $model->created_at->toJalali();
To use the jalali datetime as a string, use the following command
@@ -165,6 +171,8 @@ $dt = verta()->datetime();
Verta to Carbon, use the following command
$c = Carbon::instance($v->datetime());
+// or
+$c = $v->toCarbon();
Property
To get property, use the following command
@@ -212,7 +220,7 @@ $v = $v->timezone('Asia/Baku');
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
-$v = $v->setDateJalali(1395, 4, 25);
+$v = $v->setDate(1395, 4, 25);
$v = $v->setTimeString('12:25:48');
Format
diff --git a/docs/index.html b/docs/index.html
index 7ca3b70..20c2614 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -123,6 +123,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
$c = \Carbon::now();
return verta($c); // 1395-12-09 15:05:56
+// or
+\Illuminate\Support\Carbon::now()->toJalali(); // 1395-12-09 15:05:56
+// or with laravel helper
+now()->toJalali(); // 1395-12-09 15:05:56
+// or in models
+return $model->created_at->toJalali();
برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید
@@ -170,6 +176,8 @@ $dt = verta()->datetime();
برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید
$c = Carbon::instance($v->datetime());
+// or
+$c = $v->toCarbon();
خصوصیات
برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید
@@ -217,7 +225,7 @@ $v = $v->timezone('Asia/Baku');
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
-$v = $v->setDateJalali(1395, 4, 25);
+$v = $v->setDate(1395, 4, 25);
$v = $v->setTimeString('12:25:48');
قالب نمایش
diff --git a/src/Laravel/VertaServiceProvider.php b/src/Laravel/VertaServiceProvider.php
index cf1bc00..a38dfe7 100644
--- a/src/Laravel/VertaServiceProvider.php
+++ b/src/Laravel/VertaServiceProvider.php
@@ -3,6 +3,7 @@
namespace Hekmatinasser\Verta\Laravel;
use Hekmatinasser\Verta\Verta;
+use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
@@ -87,6 +88,10 @@ class VertaServiceProvider extends ServiceProvider
$this->app->bind('verta', function ($app) {
return new Verta();
});
+
+ Carbon::macro('toJalali', function ($timezone = null) {
+ return new Verta($this, $timezone);
+ });
}
/**
diff --git a/src/Traits/Accessor.php b/src/Traits/Accessor.php
index 454c8d8..583c38f 100644
--- a/src/Traits/Accessor.php
+++ b/src/Traits/Accessor.php
@@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits;
use DateTimeZone;
use Hekmatinasser\Verta\Verta;
use InvalidArgumentException;
+use ReturnTypeWillChange;
trait Accessor
{
@@ -229,7 +230,7 @@ trait Accessor
*/
public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0)
{
- return $this->setDateJalali($year, $month, $day)->setTime($hour, $minute, $second, $microseconds);
+ return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second, $microseconds);
}
/**
@@ -242,7 +243,8 @@ trait Accessor
*
* @return Verta
*/
- public function setDateJalali($year, $month, $day)
+ #[ReturnTypeWillChange]
+ public function setDate($year, $month, $day)
{
if (static::isValidDate($year, $month, $day)) {
list($year, $month, $day) = self::getGregorian($year, $month, $day);
diff --git a/src/Traits/Boundaries.php b/src/Traits/Boundaries.php
index 4f3d402..7805207 100644
--- a/src/Traits/Boundaries.php
+++ b/src/Traits/Boundaries.php
@@ -133,7 +133,7 @@ trait Boundaries
{
$month = $this->quarter * static::MONTHS_PER_QUARTER;
- return $this->setDateJalali($this->year, $month, 1)->endMonth();
+ return $this->setDate($this->year, $month, 1)->endMonth();
}
/**
diff --git a/src/Traits/Creator.php b/src/Traits/Creator.php
index 2543a6a..8340bb5 100644
--- a/src/Traits/Creator.php
+++ b/src/Traits/Creator.php
@@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits;
use DateTime;
use DateTimeZone;
use Exception;
+use Illuminate\Support\Carbon;
use InvalidArgumentException;
trait Creator
@@ -30,7 +31,12 @@ trait Creator
}
try {
- parent::__construct($dt, static::createTimeZone($timezone));
+ if ($datetime instanceof DateTime) {
+ parent::__construct($dt);
+ parent::setTimezone($datetime->getTimezone());
+ } else {
+ parent::__construct($dt, static::createTimeZone($timezone));
+ }
self::loadMessages();
} catch (Exception $exception) {
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
@@ -125,7 +131,19 @@ 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;
+ }
+
+ /**
+ * Create a Carbon instance from Verta
+ *
+ * @return Carbon
+ */
+ public function toCarbon()
+ {
+ return Carbon::instance($this->datetime());
}
/**
diff --git a/src/Traits/Formatting.php b/src/Traits/Formatting.php
index 584f190..40718a7 100644
--- a/src/Traits/Formatting.php
+++ b/src/Traits/Formatting.php
@@ -4,6 +4,7 @@ namespace Hekmatinasser\Verta\Traits;
use Hekmatinasser\Notowo\Notowo;
use Hekmatinasser\Verta\Verta;
+use ReturnTypeWillChange;
trait Formatting
{
@@ -325,6 +326,7 @@ trait Formatting
* @param string $format
* @return string
*/
+ #[ReturnTypeWillChange]
public function format($format)
{
return $this->date($this->strftime($format));
diff --git a/tests/AccessTest.php b/tests/AccessTest.php
index 3e5ac36..43e4205 100644
--- a/tests/AccessTest.php
+++ b/tests/AccessTest.php
@@ -97,9 +97,9 @@ class AccessTest extends TestCase
$this->assertEquals(11, $datetime->second);
}
- public function testSetDateJalali()
+ public function testSetDate()
{
- $datetime = verta()->setDateJalali(1397, 10, 11);
+ $datetime = verta()->setDate(1397, 10, 11);
$this->assertEquals(1397, $datetime->year);
$this->assertEquals(10, $datetime->month);
diff --git a/tests/CreatorTest.php b/tests/CreatorTest.php
index e91666f..6ce6a53 100644
--- a/tests/CreatorTest.php
+++ b/tests/CreatorTest.php
@@ -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,9 +68,26 @@ 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');
+ $datetime = verta('2019-01-01 10:20:11')->datetime()->format('Y-m-d H:i:s');
+
+ $this->assertEquals('2019-01-01 10:20:11', $datetime);
+ }
+
+ public function testToCarbon()
+ {
+ $datetime = verta('2019-01-01 10:20:11')->toCarbon()->format('Y-m-d H:i:s');
$this->assertEquals('2019-01-01 10:20:11', $datetime);
}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
deleted file mode 100644
index a814366..0000000
--- a/tests/bootstrap.php
+++ /dev/null
@@ -1 +0,0 @@
-