2013-06-04 00:40:00 +02:00
|
|
|
<?php
|
2015-12-02 21:44:23 +01:00
|
|
|
|
2014-02-25 22:43:31 +01:00
|
|
|
/**
|
|
|
|
* This class is useful for generating various time objects, relative to the
|
|
|
|
* user and their timezone.
|
|
|
|
*
|
|
|
|
* For now, the class exposes two sets of static methods for the two main
|
|
|
|
* calendar views - one for the conpherence calendar widget and one for the
|
|
|
|
* user profile calendar view. These have slight differences such as
|
|
|
|
* conpherence showing both a three day "today 'til 2 days from now" *and*
|
|
|
|
* a Sunday -> Saturday list, whilest the profile view shows a more simple
|
|
|
|
* seven day rolling list of events.
|
|
|
|
*/
|
2015-06-15 10:02:26 +02:00
|
|
|
final class CalendarTimeUtil extends Phobject {
|
2013-06-04 00:40:00 +02:00
|
|
|
|
|
|
|
public static function getCalendarEventEpochs(
|
|
|
|
PhabricatorUser $user,
|
2014-02-25 22:43:31 +01:00
|
|
|
$start_day_str = 'Sunday',
|
|
|
|
$days = 9) {
|
2013-06-04 00:40:00 +02:00
|
|
|
|
|
|
|
$objects = self::getStartDateTimeObjects($user, $start_day_str);
|
|
|
|
$start_day = $objects['start_day'];
|
|
|
|
$end_day = clone $start_day;
|
2014-02-25 22:43:31 +01:00
|
|
|
$end_day->modify('+'.$days.' days');
|
2013-06-04 00:40:00 +02:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'start_epoch' => $start_day->format('U'),
|
2014-10-07 15:01:04 +02:00
|
|
|
'end_epoch' => $end_day->format('U'),
|
|
|
|
);
|
2013-06-04 00:40:00 +02:00
|
|
|
}
|
|
|
|
|
2014-02-25 22:43:31 +01:00
|
|
|
public static function getCalendarWeekTimestamps(
|
|
|
|
PhabricatorUser $user) {
|
|
|
|
return self::getTimestamps($user, 'Today', 7);
|
|
|
|
}
|
|
|
|
|
2013-06-04 00:40:00 +02:00
|
|
|
public static function getCalendarWidgetTimestamps(
|
2014-02-25 22:43:31 +01:00
|
|
|
PhabricatorUser $user) {
|
|
|
|
return self::getTimestamps($user, 'Sunday', 9);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public for testing purposes only. You should probably use one of the
|
|
|
|
* functions above.
|
|
|
|
*/
|
|
|
|
public static function getTimestamps(
|
2013-06-04 00:40:00 +02:00
|
|
|
PhabricatorUser $user,
|
2014-02-25 22:43:31 +01:00
|
|
|
$start_day_str,
|
|
|
|
$days) {
|
2013-06-04 00:40:00 +02:00
|
|
|
|
|
|
|
$objects = self::getStartDateTimeObjects($user, $start_day_str);
|
|
|
|
$start_day = $objects['start_day'];
|
|
|
|
$timestamps = array();
|
2014-02-25 22:43:31 +01:00
|
|
|
for ($day = 0; $day < $days; $day++) {
|
2013-06-04 00:40:00 +02:00
|
|
|
$timestamp = clone $start_day;
|
|
|
|
$timestamp->modify(sprintf('+%d days', $day));
|
|
|
|
$timestamps[] = $timestamp;
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'today' => $objects['today'],
|
2014-10-07 15:01:04 +02:00
|
|
|
'epoch_stamps' => $timestamps,
|
|
|
|
);
|
2013-06-04 00:40:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function getStartDateTimeObjects(
|
|
|
|
PhabricatorUser $user,
|
|
|
|
$start_day_str) {
|
|
|
|
$timezone = new DateTimeZone($user->getTimezoneIdentifier());
|
|
|
|
|
|
|
|
$today_epoch = PhabricatorTime::parseLocalTime('today', $user);
|
|
|
|
$today = new DateTime('@'.$today_epoch);
|
|
|
|
$today->setTimeZone($timezone);
|
|
|
|
|
2014-02-25 22:43:31 +01:00
|
|
|
if (strtolower($start_day_str) == 'today' ||
|
|
|
|
$today->format('l') == $start_day_str) {
|
2013-06-04 00:40:00 +02:00
|
|
|
$start_day = clone $today;
|
|
|
|
} else {
|
|
|
|
$start_epoch = PhabricatorTime::parseLocalTime(
|
|
|
|
'last '.$start_day_str,
|
|
|
|
$user);
|
|
|
|
$start_day = new DateTime('@'.$start_epoch);
|
|
|
|
$start_day->setTimeZone($timezone);
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'today' => $today,
|
2014-10-07 15:01:04 +02:00
|
|
|
'start_day' => $start_day,
|
|
|
|
);
|
2013-06-04 00:40:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|