Example #1
0
 /**
  * This function is meant to be called **hourly** by cron.
  * It calls all registred handlers and it also takes care of emitting trigger
  * signals for longer periods of time than on hourly basis (daily, weekly, ...).
  * 
  * @param \DateTime forced date time (for testing purposes only)
  */
 public static function run(\DateTime $now = null)
 {
     try {
         Nette\Utils\CriticalSection::enter();
     } catch (Nette\InvalidStateException $e) {
         if ($e->getMessage() == 'Critical section has already been entered.') {
             throw new CronException('Cron job is already running');
         } else {
             throw $e;
         }
     }
     try {
         $cache = static::getCache();
         if ($now === null) {
             $now = new \DateTime('now');
         }
         // Hodinova volani =======================================================
         if (isset($cache['lastHourly'])) {
             // Pocet minut od posledni zmeny
             $offset = (intval($now->format('U')) - intval($cache['lastHourly']->format('U'))) / 60;
             if ($offset < 1) {
                 throw new CronException('Invalid time offset. Current date is earlier than already processed.');
             }
             if ($offset < 60) {
                 throw new CronException('Invalid time offset. Method was called too early. Probably because of clock skew.');
             }
         } else {
             $cache['lastHourly'] = null;
         }
         self::hourly($cache['lastHourly'], $now);
         $cache['lastHourly'] = clone $now;
         // Denni a mesicni volani ================================================
         if (intval($now->format('H')) == 0) {
             self::daily(isset($cache['lastDaily']) ? $cache['lastHourly'] : null, $now);
             $cache['lastDaily'] = clone $now;
             if (intval($now->format('d')) == 1) {
                 self::monthly(isset($cache['lastMonthly']) ? $cache['lastMonthly'] : null, $now);
                 $cache['lastMonthly'] = clone $now;
             }
         }
         Nette\Utils\CriticalSection::leave();
     } catch (\Exception $e) {
         Nette\Utils\CriticalSection::leave();
         throw $e;
     }
 }
Example #2
0
 /**
  * Parses date/time string formated by HTTP specification
  *
  * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
  *
  * @param string
  * @param DateTime|null
  */
 public static function parseDateTime($str)
 {
     // Format 1:
     // Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
     $dt = \DateTime::createFromFormat('D, d M Y H:i:s O', $str);
     if ($dt !== FALSE) {
         return $dt;
     }
     // Format 2:
     // Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
     $dt = \DateTime::createFromFormat('l, d-M-y H:i:s O', $str);
     if ($dt !== FALSE) {
         return $dt;
     }
     // Format 3:
     // Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
     $dt = \DateTime::createFromFormat('D M j H:i:s Y', $str, new \DateTimeZone('UTC'));
     if ($dt !== FALSE) {
         return $dt;
     }
     return null;
 }
Example #3
0
 /**
  * Helper for getting name of the day of the week.
  *
  * @note Output is translated.
  *
  * @param DateTime|int|string $time
  * @return string name of the day of the week
  */
 public static function weekDayName($time)
 {
     if (($time = self::timestamp($time)) == FALSE) {
         return FALSE;
     }
     return vBuilder\Utils\DateTime::weekDayName((int) date('N', $time));
 }
Example #4
0
 /**
  * Returns the last valid DT of given period
  *
  * @param  \DateTime $dt     [description]
  * @param  string vBuilder\Utils\DateTime period constant
  * @return \DateTime
  * @throws  Nette\InvalidArgumentException If invalid period is specified
  */
 public static function endOfPeriod(\DateTime $dt, $period)
 {
     $dt2 = static::startOfPeriod($dt, $period);
     switch ($period) {
         case self::MINUTE:
             return $dt2->modify('+59 seconds');
         case self::HOUR:
             return $dt2->modify('+59 minutes 59 seconds');
         case self::DAY:
             return $dt2->modify('+23 hours 59 minutes 59 seconds');
         case self::WEEK:
             return $dt2->modify('+6 days 23 hours 59 minutes 59 seconds');
         case self::MONTH:
             return $dt2->modify('+' . ($dt2->format('t') - 1) . ' days 23 hours 59 minutes 59 seconds');
         case self::YEAR:
             return \DateTime::createFromFormat('Y-m-d H:i:s', $dt->format('Y-12-31 23:59:59'), $dt->getTimezone());
     }
 }