Merge pull request #119 from mziraki/master
add syntactic sugar & fix timezone initialization
This commit is contained in:
10 files changed
+71
-11
No files matched your search
+9
-1
@@ -118,6 +118,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
|
|||||||
<pre>
|
<pre>
|
||||||
$c = \Carbon::now();
|
$c = \Carbon::now();
|
||||||
return verta($c); // 1395-12-09 15:05:56
|
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>
|
</pre>
|
||||||
<p>To use the jalali datetime as a string, use the following command</p>
|
<p>To use the jalali datetime as a string, use the following command</p>
|
||||||
<pre>
|
<pre>
|
||||||
@@ -165,6 +171,8 @@ $dt = verta()->datetime();
|
|||||||
<p>Verta to Carbon, use the following command</p>
|
<p>Verta to Carbon, use the following command</p>
|
||||||
<pre>
|
<pre>
|
||||||
$c = Carbon::instance($v->datetime());
|
$c = Carbon::instance($v->datetime());
|
||||||
|
// or
|
||||||
|
$c = $v->toCarbon();
|
||||||
</pre>
|
</pre>
|
||||||
<h3 id="property">Property</h3>
|
<h3 id="property">Property</h3>
|
||||||
<p>To get property, use the following command</p>
|
<p>To get property, use the following command</p>
|
||||||
@@ -212,7 +220,7 @@ $v = $v->timezone('Asia/Baku');
|
|||||||
<pre>
|
<pre>
|
||||||
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
|
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
|
||||||
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
|
$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');
|
$v = $v->setTimeString('12:25:48');
|
||||||
</pre>
|
</pre>
|
||||||
<h2 id="format">Format</h2>
|
<h2 id="format">Format</h2>
|
||||||
|
|||||||
+9
-1
@@ -123,6 +123,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
|
|||||||
<pre>
|
<pre>
|
||||||
$c = \Carbon::now();
|
$c = \Carbon::now();
|
||||||
return verta($c); // 1395-12-09 15:05:56
|
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>
|
</pre>
|
||||||
<p>برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید</p>
|
<p>برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید</p>
|
||||||
<pre>
|
<pre>
|
||||||
@@ -170,6 +176,8 @@ $dt = verta()->datetime();
|
|||||||
<p>برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید</p>
|
<p>برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید</p>
|
||||||
<pre>
|
<pre>
|
||||||
$c = Carbon::instance($v->datetime());
|
$c = Carbon::instance($v->datetime());
|
||||||
|
// or
|
||||||
|
$c = $v->toCarbon();
|
||||||
</pre>
|
</pre>
|
||||||
<h3 id="property">خصوصیات</h3>
|
<h3 id="property">خصوصیات</h3>
|
||||||
<p>برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید</p>
|
<p>برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید</p>
|
||||||
@@ -217,7 +225,7 @@ $v = $v->timezone('Asia/Baku');
|
|||||||
<pre>
|
<pre>
|
||||||
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
|
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
|
||||||
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
|
$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');
|
$v = $v->setTimeString('12:25:48');
|
||||||
</pre>
|
</pre>
|
||||||
<h2 id="format">قالب نمایش</h2>
|
<h2 id="format">قالب نمایش</h2>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace Hekmatinasser\Verta\Laravel;
|
namespace Hekmatinasser\Verta\Laravel;
|
||||||
|
|
||||||
use Hekmatinasser\Verta\Verta;
|
use Hekmatinasser\Verta\Verta;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
@@ -87,6 +88,10 @@ class VertaServiceProvider extends ServiceProvider
|
|||||||
$this->app->bind('verta', function ($app) {
|
$this->app->bind('verta', function ($app) {
|
||||||
return new Verta();
|
return new Verta();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Carbon::macro('toJalali', function ($timezone = null) {
|
||||||
|
return new Verta($this, $timezone);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits;
|
|||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
use Hekmatinasser\Verta\Verta;
|
use Hekmatinasser\Verta\Verta;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use ReturnTypeWillChange;
|
||||||
|
|
||||||
trait Accessor
|
trait Accessor
|
||||||
{
|
{
|
||||||
@@ -229,7 +230,7 @@ trait Accessor
|
|||||||
*/
|
*/
|
||||||
public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0)
|
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
|
* @return Verta
|
||||||
*/
|
*/
|
||||||
public function setDateJalali($year, $month, $day)
|
#[ReturnTypeWillChange]
|
||||||
|
public function setDate($year, $month, $day)
|
||||||
{
|
{
|
||||||
if (static::isValidDate($year, $month, $day)) {
|
if (static::isValidDate($year, $month, $day)) {
|
||||||
list($year, $month, $day) = self::getGregorian($year, $month, $day);
|
list($year, $month, $day) = self::getGregorian($year, $month, $day);
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ trait Boundaries
|
|||||||
{
|
{
|
||||||
$month = $this->quarter * static::MONTHS_PER_QUARTER;
|
$month = $this->quarter * static::MONTHS_PER_QUARTER;
|
||||||
|
|
||||||
return $this->setDateJalali($this->year, $month, 1)->endMonth();
|
return $this->setDate($this->year, $month, 1)->endMonth();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+19
-1
@@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits;
|
|||||||
use DateTime;
|
use DateTime;
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
|
||||||
trait Creator
|
trait Creator
|
||||||
@@ -30,7 +31,12 @@ trait Creator
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if ($datetime instanceof DateTime) {
|
||||||
|
parent::__construct($dt);
|
||||||
|
parent::setTimezone($datetime->getTimezone());
|
||||||
|
} else {
|
||||||
parent::__construct($dt, static::createTimeZone($timezone));
|
parent::__construct($dt, static::createTimeZone($timezone));
|
||||||
|
}
|
||||||
self::loadMessages();
|
self::loadMessages();
|
||||||
} catch (Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
|
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
|
||||||
@@ -125,7 +131,19 @@ trait Creator
|
|||||||
*/
|
*/
|
||||||
public function datetime()
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Hekmatinasser\Verta\Traits;
|
|||||||
|
|
||||||
use Hekmatinasser\Notowo\Notowo;
|
use Hekmatinasser\Notowo\Notowo;
|
||||||
use Hekmatinasser\Verta\Verta;
|
use Hekmatinasser\Verta\Verta;
|
||||||
|
use ReturnTypeWillChange;
|
||||||
|
|
||||||
trait Formatting
|
trait Formatting
|
||||||
{
|
{
|
||||||
@@ -325,6 +326,7 @@ trait Formatting
|
|||||||
* @param string $format
|
* @param string $format
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
#[ReturnTypeWillChange]
|
||||||
public function format($format)
|
public function format($format)
|
||||||
{
|
{
|
||||||
return $this->date($this->strftime($format));
|
return $this->date($this->strftime($format));
|
||||||
|
|||||||
@@ -97,9 +97,9 @@ class AccessTest extends TestCase
|
|||||||
$this->assertEquals(11, $datetime->second);
|
$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(1397, $datetime->year);
|
||||||
$this->assertEquals(10, $datetime->month);
|
$this->assertEquals(10, $datetime->month);
|
||||||
|
|||||||
+20
-2
@@ -3,8 +3,10 @@
|
|||||||
namespace Hekmatinasser\Verta\Tests;
|
namespace Hekmatinasser\Verta\Tests;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use DateTimeZone;
|
||||||
use Hekmatinasser\Verta\Facades\Verta as VertaFacade;
|
use Hekmatinasser\Verta\Facades\Verta as VertaFacade;
|
||||||
use Hekmatinasser\Verta\Verta;
|
use Hekmatinasser\Verta\Verta;
|
||||||
|
use Illuminate\Support\Facades\Date;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class CreatorTest extends TestCase
|
class CreatorTest extends TestCase
|
||||||
@@ -55,7 +57,6 @@ class CreatorTest extends TestCase
|
|||||||
|
|
||||||
$this->assertEquals('1397-10-11 10:20:11', $datetime);
|
$this->assertEquals('1397-10-11 10:20:11', $datetime);
|
||||||
|
|
||||||
|
|
||||||
$datetime = (string) Verta::instance('2019-01-01 10:20:11');
|
$datetime = (string) Verta::instance('2019-01-01 10:20:11');
|
||||||
$this->assertEquals('1397-10-11 10:20:11', $datetime);
|
$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);
|
$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()
|
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);
|
$this->assertEquals('2019-01-01 10:20:11', $datetime);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
<?php
|
|
||||||
Reference in new issue
Block a user