You are here

public static function DatexJalali::parser in Datex 7.2

A parser for Jalali date strings.

@TODO implement.

File

datex_api/DatexJalali.inc, line 244
Jalali calendar for datex.

Class

DatexJalali
Jalali calendar for datex.

Code

public static function parser($date, $format) {
  if (!(is_string($date) && is_string($format))) {
    throw new Exception('Datex parser: date or format not valid.');
  }
  $granularities = array();
  $j = $i = 0;
  $format_len = strlen($format);
  do {
    while (!ctype_alpha($format[$i]) && $i < $format_len) {
      $i++;
    }
    while (!ctype_digit($date[$j])) {
      $j++;
    }
    switch ($format[$i]) {
      case 'd':
        $granularities['day'] = $date[$j] . $date[++$j];
        break;
      case 'j':
        if (ctype_digit($date[$j + 1])) {
          $granularities['day'] = $date[$j] . $date[++$j];
        }
        else {
          $granularities['day'] = $date[$j];
        }
        break;
      case 'm':
        $granularities['month'] = $date[$j] . $date[++$j];
        break;
      case 'n':
        if (ctype_digit($date[$j + 1])) {
          $granularities['month'] = $date[$j] . $date[++$j];
        }
        else {
          $granularities['month'] = $date[$j];
        }
        break;
      case 'y':
      case 'Y':
        $granularities['year'] = $date[$j] . $date[++$j] . $date[++$j] . $date[++$j];
      case 'h':
        $granularities['hour'] = $date[$j] . $date[++$j];
        break;
      case 'g':
        if (ctype_digit($date[$j + 1])) {
          $granularities['hour'] = $date[$j] . $date[++$j];
        }
        else {
          $granularities['hour'] = $date[$j];
        }
        break;
      case 'H':
        $granularities['hour'] = $date[$j] . $date[++$j];
        break;
      case 'G':
        if (ctype_digit($date[$j + 1])) {
          $granularities['hour'] = $date[$j] . $date[++$j];
        }
        else {
          $granularities['hour'] = $date[$j];
        }
        break;
      case 'i':
        $granularities['minute'] = $date[$j] . $date[++$j];
        break;
      case 's':
        $granularities['second'] = $date[$j] . $date[++$j];
        break;
    }
    $j++;
  } while (++$i < $format_len);
  return $granularities;
}