Generate the occurrences for each daily recurring event.

@param  EventInterface[]		$events
@param  \DateTime			$fromDate
@param  \DateTime			$toDate
@param  int|null				$limit
@return EventInterface[]
193954
public function generateOccurrences(Array $events, \DateTime $fromDate, \DateTime $toDate, $limit = null)
	{
		$return = array();
		$object = $this;

		$dailyEvents = array_filter($events, function ($event) use ($object) {
			return $event->getRecurrenceType() === $object->getLabel();
		});

		foreach ($dailyEvents as $dailyEvent) {
			$dailyEventTime = $dailyEvent->getStartDate()->format('H:i:s');
			
			$startMarker = $fromDate > $dailyEvent->getStartDate()
				? clone($fromDate)
				: clone($dailyEvent->getStartDate());

			$maxEndMarker = clone($startMarker);
			$maxEndMarker->modify($this->limit);

			$endMarker = $dailyEvent->getRecurrenceUntil()
				? min($dailyEvent->getRecurrenceUntil(), clone($toDate), $maxEndMarker)
				: min(clone($toDate), $maxEndMarker);

			$actualEndMarker = clone($endMarker);

			// The DatePeriod class does not actually include the end date so you have to increment it first
			$endMarker->modify('+1 day');

			$dateInterval = new \DateInterval('P1D');
			$datePeriod = new \DatePeriod($startMarker, $dateInterval, $endMarker);

			$limitMarker = 0;

			foreach ($datePeriod as $date) {
				if (($limit and ($limit === $limitMarker)) or ($date > $actualEndMarker)) {
					break;
				}

				$newDailyEvent = clone($dailyEvent);
				$newStartDate = new \DateTime($date->format('Y-m-d').' '.$dailyEventTime);

				if ($newStartDate < $startMarker) {
					continue;
				}

				$duration = $newDailyEvent->getDuration();

				$newDailyEvent->setStartDate($newStartDate);

				$newEndDate = clone($newStartDate);
				$newEndDate->add($duration);
				
				$newDailyEvent->setEndDate($newEndDate);
				$newDailyEvent->setRecurrenceType();

				$return[] = $newDailyEvent;

				$limit and $limitMarker++;
			}
		}

		return $return;
	}
167045
public function events($events=array())
    {
        if (!isset($this->_events)) {
            $this->_events = new Events(array(), self::schema()->events());
            $this->_registerDefaultEventHandlers();
        }

        if(count($events))
            foreach($events as $event=>$callback)
                $this->_events->register($event, $callback);

        return $this->_events;
    }
1285123
public static function date_range(string $from, string $to, string $step = '+1 day', string $output_format = 'd-m-Y' ) : array
    {
        $dates = [];
        $date_from = strtotime($from);
        $date_to = strtotime($to);
        while($date_from <= $date_to){
            $dates[] = date($output_format, $date_from);
            $date_from = strtotime($step, $date_from);
        }
        return $dates;
    }
647116
public function getDateRange()
    {
        $fromDate = $this->request->getParam('from', self::DEFAULT_FROM_DATE);
        if ($fromDate !== self::DEFAULT_FROM_DATE) {
            $fromDate = base64_decode($fromDate);
        }
        $toDate = $this->request->getParam('to', self::DEFAULT_TO_DATE);
        if ($toDate !== self::DEFAULT_TO_DATE) {
            $toDate = base64_decode($toDate);
        }

        $dateRange = [
            'from' => $this->createDateFromText($fromDate),
            'to'   => $this->createDateFromText($toDate),
        ];

        return $this->sortDateRange($dateRange);
    }
1163558
public static function dateWord($from, $now) {
        $between = $now - $from;
        /** 如果是一天 */
        if ($between >= 0 && $between < 86400 && date('d', $from) == date('d', $now)) {
            /** 如果是一小时 */
            if ($between < 3600) {
                /** 如果是一分钟 */
                if ($between < 60) {
                    if (0 == $between) {
                        return _t('刚刚');
                    }
                    else {
                        return str_replace('%d', $between, _n('一秒前', '%d秒前', $between));
                    }
                }

                $min = floor($between / 60);
                return str_replace('%d', $min, _n('一分钟前', '%d分钟前', $min));
            }

            $hour = floor($between / 3600);
            return str_replace('%d', $hour, _n('一小时前', '%d小时前', $hour));
        }

        /** 如果是昨天 */
        if ($between > 0 && $between < 172800
            && (date('z', $from) + 1 == date('z', $now)                             // 在同一年的情况
                || date('z', $from) + 1 == date('L') + 365 + date('z', $now))
        ) {    // 跨年的情况
            return _t('昨天 %s', date('H:i', $from));
        }

        /** 如果是一个星期 */
        if ($between > 0 && $between < 604800) {
            $day = floor($between / 86400);
            return str_replace('%d', $day, _n('一天前', '%d天前', $day));
        }

        /** 如果是 */
        if (date('Y', $from) == date('Y', $now)) {
            return date(_t('n月j日'), $from);
        }

        return date(_t('Y年m月d日'), $from);
    }
632317
public function replayTransactionLog($fromDate = null, $toDate = null)
    {
        return $this->getDataUpdater()->replayTransactionLog($fromDate, $toDate);
    }
236265
public function generateOccurrences(CalendarEventVersion $version, CalendarEventVersionRepetition $repetitionEntity, $start_time, $end_time)
    {

        $saver = \Core::make(EventOccurrenceService::class);

        $repetition = $repetitionEntity->getRepetitionObject();

        $occurrences = $repetition->activeRangesBetween($start_time, $end_time);

        $initial_occurrence_time = (new \DateTime($repetition->getStartDate(), $repetition->getTimezone()))
            ->getTimestamp();
        if ($repetition->getEndDate()) {
            $initial_occurrence_time_end = (new \DateTime($repetition->getEndDate(), $repetition->getTimezone()))
                ->getTimestamp();
        } else {
            $initial_occurrence_time_end = $initial_occurrence_time;
        }

        $initial_occurrence = $this->createEventOccurrence(
            $version,
            $repetitionEntity->getRepetition(),
            $initial_occurrence_time,
            $initial_occurrence_time_end
        );

        if ($initial_occurrence_time >= $start_time && $initial_occurrence_time <= $end_time) {
            $saver->save($initial_occurrence);
        }

        $all_occurrences = array();
        foreach ($occurrences as $occurrence) {
            if ($occurrence[0] === $initial_occurrence->getStart()) {
                continue;
            }
            $all_occurrences[] = $saver->save(
                $this->createEventOccurrence($version, $repetitionEntity->getRepetition(), $occurrence[0], $occurrence[1])
            );
        }

        return $all_occurrences;
    }
1217516
public function getDeleteSQL($from, $joins, $where, $values, $order, $limit) {
        $len    = count($joins);
        $jw     = [];
        $sql [] = 'DELETE FROM ' . implode(' AS ', $from [0]);
        if ($len) {
            $sql [] = 'USING';

            foreach ($joins as $join) {
                $jw[]   = $join[1];
                $sql [] = $join [0] . ' AS ' . $join [3];
            }
        }

        if ($where && count($where) > 0) {
            $sql [] = 'WHERE';
            $sql [] = $where->getWhereCondition($this, $values);
        }
        if ($jw) {
            if ($where && count($where) > 0) {
                $sql[] = 'AND ' . implode(' AND ', $jw);
            } else {
                $sql[] = 'WHERE ' . implode(' AND ', $jw);
            }
        }

        return implode(' ', $sql);
    }
72196
public function setTimeRange($from, $to)
    {
        $fromDate = date('Y-m-d H:i:s', (int) $from);
        $toDate = date('Y-m-d H:i:s', (int) $to);

        $this->where(array("time > '$fromDate'", "time < '$toDate'"));

        return $this;
    }
1200452
public function getTotalResult($start, $limit, $sortBy, $sortOrder, $params)
    {
        $results = $this->findAll($start, $limit, $sortBy, $sortOrder, $params);

        return count($results);
    }
863928
public static function create_calendar_events($events) {
        global $DB, $USER;

        // Parameter validation.
        $params = self::validate_parameters(self::create_calendar_events_parameters(), array('events' => $events));

        $transaction = $DB->start_delegated_transaction();
        $return = array();
        $warnings = array();

        foreach ($params['events'] as $event) {

            // Let us set some defaults.
            $event['userid'] = $USER->id;
            $event['modulename'] = '';
            $event['instance'] = 0;
            $event['subscriptionid'] = null;
            $event['uuid']= '';
            $event['format'] = external_validate_format($event['format']);
            if ($event['repeats'] > 0) {
                $event['repeat'] = 1;
            } else {
                $event['repeat'] = 0;
            }

            $eventobj = new calendar_event($event);

            // Let's check if the user is allowed to delete an event.
            if (!calendar_add_event_allowed($eventobj)) {
                $warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event');
                continue;
            }
            // Let's create the event.
            $var = $eventobj->create($event);
            $var = (array)$var->properties();
            if ($event['repeat']) {
                $children = $DB->get_records('event', array('repeatid' => $var['id']));
                foreach ($children as $child) {
                    $return[] = (array) $child;
                }
            } else {
                $return[] = $var;
            }
        }

        // Everything done smoothly, let's commit.
        $transaction->allow_commit();
        return array('events' => $return, 'warnings' => $warnings);
    }
144622
public function getFilteredTasksList($search = null, $order_by = null, $start = 0, $limit = null){
        $res = array();
            
        $sql = 'SELECT majat_name, majat_status, majat_last_run, majat_last_result, majat_frequency, majat_nb_occur, majat_running' .
            ' FROM `##maj_admintasks`';
        
        $args = array();
                
        if ($order_by) {
            $sql .= ' ORDER BY ';
            $i = 0;
            foreach ($order_by as $key => $value) {
                if ($i > 0) {
                    $sql .= ',';
                }
                
                switch ($value['dir']) {
                    case 'asc':
                        $sql .= $value['column'] . ' ASC ';
                        break;
                    case 'desc':
                        $sql .= $value['column'] . ' DESC ';
                        break;
                }
                $i++;
            }
        } else {
            $sql .= ' ORDER BY majat_name ASC';
        }
        
        if ($limit) {
            $sql .= ' LIMIT :limit OFFSET :offset';
            $args['limit']  = $limit;
            $args['offset'] = $start;
        }
            
        $data = Database::prepare($sql)->execute($args)->fetchAll(\PDO::FETCH_ASSOC);

        foreach($data as $ga) {
            $task = $this->loadTaskFromRow($ga);
			if($task && (empty($search) || ($search && strpos($task->getTitle(), $search) !== false)))
			{
				$res[] = $task;
			}
        }
		
        return $res;
    }
770797
public function getUpdates($lastUpdateId = null, int $limit = 5, int $timeout = 30) : \Generator
    {
        if ($lastUpdateId !== null) {
            $this->lastUpdateId = $lastUpdateId;
        }

        $method = new GetUpdates($this->lastUpdateId + 1, $limit, $timeout);

        $updates = yield from $this->execute($method);
        $this->lastUpdateId = $method->getLastUpdateId();

        return $updates;
    }
760447
public static function pf_date_indexed($from, $to, $step = '+1 day', $outputFormat = 'Y-m-d') {
        $dates = array();
        $current = strtotime($from);
        $last = strtotime($to);
        while($current <= $last) {
            $dates[] = date($outputFormat, $current);
            $current = strtotime($step, $current);
        }
        return $dates;
    }
382461
public function getOccurrencesBetween($startDate, $endDate, $limit = NULL)
    {

        $thisClone = clone $this;

        // Enforce consistent time zones. Date comparisons don't require them, but +P1D loop does.
        if ($tz = $thisClone->getTimeZone())
        {
            $startDate->setTimeZone($tz);
            $endDate->setTimeZone($tz);
        }

        $occurrences = [];

        if ($endDate <= $startDate)
        {
            return $occurrences;
        }

        // if existing UNTIL < startDate - we have nothing
        if (isset($thisClone->until) && $thisClone->until < $startDate)
        {
            return $occurrences;
        } // prevent unnecessary leg-work - our endDate is our new UNTIL
        elseif (!isset($thisClone->until))
        {
            $thisClone->until = $endDate;
        }

        $thisClone->generateOccurrences();
        $all_occurrences = $thisClone->occurrences;

        // nothing found in $thisClone->generateOccurrences();
        if (empty($all_occurrences))
        {
            return $occurrences;
        }

        $last_occurrence = end($all_occurrences);

        // if we've hit the rangeLimit, restart looking but start at this last_occurrence
        if ($thisClone->rangeLimit == count($all_occurrences)
            && $thisClone->startDate != $last_occurrence)
        {
            $thisClone->startDate = clone $last_occurrence;

            if (isset($thisClone->limit))
            {
                $thisClone->limit = $thisClone->limit - 200;
            }

            // clear all occurrences before our start date
            foreach ($thisClone->occurrences as $key => $occurrence)
            {
                if ($occurrence < $startDate)
                {
                    unset($thisClone->occurrences[$key]);
                }
            }

            return $thisClone->getOccurrencesBetween($startDate, $endDate, $limit);
        }

        // if our last occurrence is is before our startDate, we have nothing
        if ($last_occurrence < $startDate)
        {
            return $occurrences;
        }

        // we have something to report, so reset our array pointer
        reset($all_occurrences);

        $count = 0;

        foreach ($all_occurrences as $occurrence)
        {
            // fastforward our pointer to where it's >= startDate
            if ($occurrence < $startDate)
            {
                continue;
            }
            // if current occurence is past our endDate - we're done
            if ($occurrence > $endDate)
            {
                break;
            }
            // if we reach getOccurrencesBetween()'s limit - we're done
            if (NULL != $limit && ++$count > $limit)
            {
                break;
            }

            $occurrences[] = $occurrence;
        }

        return $occurrences;
    }
28198
public function replayTransactionLog($fromDate=null, $toDate=null)
    {

        $cursor = $this->getTransactionLog()->getCompletedTransactions($this->storeName, $this->podName, $fromDate, $toDate);
        foreach($cursor as $result)
        {
            $this->applyTransaction($result);
        }

        return true;
    }
1276793
private DateTime parseToDate(String toDate) {
        // Default to now
        return parseDate(toDate, new DateTime().withZone(DateTimeZone.UTC), false);
    }