From 3b576adaf1030c6ed75052c191a078f90219b311 Mon Sep 17 00:00:00 2001 From: kundan Date: Mon, 4 Mar 2024 10:20:10 +0545 Subject: [PATCH] added toAd function converts bs date to ad date and return carbon date --- src/Facades/NepaliDateConverter.php | 2 ++ src/NepaliDateConverter.php | 16 ++++++++++++++++ tests/Unit/NepaliDateConverterTest.php | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/src/Facades/NepaliDateConverter.php b/src/Facades/NepaliDateConverter.php index de93fd3..5fc0aa8 100644 --- a/src/Facades/NepaliDateConverter.php +++ b/src/Facades/NepaliDateConverter.php @@ -6,10 +6,12 @@ /** * @method static toBs() + * @method static toAd() * @method static toBsDate() * @method static toBsFormattedDate() * @method static getAdDateByBsDate() * @method static getBsDateByAdDate() + * @see \Proshore\NepaliDate\NepaliDateConverter */ class NepaliDateConverter extends Facade { diff --git a/src/NepaliDateConverter.php b/src/NepaliDateConverter.php index 3301910..efc4ac0 100644 --- a/src/NepaliDateConverter.php +++ b/src/NepaliDateConverter.php @@ -80,4 +80,20 @@ public function getBsDateByAdDate(int $adYear, int $adMonth, int $adDate) : arra return $this->converter->getBsDateByAdDate($adYear, $adMonth, $adDate); } + /** + * @param int $bsYear + * @param int $bsMonth + * @param int $bsDay + * @return Carbon|null + */ + public function toAd(int $bsYear, int $bsMonth, int $bsDay): Carbon|null + { + $date = $this->converter->getAdDateByBsDate($bsYear, $bsMonth, $bsDay); + if(! $date) { + return null; + } + + return Carbon::createFromDate($date['year'], $date['month'], $date['date']); + } + } diff --git a/tests/Unit/NepaliDateConverterTest.php b/tests/Unit/NepaliDateConverterTest.php index e92e086..cf6bf18 100644 --- a/tests/Unit/NepaliDateConverterTest.php +++ b/tests/Unit/NepaliDateConverterTest.php @@ -25,3 +25,8 @@ $nepaliDate = Carbon::now()->addYears(100)->toBs(); expect($nepaliDate)->toBeNull(); }); + +test("can_get_ad_carbon_date_from_nepali_date", function () { + $date = \Proshore\NepaliDate\Facades\NepaliDateConverter::toAd(2080, 01, 06); + expect($date)->toBeInstanceOf(Carbon::class); +});