Skip to content

Commit

Permalink
PHP/MySQL should now both use system timezone setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mythril committed Jun 7, 2014
1 parent c9944f7 commit 36c585e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
6 changes: 5 additions & 1 deletion cli
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ set_error_handler(function ($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

Container::registerSingleton('DB');
Container::registerSingleton('DB', function () {
return new DB(new DateTimeZone(trim(file_get_contents('/etc/timezone'))));
});

date_default_timezone_set(trim(file_get_contents('/etc/timezone')));

Container::registerSingleton('Environment\Arguments', function () use ($argv) {
return new Environment\Arguments($argv);
Expand Down
2 changes: 1 addition & 1 deletion includes/BillLogCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function save($purchaseId) {

public static function test() {
require_once __DIR__ . '/autoload.php';
$b = new BillLogCSV(new DB());
$b = new BillLogCSV(Container::dispense('DB'));
echo "CSV Saved as: ", $b->save(19), "\n";
}
}
Expand Down
6 changes: 5 additions & 1 deletion includes/DB.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

class DB extends PDO {
public function __construct() {
public function __construct(DateTimeZone $tz) {
parent::__construct(
'mysql:dbname=skyhook;host=127.0.0.1',
'skyhook'
);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$seconds = $tz->getOffset(new DateTime("now", new DateTimeZone('UTC')));
$hrs = round($seconds / 60 / 60);
$mins = round((abs($seconds) - (abs($hrs) * 60 * 60)) / 60);
$this->exec("SET time_zone = '{$hrs}:{$mins}';");
}
}

Expand Down
9 changes: 0 additions & 9 deletions includes/TransactionCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,3 @@ public function save($from = 0) {
}


/*
Example usage:
*/
/*
$t = new TransactionCSV(new DB(include "cfg.php"));
echo "CSV Saved as: ", $t->save(), "\n";
//*/


11 changes: 6 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
ini_set('display_errors', 'on');
error_reporting(E_ALL);

if (trim(file_get_contents('/etc/timezone')) !== '') {
date_default_timezone_set(trim(file_get_contents('/etc/timezone')));
}

Localization::init();

set_error_handler(function ($errno, $errstr, $errfile, $errline ) {
Expand Down Expand Up @@ -47,6 +43,7 @@
);
});


// Register Environment Singletons
foreach (array(
'Get' => &$_GET,
Expand Down Expand Up @@ -84,7 +81,11 @@
});
}

Container::registerSingleton('DB');
Container::registerSingleton('DB', function () {
return new DB(new DateTimeZone(trim(file_get_contents('/etc/timezone'))));
});

date_default_timezone_set(trim(file_get_contents('/etc/timezone')));

$router = Container::dispense("Router");
$server = Container::dispense('Environment\Server');
Expand Down
4 changes: 3 additions & 1 deletion update.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

require_once __DIR__ . '/includes/autoload.php';

$db = new DB();
$db = new DB(
new DateTimeZone(trim(file_get_contents('/etc/timezone')))
);

$stmt = $db->query("SHOW COLUMNS FROM `purchases`;");

Expand Down

0 comments on commit 36c585e

Please sign in to comment.