Esempio n. 1
0
 /**
  * @covers Jsvrcek\ICS\Formatter::getFormattedUTCDateTime
  */
 public function testGetFormattedUTCDateTime()
 {
     $ce = new Formatter();
     $dateTime = new \DateTime('1998-01-18 23:00:00', new \DateTimeZone('America/New_York'));
     $expected = '19980119T040000Z';
     $actual = $ce->getFormattedUTCDateTime($dateTime);
     $this->assertEquals($expected, $actual);
     $ce = new Formatter();
     $dateTime = new \DateTime('1998-01-18 11:00:00', new \DateTimeZone('America/New_York'));
     $expected = '19980118T160000Z';
     $actual = $ce->getFormattedUTCDateTime($dateTime);
     $this->assertEquals($expected, $actual);
 }
Esempio n. 2
0
 /**
  * @return string
  */
 public function getStream()
 {
     $this->stream->reset();
     /* @var $cal Calendar */
     foreach ($this->getCalendars() as $cal) {
         //start calendar
         $this->stream->addItem('BEGIN:VCALENDAR')->addItem('VERSION:' . $cal->getVersion())->addItem('PRODID:' . $cal->getProdId())->addItem('CALSCALE:' . $cal->getCalendarScale())->addItem('METHOD:' . $cal->getMethod());
         //custom headers
         foreach ($cal->getCustomHeaders() as $key => $value) {
             $this->stream->addItem($key . ':' . $value);
         }
         //timezone
         $this->stream->addItem('BEGIN:VTIMEZONE');
         $tz = $cal->getTimezone();
         $transitions = $tz->getTransitions(strtotime('1970-01-01'), strtotime('1970-12-31'));
         $daylightSavings = array('exists' => false, 'start' => '', 'offsetTo' => '', 'offsetFrom' => '');
         $standard = array('start' => '', 'offsetTo' => '', 'offsetFrom' => '');
         foreach ($transitions as $transition) {
             $varName = $transition['isdst'] ? 'daylightSavings' : 'standard';
             ${$varName}['exists'] = true;
             ${$varName}['start'] = $this->formatter->getFormattedDateTime(new \DateTime($transition['time']));
             ${$varName}['offsetTo'] = $this->formatter->getFormattedTimeOffset($transition['offset']);
             //get previous offset
             $previousTimezoneObservance = $transition['ts'] - 100;
             $tzDate = new \DateTime('now', $tz);
             $tzDate->setTimestamp($previousTimezoneObservance);
             $offset = $tzDate->getOffset();
             ${$varName}['offsetFrom'] = $this->formatter->getFormattedTimeOffset($offset);
         }
         $this->stream->addItem('TZID:' . $tz->getName());
         $this->stream->addItem('BEGIN:STANDARD')->addItem('DTSTART:' . $standard['start'])->addItem('TZOFFSETTO:' . $standard['offsetTo'])->addItem('TZOFFSETFROM:' . $standard['offsetFrom']);
         if ($daylightSavings['exists']) {
             $this->stream->addItem('RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU');
         }
         $this->stream->addItem('END:STANDARD');
         if ($daylightSavings['exists']) {
             $this->stream->addItem('BEGIN:DAYLIGHT')->addItem('DTSTART:' . $daylightSavings['start'])->addItem('TZOFFSETTO:' . $daylightSavings['offsetTo'])->addItem('TZOFFSETFROM:' . $daylightSavings['offsetFrom'])->addItem('RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU')->addItem('END:DAYLIGHT');
         }
         $this->stream->addItem('END:VTIMEZONE');
         //add events
         /* @var $event CalendarEvent */
         foreach ($cal->getEvents() as $event) {
             $this->stream->addItem('BEGIN:VEVENT')->addItem('UID:' . $event->getUid())->addItem('DTSTART:' . $this->formatter->getFormattedUTCDateTime($event->getStart()))->addItem('DTEND:' . $this->formatter->getFormattedUTCDateTime($event->getEnd()));
             if ($event->getRecurrenceRule() instanceof RecurrenceRule) {
                 $this->stream->addItem($event->getRecurrenceRule()->__toString());
             }
             $this->stream->addItem('STATUS:' . $event->getStatus())->addItem('SUMMARY:' . $event->getSummary())->addItem('DESCRIPTION:' . $event->getDescription());
             if ($event->getClass()) {
                 $this->stream->addItem('CLASS:' . $event->getClass());
             }
             /* @var $location Location */
             foreach ($event->getLocations() as $location) {
                 $this->stream->addItem('LOCATION' . $location->getUri() . $location->getLanguage() . ':' . $location->getName());
             }
             if ($event->getPriority() > 0 && $event->getPriority() <= 9) {
                 $this->stream->addItem('PRIORITY:' . $event->getPriority());
             }
             if ($event->getGeo()) {
                 $this->stream->addItem('GEO:' . $event->getGeo()->getLatitude() . ';' . $event->getGeo()->getLongitude());
             }
             if ($event->getUrl()) {
                 $this->stream->addItem('URL:' . $event->getUrl());
             }
             if ($event->getTimestamp()) {
                 $this->stream->addItem('DTSTAMP:' . $this->formatter->getFormattedUTCDateTime($event->getTimestamp()));
             }
             if ($event->getCreated()) {
                 $this->stream->addItem('CREATED:' . $this->formatter->getFormattedUTCDateTime($event->getCreated()));
             }
             if ($event->getLastModified()) {
                 $this->stream->addItem('LAST-MODIFIED:' . $this->formatter->getFormattedUTCDateTime($event->getLastModified()));
             }
             foreach ($event->getAttendees() as $attendee) {
                 $this->stream->addItem($attendee->__toString());
             }
             if ($event->getOrganizer()) {
                 $this->stream->addItem($event->getOrganizer()->__toString());
             }
             if (is_array($event->getCategories())) {
                 $this->stream->addItem('CATEGORIES:' . implode(',', $event->getCategories()));
             }
             $this->stream->addItem('END:VEVENT');
         }
         //end calendar
         $this->stream->addItem('END:VCALENDAR');
     }
     return $this->stream->getStream();
 }
Esempio n. 3
0
 /**
  * @return string
  */
 public function __toString()
 {
     $items = array($this->getFrequency()->__toString());
     if ($this->interval) {
         $items[] = 'INTERVAL=' . $this->interval;
     }
     if ($this->until) {
         $items[] = 'UNTIL=' . $this->formatter->getFormattedUTCDateTime($this->until);
     }
     if ($this->count) {
         $items[] = 'COUNT=' . $this->count;
     }
     if ($this->byDayList) {
         $items[] = 'BYDAY=' . implode(',', $this->byDayList);
     }
     return self::KEY . implode(';', $items);
 }