diff options
author | AlexKlimenkov <shurick.klimenkov@gmail.com> | 2016-03-23 15:04:13 +0300 |
---|---|---|
committer | AlexKlimenkov <shurick.klimenkov@gmail.com> | 2016-03-23 15:04:13 +0300 |
commit | 2aebc0fb22e7502bbf610e7ffcf655ddd77fefae (patch) | |
tree | c00425f5d31b2eef66509f01fe8aa64a7e8ad4f5 | |
parent | 212fd89925d606aec583e83cdbeaeb5b78ff522e (diff) | |
parent | 61fed06a49146a57fc99ffcbd659f5b6912c518e (diff) | |
download | scheduler-helper-php-2aebc0fb22e7502bbf610e7ffcf655ddd77fefae.zip scheduler-helper-php-2aebc0fb22e7502bbf610e7ffcf655ddd77fefae.tar.gz scheduler-helper-php-2aebc0fb22e7502bbf610e7ffcf655ddd77fefae.tar.bz2 |
Merge pull request #20 from mperednya/master
Fix dates difference calculation and day of week calculation.
-rw-r--r-- | RecurringType.php | 43 | ||||
-rwxr-xr-x | SchedulerHelperDate.php | 6 | ||||
-rw-r--r-- | tests/Data_getData/MonthRecurrings2/source.json | 41 | ||||
-rw-r--r-- | tests/Data_getData/MonthRecurrings2/target.json | 31 | ||||
-rw-r--r-- | tests/Data_getData/YearRecurrings/source.json | 11 | ||||
-rw-r--r-- | tests/Data_getData/YearRecurrings/target.json | 9 | ||||
-rw-r--r-- | tests/Data_getData/get_data_from_scheduler.txt | 40 |
7 files changed, 172 insertions, 9 deletions
diff --git a/RecurringType.php b/RecurringType.php index 9b88c44..55b79e2 100644 --- a/RecurringType.php +++ b/RecurringType.php @@ -1,5 +1,6 @@ <?php namespace DHTMLX_Scheduler; +use DateTime; use Exception; class RecurringType { @@ -276,12 +277,10 @@ class RecurringType { } else { $differenceStartDates = SchedulerHelperDate::differenceBetweenDates($intervalStartDateStamp, $recurringStartDateStamp); - $differenceEndDates = SchedulerHelperDate::differenceBetweenDates($intervalEndDateStamp, $recurringEndDateStamp); $dateUnits = SchedulerHelperDate::$DATE_UNITS; //Add years. $recurringInterval["start_date_stamp"] = SchedulerHelperDate::addYears($recurringStartDateStamp, $differenceStartDates[$dateUnits["year"]]); - $recurringInterval["end_date_stamp"] = SchedulerHelperDate::addYears($recurringEndDateStamp, -$differenceEndDates[$dateUnits["year"]]); if ($type == self::REC_TYPE_YEAR) @@ -289,7 +288,6 @@ class RecurringType { //Add months. $recurringInterval["start_date_stamp"] = SchedulerHelperDate::addMonths($recurringInterval["start_date_stamp"], $differenceStartDates[$dateUnits["month"]]); - $recurringInterval["end_date_stamp"] = SchedulerHelperDate::addMonths($recurringInterval["end_date_stamp"], -$differenceEndDates[$dateUnits["month"]]); if ($type == self::REC_TYPE_MONTH) return $recurringInterval; } @@ -301,6 +299,36 @@ class RecurringType { * Get step to recurring day from current day of week in date. * @param $dateStamp * @param $recurringWeekDay + * @param $weekNumber + * @return int + */ + private function _getDayOnWeek($dateStamp, $recurringWeekDay, $weekNumber = 0) + { + $date = new DateTime(); + $date->setTimestamp($dateStamp); + + $m = $date->format('m'); + $y = $date->format('Y'); + $date->setDate($y, $m, 1); + + $weekDays = 0; + if ($weekNumber != 0) { + $weekDays = ($weekNumber - 1) * SchedulerHelperDate::DAYS_IN_WEEK; + } + + $weekDay = SchedulerHelperDate::getDayOfWeek($date->getTimestamp()); + $newDay = $recurringWeekDay + $weekDays - $weekDay + 1; + + $newDay = $newDay <= $weekDays ? $newDay + SchedulerHelperDate::DAYS_IN_WEEK : $newDay; + $date->setDate($y, $m, $newDay); + + return $date->getTimestamp(); + } + + /** + * Get step to recurring day from current day of week in date. + * @param $dateStamp + * @param $recurringWeekDay * @return int */ private function _getRecurringDayStep($dateStamp, $recurringWeekDay) @@ -326,6 +354,9 @@ class RecurringType { //If recurring type has list of days, then get those days. $recurringWeekDays = $this->getWeekDaysListValue(); + $weekDay = $this->getWeekDayValue(); + $weekNumber = $this->getWeekNumberValue(); + if($recurringWeekDays) { $daysCount = count($recurringWeekDays); for($i = 0; $i < $daysCount; $i++) { @@ -336,10 +367,8 @@ class RecurringType { } } //Else if recurring type has day of week and step for it, then get this day. - elseif($this->getWeekDayValue() && $this->getWeekNumberValue()) { - $dayStep = $this->_getRecurringDayStep($dateStamp, $this->getWeekDayValue()); - $dayStep += (SchedulerHelperDate::DAYS_IN_WEEK * ($this->getWeekNumberValue() - 1)); - $stamp = SchedulerHelperDate::addDays($dateStamp, $dayStep); + elseif(isset($weekDay) && $weekNumber) { + $stamp = $this->_getDayOnWeek($dateStamp, $weekDay, $weekNumber); if((!$start || $stamp >= $start) && (!$end|| $stamp < $end)) array_push($recurringDays, $stamp); } diff --git a/SchedulerHelperDate.php b/SchedulerHelperDate.php index 89308cb..b664492 100755 --- a/SchedulerHelperDate.php +++ b/SchedulerHelperDate.php @@ -35,8 +35,10 @@ class SchedulerHelperDate $dateUnits = self::$DATE_UNITS; $differenceArray = array(); - foreach($dateUnits as $dateUnit) - $differenceArray[$dateUnit] = $firstDate->diff($secondDate, true)->format("%".$dateUnit); + foreach($dateUnits as $dateUnit) { + //diff function can't be used here because it gets difference in UTC + $differenceArray[$dateUnit] = abs($firstDate->format($dateUnit) - $secondDate->format($dateUnit)); + } return $differenceArray; } diff --git a/tests/Data_getData/MonthRecurrings2/source.json b/tests/Data_getData/MonthRecurrings2/source.json new file mode 100644 index 0000000..4ff1a21 --- /dev/null +++ b/tests/Data_getData/MonthRecurrings2/source.json @@ -0,0 +1,41 @@ +{ + "settings": { + "start_date": "2016-03-01 00:00:00", + "end_date": "2016-04-01 00:00:00" + }, + "data": [ + { + "start_date": "2016-01-01 06:50:00", + "end_date": "9999-02-01 00:00:00", + "text": "Month Event 1", + "id": "2", + "event_pid": "", + "event_length": "25800", + "rec_pattern": "month_2___", + "rec_type": "month_2___#no", + "!nativeeditor_status": "" + }, + { + "start_date": "2016-01-01 00:00:00", + "end_date": "9999-02-01 00:00:00", + "text": "Month Event 2", + "id": "3", + "event_pid": "", + "event_length": "300", + "rec_pattern": "month_1_1_1_", + "rec_type": "month_1_1_1_#no", + "!nativeeditor_status": "" + }, + { + "start_date": "2016-01-02 08:00:00", + "end_date": "9999-02-01 00:00:00", + "text": "Month Event 3", + "id": "4", + "event_pid": "", + "event_length": "21600", + "rec_pattern": "month_2_0_2_", + "rec_type": "month_2_0_2_#no", + "!nativeeditor_status": "" + } + ] +}
\ No newline at end of file diff --git a/tests/Data_getData/MonthRecurrings2/target.json b/tests/Data_getData/MonthRecurrings2/target.json new file mode 100644 index 0000000..23255db --- /dev/null +++ b/tests/Data_getData/MonthRecurrings2/target.json @@ -0,0 +1,31 @@ +{ + "data": [ + { + "start_date": "2016-03-01 06:50:00", + "end_date": "2016-03-01 14:00:00", + "event_pid": "0", + "text": "Month Event 1", + "id": "2", + "rec_type": "month_2___#no", + "event_length": 25800 + }, + { + "start_date": "2016-03-07 00:00:00", + "end_date": "2016-03-07 00:05:00", + "event_pid": "0", + "text": "Month Event 2", + "id": "3", + "rec_type": "month_1_1_1_#no", + "event_length": 300 + }, + { + "start_date": "2016-03-13 08:00:00", + "end_date": "2016-03-13 14:00:00", + "event_pid": "0", + "text": "Month Event 3", + "id": "4", + "rec_type": "month_2_0_2_#no", + "event_length": 21600 + } + ] +}
\ No newline at end of file diff --git a/tests/Data_getData/YearRecurrings/source.json b/tests/Data_getData/YearRecurrings/source.json index 838696c..67f1923 100644 --- a/tests/Data_getData/YearRecurrings/source.json +++ b/tests/Data_getData/YearRecurrings/source.json @@ -53,6 +53,17 @@ "event_pid": "0", "event_length": "300", "rec_pattern": "year_1_7_4_" + }, + { + "start_date": "2016-01-01 00:00:00", + "end_date": "9999-02-01 00:00:00", + "text": "Year Event 6", + "id": "1", + "event_pid": "", + "event_length": "300", + "rec_pattern": "year_1_1_1_", + "rec_type": "year_1_1_1_#no", + "!nativeeditor_status": "" } ] }
\ No newline at end of file diff --git a/tests/Data_getData/YearRecurrings/target.json b/tests/Data_getData/YearRecurrings/target.json index 131ecd4..36a2802 100644 --- a/tests/Data_getData/YearRecurrings/target.json +++ b/tests/Data_getData/YearRecurrings/target.json @@ -44,6 +44,15 @@ "id": "11", "rec_type": "year_1_7_4_#no", "event_length": 300 + }, + { + "start_date": "2016-01-04 00:00:00", + "end_date": "2016-01-04 00:05:00", + "event_pid": "0", + "text": "Year Event 6", + "id": "1", + "rec_type": "year_1_1_1_#no", + "event_length": 300 } ] }
\ No newline at end of file diff --git a/tests/Data_getData/get_data_from_scheduler.txt b/tests/Data_getData/get_data_from_scheduler.txt new file mode 100644 index 0000000..2f02c74 --- /dev/null +++ b/tests/Data_getData/get_data_from_scheduler.txt @@ -0,0 +1,40 @@ +Get source data: +scheduler.toJSON(); + +Get target data: +function getSerrializedEvents(start, end){ + var realEvs = scheduler.getEvents(start,end); + var evs = []; + + var format = scheduler.templates.xml_format; + + for(var i = 0; i < realEvs.length; i++){ + var realEv = realEvs[i]; + var ev = {}; + for(var j in realEv){ + if(j[0] == "_" || j[0] == "!") continue; + if(typeof realEv[j] == "object"){ + if(realEv[j] instanceof Date) + ev[j] = format(realEv[j]); + } + else { + ev[j] = realEv[j]; + if(j == "id"){ + ev[j] += ""; + } + } + } + var sharpIndex = ev.id.indexOf("#"); + if(sharpIndex != -1) { + ev.id = ev.id.substring(0, sharpIndex); + var recEv = scheduler.getEvent(ev.id); + ev.rec_type = recEv.rec_type; + ev.event_length = recEv.event_length; + ev.event_pid = "0"; + } + + evs.push(ev); + } + + return JSON.stringify(evs); +}
\ No newline at end of file |