You are here

private static function CalendarSystemsPoorMansJaliliCalendar::toGregorian in Calendar Systems 8.3

Converts Jalali date to gregorian date.

by Sallar Kaboli

1 call to CalendarSystemsPoorMansJaliliCalendar::toGregorian()
CalendarSystemsPoorMansJaliliCalendar::setDateLocale in src/CalendarSystems/CalendarSystemsPoorMansJaliliCalendar.php

File

src/CalendarSystems/CalendarSystemsPoorMansJaliliCalendar.php, line 569
Fallback calendar implementation in case php-intl is not avaiable.

Class

CalendarSystemsPoorMansJaliliCalendar
Jalali calendar for calendar_systems.

Namespace

Drupal\calendar_systems\CalendarSystems

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,
  ];
}