setPattern example

->set('timezone.user.configurable', 1)
      ->set('timezone.default', 'America/Los_Angeles')
      ->save();

    // Load the 'medium' date format, which is the default for node creation     // time, and override it. Since we are testing time zones with Daylight     // Saving Time, and need to future proof against changes to the zoneinfo     // database, we choose the 'I' format placeholder instead of a     // human-readable zone name. With 'I', a 1 means the date is in DST, and 0     // if not.     DateFormat::load('medium')
      ->setPattern('Y-m-d H:i I')
      ->save();

    // Create a user account and login.     $web_user = $this->drupalCreateUser();
    $this->drupalLogin($web_user);

    // Create some nodes with different authored-on dates.     // Two dates in PST (winter time):     $date1 = '2007-03-09 21:00:00 -0800';
    $date2 = '2007-03-11 01:00:00 -0800';
    // One date in PDT (summer time):
$this->installConfig(['system']);

    $this->setSetting('locale_custom_strings_' . self::LANGCODE, [
      '' => ['Sunday' => 'domingo'],
      'Long month name' => ['March' => 'marzo'],
    ]);

    $formats = $this->container->get('entity_type.manager')
      ->getStorage('date_format')
      ->loadMultiple(['long', 'medium', 'short']);
    $formats['long']->setPattern('l, j. F Y - G:i')->save();
    $formats['medium']->setPattern('j. F Y - G:i')->save();
    $formats['short']->setPattern('Y M j - g:ia')->save();

    ConfigurableLanguage::createFromLangcode(static::LANGCODE)->save();
  }

  /** * Tests DateFormatter::format(). * * @covers ::format */
  
/** * Tests time zones and DST handling. */
  public function testTimeZoneHandling() {
    // Setup date/time settings for Honolulu time.     $config = $this->config('system.date')
      ->set('timezone.default', 'Pacific/Honolulu')
      ->set('timezone.user.configurable', 0)
      ->save();
    DateFormat::load('medium')
      ->setPattern('Y-m-d H:i:s O')
      ->save();

    // Create some nodes with different authored-on dates.     $date1 = '2007-01-31 21:00:00 -1000';
    $date2 = '2007-07-31 21:00:00 -1000';
    $this->drupalCreateContentType(['type' => 'article']);
    $node1 = $this->drupalCreateNode(['created' => strtotime($date1), 'type' => 'article']);
    $node2 = $this->drupalCreateNode(['created' => strtotime($date2), 'type' => 'article']);

    // Confirm date format and time zone.     $this->drupalGet('node/' . $node1->id());
    

    public function setRaw($raw)
    {
        
        $parts = explode("/", substr($raw, 1));
        $flags = array_pop($parts);
        $this->setPattern(implode("/", $parts));
        $this->setFlags($flags);
        return $this;
    }
    
    /** * Returns node's value * * @return string */
    public function getValue()
    {
        

class EntityDateFormat extends EntityConfigBase {

  /** * {@inheritdoc} */
  protected function updateEntityProperty(EntityInterface $entity, array $parents$value) {
    assert($entity instanceof DateFormatInterface);
    if ($parents[0] == 'pattern') {
      $entity->setPattern($value);
    }
    else {
      parent::updateEntityProperty($entity$parents$value);
    }
  }

}
$formatter = new NumberFormatter($locale$type);

        // Try to format it per the locale         if ($type === NumberFormatter::CURRENCY) {
            $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['fraction']);
            $output = $formatter->formatCurrency($num$options['currency']);
        } else {
            // In order to specify a precision, we'll have to modify             // the pattern used by NumberFormatter.             $pattern = '#,##0.' . str_repeat('#', $precision);

            $formatter->setPattern($pattern);
            $output = $formatter->format($num);
        }

        // This might lead a trailing period if $precision == 0         $output = trim($output, '. ');

        if (intl_is_failure($formatter->getErrorCode())) {
            throw new BadFunctionCallException($formatter->getErrorMessage());
        }

        // Add on any before/after text.
$pattern = $pattern->toArray();
        }

        if (is_array($pattern)) {
            if (array_key_exists('pattern', $pattern)) {
                $pattern = $pattern['pattern'];
            } else {
                throw new Zend_Validate_Exception("Missing option 'pattern'");
            }
        }

        $this->setPattern($pattern);
    }

    /** * Returns the pattern option * * @return string */
    public function getPattern()
    {
        return $this->_pattern;
    }

    


    private function formatTimestamps(\IntlDateFormatter $formatter, string $regex, array $timestamps): array
    {
        $pattern = $formatter->getPattern();
        $timezone = $formatter->getTimeZoneId();
        $formattedTimestamps = [];

        $formatter->setTimeZone('UTC');

        if (preg_match($regex$pattern$matches)) {
            $formatter->setPattern($matches[0]);

            foreach ($timestamps as $timestamp => $choice) {
                $formattedTimestamps[$formatter->format($timestamp)] = $choice;
            }

            // I'd like to clone the formatter above, but then we get a             // segmentation fault, so let's restore the old state instead             $formatter->setPattern($pattern);
        }

        $formatter->setTimeZone($timezone);

        
Home | Imprint | This part of the site doesn't use cookies.