[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/calendar/util/ -> CalendarTimeUtil.php (source)

   1  <?php
   2  /**
   3   * This class is useful for generating various time objects, relative to the
   4   * user and their timezone.
   5   *
   6   * For now, the class exposes two sets of static methods for the two main
   7   * calendar views - one for the conpherence calendar widget and one for the
   8   * user profile calendar view. These have slight differences such as
   9   * conpherence showing both a three day "today 'til 2 days from now" *and*
  10   * a Sunday -> Saturday list, whilest the profile view shows a more simple
  11   * seven day rolling list of events.
  12   */
  13  final class CalendarTimeUtil {
  14  
  15    public static function getCalendarEventEpochs(
  16      PhabricatorUser $user,
  17      $start_day_str = 'Sunday',
  18      $days = 9) {
  19  
  20      $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');
  24  
  25      return array(
  26        'start_epoch' => $start_day->format('U'),
  27        'end_epoch' => $end_day->format('U'),
  28      );
  29    }
  30  
  31    public static function getCalendarWeekTimestamps(
  32      PhabricatorUser $user) {
  33      return self::getTimestamps($user, 'Today', 7);
  34    }
  35  
  36    public static function getCalendarWidgetTimestamps(
  37      PhabricatorUser $user) {
  38      return self::getTimestamps($user, 'Sunday', 9);
  39    }
  40  
  41    /**
  42     * Public for testing purposes only. You should probably use one of the
  43     * functions above.
  44     */
  45    public static function getTimestamps(
  46      PhabricatorUser $user,
  47      $start_day_str,
  48      $days) {
  49  
  50      $objects = self::getStartDateTimeObjects($user, $start_day_str);
  51      $start_day = $objects['start_day'];
  52      $timestamps = array();
  53      for ($day = 0; $day < $days; $day++) {
  54        $timestamp = clone $start_day;
  55        $timestamp->modify(sprintf('+%d days', $day));
  56        $timestamps[] = $timestamp;
  57      }
  58      return array(
  59        'today' => $objects['today'],
  60        'epoch_stamps' => $timestamps,
  61      );
  62    }
  63  
  64    private static function getStartDateTimeObjects(
  65      PhabricatorUser $user,
  66      $start_day_str) {
  67      $timezone = new DateTimeZone($user->getTimezoneIdentifier());
  68  
  69      $today_epoch = PhabricatorTime::parseLocalTime('today', $user);
  70      $today = new DateTime('@'.$today_epoch);
  71      $today->setTimeZone($timezone);
  72  
  73      if (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      }
  83      return array(
  84        'today' => $today,
  85        'start_day' => $start_day,
  86      );
  87    }
  88  
  89  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1