/** * Test that DrupalDateTime can detect the right timezone to use when * constructed from a datetime object. */ public function testDateTimezoneWithDateTimeObject() { // Create a date object with another date object. $input = new \DateTime('now', new \DateTimeZone('Pacific/Midway')); $timezone = NULL; $expected_timezone = 'Pacific/Midway'; $message = 'DateTimePlus uses the specified timezone if provided.'; $date = DateTimePlus::createFromDateTime($input, $timezone); $timezone = $date->getTimezone()->getName(); $this->assertEquals($timezone, $expected_timezone, $message); }
/** * {@inheritdoc} */ public function render() { if (empty($this->view->rowPlugin)) { debug('Drupal\\views\\Plugin\\views\\style\\VerticalTimeline: Missing row plugin'); return; } // If anything needs to be different in preview, do it here. if (!empty($this->view->live_preview)) { //return; } $rows = array(); $options = $this->options; $field = $options['date_field']; $prev_group = ''; foreach ($this->view->result as $row_index => $row) { $this->view->row_index = $row_index; $row = $this->view->rowPlugin->render($row); $date = ''; if (isset($this->view->field[$field])) { // Create the group header, when required, and insert it into the rows array. $node = $row['#row']->_entity; $raw = $node->get($options['date_field'])->value; // Massage the date into the format required by the header. switch ($options['group_heading']) { case 'century': $obj = is_numeric($raw) ? DateTimePlus::createFromTimestamp($raw) : new DateTimePlus($raw); $date = substr($obj->format('Y'), 0, 2) . '00'; break; case 'format': $obj = is_numeric($raw) ? DateTimePlus::createFromTimestamp($raw) : new DateTimePlus($raw); $date = $obj->format($options['group_heading_format']); break; case 'date': $date = $style->getField($id, $field); $date = strip_tags(htmlspecialchars_decode($date)); //$date = \Drupal::service('renderer')->render($date); break; default: $date = NULL; break; } // See if this is a new header, different than the previous one. $group = $date; if ($group != $prev_group) { $row['group'] = ['#type' => 'markup', '#markup' => $date]; } $prev_group = $group; } $rows[] = $row; } $build = array('#theme' => $this->themeFunctions(), '#view' => $this->view, '#options' => $this->options, '#rows' => $rows); unset($this->view->row_index); return $build; }
/** * Overrides format(). * * @param string $format * A format string using either PHP's date(). * @param array $settings * - timezone: (optional) String timezone name. Defaults to the timezone * of the date object. * - langcode: (optional) String two letter language code used to control * the result of the format() method. Defaults to NULL. * * @return string * The formatted value of the date. */ public function format($format, $settings = array()) { $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; $value = ''; // Format the date and catch errors. try { // Encode markers that should be translated. 'A' becomes // '\xEF\AA\xFF'. xEF and xFF are invalid UTF-8 sequences, // and we assume they are not in the input string. // Paired backslashes are isolated to prevent errors in // read-ahead evaluation. The read-ahead expression ensures that // A matches, but not \A. $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("ï\\\\\\\\ÿ", "ï\\\\\$1\$1ÿ"), $format); // Call date_format(). $format = parent::format($format, $settings); // Translates a formatted date string. $translation_callback = function ($matches) use($langcode) { $code = $matches[1]; $string = $matches[2]; if (!isset($this->formatTranslationCache[$langcode][$code][$string])) { $options = array('langcode' => $langcode); if ($code == 'F') { $options['context'] = 'Long month name'; } if ($code == '') { $this->formatTranslationCache[$langcode][$code][$string] = $string; } else { $this->formatTranslationCache[$langcode][$code][$string] = $this->t($string, array(), $options); } } return $this->formatTranslationCache[$langcode][$code][$string]; }; // Translate the marked sequences. $value = preg_replace_callback('/\\xEF([AaeDlMTF]?)(.*?)\\xFF/', $translation_callback, $format); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); } return $value; }
/** * Overrides format(). * * @param string $format * A format string using either PHP's date(). * @param array $settings * - timezone: (optional) String timezone name. Defaults to the timezone * of the date object. * - langcode: (optional) String two letter language code used to control * the result of the format() method. Defaults to NULL. * * @return string * The formatted value of the date. */ public function format($format, $settings = array()) { $settings['langcode'] = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; // Format the date and catch errors. try { // Encode markers that should be translated. 'A' becomes // '\xEF\AA\xFF'. xEF and xFF are invalid UTF-8 sequences, // and we assume they are not in the input string. // Paired backslashes are isolated to prevent errors in // read-ahead evaluation. The read-ahead expression ensures that // A matches, but not \A. $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("ï\\\\\\\\ÿ", "ï\\\\\$1\$1ÿ"), $format); // Call date_format(). $format = parent::format($format); // Pass the langcode to _format_date_callback(). _format_date_callback(NULL, $settings['langcode']); // Translate the marked sequences. $value = preg_replace_callback('/\\xEF([AaeDlMTF]?)(.*?)\\xFF/', '_format_date_callback', $format); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); } return $value; }
/** * Provides data for date tests. * * @return array * An array of arrays, each containing the input parameters for * DateTimePlusTest::testInvalidDateDiff(). * * @see DateTimePlusTest::testInvalidDateDiff() */ public function providerTestInvalidDateDiff() { return array(array('input1' => DateTimePlus::createFromFormat('U', 3600), 'input2' => '1970-01-01 00:00:00', 'absolute' => FALSE), array('input1' => DateTimePlus::createFromFormat('U', 3600), 'input2' => NULL, 'absolute' => FALSE)); }