Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Problem with date storage #108

Closed
wants to merge 6 commits into from

2 participants

@markgardner

I found an issue that the date stored in the database wasn't being stored as UTC. I've changed all date methods to reference the UTC value. It might be worth mentioning that getTime/setTime are UTC relative methods while all else are timezone relative unless otherwise specified.

Since this might break everyone that has used tedious before it might be beneficial to make this type of conversion to be a configuration setting. What do you think?

@pekim
Owner

Would this explain issue #95?

@markgardner

Yeah I think so. I was seeing similar problems on my end.

Btw just noticed the tests failed I'll get on fixing them

@markgardner

Glade you had some tests around this. When looking at the test I realized that I didn't account for the difference in tds and js epoch. Everything should be working now.

@markgardner

Also I noticed that when datetime is being written that it was using a UInt for days when it should just use Int. I believe pull request #106 fixed part of this. Looks like small date should both be UInt though.

Mark Gardner Merge pull request #1 from markgardner/varchar-param
zero length varchar params don't work unless a length greater than zero ...
28b34e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 3, 2013
  1. @markgardner
Commits on Jul 4, 2013
  1. Changing EPOCH to be UTC and days to be written as Int32 as per spec …

    Mark Gardner authored
    …(2.2.5.5.1.8 Date/Times).
  2. Adding UTC marker to datetime test

    Mark Gardner authored
Commits on Jul 6, 2013
  1. @markgardner
Commits on Dec 14, 2013
  1. Merge pull request #1 from markgardner/varchar-param

    Mark Gardner authored
    zero length varchar params don't work unless a length greater than zero ...
This page is out of date. Refresh to see the latest.
View
16 src/data-type.coffee
@@ -1,6 +1,6 @@
guidParser = require('./guid-parser')
NULL = (1 << 16) - 1
-EPOCH_DATE = new Date(1900, 0, 1)
+EPOCH_DATE = new Date('January 1, 1900 00:00:00 GMT')
MAX = (1 << 16) - 1
TYPE =
@@ -129,15 +129,15 @@ TYPE =
days = (parameter.value.getTime() - EPOCH_DATE.getTime()) / (1000 * 60 * 60 * 24)
days = Math.floor(days)
- seconds = parameter.value.getHours() * 60 * 60
- seconds += parameter.value.getMinutes() * 60
- seconds += parameter.value.getSeconds()
- milliseconds = (seconds * 1000) + parameter.value.getMilliseconds()
+ seconds = parameter.value.getUTCHours() * 60 * 60
+ seconds += parameter.value.getUTCMinutes() * 60
+ seconds += parameter.value.getUTCSeconds()
+ milliseconds = (seconds * 1000) + parameter.value.getUTCMilliseconds()
threeHundredthsOfSecond = milliseconds / (3 + (1 / 3))
threeHundredthsOfSecond = Math.floor(threeHundredthsOfSecond)
buffer.writeUInt8(8)
- buffer.writeUInt32LE(days)
+ buffer.writeInt32LE(days)
buffer.writeUInt32LE(threeHundredthsOfSecond)
else
buffer.writeUInt8(0)
@@ -295,7 +295,7 @@ TYPE =
length = @.maximumLength
if length <= @maximumLength
- "varchar(#{length})"
+ "varchar(#{Math.max(length,1)})"
else
"varchar(max)"
writeParameterData: (buffer, parameter) ->
@@ -355,7 +355,7 @@ TYPE =
length = @maximumLength
if length <= @maximumLength
- "nvarchar(#{length})"
+ "nvarchar(#{Math.max(length,1)})"
else
"nvarchar(max)"
writeParameterData: (buffer, parameter) ->
View
11 src/value-parser.coffee
@@ -7,6 +7,9 @@ NULL = (1 << 16) - 1
MAX = (1 << 16) - 1
THREE_AND_A_THIRD = 3 + (1 / 3)
MONEY_DIVISOR = 10000
+DAYS_TO_MILLISECOND = 86400000
+MINUTES_TO_MILLISECOND = 60000
+TDS_EPOCH_TO_JS_EPOCH = -2208988800000
PLP_NULL = new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
UNKNOWN_PLP_LEN = new Buffer([0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
@@ -262,9 +265,7 @@ readSmallDateTime = (buffer) ->
days = buffer.readUInt16LE()
minutes = buffer.readUInt16LE()
- value = new Date(1900, 0, 1)
- value.setDate(value.getDate() + days)
- value.setMinutes(value.getMinutes() + minutes)
+ value = new Date(TDS_EPOCH_TO_JS_EPOCH + (days * DAYS_TO_MILLISECOND) + (minutes * MINUTES_TO_MILLISECOND));
value
readDateTime = (buffer) ->
@@ -272,9 +273,7 @@ readDateTime = (buffer) ->
threeHundredthsOfSecond = buffer.readUInt32LE()
milliseconds = threeHundredthsOfSecond * THREE_AND_A_THIRD
- value = new Date(1900, 0, 1)
- value.setDate(value.getDate() + days)
- value.setMilliseconds(value.getMilliseconds() + milliseconds)
+ value = new Date(TDS_EPOCH_TO_JS_EPOCH + milliseconds + (days * DAYS_TO_MILLISECOND))
value
module.exports = parse
View
2  test/unit/token/row-token-parser-test.coffee
@@ -493,7 +493,7 @@ module.exports.datetime = (test) ->
#console.log(token)
test.strictEqual(token.columns.length, 1)
- test.strictEqual(token.columns[0].value.getTime(), new Date('January 3, 1900 00:00:45').getTime())
+ test.strictEqual(token.columns[0].value.getTime(), new Date('January 3, 1900 00:00:45 GMT').getTime())
test.done()
Something went wrong with that request. Please try again.