Merge pull request #119 from mziraki/master

add syntactic sugar & fix timezone initialization
This commit is contained in:
hekmatinasser authored and GitHub committed 2022-02-24 10:09:32 +03:30
commit 5809ea46b4
10 files changed
+72 -12

No files matched your search

+9 -1
View File
@@ -118,6 +118,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
<pre>
$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();
</pre>
<p>To use the jalali datetime as a string, use the following command</p>
<pre>
@@ -165,6 +171,8 @@ $dt = verta()->datetime();
<p>Verta to Carbon, use the following command</p>
<pre>
$c = Carbon::instance($v->datetime());
// or
$c = $v->toCarbon();
</pre>
<h3 id="property">Property</h3>
<p>To get property, use the following command</p>
@@ -212,7 +220,7 @@ $v = $v->timezone('Asia/Baku');
<pre>
$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');
</pre>
<h2 id="format">Format</h2>
+9 -1
View File
@@ -123,6 +123,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
<pre>
$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();
</pre>
<p>برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید</p>
<pre>
@@ -170,6 +176,8 @@ $dt = verta()->datetime();
<p>برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید</p>
<pre>
$c = Carbon::instance($v->datetime());
// or
$c = $v->toCarbon();
</pre>
<h3 id="property">خصوصیات</h3>
<p>برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید</p>
@@ -217,7 +225,7 @@ $v = $v->timezone('Asia/Baku');
<pre>
$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');
</pre>
<h2 id="format">قالب نمایش</h2>
+5
View File
@@ -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);
});
}
/**
+4 -2
View File
@@ -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);
+1 -1
View File
@@ -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();
}
/**
+20 -2
View File
@@ -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());
}
/**
+2
View File
@@ -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));
+2 -2
View File
@@ -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);
+20 -2
View File
@@ -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);
}
-1
View File
@@ -1 +0,0 @@
<?php