/** * Constructs a new Period with a config array * * @param int $weekday Weekday represented by integer. Monday: 0 - Sunday: 7 * @param string $timeStart The start time in standard time format * @param string $timeEnd The end time in standard time format * @param bool $dummy Whether this period is a dummy. default: false * * @throws InvalidArgumentException On validation error */ public function __construct($weekday, $timeStart, $timeEnd, $dummy = false) { if (!is_int($weekday) or $weekday < 0 or $weekday > 6) { throw new InvalidArgumentException(sprintf('$weekday must be an integer between 0 and 6. got %s', (string) $weekday)); } if (!Dates::isValidTime($timeStart)) { throw new InvalidArgumentException(sprintf('$timeStart must be in standard time format %s. got %s', Dates::STD_TIME_FORMAT, $timeStart)); } if (!Dates::isValidTime($timeEnd)) { throw new InvalidArgumentException(sprintf('$timeEnd must be in standard time format %s. got %s', Dates::STD_TIME_FORMAT, $timeEnd)); } $this->weekday = $weekday; $this->timeStart = Dates::applyWeekContext(new DateTime($timeStart, Dates::getTimezone()), $weekday); $this->timeEnd = Dates::applyWeekContext(new DateTime($timeEnd, Dates::getTimezone()), $weekday); $this->dummy = $dummy; $this->spansTwoDays = Dates::compareTime($this->timeStart, $this->timeEnd) >= 0; if ($this->spansTwoDays) { $this->timeEnd->add(new DateInterval('P1D')); } }
/** * Getter: Date * @return DateTime */ public function getDate() { return new DateTime($this->getTimeStart()->format(Dates::STD_DATE_FORMAT), Dates::getTimezone()); }
public function testApplyTimezone() { $date = new DateTime('now', new DateTimeZone('America/Anchorage')); Dates::applyTimeZone($date); $this->assertEquals(Dates::getTimezone(), $date->getTimezone()); }
/** * Checks if the specified post representing a set matches the criteria * * @param WP_Post $post The child post * @return bool Whether the child post matches the criteria */ public static function postMatchesCriteria(WP_Post $post) { $detailDateStart = get_post_detail('date-start', $post->ID); $detailDateEnd = get_post_detail('date-end', $post->ID); $detailWeekScheme = get_post_detail('week-scheme', $post->ID); $detailDateStart = !empty($detailDateStart) ? new DateTime($detailDateStart, Dates::getTimezone()) : null; $detailDateEnd = !empty($detailDateEnd) ? new DateTime($detailDateEnd, Dates::getTimezone()) : null; if ($detailDateEnd !== null) { $detailDateEnd->setTime(23, 59, 59); } if ($detailDateStart == null and $detailDateEnd == null and ($detailWeekScheme == 'all' or empty($detailWeekScheme))) { return false; } $now = Dates::getNow(); if ($detailDateStart != null and $now < $detailDateStart) { return false; } if ($detailDateEnd != null and $now > $detailDateEnd) { return false; } $week_number_modulo = (int) $now->format('W') % 2; if ($detailWeekScheme == 'even' and $week_number_modulo === 1) { return false; } if ($detailWeekScheme == 'odd' and $week_number_modulo === 0) { return false; } return true; }