You are here

private static function DatexPoorMansJaliliCalendar::toGregorian in Datex 8

Converts Jalali date to gregorian date.

by Sallar Kaboli

1 call to DatexPoorMansJaliliCalendar::toGregorian()
DatexPoorMansJaliliCalendar::setDateLocale in src/Datex/DatexPoorMansJaliliCalendar.php

File

src/Datex/DatexPoorMansJaliliCalendar.php, line 370
Fallback calendar implementation in case php-intl is not avaiable.

Class

DatexPoorMansJaliliCalendar
Jalali calendar for datex.

Namespace

Drupal\datex\Datex

Code

private static function toGregorian($j_y, $j_m, $j_d) {
  $g_days_in_month = [
    31,
    28,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31,
  ];
  $j_days_in_month = [
    31,
    31,
    31,
    31,
    31,
    31,
    30,
    30,
    30,
    30,
    30,
    29,
  ];
  $jy = $j_y - 979;
  $jm = $j_m - 1;
  $jd = $j_d - 1;
  $j_day_no = 365 * $jy + self::div($jy, 33) * 8 + self::div($jy % 33 + 3, 4);
  for ($i = 0; $i < $jm; ++$i) {
    $j_day_no += $j_days_in_month[$i];
  }
  $j_day_no += $jd;
  $g_day_no = $j_day_no + 79;
  $gy = 1600 + 400 * self::div($g_day_no, 146097);
  $g_day_no = $g_day_no % 146097;
  $leap = TRUE;
  if ($g_day_no >= 36525) {
    $g_day_no--;
    $gy += 100 * self::div($g_day_no, 36524);
    $g_day_no = $g_day_no % 36524;
    if ($g_day_no >= 365) {
      $g_day_no++;
    }
    else {
      $leap = FALSE;
    }
  }
  $gy += 4 * self::div($g_day_no, 1461);
  $g_day_no %= 1461;
  if ($g_day_no >= 366) {
    $leap = FALSE;
    $g_day_no--;
    $gy += self::div($g_day_no, 365);
    $g_day_no = $g_day_no % 365;
  }
  for ($i = 0; $g_day_no >= $g_days_in_month[$i] + ($i == 1 && $leap); $i++) {
    $g_day_no -= $g_days_in_month[$i] + ($i == 1 && $leap);
  }
  $gm = $i + 1;
  $gd = $g_day_no + 1;
  return [
    $gy,
    $gm,
    $gd,
  ];
}