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.
This commit is contained in:
Andrew Ruthven 2021-09-19 00:01:01 +12:00
parent 1e5c1fd1f3
commit 326afc8cfc

View File

@ -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 <<<EOTXT
@ -33,6 +42,10 @@ class RRuleTest {
$this->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;
}
}