Skip to content

Commit

Permalink
decode timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Feb 10, 2019
1 parent ec5d7ac commit 20c62de
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 0 additions & 2 deletions connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ export class Connection {
if (responseCode !== 0) {
throw new Error(`Unexpected auth response code: ${responseCode}.`);
}

console.log('read auth ok!');
}

private async _authCleartext() {
Expand Down
6 changes: 4 additions & 2 deletions decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ function decodeDate(dateStr: string): null | Date {
* Returns offset in miliseconds.
*/
function decodeTimezoneOffset(dateStr: string): null | number {
const matches = TIMEZONE_RE.exec(dateStr);
// get rid of date part as TIMEZONE_RE would match '-MM` part
const timeStr = dateStr.split(' ')[1];
const matches = TIMEZONE_RE.exec(timeStr);

if (!matches) {
return null;
Expand Down Expand Up @@ -105,7 +107,6 @@ function decodeDatetime(dateStr: string): null | number | Date {
let date: Date;

const offset = decodeTimezoneOffset(dateStr);

if (offset === null) {
date = new Date(year, month, day, hour, minute, second, ms);
} else {
Expand All @@ -119,6 +120,7 @@ function decodeDatetime(dateStr: string): null | number | Date {
// century `Date`'s compatibility for millenium bug
// would set it as 19XX
date.setUTCFullYear(year);
return date;
}

function decodeBinary() {
Expand Down
6 changes: 1 addition & 5 deletions query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RowDescription, Column, Format } from "./connection.ts";
import { Connection } from "./connection.ts";
import { encode, EncodedArg } from "./encode.ts";

import { decode } from "./decode.ts";

export interface QueryConfig {
Expand Down Expand Up @@ -70,13 +71,8 @@ export class Query {

constructor(public connection: Connection, config: QueryConfig) {
this.text = config.text;
<<<<<<< HEAD
this.args = this._prepareArgs(config);
this.result = new QueryResult();
=======
this.args = this.prepareArgs(config.args);
this.result = new QueryResult(this);
>>>>>>> first pass at decoding data rows
}

private _prepareArgs(config: QueryConfig): EncodedArg[] {
Expand Down
20 changes: 13 additions & 7 deletions tests/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ test(async function beforeEach() {

await client.query("DROP TABLE IF EXISTS ids;");
await client.query("CREATE TABLE ids(id integer);");
await client.query("INSERT INTO ids(id) values(1);");
await client.query("INSERT INTO ids(id) values(2);");
await client.query("INSERT INTO ids(id) VALUES(1);");
await client.query("INSERT INTO ids(id) VALUES(2);");

await client.query("DROP TABLE IF EXISTS timestamps;");
await client.query("CREATE TABLE timestamps(dt timestamp);");
await client.query(`INSERT INTO timestamps(dt) values("2019-02-10T10:20:30.005+04:30");`);
await client.query("CREATE TABLE timestamps(dt timestamptz);");
await client.query(`INSERT INTO timestamps(dt) VALUES('2019-02-10T10:30:40.005+04:30');`);
});


Expand Down Expand Up @@ -61,10 +61,16 @@ test(async function nativeType() {
const client = await getTestClient();

const result = await client.query("SELECT * FROM timestamps;");
const row = result.rows[0];

console.log(result);
await client.query('INSERT INTO timestamps(dt) values($1);', new Date());

const expectedDate = Date.UTC(2019, 1, 10, 6, 0, 40, 5);

assertEqual(
row[0].toUTCString(),
new Date(expectedDate).toUTCString()
)

await client.query('INSERT INTO timestamps(dt) values($1);', new Date());
});

test(async function tearDown() {
Expand Down

0 comments on commit 20c62de

Please sign in to comment.