From a9020f1445cd9b1dcf90e9d3d3f895516669009f Mon Sep 17 00:00:00 2001 From: ShahrokhNiakan Date: Fri, 8 Dec 2017 13:14:02 +0330 Subject: [PATCH] Fix issue #28 Fixed problem of one day diffrent when last year is leap year --- src/CalendarSettings/Hijri.php | 8 +++++++- src/CalendarSettings/Jalali.php | 15 +++++++++------ tests/ConvertTest.php | 8 ++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/CalendarSettings/Hijri.php b/src/CalendarSettings/Hijri.php index 52b7f92..8531f6a 100644 --- a/src/CalendarSettings/Hijri.php +++ b/src/CalendarSettings/Hijri.php @@ -97,6 +97,8 @@ */ 'convert_from' => function ($date_time) { + $config = include 'Gregorian.php'; + $year = $date_time->format('Y'); $month = $date_time->format('m'); @@ -114,6 +116,10 @@ $year = $result[2]; + if (! $day || $config['leap_year']($year - 1) ) { + $day++; + } + $date_time->setDate($year, $month, $day); return $date_time; @@ -177,7 +183,7 @@ *\_________________________________________________________/ */ 'days_of_week' => array ( - + 'al-Aḥad', 'al-Ithnayn', 'ath-Thulatha\'', diff --git a/src/CalendarSettings/Jalali.php b/src/CalendarSettings/Jalali.php index 8f66ab4..4fffa6e 100644 --- a/src/CalendarSettings/Jalali.php +++ b/src/CalendarSettings/Jalali.php @@ -161,11 +161,11 @@ $gregorian_month -= $value; } - $gregorian_day = $gregorian_month; + $gregorian_day = $gregorian_month; - $gregorian_month = $month; + $gregorian_month = $month; - if ( ( ( $gregorian_year % 4 ) == 0 ) && ( ( ( $gregorian_year % 100 ) != 0 ) || ( ( $gregorian_year % 400 ) == 0 ) ) ) { + if ( ( ( $gregorian_year % 4 ) == 0 ) && ( ( ( $gregorian_year % 100 ) != 0 ) || ( ( $gregorian_year % 400 ) == 0 ) ) ) { if ( $gregorian_month < 3 || ( $gregorian_month == 3 && $gregorian_day < 22 ) ) { @@ -173,12 +173,15 @@ } - } + } - $date_time->setDate($gregorian_year, $gregorian_month, $gregorian_day); + if (! $gregorian_day || $config['leap_year']($gregorian_year - 1) ) { + $gregorian_day++; + } + $date_time->setDate($gregorian_year, $gregorian_month, $gregorian_day); - return $date_time; + return $date_time; }, diff --git a/tests/ConvertTest.php b/tests/ConvertTest.php index 598d1a0..7cd08f6 100644 --- a/tests/ConvertTest.php +++ b/tests/ConvertTest.php @@ -36,6 +36,10 @@ public function testJalaliToGregorian() $this->assertEquals('2016-01-25 00:00:00', $date); + $date = Datium::create(1396, 9, 11)->from('jalali')->get(); + + $this->assertEquals('2017-12-02 00:00:00', $date); + } public function testHijriToGregorian() @@ -45,6 +49,10 @@ public function testHijriToGregorian() $this->assertEquals('2016-06-13 00:00:00', $date); + $date = Datium::create(1439, 03, 12)->from('hijri')->get(); + + $this->assertEquals('2017-12-02 00:00:00', $date); + } public function testJalaliToHijri()