From 326afc8cfcc5b9d83d75f170fbd826d12d2f69c7 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Sun, 19 Sep 2021 00:01:01 +1200 Subject: [PATCH] Teach how to set timezone when running standalone It is useful for debugging to run this command standalone, but in that case it typically has to set the timezone for the database to ensure that the times in the results are the same as the PHP times. --- testing/test-RRULE-v2.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/testing/test-RRULE-v2.php b/testing/test-RRULE-v2.php index d8ab2a76..756faf43 100755 --- a/testing/test-RRULE-v2.php +++ b/testing/test-RRULE-v2.php @@ -12,6 +12,15 @@ $c->dbg = array(); require_once("RRule.php"); require_once('AwlQuery.php'); +# Check database timezone +$qry = new AwlQuery(); +$qry->QDo("SHOW TIMEZONE"); +$row = $qry->Fetch(); + +$initial_tz = $row->TimeZone; + +echo "Database Time Zone: $initial_tz\n"; + @header("Content-Type: text/plain; charset=UTF-8"); echo <<recur = $recur; $this->result_description = $result_description; $this->result_limit = 30; + + if ( preg_match('/^.*? (.*)$/', $this->dtstart, $matches) ) { + $this->tz = $matches[1]; + } } function PHPTest() { @@ -50,10 +63,25 @@ class RRuleTest { } function SQLTest() { + $qry = new AwlQuery; + global $initial_tz; + + # This is a hack to get the output time to be in "local time" so that we + # get the same time out of the PHP code to allow the diff between the + # results to work. + $changed_tz = 0; + if (isset($this->tz) && $this->tz != $initial_tz) { + echo "Setting database time zone to: $this->tz\n"; + $qry->QDo("SET TIMEZONE TO :tz", array( ':tz' => $this->tz )); + $changed_tz = 1; + } + $result = ''; $sql = "SELECT event_instances::timestamp AS event_date FROM event_instances(:dtstart,:rrule) LIMIT ".$this->result_limit; $start = microtime(true); - $qry = new AwlQuery($sql, array( ':dtstart' => $this->dtstart, ':rrule' => $this->recur) ); + $qry->SetSql($sql); + $qry->Bind(array( ':dtstart' => $this->dtstart, ':rrule' => $this->recur) ); + // printf( "%s\n", $qry->querystring); if ( $qry->Exec("test") && $qry->rows() > 0 ) { $i = 0; @@ -63,6 +91,11 @@ class RRuleTest { } } $this->SQL_time = microtime(true) - $start; + + if ($changed_tz) { + $qry->QDo("SET TIMEZONE TO '$initial_tz'"); + } + return $result; } }