설명 없음

utils.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (isset($this->end)) {
  50. $eventEnd = stripTime($this->end); // normalize
  51. }
  52. else {
  53. $eventEnd = $eventStart; // consider this a zero-duration event
  54. }
  55. // Check if the two whole-day ranges intersect.
  56. return $eventStart < $rangeEnd && $eventEnd >= $rangeStart;
  57. }
  58. // Converts this Event object back to a plain data array, to be used for generating JSON
  59. public function toArray() {
  60. // Start with the misc properties (don't worry, PHP won't affect the original array)
  61. $array = $this->properties;
  62. $array['title'] = $this->title;
  63. // Figure out the date format. This essentially encodes allDay into the date string.
  64. if ($this->allDay) {
  65. $format = 'Y-m-d'; // output like "2013-12-29"
  66. }
  67. else {
  68. $format = 'c'; // full ISO8601 output, like "2013-12-29T09:00:00+08:00"
  69. }
  70. // Serialize dates into strings
  71. $array['start'] = $this->start->format($format);
  72. if (isset($this->end)) {
  73. $array['end'] = $this->end->format($format);
  74. }
  75. return $array;
  76. }
  77. }
  78. // Date Utilities
  79. //----------------------------------------------------------------------------------------------
  80. // Parses a string into a DateTime object, optionally forced into the given timeZone.
  81. function parseDateTime($string, $timeZone=null) {
  82. $date = new DateTime(
  83. $string,
  84. $timeZone ? $timeZone : new DateTimeZone('UTC')
  85. // Used only when the string is ambiguous.
  86. // Ignored if string has a timeZone offset in it.
  87. );
  88. if ($timeZone) {
  89. // If our timeZone was ignored above, force it.
  90. $date->setTimezone($timeZone);
  91. }
  92. return $date;
  93. }
  94. // Takes the year/month/date values of the given DateTime and converts them to a new DateTime,
  95. // but in UTC.
  96. function stripTime($datetime) {
  97. return new DateTime($datetime->format('Y-m-d'));
  98. }