setTimeZone example

EOT;

        $this->assertSameData(['date' => 1039829025.67]$this->parser->parse($yaml));
    }

    public function testParseDateAsMappingValue()
    {
        $yaml = <<<'EOT' date: 2002-12-14 EOT;
        $expectedDate = (new \DateTimeImmutable())
            ->setTimeZone(new \DateTimeZone('UTC'))
            ->setDate(2002, 12, 14)
            ->setTime(0, 0, 0);

        $this->assertSameData(['date' => $expectedDate]$this->parser->parse($yaml, Yaml::PARSE_DATETIME));
    }

    /** * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider */
    public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber$yaml)
    {
        
return $form;
  }

  /** * {@inheritdoc} */
  public function settingsSummary() {
    $summary = parent::settingsSummary();

    $date = new DrupalDateTime();
    $this->setTimeZone($date);
    $summary[] = $date->format($this->getSetting('date_format')$this->getFormatSettings());

    return $summary;
  }

}
namespace Kint\Renderer\Rich;

use DateTime;
use DateTimeZone;
use Kint\Zval\Representation\Representation;

class TimestampPlugin extends AbstractPlugin implements TabPluginInterface
{
    public function renderTab(Representation $r): ?string
    {
        if ($dt = DateTime::createFromFormat('U', (string) $r->contents)) {
            return '<pre>'.$dt->setTimeZone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s T').'</pre>';
        }

        return null;
    }
}

  protected function setTimeZone(DrupalDateTime $date) {
    if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
      // A date without time has no timezone conversion.       $timezone = DateTimeItemInterface::STORAGE_TIMEZONE;
    }
    else {
      $timezone = date_default_timezone_get();
    }
    $date->setTimeZone(timezone_open($timezone));
  }

  /** * Gets a settings array suitable for DrupalDateTime::format(). * * @return array * The settings array that can be passed to DrupalDateTime::format(). */
  protected function getFormatSettings() {
    $settings = [];

    
foreach ($values as &$item) {
      if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {
        /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
        $start_date = $item['value'];

        if ($datetime_type === DateRangeItem::DATETIME_TYPE_ALLDAY) {
          // All day fields start at midnight on the starting date, but are           // stored like datetime fields, so we need to adjust the time.           // This function is called twice, so to prevent a double conversion           // we need to explicitly set the timezone.           $start_date->setTimeZone($user_timezone)->setTime(0, 0, 0);
        }

        // Adjust the date for storage.         $item['value'] = $start_date->setTimezone($storage_timezone)->format($storage_format);
      }

      if (!empty($item['end_value']) && $item['end_value'] instanceof DrupalDateTime) {
        /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
        $end_date = $item['end_value'];

        if ($datetime_type === DateRangeItem::DATETIME_TYPE_ALLDAY) {
          

        $expectedDate = (new \DateTimeImmutable($yamlnew \DateTimeZone('UTC')))->format('U');
        $this->assertSame($microsecond ? (float) "$expectedDate.$microsecond" : (int) $expectedDate, Inline::parse($yaml));
    }

    /** * @dataProvider getTimestampTests */
    public function testParseTimestampAsDateTimeObject(string $yaml, int $year, int $month, int $day, int $hour, int $minute, int $second, int $microsecond, string $timezone)
    {
        $expected = (new \DateTimeImmutable($yaml))
            ->setTimeZone(new \DateTimeZone('UTC'))
            ->setDate($year$month$day)
            ->setTime($hour$minute$second$microsecond);

        $date = Inline::parse($yaml, Yaml::PARSE_DATETIME);
        $this->assertEquals($expected$date);
        $this->assertSame($timezone$date->format('O'));
    }

    public static function getTimestampTests(): array
    {
        return [
            
public function getBlockPrefix(): string
    {
        return 'date';
    }

    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);
        }
Home | Imprint | This part of the site doesn't use cookies.