1#!/usr/bin/php
2<?php
3
4if ( @file_exists('../../awl/inc/AWLUtilities.php') ) {
5  set_include_path('../inc:../htdocs:../../awl/inc');
6}
7else {
8  set_include_path('../inc:../htdocs:/usr/share/awl/inc');
9}
10require_once("always.php");
11$c->dbg = array();
12require_once("RRule.php");
13require_once('AwlQuery.php');
14
15@header("Content-Type: text/plain; charset=UTF-8");
16
17echo <<<EOTXT
18Testing the RRule v2 Library
19
20EOTXT;
21
22class RRuleTest {
23  var $dtstart;
24  var $recur;
25  var $description;
26  var $result_description;
27  var $PHP_time;
28  var $SQL_time;
29
30  function __construct( $description, $start, $recur, $result_description = null ) {
31    $this->description = $description;
32    $this->dtstart = $start;
33    $this->recur = $recur;
34    $this->result_description = $result_description;
35    $this->result_limit = 30;
36  }
37
38  function PHPTest() {
39    $result = '';
40    $start = microtime(true);
41    $rule = new RepeatRule( $this->dtstart, $this->recur );
42    $i = 0;
43    while( $date = $rule->next() ) {
44      if ( ($i++ % 4) == 0 ) $result .= "\n";
45      $result .= "   " . $date->format('Y-m-d H:i:s');
46      if ( $i >= $this->result_limit ) break;
47    }
48    $this->PHP_time = microtime(true) - $start;
49    return $result;
50  }
51
52  function SQLTest() {
53    $result = '';
54    $sql = "SELECT event_instances::timestamp AS event_date FROM event_instances(:dtstart,:rrule) LIMIT ".$this->result_limit;
55    $start = microtime(true);
56    $qry = new AwlQuery($sql, array( ':dtstart' => $this->dtstart, ':rrule' => $this->recur) );
57    // printf( "%s\n", $qry->querystring);
58    if ( $qry->Exec("test") && $qry->rows() > 0 ) {
59      $i = 0;
60      while( $row = $qry->Fetch() ) {
61        if ( ($i++ % 4) == 0 ) $result .= "\n";
62        $result .= "   " . $row->event_date;
63      }
64    }
65    $this->SQL_time = microtime(true) - $start;
66    return $result;
67  }
68}
69
70
71$tests = array(
72    new RRuleTest( "Daily for 7 days", "20061103T073000", "RRULE:FREQ=DAILY;COUNT=7" )
73  , new RRuleTest( "Weekly for 26 weeks", "20061102T100000", "RRULE:FREQ=WEEKLY;COUNT=26;INTERVAL=1;BYDAY=TH" )
74  , new RRuleTest( "Fortnightly for 4 events", "20061103T160000", "RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4" )
75  , new RRuleTest( "Fortnightly for 28 events", "20061103T160000", "RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=20071122T235900" )
76  , new RRuleTest( "3/wk for 5 weeks", "20081101T160000", "RRULE:FREQ=WEEKLY;COUNT=15;INTERVAL=1;BYDAY=MO,WE,FR" )
77  , new RRuleTest( "Monthly forever", "20061104T073000", "RRULE:FREQ=MONTHLY" )
78  , new RRuleTest( "Monthly, on the 1st monday, 2nd wednesday, 3rd friday and last sunday, forever", "20061117T073000", "RRULE:FREQ=MONTHLY;BYDAY=1MO,2WE,3FR,-1SU" )
79  , new RRuleTest( "The working days of each month", "20061107T113000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;UNTIL=20070101T000000" )
80  , new RRuleTest( "The last working day of each month", "20061107T113000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1;COUNT=30" )
81  , new RRuleTest( "Every working day", "20081020T103000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;COUNT=30" )
82  , new RRuleTest( "Every working day", "20081020T110000", "RRULE:FREQ=DAILY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;COUNT=30" )
83  , new RRuleTest( "The last day of each month", "20110831", "RRULE:FREQ=MONTHLY;BYMONTHDAY=-1" )
84  , new RRuleTest( "1st Tuesday, 2nd Wednesday, 3rd Thursday & 4th Friday, every March, June, September, October and December (SQL is wrong)", "20081001T133000", "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=1TU,2WE,3TH,4FR;BYMONTH=3,6,9,10,12" )
85  , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=TU,FR;COUNT=30" )
86  , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,FR;COUNT=30" )
87  , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=DAILY;INTERVAL=1;BYDAY=TU,FR;COUNT=30" )
88  , new RRuleTest( "Time zone 1", "19700315T030000", "FREQ=YEARLY;INTERVAL=1;BYDAY=3SU;BYMONTH=3" )
89  , new RRuleTest( "Time zone 2", "19700927T020000", "FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=9" )
90  , new RRuleTest( "Time zone 3", "19810329T030000", "FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU" )
91  , new RRuleTest( "Time zone 4", "20000404T010000", "FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;COUNT=15" )
92  , new RRuleTest( "Six Working Days", "20110905", "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR;COUNT=6" )
93  , new RRuleTest( "Six Working Days", "20110905", "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;COUNT=6" )
94);
95
96foreach( $tests AS $k => $test ) {
97  echo "=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=\n";
98  echo "$test->dtstart - $test->recur\n";
99  echo "$test->description\n";
100  $php_result = $test->PHPTest();
101  $sql_result = $test->SQLTest();
102  if ( $php_result == $sql_result ) {
103    printf( 'PHP & SQL results are identical (-: P: %6.4lf  & S: %6.4lf'."\n", $test->PHP_time, $test->SQL_time);
104  }
105  else {
106    printf( 'PHP & SQL results differ :-( P: %6.4lf  & S: %6.4lf'."\n", $test->PHP_time, $test->SQL_time);
107    echo "PHP Result:\n$php_result\n\n";
108    echo "SQL Result:\n$sql_result\n\n";  // Still under development
109  }
110}
111
112
113exit(0);
114