Skip to content

Commit 92fcf9b

Browse files
committed
Fix JS Date --> Oracle Date conversion to use UTC methods.
Update unit test, since TIMESTAMPs do store time info. Update README to explain how DATEs and TIMESTAMPs are handled
1 parent eb749cb commit 92fcf9b

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,43 @@ if (!connection.isConnected()) {
154154
}
155155
```
156156
157+
### Dates
158+
For DATE and TIMESTAMP types, the driver uses the UTC methods from the Javascript Date object. This means the DATE
159+
value stored will match the value of `new Date().toISOString()` on your client machine. Consider this example
160+
for a client machine in "GMT-0700":
157161
158-
# Limitations
162+
Table schema:
159163
160-
* Currently no support for column type "Timestamp With Timezone" (Issue #67)
161-
* Currently no native support for connection pooling (forthcoming; use generic-pool for now.)
162-
* Use getUTC... javascript functions when dealing with dates (See Github discussion on 44b872f)
164+
```sql
165+
CREATE TABLE date_test (mydate DATE)
166+
```
167+
168+
Javascript code:
163169
170+
```javascript
171+
...
172+
var date = new Date(2013, 11, 24, 18, 0, 1); // Client timezone dependent
173+
console.log(date.toString()); // Tue Dec 24 2013 18:00:01 GMT-0700 (MST)
174+
console.log(date.toISOString()); // 2013-12-25T01:00:01.000Z
175+
176+
connection.execute(
177+
"INSERT INTO date_test (mydate) VALUES (:1) " +
178+
"RETURNING mydate, to_char(mydate, 'YYYY-MM-DD HH24:MI:SS') INTO :2, :3",
179+
[date, new oracle.OutParam(oracle.OCCIDATE), new oracle.OutParam(oracle.OCCISTRING)],
180+
function(err, results) {
181+
console.log(results.returnParam.toString()); // Tue Dec 24 2013 18:00:01 GMT-0700 (MST)
182+
console.log(results.returnParam1); // 2013-12-25 01:00:01
183+
}
184+
);
185+
...
186+
```
187+
188+
# Limitations/Caveats
189+
190+
* Currently no native support for connection pooling (forthcoming; use generic-pool for now.)
191+
* Currently no support for column type "Timestamp With Timezone" (Issue #67)
192+
* While the Oracle TIMESTAMP type provides fractional seconds up to 9 digits (nanoseconds), this will be rounded
193+
to the nearest millisecond when converted to a Javascript date (a _data loss_).
164194
165195
# Development
166196
* Clone the source repo

src/executeBaton.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ double CallDateMethod(v8::Local<v8::Date> date, const char* methodName) {
6161
}
6262

6363
oracle::occi::Date* V8DateToOcciDate(oracle::occi::Environment* env, v8::Local<v8::Date> val) {
64-
int year = CallDateMethod(val, "getFullYear");
65-
int month = CallDateMethod(val, "getMonth") + 1;
66-
int day = CallDateMethod(val, "getDate");
67-
int hours = CallDateMethod(val, "getHours");
68-
int minutes = CallDateMethod(val, "getMinutes");
69-
int seconds = CallDateMethod(val, "getSeconds");
64+
int year = CallDateMethod(val, "getUTCFullYear");
65+
int month = CallDateMethod(val, "getUTCMonth") + 1;
66+
int day = CallDateMethod(val, "getUTCDate");
67+
int hours = CallDateMethod(val, "getUTCHours");
68+
int minutes = CallDateMethod(val, "getUTCMinutes");
69+
int seconds = CallDateMethod(val, "getUTCSeconds");
70+
//TODO: int milliseconds = CallDateMethod(val, "getUTCMilliseconds");
7071
oracle::occi::Date* d = new oracle::occi::Date(env, year, month, day, hours, minutes, seconds);
7172
return d;
7273
}

test/integration.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ exports['IntegrationTest'] = nodeunit.testCase({
9494

9595
"datatypes": function(test) {
9696
var self = this;
97-
var date1 = new Date(2011, 10, 30, 1, 2, 3);
97+
var date1 = new Date(2011, 10, 30, 1, 2, 3);
9898
var date2 = new Date(2011, 11, 1, 1, 2, 3);
9999
self.connection.execute(
100100
"INSERT INTO datatype_test "
@@ -126,8 +126,7 @@ exports['IntegrationTest'] = nodeunit.testCase({
126126
test.equal(results[0]['TNCHAR'], "tnchar value ");
127127
test.equal(results[0]['TNUMBER'], 42.5);
128128
test.equal(results[0]['TDATE'].getTime(), date1.getTime());
129-
var date2Timestamp = new Date(2011, 11, 1, 0, 0, 0); // same as date2 but without time
130-
test.equal(results[0]['TTIMESTAMP'].getTime(), date2Timestamp.getTime());
129+
test.equal(results[0]['TTIMESTAMP'].getTime(), date2.getTime());
131130
test.equal(results[0]['TCLOB'], "tclob value");
132131
test.equal(results[0]['TNCLOB'], "tnclob value");
133132
// todo: test.equal(results[0]['TBLOB'], null);

0 commit comments

Comments
 (0)