Example #1
0
    public function testInistantiation()
    {
        $db = $this->connectionFactory->get('RW');
        $time = $db->query(new Query(<<<SQL
            SELECT
                NOW() AS dt,
                CAST( NOW() AS TIMESTAMP WITHOUT TIME ZONE ) AS dt_no_zone,
                EXTRACT('epoch' FROM now())::int AS unix_timestamp
SQL
))->fetch(Result::FETCH_SINGLE);
        $obj1 = new DateTime($time['dt']);
        $obj2 = new DateTime($time['dt_no_zone']);
        // Gotcha (well, it got me!) - DateTime cannot be instantiated
        // with a unix timestamp unless it is prefixed with an @
        $obj3 = new DateTime('@' . $time['unix_timestamp']);
        // right pad our postgres timestamps
        $time['dt_no_zone'] = str_pad($time['dt_no_zone'], 26, '0', \STR_PAD_RIGHT);
        $time['dt_no_zone'] = rtrim($time['dt_no_zone'], '0');
        $this->assertSame($obj1->parse($db), "'{$time['dt_no_zone']}'");
        $this->assertSame((string) $obj3->getTimestamp(), (string) $time['unix_timestamp']);
    }