You are here

class PersianDateFactory in Persian Date for Drupal 8 8

Same name and namespace in other branches
  1. 8.4 src/Converter/PersianDateFactory.php \Drupal\persian_date\Converter\PersianDateFactory

This class is responsible to build Persian DateTime instances.

@package Drupal\persian_date\Convertor

Hierarchy

Expanded class hierarchy of PersianDateFactory

2 files declare their use of PersianDateFactory
PersianDateFormatter.php in src/Service/Formatter/PersianDateFormatter.php
PersianDrupalDateTime.php in src/Plugin/Datetime/PersianDrupalDateTime.php

File

src/Converter/PersianDateFactory.php, line 19

Namespace

Drupal\persian_date\Converter
View source
class PersianDateFactory {

  /**
   * Build PersianDateTime instance from given parameters.
   *
   * @param int $hour
   * @param int $minute
   * @param int $second
   * @param int $month
   * @param int $day
   * @param int $year
   * @param int $is_dst
   *
   * @return PersianDate
   */
  static function buildFromExactDate($hour = null, $minute = null, $second = null, $month = null, $day = null, $year = null, $is_dst = -1) {
    if (!$hour and !$minute and !$second and !$month and !$day and !$year) {
      $timestamp = mktime();
    }
    else {
      list($gy, $gm, $gd) = PersianDateConverter::jalali_to_gregorian($year, $month, $day);
      $timestamp = mktime($hour, $minute, $second, $gm, $gd, $gy);
    }
    $object = new PersianDate();
    $object
      ->setTimestamp($timestamp);
    return $object;
  }

  /**
   * Bulid PersianDateTime instance from original PHP DateTime object.
   *
   * @param \DateTime $dateTime
   * @return PersianDate
   */
  static function buildFromOriginalDateTime(\DateTime $dateTime) {
    $object = new PersianDate();
    $object
      ->setTimezone($dateTime
      ->getTimezone());
    $object
      ->setTimestamp($dateTime
      ->getTimestamp());
    return $object;
  }

  /**
   * @param integer $timestamp
   * @param \DateTimeZone $timezone
   * @return PersianDate
   */
  public static function buildFromTimestamp($timestamp, \DateTimeZone $timezone = null) {
    $dateTime = new PersianDate();
    $dateTime
      ->setTimestamp($timestamp);
    if ($timezone) {
      $dateTime
        ->setTimezone($timezone);
    }
    return $dateTime;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PersianDateFactory::buildFromExactDate static function Build PersianDateTime instance from given parameters.
PersianDateFactory::buildFromOriginalDateTime static function Bulid PersianDateTime instance from original PHP DateTime object.
PersianDateFactory::buildFromTimestamp public static function