From 36c585e7a8fddc388f495285ab3003f3376eef9b Mon Sep 17 00:00:00 2001 From: Lucas Green Date: Fri, 6 Jun 2014 18:07:08 -0700 Subject: [PATCH] PHP/MySQL should now both use system timezone setting --- cli | 6 +++++- includes/BillLogCSV.php | 2 +- includes/DB.php | 6 +++++- includes/TransactionCSV.php | 9 --------- index.php | 11 ++++++----- update.php | 4 +++- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cli b/cli index e2507dd..3a03e10 100755 --- a/cli +++ b/cli @@ -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); diff --git a/includes/BillLogCSV.php b/includes/BillLogCSV.php index 5d95c83..ecce80c 100644 --- a/includes/BillLogCSV.php +++ b/includes/BillLogCSV.php @@ -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"; } } diff --git a/includes/DB.php b/includes/DB.php index 8ba443c..75d0ec8 100644 --- a/includes/DB.php +++ b/includes/DB.php @@ -1,12 +1,16 @@ 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}';"); } } diff --git a/includes/TransactionCSV.php b/includes/TransactionCSV.php index beb438c..b84c30e 100644 --- a/includes/TransactionCSV.php +++ b/includes/TransactionCSV.php @@ -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"; -//*/ - - diff --git a/index.php b/index.php index 76844f4..9cd7eee 100644 --- a/index.php +++ b/index.php @@ -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 ) { @@ -47,6 +43,7 @@ ); }); + // Register Environment Singletons foreach (array( 'Get' => &$_GET, @@ -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'); diff --git a/update.php b/update.php index 7411dde..55d0ab2 100644 --- a/update.php +++ b/update.php @@ -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`;");