Skip to content

Commit

Permalink
Merge pull request #149 from morilog/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
morilog authored Aug 7, 2022
2 parents 4543f35 + 6b96a60 commit 6051d96
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
composer.phar
composer.lock
.DS_Store
.idea
.idea
.phpunit.*
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
stopOnFailure="true">
<testsuites>
<testsuite name="Jalali Date Time test">
<directory suffix=".php">./tests/</directory>
Expand Down
126 changes: 55 additions & 71 deletions src/CalendarUtils.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Morilog\Jalali;

use Carbon\Carbon;
Expand All @@ -9,8 +10,34 @@
*/
class CalendarUtils
{
public const IRANIAN_MONTHS_NAME = ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'];
public const AFGHAN_MONTHS_NAME = ['حمل', 'ثور', 'جوزا', 'سرطان', 'اسد', 'سنبله', 'میزان', 'عقرب', 'قوس', 'جدی', 'دلو', 'حوت'];

private static $monthNames = self::IRANIAN_MONTHS_NAME;


private static $temp;

/**
* Set globally afghan months names as default month name
*
* @return void
*/
public static function useAfghanMonthsName()
{
self::$monthNames = self::AFGHAN_MONTHS_NAME;
}

/**
* Set globally iranian months names as default month name
*
* @return void
*/
public static function useIranianMonthsName()
{
self::$monthNames = self::IRANIAN_MONTHS_NAME;
}

/**
* Converts a Gregorian date to Jalali.
*
Expand Down Expand Up @@ -59,8 +86,8 @@ public static function toGregorianDate($jy, $jm, $jd)
$day = $georgianDateArr[2];
$georgianDate = new \DateTime();
$georgianDate->setDate($year, $month, $day);


return $georgianDate;
}

Expand All @@ -75,8 +102,8 @@ public static function toGregorianDate($jy, $jm, $jd)
public static function isValidateJalaliDate($jy, $jm, $jd)
{
return $jy >= -61 && $jy <= 3177
&& $jm >= 1 && $jm <= 12
&& $jd >= 1 && $jd <= self::jalaliMonthLength($jy, $jm);
&& $jm >= 1 && $jm <= 12
&& $jd >= 1 && $jd <= self::jalaliMonthLength($jy, $jm);
}

/**
Expand Down Expand Up @@ -141,8 +168,8 @@ public static function jalaliMonthLength($jy, $jm)
*/
public static function jalaliCal($jy)
{
$breaks = [-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210
, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
$breaks = [
-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
];

$breaksCount = count($breaks);
Expand Down Expand Up @@ -203,19 +230,19 @@ public static function jalaliCal($jy)
* @param $a
* @param $b
*/
public static function div($a, $b):int
public static function div($a, $b): int
{
return intdiv($a ,$b);
return intdiv($a, $b);
}

/**
* @param $a
* @param $b
* @return mixed
*/
public static function mod($a, $b):int
public static function mod($a, $b): int
{
return $a - intdiv($a , $b) * $b;
return $a - intdiv($a, $b) * $b;
}

/**
Expand Down Expand Up @@ -249,12 +276,10 @@ public static function d2g($jdn)
*/
public static function g2d($gy, $gm, $gd)
{
return (
self::div(($gy + self::div($gm - 8, 6) + 100100) * 1461, 4)
return (self::div(($gy + self::div($gm - 8, 6) + 100100) * 1461, 4)
+ self::div(153 * self::mod($gm + 9, 12) + 2, 5)
+ $gd - 34840408
) - self::div(self::div($gy + 100100 + self::div($gm - 8, 6), 100) * 3, 4) + 752;

}

/**
Expand Down Expand Up @@ -374,7 +399,6 @@ public static function date($format, $stamp = false, $timezone = null)
$values = array();

foreach ($keys as $k => $key) {

$v = '';
switch ($key) {
//Day
Expand Down Expand Up @@ -407,27 +431,27 @@ public static function date($format, $stamp = false, $timezone = null)
}
self::$temp['z'] = $v;
break;
//Week
//Week
case 'W':
$v = is_int(self::$temp['z'] / 7) ? (self::$temp['z'] / 7) : intval(self::$temp['z'] / 7 + 1);
break;
//Month
//Month
case 'F':
$v = self::getMonthNames($jMonth);
$v = self::getMonthName($jMonth);
break;
case 'm':
$v = sprintf("%02d", $jMonth);
break;
case 'M':
$v = self::getMonthNames($jMonth, true);
$v = self::getMonthName($jMonth, true);
break;
case 'n':
$v = $jMonth;
break;
case 't':
$v = ($jMonth == 12) ? (self::isLeapJalaliYear($jYear) ? 30 : 29) : ($jMonth > 6 ? 30 : 31);
break;
//Year
//Year
case 'L':
$tmpObj = static::createDateTime(time() - 31536000, $timezone);
$v = $tmpObj->format('L');
Expand All @@ -442,34 +466,34 @@ public static function date($format, $stamp = false, $timezone = null)
$v = '0' . $v;
}
break;
//Time
//Time
case 'a':
$v = ($dateTime->format('a') == 'am') ? 'ق.ظ' : 'ب.ظ';
break;
case 'A':
$v = ($dateTime->format('A') == 'AM') ? 'قبل از ظهر' : 'بعد از ظهر';
break;
//Full Dates
//Full Dates
case 'c':
$v = $jYear . '-' . sprintf("%02d", $jMonth) . '-' . sprintf("%02d", $jDay) . 'T';
$v .= $dateTime->format('H') . ':' . $dateTime->format('i') . ':' . $dateTime->format('s') . $dateTime->format('P');
break;
case 'r':
$v = self::getDayNames($dateTime->format('D'), true) . ', ' . sprintf("%02d",
$jDay) . ' ' . self::getMonthNames($jMonth, true);
$v = self::getDayNames($dateTime->format('D'), true) . ', ' . sprintf(
"%02d",
$jDay
) . ' ' . self::getMonthName($jMonth, true);
$v .= ' ' . $jYear . ' ' . $dateTime->format('H') . ':' . $dateTime->format('i') . ':' . $dateTime->format('s') . ' ' . $dateTime->format('P');
break;
//Timezone
//Timezone
case 'e':
$v = $dateTime->format('e');
break;
case 'T':
$v = $dateTime->format('T');
break;

}
$values[$k] = $v;

}
//End Changed Keys

Expand Down Expand Up @@ -627,49 +651,11 @@ private static function getDayNames($day, $shorten = false, $len = 1, $numeric =
return ($numeric) ? $n : (($shorten) ? mb_substr($ret, 0, $len, 'UTF-8') : $ret);
}

private static function getMonthNames($month, $shorten = false, $len = 3)
private static function getMonthName($month, $shorten = false, $len = 3)
{
$ret = '';
switch ($month) {
case '1':
$ret = 'فروردین';
break;
case '2':
$ret = 'اردیبهشت';
break;
case '3':
$ret = 'خرداد';
break;
case '4':
$ret = 'تیر';
break;
case '5':
$ret = 'مرداد';
break;
case '6':
$ret = 'شهریور';
break;
case '7':
$ret = 'مهر';
break;
case '8':
$ret = 'آبان';
break;
case '9':
$ret = 'آذر';
break;
case '10':
$ret = 'دی';
break;
case '11':
$ret = 'بهمن';
break;
case '12':
$ret = 'اسفند';
break;
}

return ($shorten) ? mb_substr($ret, 0, $len, 'UTF-8') : $ret;
$monthIndex = ((int)$month) -1 ;
$monthName = static::$monthNames[$monthIndex];
return ($shorten) ? mb_substr($monthName, 0, $len, 'UTF-8') : $monthName;
}

private static function filterArray($needle, $haystack, $always = array())
Expand All @@ -678,7 +664,6 @@ private static function filterArray($needle, $haystack, $always = array())
if (!in_array($v, $needle) && !in_array($v, $always)) {
unset($haystack[$k]);
}

}


Expand Down Expand Up @@ -797,7 +782,7 @@ public static function createDatetimeFromFormat($format, $str, $timezone = null)
public static function createCarbonFromFormat($format, $str, $timezone = null)
{
$dateTime = self::createDatetimeFromFormat($format, $str, $timezone);

return Carbon::createFromTimestamp($dateTime->getTimestamp(), $dateTime->getTimezone());
}

Expand Down Expand Up @@ -868,6 +853,5 @@ public static function createTimeZone($timezone = null)


throw new \InvalidArgumentException('timezone is not valid');

}
}
66 changes: 57 additions & 9 deletions tests/CalendarUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,60 @@ public function testIsLeapJalaliYear()

public function testStrftime()
{
$this->assertTrue(CalendarUtils::strftime('Y-m-d', strtotime('2016-05-8')) === '1395-02-19');
$this->assertTrue(CalendarUtils::convertNumbers(CalendarUtils::strftime('Y-m-d',
strtotime('2016-05-8'))) === '۱۳۹۵-۰۲-۱۹');

$dateString = CalendarUtils::convertNumbers('۱۳۹۵-۰۲-۱۹', true); // 1395-02-19
$this->assertTrue(CalendarUtils::createCarbonFromFormat('Y-m-d',
$dateString)->format('Y-m-d') === '2016-05-08');
$this->assertFalse(CalendarUtils::strftime('Y-m-d', strtotime('2016-05-8')) === '۱۳۹۵-۰۲-۱۹');
$table = [
[
'2016-05-08',
'Y-m-d',
'1395-02-19'
],
[
'2022-03-24',
'y-m-d',
'01-01-04'
],
[
'2023-03-24',
'y-m-D',
'02-01-ج'
],
];

foreach ($table as $row) {
list($dateTimeString, $format, $expected) = $row;
$timestamp = strtotime($dateTimeString);
$this->assertEquals($expected, CalendarUtils::strftime($format, $timestamp));
}
}

public function testFormatMonthName()
{
$months = range(1, 12);

// Should returns iranian months name as default
foreach ($months as $month) {
$date = sprintf('1401/%d/10', $month);
$actual = Jalalian::fromFormat('Y/n/d', $date)->format('F');
$expected = CalendarUtils::IRANIAN_MONTHS_NAME[$month - 1];
$this->assertEquals($expected, $actual);
}

// Should returns afghan months name when set
CalendarUtils::useAfghanMonthsName();
foreach ($months as $month) {
$date = sprintf('1401/%d/10', $month);
$actual = Jalalian::fromFormat('Y/n/d', $date)->format('F');
$expected = CalendarUtils::AFGHAN_MONTHS_NAME[$month - 1];
$this->assertEquals($expected, $actual);
}

// Should returns afghan months name when set
CalendarUtils::useIranianMonthsName();
foreach ($months as $month) {
$date = sprintf('1401/%d/10', $month);
$actual = Jalalian::fromFormat('Y/n/d', $date)->format('F');
$expected = CalendarUtils::IRANIAN_MONTHS_NAME[$month - 1];
$this->assertEquals($expected, $actual);
}
}

public function test_parseFromPersian()
Expand Down Expand Up @@ -87,6 +133,9 @@ public function testCreateCarbonFormFormat()
$this->assertFalse($jalaiDateFormatted === '1394-11-25 15:00:00');
$this->assertTrue($jalaiDateTimeFormatted === '1394-11-25 15:00:00');

// Test support years after 1416
$carbon = CalendarUtils::createCarbonFromFormat('Y/m/d', '1417/10/11');
$this->assertEquals('2039-01-01', $carbon->format('Y-m-d'));
}

public function testTimezone()
Expand All @@ -104,7 +153,6 @@ public function testTimezone()
$tzOffset = $this->getTimeZoneOffset('Asia/Tehran', 'UTC');

$this->assertTrue((((($utcHour * 60) + $utcMin) * 60) - ((($tehranHour * 60) + $tehranMin) * 60)) === $tzOffset);

}


Expand Down

0 comments on commit 6051d96

Please sign in to comment.