Path: blob/master/src/applications/calendar/util/CalendarTimeUtil.php
12262 views
<?php12/**3* This class is useful for generating various time objects, relative to the4* user and their timezone.5*6* For now, the class exposes two sets of static methods for the two main7* calendar views - one for the conpherence calendar widget and one for the8* user profile calendar view. These have slight differences such as9* conpherence showing both a three day "today 'til 2 days from now" *and*10* a Sunday -> Saturday list, whilst the profile view shows a more simple11* seven day rolling list of events.12*/13final class CalendarTimeUtil extends Phobject {1415public static function getCalendarEventEpochs(16PhabricatorUser $user,17$start_day_str = 'Sunday',18$days = 9) {1920$objects = self::getStartDateTimeObjects($user, $start_day_str);21$start_day = $objects['start_day'];22$end_day = clone $start_day;23$end_day->modify('+'.$days.' days');2425return array(26'start_epoch' => $start_day->format('U'),27'end_epoch' => $end_day->format('U'),28);29}3031public static function getCalendarWeekTimestamps(32PhabricatorUser $user) {33return self::getTimestamps($user, 'Today', 7);34}3536public static function getCalendarWidgetTimestamps(37PhabricatorUser $user) {38return self::getTimestamps($user, 'Sunday', 9);39}4041/**42* Public for testing purposes only. You should probably use one of the43* functions above.44*/45public static function getTimestamps(46PhabricatorUser $user,47$start_day_str,48$days) {4950$objects = self::getStartDateTimeObjects($user, $start_day_str);51$start_day = $objects['start_day'];52$timestamps = array();53for ($day = 0; $day < $days; $day++) {54$timestamp = clone $start_day;55$timestamp->modify(sprintf('+%d days', $day));56$timestamps[] = $timestamp;57}58return array(59'today' => $objects['today'],60'epoch_stamps' => $timestamps,61);62}6364private static function getStartDateTimeObjects(65PhabricatorUser $user,66$start_day_str) {67$timezone = new DateTimeZone($user->getTimezoneIdentifier());6869$today_epoch = PhabricatorTime::parseLocalTime('today', $user);70$today = new DateTime('@'.$today_epoch);71$today->setTimezone($timezone);7273if (strtolower($start_day_str) == 'today' ||74$today->format('l') == $start_day_str) {75$start_day = clone $today;76} else {77$start_epoch = PhabricatorTime::parseLocalTime(78'last '.$start_day_str,79$user);80$start_day = new DateTime('@'.$start_epoch);81$start_day->setTimezone($timezone);82}83return array(84'today' => $today,85'start_day' => $start_day,86);87}8889}909192