Skip to content

Commit

Permalink
feat: now months name could be configured globally
Browse files Browse the repository at this point in the history
Iranian months name used as default. To switch between iranian and aghan
month names you can use `CalendarUtils::useAfghanMonthsName()` and
`CalendarUtils::useIranianMonthsName()`
  • Loading branch information
morilog committed Aug 7, 2022
1 parent 1b0d8f0 commit 6b96a60
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 71 deletions.
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');

}
}
31 changes: 31 additions & 0 deletions tests/CalendarUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ public function testStrftime()
}
}

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()
{
$jalaliDate = '1393/03/27';
Expand Down

0 comments on commit 6b96a60

Please sign in to comment.