class PersianDrupalDateTime in Persian Date for Drupal 8 8
Same name and namespace in other branches
- 8.4 src/Plugin/Datetime/PersianDrupalDateTime.php \Drupal\persian_date\Plugin\Datetime\PersianDrupalDateTime
Hierarchy
- class \Drupal\Component\Datetime\DateTimePlus uses ToStringTrait
- class \Drupal\Core\Datetime\DrupalDateTime uses StringTranslationTrait
- class \Drupal\persian_date\Plugin\Datetime\PersianDrupalDateTime
- class \Drupal\Core\Datetime\DrupalDateTime uses StringTranslationTrait
Expanded class hierarchy of PersianDrupalDateTime
5 files declare their use of PersianDrupalDateTime
- DateTimeWidgetBase.php in src/
Plugin/ Field/ FieldWidget/ DateTimeWidgetBase.php - PersianDateHelper.php in src/
PersianDateHelper.php - PersianDateList.php in src/
Element/ PersianDateList.php - PersianDateTime.php in src/
Element/ PersianDateTime.php - PersianTimestampDateTimeDefaultWidget.php in src/
Plugin/ Field/ FieldWidget/ PersianTimestampDateTimeDefaultWidget.php
File
- src/
Plugin/ Datetime/ PersianDrupalDateTime.php, line 16
Namespace
Drupal\persian_date\Plugin\DatetimeView source
class PersianDrupalDateTime extends DrupalDateTime {
public function format($format, $settings = []) {
$dateTime = PersianDateFactory::buildFromOriginalDateTime($this->dateTimeObject);
return $dateTime
->format($format);
}
public static function createFromDrupalDateTime(DrupalDateTime $dateTime) {
$object = new self();
$object->dateTimeObject = $dateTime->dateTimeObject;
$object->langcode = $dateTime->langcode;
$object->formatTranslationCache = $dateTime->formatTranslationCache;
$object->stringTranslation = $dateTime->stringTranslation;
$object->errors = $dateTime->errors;
$object->inputFormatAdjusted = $dateTime->inputFormatAdjusted;
$object->inputFormatRaw = $dateTime->inputFormatRaw;
$object->inputTimeZoneRaw = $dateTime->inputTimeZoneRaw;
$object->inputTimeZoneAdjusted = $dateTime->inputTimeZoneAdjusted;
$object->inputTimeRaw = $dateTime->inputTimeRaw;
$object->inputTimeAdjusted = $dateTime->inputTimeAdjusted;
return $object;
}
public static function createFromFormat($format, $time, $timezone = NULL, $settings = []) {
if (!isset($settings['validate_format'])) {
$settings['validate_format'] = TRUE;
}
// Tries to create a date from the format and use it if possible.
// A regular try/catch won't work right here, if the value is
// invalid it doesn't return an exception.
$datetimeplus = new static('', $timezone, $settings);
// todo: fix to use format instead of hardcoded format
$date = false;
if ($time) {
list($dateString, $timeString) = explode(' ', $time);
list($year, $month, $day) = explode('-', $dateString);
list($hour, $minute, $second) = explode(':', $timeString);
$date = PersianDateFactory::buildFromExactDate($hour, $minute, $second, $month, $day, $year);
}
if (!$date instanceof \DateTime) {
throw new \InvalidArgumentException('The date cannot be created from a format.');
}
else {
// Functions that parse date is forgiving, it might create a date that
// is not exactly a match for the provided value, so test for that by
// re-creating the date/time formatted string and comparing it to the input. For
// instance, an input value of '11' using a format of Y (4 digits) gets
// created as '0011' instead of '2011'.
if ($date instanceof DateTimePlus) {
$test_time = $date
->format($format, $settings);
}
elseif ($date instanceof \DateTime) {
$test_time = $date
->format($format);
}
// fixme: 31 shahrivar
if ($settings['validate_format'] && $test_time != $time) {
throw new \UnexpectedValueException('The created date does not match the input value.');
}
$date = $date
->getOriginalDateTime();
$date
->setTimezone($datetimeplus
->getTimezone());
$datetimeplus
->setTimestamp($date
->getTimestamp());
$datetimeplus
->setTimezone($date
->getTimezone());
}
return $datetimeplus;
}
public function getDateTime() {
return $this->dateTimeObject;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateTimePlus:: |
protected static | property | An array of possible date parts. | |
DateTimePlus:: |
protected | property | The DateTime object. | |
DateTimePlus:: |
protected | property | An array of errors encountered when creating this date. | |
DateTimePlus:: |
protected | property | The prepared format, if provided. | |
DateTimePlus:: |
protected | property | The value of the format passed to the constructor. | |
DateTimePlus:: |
protected | property | The prepared time, without timezone, for this date. | |
DateTimePlus:: |
protected | property | The value of the time value passed to the constructor. | |
DateTimePlus:: |
protected | property | The prepared timezone object used to construct this date. | |
DateTimePlus:: |
protected | property | The value of the timezone passed to the constructor. | |
DateTimePlus:: |
protected | property | The value of the language code passed to the constructor. | |
DateTimePlus:: |
public static | function | Creates an ISO date from an array of values. | |
DateTimePlus:: |
public static | function | Checks that arrays of date parts will create a valid date. | |
DateTimePlus:: |
public | function | Examines getLastErrors() to see what errors to report. | |
DateTimePlus:: |
public static | function | Creates a date object from an array of date parts. | |
DateTimePlus:: |
public static | function | Creates a date object from an input date object. | |
DateTimePlus:: |
public static | function | Creates a date object from timestamp input. | |
DateTimePlus:: |
public static | function | Pads date parts with zeros. | |
DateTimePlus:: |
public | function | Returns the difference between two DateTimePlus objects. | |
DateTimePlus:: |
constant | |||
DateTimePlus:: |
public | function | Gets error messages. | |
DateTimePlus:: |
public | function | Gets a clone of the proxied PHP \DateTime object wrapped by this class. | |
DateTimePlus:: |
public | function | Detects if there were errors in the processing of this date. | |
DateTimePlus:: |
public static | function | Creates a complete array from a possibly incomplete array of date parts. | |
DateTimePlus:: |
protected | function | Prepares the input format value. | |
DateTimePlus:: |
protected | function | Prepares the input time value. | |
DateTimePlus:: |
public | function |
Renders the timezone name. Overrides ToStringTrait:: |
|
DateTimePlus:: |
constant | A RFC7231 Compliant date. | ||
DateTimePlus:: |
public | function | Sets the default time for an object built from date-only data. | |
DateTimePlus:: |
public | function | Implements the magic __call method. | |
DateTimePlus:: |
public static | function | Implements the magic __callStatic method. | |
DateTimePlus:: |
public | function | Implements the magic __clone method. | |
DrupalDateTime:: |
protected | property | Formatted strings translation cache. | |
DrupalDateTime:: |
protected | function |
Overrides prepareTimezone(). Overrides DateTimePlus:: |
|
DrupalDateTime:: |
public | function |
Constructs a date object. Overrides DateTimePlus:: |
|
PersianDrupalDateTime:: |
public static | function | ||
PersianDrupalDateTime:: |
public static | function |
Creates a date object from an input format. Overrides DateTimePlus:: |
|
PersianDrupalDateTime:: |
public | function |
Overrides format(). Overrides DrupalDateTime:: |
|
PersianDrupalDateTime:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ToStringTrait:: |
protected | function | For test purposes, wrap die() in an overridable method. | |
ToStringTrait:: |
public | function | Implements the magic __toString() method. |