Esempio n. 1
0
 /**
  * @return Timestamp
  * Emulates PostgreSQL's date_trunc() function
  **/
 public function truncate(Date $time, $ceil = false)
 {
     $time = $time->toTimestamp();
     $function = $ceil ? 'ceil' : 'floor';
     if ($this->seconds) {
         if ($this->seconds < 1) {
             return $time->spawn();
         }
         $truncated = (int) ($function($time->toStamp() / $this->seconds) * $this->seconds);
         return Timestamp::create($truncated);
     } elseif ($this->days) {
         $epochStartTruncated = Date::create('1970-01-05');
         $truncatedDate = Date::create($time->toDate());
         if ($ceil && $truncatedDate->toStamp() < $time->toStamp()) {
             $truncatedDate->modify('+1 day');
         }
         $difference = Date::dayDifference($epochStartTruncated, $truncatedDate);
         $truncated = (int) ($function($difference / $this->days) * $this->days);
         return Timestamp::create($epochStartTruncated->spawn($truncated . ' days')->toStamp());
     } elseif ($this->months) {
         $monthsCount = $time->getYear() * 12 + ($time->getMonth() - 1);
         if ($ceil && $time->getDay() - 1 + $time->getHour() + $time->getMinute() + $time->getSecond() > 0) {
             $monthsCount += 0.1;
         }
         // delta
         $truncated = (int) ($function($monthsCount / $this->months) * $this->months);
         $months = $truncated % 12;
         $years = ($truncated - $months) / 12;
         Assert::isEqual($years, (int) $years);
         $years = (int) $years;
         $months = $months + 1;
         return Timestamp::create("{$years}-{$months}-01 00:00:00");
     }
     Assert::isUnreachable();
 }
Esempio n. 2
0
 public function makeItems(\SimpleXMLElement $xmlFeed)
 {
     $result = array();
     if (isset($xmlFeed->channel->item)) {
         foreach ($xmlFeed->channel->item as $item) {
             $feedItem = FeedItem::create((string) $item->title)->setContent(FeedItemContent::create()->setBody((string) $item->description))->setPublished(Timestamp::create(strtotime((string) $item->pubDate)))->setLink((string) $item->link);
             if (isset($item->guid)) {
                 $feedItem->setId($item->guid);
             }
             if (isset($item->category)) {
                 $feedItem->setCategory((string) $item->category);
             }
             $result[] = $feedItem;
         }
     }
     return $result;
 }
Esempio n. 3
0
 public function makeItems(\SimpleXMLElement $xmlFeed)
 {
     $result = [];
     foreach ($xmlFeed->entry as $entry) {
         $feedItem = FeedItem::create((string) $entry->title);
         if (isset($entry->content)) {
             $feedItem->setContent($this->makeFeedItemContent($entry->content));
         }
         if (isset($entry->summary)) {
             $feedItem->setSummary($this->makeFeedItemContent($entry->summary));
         }
         if (isset($entry->id)) {
             $feedItem->setId($entry->id);
         }
         $result[] = $feedItem->setPublished(Timestamp::create(strtotime((string) $entry->updated)))->setLink((string) $entry->link);
     }
     return $result;
 }
Esempio n. 4
0
 public function makeItems(\SimpleXMLElement $xmlFeed)
 {
     $xmlFeed->registerXPathNamespace(YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX, YandexRssFeedFormat::YANDEX_NAMESPACE_URI);
     $fullTextList = $xmlFeed->xpath('//' . YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX . ':full-text');
     $result = array();
     $i = 0;
     if (isset($xmlFeed->channel->item)) {
         foreach ($xmlFeed->channel->item as $item) {
             $feedItem = YandexRssFeedItem::create((string) $item->title)->setContent(FeedItemContent::create()->setBody((string) $item->description))->setPublished(Timestamp::create(strtotime((string) $item->pubDate)))->setFullText((string) $fullTextList[$i++])->setLink((string) $item->link);
             if (isset($item->guid)) {
                 $feedItem->setId($item->guid);
             }
             if (isset($item->category)) {
                 $feedItem->setCategory((string) $item->category);
             }
             $result[] = $feedItem;
         }
     }
     return $result;
 }
Esempio n. 5
0
 /**
  * @return Message
  **/
 public function receive($uTimeout = null)
 {
     if (!$this->queue) {
         throw new WrongStateException('you must set the queue first');
     }
     if ($uTimeout && $this->getStream()->isEof()) {
         usleep($uTimeout);
     }
     $string = $this->getStream()->readString();
     if (!$string && $this->getStream()->isEof()) {
         return null;
     }
     $this->getQueue()->setOffset($this->getStream()->getOffset());
     $string = rtrim($string, PHP_EOL);
     $chunks = preg_split("/\t/", $string, 2);
     $time = isset($chunks[0]) ? $chunks[0] : null;
     $text = isset($chunks[1]) ? $chunks[1] : null;
     Assert::isNotNull($time);
     $result = TextMessage::create(Timestamp::create($time))->setText($text);
     return $result;
 }
Esempio n. 6
0
 /**
  * @return Timestamp
  **/
 public function toTimestamp()
 {
     return Timestamp::create($this->toStamp());
 }
Esempio n. 7
0
 /**
  * @return Timestamp
  **/
 public static function alignToSeconds(Timestamp $stamp, $seconds)
 {
     $rawStamp = $stamp->toStamp();
     $align = floor($rawStamp / $seconds);
     return Timestamp::create($align * $seconds);
 }