/** * Constructor. * * @param string $date String in a format accepted by strtotime(), defaults to "now". * @param mixed $tz Time zone to be used for the date. Might be a string or a DateTimeZone object. */ public function __construct($date = 'now', $tz = null) { // Create the base GMT and server time zone objects. if (empty(self::$gmt) || empty(self::$stz)) { self::$gmt = new \DateTimeZone('GMT'); self::$stz = new \DateTimeZone(@date_default_timezone_get()); } // If the time zone object is not set, attempt to build it. if (!$tz instanceof \DateTimeZone) { if ($tz === null) { $tz = self::$gmt; } elseif (is_string($tz)) { $tz = new \DateTimeZone($tz); } } // If the date is numeric assume a unix timestamp and convert it. date_default_timezone_set('UTC'); $date = is_numeric($date) ? date('c', $date) : $date; // Call the DateTime constructor. parent::__construct($date, $tz); // Reset the timezone for 3rd party libraries/extension that does not use JDate date_default_timezone_set(self::$stz->getName()); // Set the timezone object for access later. $this->tz = $tz; }
public function setTimezone($timezone) { $this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); $this->TZID = $this->timezone->getName(); list($standardTransition, $daylightTransition) = $transitions = $this->_getTransitionsForTimezoneAndYear($this->timezone, date('Y')); $dtstart = new Sabre_VObject_Property_DateTime('DTSTART'); $dtstart->setDateTime(new DateTime(), Sabre_VObject_Element_DateTime::LOCAL); if ($daylightTransition !== null) { $offsetTo = ($daylightTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($daylightTransition['offset'])); $offsetFrom = ($standardTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($standardTransition['offset'])); $daylight = new Sabre_VObject_Component('DAYLIGHT'); $daylight->TZOFFSETFROM = $offsetFrom; $daylight->TZOFFSETTO = $offsetTo; $daylight->TZNAME = $daylightTransition['abbr']; $daylight->DTSTART = $dtstart; #$daylight->RRULE = 'FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3'; $this->add($daylight); } if ($standardTransition !== null) { $offsetTo = ($standardTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($standardTransition['offset'])); if ($daylightTransition !== null) { $offsetFrom = ($daylightTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($daylightTransition['offset'])); } else { $offsetFrom = $offsetTo; } $standard = new Sabre_VObject_Component('STANDARD'); $standard->TZOFFSETFROM = $offsetFrom; $standard->TZOFFSETTO = $offsetTo; $standard->TZNAME = $standardTransition['abbr']; $standard->DTSTART = $dtstart; #$standard->RRULE = 'FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10'; $this->add($standard); } }
/** * Get a user-friendly timezone name * @param \DateTimeZone $timezone The timezone * @return string */ private function getPrettyTimezoneName(\DateTimeZone $timezone) { $offset = $timezone->getOffset(new \DateTime('now')); if ($offset == 0) { $formattedOffset = 'GMT'; } else { $sign = $offset > 0 ? '+' : '-'; $formattedOffset = 'GMT' . $sign . date('H:i', mktime(0, abs($offset) / 60)); } if (substr($timezone->getName(), 0, 7) === 'Etc/GMT') { // Just return the GMT+02:00 text for hardcoded timezone offsets return $formattedOffset; } else { return "({$formattedOffset}) " . $timezone->getName(); } }
/** * * @param string $pValue * @param \DateTimeZone $pDateTimeZone * @return \DateTime */ public function fromString($pValue, \DateTimeZone $pDateTimeZone) { $lDateTime = new \DateTime($pValue, $pDateTimeZone); if ($lDateTime->getTimezone()->getName() !== $pDateTimeZone->getName()) { $lDateTime->setTimezone($pDateTimeZone); } return $lDateTime; }
function formatted_time($datetime_str = 'now', $timestamp_format = NULL, $timezone = NULL) { $tz = new DateTimeZone($timezone ? $timezone : date_default_timezone_get()); $time = new DateTime($datetime_str, $tz); if ($time->getTimeZone()->getName() !== $tz->getName()) { $time->setTimeZone($tz); } return $time->format($timestamp_format); }
/** * Constructor. * * @param Carbon|null $after * @param Carbon|null $before * @param null|string $name * * @throws TimezoneException|DateRangeException */ public function __construct(Carbon $after = null, Carbon $before = null, $name = null) { $this->name = $name; $this->after = $after; $this->before = $before; if (is_null($after) && is_null($before)) { throw new DateRangeException('Either an after date or before date must be provided.'); } elseif (is_null($after) && !is_null($before)) { $this->timezone = $before->getTimezone(); } elseif (is_null($before)) { $this->timezone = $after->getTimezone(); } else { $this->timezone = $this->after->getTimezone(); $before_tz = is_null($this->before->getTimezone()) ? null : $this->before->getTimezone(); if ($before_tz->getName() !== $this->timezone->getName()) { throw new TimezoneException('Multiple timezones are not supported.'); } } }
/** * Test Cases for __construct * * @return array * * @since 11.3 */ public function cases__construct() { $cases = array('basic' => array('12/23/2008 13:45', null, 'Tue 12/23/2008 13:45'), 'tzCT' => array('12/23/2008 13:45', 'US/Central', 'Tue 12/23/2008 13:45'), 'DateTime tzCT' => array('12/23/2008 13:45', new DateTimeZone('US/Central'), 'Tue 12/23/2008 13:45')); // Backup the default timezone before continuing - Using the system timezone apparently causes test failures $timezone = new DateTimeZone(date_default_timezone_get()); date_default_timezone_set('UTC'); $cases['unix'] = array(strtotime('12/26/2008 13:45'), null, 'Fri 12/26/2008 13:45'); // Restore the timezone date_default_timezone_set($timezone->getName()); return $cases; }
/** * Verifies that the given timezone exists and sets the timezone to the selected timezone * * @TODO Verify that the given timezone exists * * @param string $timezoneName * @return string */ public function setTimezone($timeZoneName = 'UTC') { if (!$this->isValidTimeZone($timeZoneName)) { $timeZoneName = 'UTC'; } $this->timezone = new \DateTimeZone($timeZoneName); $this->timezoneId = $this->timezone->getName(); $transitions = $this->timezone->getTransitions(); $this->timezoneInDST = $transitions[0]['isdst']; return $this->timezoneId; }
/** * @return array */ public function toRaw() { $fromDate = clone $this->fromDate; $fromDate->setTimezone($this->timezone); $toDate = clone $this->toDate; $toDate->setTimezone($this->timezone); $requestParams = ['tzid' => $this->timezone->getName(), 'from' => $fromDate->format('Y-m-d'), 'to' => $toDate->format('Y-m-d')]; if ($this->calendars !== null) { $requestParams['calendar_ids'] = $this->calendars; } return $requestParams; }
/** * Creates a new component. * * By default this object will iterate over its own children, but this can * be overridden with the iterator argument * * @param string $name * @param Sabre\VObject\ElementList $iterator */ public function __construct() { parent::__construct(); $tz = new \DateTimeZone(\GO::user() ? \GO::user()->timezone : date_default_timezone_get()); //$tz = new \DateTimeZone("Europe/Amsterdam"); $transitions = $tz->getTransitions(); $start_of_year = mktime(0, 0, 0, 1, 1); $to = \GO\Base\Util\Date::get_timezone_offset(time()); if ($to < 0) { if (strlen($to) == 2) { $to = '-0' . $to * -1; } } else { if (strlen($to) == 1) { $to = '0' . $to; } $to = '+' . $to; } $STANDARD_TZOFFSETFROM = $STANDARD_TZOFFSETTO = $DAYLIGHT_TZOFFSETFROM = $DAYLIGHT_TZOFFSETTO = $to; $STANDARD_RRULE = ''; $DAYLIGHT_RRULE = ''; for ($i = 0, $max = count($transitions); $i < $max; $i++) { if ($transitions[$i]['ts'] > $start_of_year) { $weekday1 = $this->_getDay($transitions[$i]['time']); $weekday2 = $this->_getDay($transitions[$i + 1]['time']); if ($transitions[$i]['isdst']) { $dst_start = $transitions[$i]; $dst_end = $transitions[$i + 1]; } else { $dst_end = $transitions[$i]; $dst_start = $transitions[$i + 1]; } $STANDARD_TZOFFSETFROM = $this->_formatVtimezoneTransitionHour($dst_start['offset'] / 3600); $STANDARD_TZOFFSETTO = $this->_formatVtimezoneTransitionHour($dst_end['offset'] / 3600); $DAYLIGHT_TZOFFSETFROM = $this->_formatVtimezoneTransitionHour($dst_end['offset'] / 3600); $DAYLIGHT_TZOFFSETTO = $this->_formatVtimezoneTransitionHour($dst_start['offset'] / 3600); $DAYLIGHT_RRULE = "FREQ=YEARLY;BYDAY={$weekday1};BYMONTH=" . date('n', $dst_start['ts']); $STANDARD_RRULE = "FREQ=YEARLY;BYDAY={$weekday2};BYMONTH=" . date('n', $dst_end['ts']); break; } } $this->tzid = $tz->getName(); // $this->add("last-modified", "19870101T000000Z"); $rrule = new \Sabre\VObject\Recur\RRuleIterator($STANDARD_RRULE, new \DateTime('1970-01-01 ' . substr($STANDARD_TZOFFSETFROM, 1) . ':00')); $rrule->next(); $rrule->next(); $this->add($this->createComponent("standard", array('dtstart' => $rrule->current()->format('Ymd\\THis'), 'rrule' => $STANDARD_RRULE, 'tzoffsetfrom' => $STANDARD_TZOFFSETFROM . "00", 'tzoffsetto' => $STANDARD_TZOFFSETTO . "00"))); $rrule = new \Sabre\VObject\Recur\RRuleIterator($DAYLIGHT_RRULE, new \DateTime('1970-01-01 ' . substr($DAYLIGHT_TZOFFSETFROM, 1) . ':00')); $rrule->next(); $rrule->next(); $this->add($this->createComponent("daylight", array('dtstart' => $rrule->current()->format('Ymd\\THis'), 'rrule' => $DAYLIGHT_RRULE, 'tzoffsetfrom' => $DAYLIGHT_TZOFFSETFROM . "00", 'tzoffsetto' => $DAYLIGHT_TZOFFSETTO . "00"))); }
/** * Run the application * * @param Library\CommandContext $context A command context object */ protected function _actionRun(Library\CommandContext $context) { //Set the site error reporting $this->getEventDispatcher()->setDebugMode($this->getCfg('debug_mode')); //Set the paths $params = $this->getObject('application.extensions')->files->params; define('JPATH_FILES', JPATH_SITES . '/' . $this->getSite() . '/files'); define('JPATH_CACHE', $this->getCfg('cache_path', JPATH_ROOT . '/cache')); // Set timezone to user's setting, falling back to global configuration. $timezone = new \DateTimeZone($context->user->get('timezone', $this->getCfg('timezone'))); date_default_timezone_set($timezone->getName()); //Route the request $this->route(); }
protected function adjustTimeZone() { $item_list = $this->getItemList('integer'); $date = new SwatDate(); $photo_tz = new DateTimeZone($this->ui->getWidget('photo_time_zone')->value); $camera_tz = new DateTimeZone($this->ui->getWidget('camera_time_zone')->value); $photo_offset = $photo_tz->getOffset($date); $camera_offset = $camera_tz->getOffset($date); $offset_s = $photo_offset - $camera_offset; $instance_id = $this->app->getInstanceId(); $sql = sprintf('update PinholePhoto set photo_time_zone = %s, photo_date = convertToUTC(convertTZ(photo_date, photo_time_zone) + interval %s, %s) where PinholePhoto.image_set in ( select id from ImageSet where instance %s %s)', $this->app->db->quote($photo_tz->getName(), 'text'), $this->app->db->quote($offset_s . ' seconds', 'text'), $this->app->db->quote($photo_tz->getName(), 'text'), SwatDB::equalityOperator($instance_id), $this->app->db->quote($instance_id, 'integer')); // note the only page with an extended-selection that accesses this // is the pending photos page - so enforce status here. if ($this->extended_selected) { $sql .= sprintf(' and PinholePhoto.status = %s', $this->app->db->quote(PinholePhoto::STATUS_PENDING, 'integer')); } else { $sql .= sprintf(' and PinholePhoto.id in (%s)', $item_list); } return SwatDB::exec($this->app->db, $sql); }
/** * @Route("/dzisiaj", name="dzisiaj", options={"expose"=true}) * @Template("LogopediaBundle:Spotkanie:dzisiaj.html.twig") */ public function dzisiajAction() { $time_zone = new \DateTimeZone('UTC'); $time_zone->getName(); // definiujemy dzisiejszy dzień $poczatek = new \DateTime(); $poczatek->setTime(00, 00, 00); $koniec = new \DateTime(); $koniec->setTime(23, 59, 59); $em = $this->getDoctrine()->getManager(); $query = $em->createQuery('SELECT s, p FROM LogopediaBundle:Spotkanie s JOIN s.idPacjenta p WHERE s.start >= :poczatek AND s.end <= :koniec ORDER BY s.start ASC')->setParameters(array('poczatek' => $poczatek, 'koniec' => $koniec)); $dzisiaj = $query->getResult(); return array('dzisiaj' => $dzisiaj); }
public function testMarried() { $currentTimeZone = new DateTimeZone(date_default_timezone_get()); $now = new DateTime('2012-02-01', $currentTimeZone); $zone = $now->format('O'); $prm = Primitive::timestampTZ('test')->setComplex(); $array = array('test' => array(PrimitiveDate::DAY => '1', PrimitiveDate::MONTH => '2', PrimitiveDate::YEAR => '', PrimitiveTimestamp::HOURS => '17', PrimitiveTimestamp::MINUTES => '38', PrimitiveTimestamp::SECONDS => '59', PrimitiveTimestampTZ::ZONE => $currentTimeZone->getName())); $this->assertFalse($prm->import($array)); $this->assertEquals($array['test'], $prm->getRawValue()); $this->assertEmpty(array_filter($prm->exportValue())); //not supported other epochs $array['test'][PrimitiveDate::YEAR] = '3456'; $this->assertTrue($prm->import($array)); $this->assertEquals(3456, $prm->getValue()->getYear()); $this->assertEquals(17, $prm->getValue()->getHour()); $this->assertEquals($array['test'], $prm->exportValue()); $array['test'][PrimitiveDate::YEAR] = '2012'; $this->assertTrue($prm->import($array)); $this->assertEquals('2012-02-01 17:38:59' . $zone, $prm->getValue()->toString()); }
public function __construct($useMicroseconds, \DateTimeZone $timezone = null) { $this->useMicroseconds = $useMicroseconds; $date = 'now'; if ($useMicroseconds) { $timestamp = microtime(true); // apply offset of the timezone as microtime() is always UTC if ($timezone && $timezone->getName() !== 'UTC') { $timestamp += (new \DateTime('now', $timezone))->getOffset(); } // Circumvent DateTimeImmutable::createFromFormat() which always returns \DateTimeImmutable instead of `static` // @link https://bugs.php.net/bug.php?id=60302 // // So we create a DateTime but then format it so we // can re-create one using the right class $dt = self::createFromFormat('U.u', sprintf('%.6F', $timestamp)); $date = $dt->format('Y-m-d H:i:s.u'); } parent::__construct($date, $timezone); }
private function prepareUrl() { $url = 'https://www.googleapis.com/calendar/v3/calendars/'; $url .= urlencode($this->calendarId); $url .= '/events?key=' . $this->apiKey; $url .= '&timeMin=' . urlencode($this->timeMin); $url .= '&timeMax=' . urlencode($this->timeMax); if (!empty($this->expandRecurringEvents)) { $url .= '&singleEvents=true'; } if (isset($this->timeZone)) { $url .= '&timeZone=' . urlencode($this->timeZone->getName()); } if (isset($this->searchTerm)) { $url .= '&q=' . urlencode($this->searchTerm); } if (!empty($this->showDeleted)) { $url .= '&showDeleted=true'; } return $url; }
<?php date_default_timezone_set('Europe/Oslo'); $tz1 = new DateTimeZone('UTC'); $tz2 = date_create('UTC')->getTimeZone(); echo $tz1->getName(), PHP_EOL; echo $tz2->getName(), PHP_EOL; $d = new DateTime('2008-01-01 12:00:00+0200'); $d->setTimeZone($tz1); echo $d->format(DATE_ISO8601), PHP_EOL; $d = new DateTime('2008-01-01 12:00:00+0200'); $d->setTimeZone($tz2); echo $d->format(DATE_ISO8601), PHP_EOL;
/** * Set the formatter's timezone identifier. * * @param string $timeZoneId The time zone ID string of the time zone to use. * If NULL or the empty string, the default time zone for the * runtime is used. * * @return bool true on success or false on failure * * @see http://www.php.net/manual/en/intldateformatter.settimezoneid.php */ public function setTimeZoneId($timeZoneId) { if (null === $timeZoneId) { // In PHP 5.5 if $timeZoneId is null it fallbacks to `date_default_timezone_get()` method if (PHP_VERSION_ID >= 50500 || extension_loaded('intl') && method_exists('IntlDateFormatter', 'setTimeZone')) { $timeZoneId = date_default_timezone_get(); } else { // TODO: changes were made to ext/intl in PHP 5.4.4 release that need to be investigated since it will // use ini's date.timezone when the time zone is not provided. As a not well tested workaround, uses UTC. // See the first two items of the commit message for more information: // https://github.com/php/php-src/commit/eb346ef0f419b90739aadfb6cc7b7436c5b521d9 $timeZoneId = getenv('TZ') ?: 'UTC'; } $this->uninitializedTimeZoneId = true; } // Backup original passed time zone $timeZone = $timeZoneId; // Get an Etc/GMT time zone that is accepted for \DateTimeZone if ('GMT' !== $timeZoneId && 0 === strpos($timeZoneId, 'GMT')) { try { $timeZoneId = DateFormat\TimeZoneTransformer::getEtcTimeZoneId($timeZoneId); } catch (\InvalidArgumentException $e) { // Does nothing, will fallback to UTC } } try { $this->dateTimeZone = new \DateTimeZone($timeZoneId); if ('GMT' !== $timeZoneId && $this->dateTimeZone->getName() !== $timeZoneId) { $timeZoneId = $timeZone = $this->getTimeZoneId(); } } catch (\Exception $e) { if (PHP_VERSION_ID >= 50500 || extension_loaded('intl') && method_exists('IntlDateFormatter', 'setTimeZone')) { $timeZoneId = $timeZone = $this->getTimeZoneId(); } else { $timeZoneId = 'UTC'; } $this->dateTimeZone = new \DateTimeZone($timeZoneId); } $this->timeZoneId = $timeZone; return true; }
public function getTimezoneIdentifier() { return is_object($this->_timezone) ? $this->_timezone->getName() : $this->_timezone; }
/** * Guess the timezone for the current user * @param int $userOffset Offset from GMT in minutes * @return string */ public static function guessTimezone($userOffset = 0) { if (!is_numeric($userOffset)) { return ''; } $defaultZones = array('America/Anchorage', 'America/Los_Angeles', 'America/Phoenix', 'America/Chicago', 'America/New_York', 'America/Argentina/Buenos_Aires', 'America/Montevideo', 'Europe/London', 'Europe/Amsterdam', 'Europe/Athens', 'Europe/Moscow', 'Asia/Tbilisi', 'Asia/Omsk', 'Asia/Jakarta', 'Asia/Hong_Kong', 'Asia/Tokyo', 'Pacific/Guam', 'Australia/Sydney', 'Australia/Perth'); $now = new DateTime(); $tzlist = timezone_identifiers_list(); if ($userOffset == 0) { $gmtOffset = date('Z'); $nowtz = date('e'); if (in_array($nowtz, $tzlist)) { array_unshift($defaultZones, $nowtz); } else { $nowtz = timezone_name_from_abbr(date('T'), $gmtOffset, date('I')); if (in_array($nowtz, $tzlist)) { array_unshift($defaultZones, $nowtz); } } } else { $gmtOffset = $userOffset * 60; } foreach ($defaultZones as $zoneName) { $tz = new DateTimeZone($zoneName); if ($tz->getOffset($now) == $gmtOffset) { return $tz->getName(); } } // try all zones foreach ($tzlist as $zoneName) { $tz = new DateTimeZone($zoneName); if ($tz->getOffset($now) == $gmtOffset) { return $tz->getName(); } } return null; }
public function __toString() { return parent::getName(); }
protected static function getTimezoneNameFromTimezone(\DateTimeZone $tz) { if (defined('\\HHVM_VERSION')) { $testDT = new \DateTime('now', $tz); $result = $testDT->format('e'); if (!preg_match('/[0-9][0-9]/', $result)) { $result = $tz->getName(); } } else { $result = $tz->getName(); } return $result; }
function testWeirdSystemVLICs() { $vobj = <<<HI BEGIN:VCALENDAR CALSCALE:GREGORIAN PRODID:-//Ximian//NONSGML Evolution Calendar//EN VERSION:2.0 BEGIN:VTIMEZONE TZID:/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT X-LIC-LOCATION:SystemV/EST5EDT BEGIN:STANDARD TZNAME:EST DTSTART:19701104T020000 RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11 TZOFFSETFROM:-0400 TZOFFSETTO:-0500 END:STANDARD BEGIN:DAYLIGHT TZNAME:EDT DTSTART:19700311T020000 RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3 TZOFFSETFROM:-0500 TZOFFSETTO:-0400 END:DAYLIGHT END:VTIMEZONE BEGIN:VEVENT UID:20121026T021107Z-6301-1000-1-0@chAir DTSTAMP:20120905T172126Z DTSTART;TZID=/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT: 20121026T153000 DTEND;TZID=/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT: 20121026T160000 TRANSP:OPAQUE SEQUENCE:5 SUMMARY:pick up Ibby CLASS:PUBLIC CREATED:20121026T021108Z LAST-MODIFIED:20121026T021118Z X-EVOLUTION-MOVE-CALENDAR:1 END:VEVENT END:VCALENDAR HI; $tz = TimeZoneUtil::getTimeZone('/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT', Reader::read($vobj), true); $ex = new \DateTimeZone('America/New_York'); $this->assertEquals($ex->getName(), $tz->getName()); }
<?php $timezone = new DateTimeZone('Europe/Rome'); echo $timezone->getName(); print_r($timezone->getLocation()); ?>
/** * Improved equivalent to strtotime() * * @param string $date Date string * @param DateTimeZone $timezone Timezone to use for DateTime object * * @return int Unix timestamp */ public static function strtotime($date, $timezone = null) { $date = self::clean_datestr($date); $tzname = $timezone ? ' ' . $timezone->getName() : ''; // unix timestamp if (is_numeric($date)) { return (int) $date; } // if date parsing fails, we have a date in non-rfc format. // remove token from the end and try again while (($ts = @strtotime($date . $tzname)) === false || $ts < 0) { $d = explode(' ', $date); array_pop($d); if (!$d) { break; } $date = implode(' ', $d); } return (int) $ts; }
/** * Internal getter for client's (browser) timezone identifier */ private function client_timezone() { // @TODO: remove this legacy timezone handling in the future $props = $this->fix_legacy_props(array('timezone' => $_SESSION['timezone'])); if (!empty($props['timezone'])) { try { $tz = new DateTimeZone($props['timezone']); return $tz->getName(); } catch (Exception $e) { /* gracefully ignore */ } } // fallback to server's timezone return date_default_timezone_get(); }
/** * @param \DateTimeZone $dateTimeZone * * @return TimeZone */ public static function fromDateTimeZone(\DateTimeZone $dateTimeZone) { return TimeZone::parse($dateTimeZone->getName()); }
/** * testUserOffset method * * @return void */ public function testUserOffset() { $timezoneServer = new DateTimeZone(date_default_timezone_get()); $timeServer = new DateTime('now', $timezoneServer); $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR; $expected = time(); $result = $this->Time->fromString(time(), $yourTimezone); $this->assertWithinMargin($expected, $result, 1); $result = $this->Time->fromString(time(), $timezoneServer->getName()); $this->assertWithinMargin($expected, $result, 1); $result = $this->Time->fromString(time(), $timezoneServer); $this->assertWithinMargin($expected, $result, 1); Configure::write('Config.timezone', $timezoneServer->getName()); $result = $this->Time->fromString(time()); $this->assertWithinMargin($expected, $result, 1); Configure::delete('Config.timezone'); }
function getVTimeZone($vcalendar, $tzid, $from = 0, $to = 0) { if (!$from) { $from = time(); } if (!$to) { $to = $from; } try { $tz = new \DateTimeZone($tzid); } catch (\Exception $e) { return false; } // get all transitions for one year back/ahead $year = 86400 * 360; $transitions = $tz->getTransitions($from - $year, $to + $year); $vt = $vcalendar->createComponent('VTIMEZONE'); $vt->TZID = $tz->getName(); $vt->TZURL = 'http://tzurl.org/zoneinfo/' . $tz->getName(); $vt->add('X-LIC-LOCATION', $tz->getName()); $std = null; $dst = null; foreach ($transitions as $i => $trans) { $cmp = null; // skip the first entry... if ($i == 0) { // ... but remember the offset for the next TZOFFSETFROM value $tzfrom = $trans['offset'] / 3600; continue; } // daylight saving time definition if ($trans['isdst']) { $t_dst = $trans['ts']; $dst = $vcalendar->createComponent('DAYLIGHT'); $cmp = $dst; } else { $t_std = $trans['ts']; $std = $vcalendar->createComponent('STANDARD'); $cmp = $std; } if ($cmp) { $dt = new DateTime($trans['time']); $offset = $trans['offset'] / 3600; $cmp->DTSTART = $dt->format('Ymd\\THis'); $cmp->TZOFFSETFROM = sprintf('%s%02d%02d', $tzfrom >= 0 ? '+' : '', floor($tzfrom), ($tzfrom - floor($tzfrom)) * 60); $cmp->TZOFFSETTO = sprintf('%s%02d%02d', $offset >= 0 ? '+' : '', floor($offset), ($offset - floor($offset)) * 60); // add abbreviated timezone name if available if (!empty($trans['abbr'])) { $cmp->TZNAME = $trans['abbr']; } $tzfrom = $offset; $vt->add($cmp); } // we covered the entire date range if ($std && $dst && min($t_std, $t_dst) < $from && max($t_std, $t_dst) > $to) { break; } } // add X-MICROSOFT-CDO-TZID if available $microsoftExchangeMap = array_flip(Sabre\VObject\TimeZoneUtil::$microsoftExchangeMap); if (array_key_exists($tz->getName(), $microsoftExchangeMap)) { $vt->add('X-MICROSOFT-CDO-TZID', $microsoftExchangeMap[$tz->getName()]); } return $vt; }
/** * @Route("/dodaj_zdarzenie", name="dodaj_zdarzenie", options={"expose"=true}) * @param Request $request * @return Response */ public function dodajZdarzenieAction(Request $request) { $time_zone = new \DateTimeZone('UTC'); $time_zone->getName(); $dane = new Zdarzenia(); $start = new \DateTime($request->get('start')); //$start->createFromFormat('ATOM', $request->get('start'),$time_zone); $end = new \DateTime($request->get('end')); //$end->createFromFormat('ATOM', $request->get('end'),$time_zone); $dane->setTitle($request->get('title')); $dane->setDescription($request->get('description')); $dane->setLocation($request->get('location')); $dane->setContact($request->get('contact')); $dane->setUrl($request->get('url')); $dane->setStart($start); $dane->setEnd($end); $dane->setAlldayevent($request->get('alldayevent')); $em = $this->getDoctrine()->getManager(); $em->persist($dane); $em->flush(); $response = new \Symfony\Component\HttpFoundation\Response(); $response->headers->set('Content-Type', 'application/json'); $response->setContent(json_encode($dane)); return $response; }