You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

131 line
3.9 KiB

  1. <?php
  2. //--------------------------------------------------------------------------------------------------
  3. // Utilities for our event-fetching scripts.
  4. //
  5. // Requires PHP 5.2.0 or higher.
  6. //--------------------------------------------------------------------------------------------------
  7. // PHP will fatal error if we attempt to use the DateTime class without this being set.
  8. date_default_timezone_set('UTC');
  9. class Event {
  10. // Tests whether the given ISO8601 string has a time-of-day or not
  11. const ALL_DAY_REGEX = '/^\d{4}-\d\d-\d\d$/'; // matches strings like "2013-12-29"
  12. public $title;
  13. public $allDay; // a boolean
  14. public $start; // a DateTime
  15. public $end; // a DateTime, or null
  16. public $properties = array(); // an array of other misc properties
  17. // Constructs an Event object from the given array of key=>values.
  18. // You can optionally force the timezone of the parsed dates.
  19. public function __construct($array, $timezone=null) {
  20. $this->title = $array['title'];
  21. if (isset($array['allDay'])) {
  22. // allDay has been explicitly specified
  23. $this->allDay = (bool)$array['allDay'];
  24. }
  25. else {
  26. // Guess allDay based off of ISO8601 date strings
  27. $this->allDay = preg_match(self::ALL_DAY_REGEX, $array['start']) &&
  28. (!isset($array['end']) || preg_match(self::ALL_DAY_REGEX, $array['end']));
  29. }
  30. if ($this->allDay) {
  31. // If dates are allDay, we want to parse them in UTC to avoid DST issues.
  32. $timezone = null;
  33. }
  34. // Parse dates
  35. $this->start = parseDateTime($array['start'], $timezone);
  36. $this->end = isset($array['end']) ? parseDateTime($array['end'], $timezone) : null;
  37. // Record misc properties
  38. foreach ($array as $name => $value) {
  39. if (!in_array($name, array('title', 'allDay', 'start', 'end'))) {
  40. $this->properties[$name] = $value;
  41. }
  42. }
  43. }
  44. // Returns whether the date range of our event intersects with the given all-day range.
  45. // $rangeStart and $rangeEnd are assumed to be dates in UTC with 00:00:00 time.
  46. public function isWithinDayRange($rangeStart, $rangeEnd) {
  47. // Normalize our event's dates for comparison with the all-day range.
  48. $eventStart = stripTime($this->start);
  49. $eventEnd = isset($this->end) ? stripTime($this->end) : null;
  50. if (!$eventEnd) {
  51. // No end time? Only check if the start is within range.
  52. return $eventStart < $rangeEnd && $eventStart >= $rangeStart;
  53. }
  54. else {
  55. // Check if the two ranges intersect.
  56. return $eventStart < $rangeEnd && $eventEnd > $rangeStart;
  57. }
  58. }
  59. // Converts this Event object back to a plain data array, to be used for generating JSON
  60. public function toArray() {
  61. // Start with the misc properties (don't worry, PHP won't affect the original array)
  62. $array = $this->properties;
  63. $array['title'] = $this->title;
  64. // Figure out the date format. This essentially encodes allDay into the date string.
  65. if ($this->allDay) {
  66. $format = 'Y-m-d'; // output like "2013-12-29"
  67. }
  68. else {
  69. $format = 'c'; // full ISO8601 output, like "2013-12-29T09:00:00+08:00"
  70. }
  71. // Serialize dates into strings
  72. $array['start'] = $this->start->format($format);
  73. if (isset($this->end)) {
  74. $array['end'] = $this->end->format($format);
  75. }
  76. return $array;
  77. }
  78. }
  79. // Date Utilities
  80. //----------------------------------------------------------------------------------------------
  81. // Parses a string into a DateTime object, optionally forced into the given timezone.
  82. function parseDateTime($string, $timezone=null) {
  83. $date = new DateTime(
  84. $string,
  85. $timezone ? $timezone : new DateTimeZone('UTC')
  86. // Used only when the string is ambiguous.
  87. // Ignored if string has a timezone offset in it.
  88. );
  89. if ($timezone) {
  90. // If our timezone was ignored above, force it.
  91. $date->setTimezone($timezone);
  92. }
  93. return $date;
  94. }
  95. // Takes the year/month/date values of the given DateTime and converts them to a new DateTime,
  96. // but in UTC.
  97. function stripTime($datetime) {
  98. return new DateTime($datetime->format('Y-m-d'));
  99. }