Skip to content

Commit

Permalink
move JS binding for time to use epoch 01/01/1970 rather than 1900
Browse files Browse the repository at this point in the history
  • Loading branch information
TimelordUK committed Dec 20, 2022
1 parent 04a3f44 commit 2c31575
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -77,5 +77,5 @@ runtest-linux
.gitignore
runtest-win.exe
runtest-macos
/.idea/dictionaries/Stephen.xml
/mochawesome-report/
.idea/
4 changes: 2 additions & 2 deletions src/OdbcStatement.cpp
Expand Up @@ -1146,7 +1146,7 @@ namespace mssql
}

SQL_SS_TIMESTAMPOFFSET_STRUCT datetime = {};
datetime.year = SQL_SERVER_DEFAULT_YEAR;
datetime.year = JS_DEFAULT_YEAR;
datetime.month = SQL_SERVER_DEFAULT_MONTH;
datetime.day = SQL_SERVER_DEFAULT_DAY;
datetime.hour = time.hour;
Expand Down Expand Up @@ -1422,7 +1422,7 @@ namespace mssql
}

SQL_SS_TIMESTAMPOFFSET_STRUCT datetime = {};
datetime.year = SQL_SERVER_DEFAULT_YEAR;
datetime.year = JS_DEFAULT_YEAR;
datetime.month = SQL_SERVER_DEFAULT_MONTH;
datetime.day = SQL_SERVER_DEFAULT_DAY;
datetime.hour = time.hour;
Expand Down
2 changes: 2 additions & 0 deletions src/stdafx.h
Expand Up @@ -75,4 +75,6 @@ const int SQL_SERVER_DEFAULT_YEAR = 1900;
const int SQL_SERVER_DEFAULT_MONTH = 1; // JS months are 0 based, SQL Server months are 1 based
const int SQL_SERVER_DEFAULT_DAY = 1;

const int JS_DEFAULT_YEAR = 1970;

#define ErrorIf(x) if (x) goto Error;
16 changes: 8 additions & 8 deletions test/bulk.js
Expand Up @@ -69,56 +69,56 @@ describe('bulk', function () {
it('connection: use tableMgr bulk insert vector non UTC based time(7) with time col - no MS', async function handler () {
t3Size = 10
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate)
})

it('pool: use tableMgr bulk insert vector non UTC based time with time(7) col - no MS', async function handler () {
t3Size = 10
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate)
})

it('connection: use tableMgr bulk insert vector non UTC based time(0) with time col - no MS', async function handler () {
t3Size = 10
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate, 0)
})

it('pool: use tableMgr bulk insert vector non UTC based time with time(0) col - no MS', async function handler () {
t3Size = 10
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate, 0)
})

it('connection: use tableMgr bulk insert vector non UTC based time with time(7) col - with MS', async function handler () {
t3Size = 10
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSSMS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate)
})

it('pool: use tableMgr bulk insert vector non UTC based time with time(7) col - with MS', async function handler () {
t3Size = 10
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSSMS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate)
})

it('connection: use tableMgr bulk insert single non UTC based time with time(7) col - no MS', async function handler () {
t3Size = 1
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate)
})

it('pool: use tableMgr bulk insert single non UTC based time with time(7) col - no MS', async function handler () {
t3Size = 1
const timeHelper = env.timeHelper
const testDate = timeHelper.getUTCTime1900HHMMSS()
const testDate = timeHelper.getUTCTime1970HHMMSS()
await t3(env.theConnection, testDate)
})

Expand Down
114 changes: 22 additions & 92 deletions test/datatypes.js
Expand Up @@ -107,16 +107,14 @@ describe('datatypes', function () {
await proxy.verifyData_Datetime(rowWithNullData, jsDateExpected, testname)
}) // end of it(

it('test 003_a - insert valid data into time(7) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 16
const testdatetimescale = 7
async function timeTest (testdatetimescale) {
const testcolumntype = ` time(${testdatetimescale})`
// var testcolumnclienttype = 'date'
const testcolumnname = 'col2'
const testdata1 = null
const rowWithNullData = 1
// test date = <default date> 12:10:05.1234567
const year = 1900
const year = 1970
const month = 1
const day = 1
const hour = 12
Expand All @@ -127,45 +125,24 @@ describe('datatypes', function () {
const jsDateExpected = new Date(year, month - 1, day, hour - env.commonTestFns.getTimezoneOffsetInHours(year, month, day), minute, second, nanosecond)
const testdata2Expected = '12:10:05.1234567'
const testdata2TsqlInsert = `'${testdata2Expected}'`

const proxy = env.makeTestFnProxy(tablename, testcolumnname)
await proxy.create(testcolumntype)
await proxy.insert(testdata1)
await proxy.insert(testdata2TsqlInsert)
await proxy.verifyData_Datetime(rowWithNullData, jsDateExpected, testname)
}

it('test 003_a - insert valid data into time(7) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 16
await timeTest(7)
}) // end of it()

it('test 003_b - insert valid data into time(0) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 16
const testdatetimescale = 0
const testcolumntype = ` time(${testdatetimescale})`
// var testcolumnclienttype = 'date'
const testcolumnname = 'col2'
const testdata1 = null
const rowWithNullData = 1
// test date = <default date> 12:10:05
const year = 1900
const month = 1
const day = 1
const hour = 12
const minute = 10
const second = 5
const nanosecond = 0
// Month in JS is 0-based, so expected will be month minus 1
const jsDateExpected = new Date(year, month - 1, day, hour - env.commonTestFns.getTimezoneOffsetInHours(year, month, day), minute, second, nanosecond)
const testdata2Expected = '12:10:05.1234567'
const testdata2TsqlInsert = `'${testdata2Expected}'`

const proxy = env.makeTestFnProxy(tablename, testcolumnname)
await proxy.create(testcolumntype)
await proxy.insert(testdata1)
await proxy.insert(testdata2TsqlInsert)
await proxy.verifyData_Datetime(rowWithNullData, jsDateExpected, testname)
await timeTest(0)
}) // end of it()

it('test 004_a - insert valid data into datetime2(7) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 27
const testdatetimescale = 7
async function t04 (testdatetimescale, second) {
const testcolumntype = ` datetime2(${testdatetimescale})`
// var testcolumnclienttype = 'date'
const testcolumnname = 'col2'
Expand All @@ -177,7 +154,6 @@ describe('datatypes', function () {
const day = 10
const hour = 10
const minute = 12
const second = 59.1234567
const nanosecond = 0
// Month in JS is 0-based, so expected will be month minus 1
const jsDateExpected = new Date(year, month - 1, day, hour - env.commonTestFns.getTimezoneOffsetInHours(year, month, day), minute, second, nanosecond)
Expand All @@ -189,39 +165,19 @@ describe('datatypes', function () {
await proxy.insert(testdata1)
await proxy.insert(testdata2TsqlInsert)
await proxy.verifyData_Datetime(rowWithNullData, jsDateExpected, testname)
}

it('test 004_a - insert valid data into datetime2(7) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 27
await t04(7, 59.1234567)
})

it('test 004_b - insert valid data into datetime2(0) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 19
const testdatetimescale = 0
const testcolumntype = ` datetime2(${testdatetimescale})`
// var testcolumnclienttype = 'date'
const testcolumnname = 'col2'
const testdata1 = null
const rowWithNullData = 1
// test date = 2001-04-10 10:12:59.1234567
const year = 2001
const month = 4
const day = 10
const hour = 10
const minute = 12
const second = 59
const nanosecond = 0
// Month in JS is 0-based, so expected will be month minus 1
const jsDateExpected = new Date(year, month - 1, day, hour - env.commonTestFns.getTimezoneOffsetInHours(year, month, day), minute, second, nanosecond)
const testdata2Expected = '2001-04-10 10:12:59.1234567'
const testdata2TsqlInsert = `'${testdata2Expected}'`

const proxy = env.makeTestFnProxy(tablename, testcolumnname)
await proxy.create(testcolumntype)
await proxy.insert(testdata1)
await proxy.insert(testdata2TsqlInsert)
await proxy.verifyData_Datetime(rowWithNullData, jsDateExpected, testname)
await t04(0, 59)
}) // end of it()

it('test 005_a - insert valid data into datetimeoffset(7) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 34
const testdatetimescale = 7
async function t05 (testdatetimescale, second) {
const testcolumntype = ` datetimeoffset(${testdatetimescale})`
// var testcolumnclienttype = 'date'
const testcolumnname = 'col2'
Expand All @@ -233,7 +189,6 @@ describe('datatypes', function () {
const day = 10
const hour = 10
const minute = 12
const second = 59.1234567
const nanosecond = 0
const offsetHours = 13
const offsetMinutes = 30
Expand All @@ -252,41 +207,16 @@ describe('datatypes', function () {
await proxy.insert(testdata1)
await proxy.insert(testdata2TsqlInsert)
await proxy.verifyData_Datetime(rowWithNullData, jsDateExpected, testname)
}

it('test 005_a - insert valid data into datetimeoffset(7) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 34
await t05(7, 59.1234567)
}) // end of it()

it('test 005_b - insert valid data into datetimeoffset(0) via TSQL, fetch as date', async function handler () {
// var testcolumnsize = 26
const testdatetimescale = 0
const testcolumntype = ` datetimeoffset(${testdatetimescale})`
// var testcolumnclienttype = 'date'
const testcolumnname = 'col2'
const testdata1 = null
const rowWithNullData = 1
// test date = 2001-04-10 10:12:59 +13:30
const year = 2001
const month = 4
const day = 10
const hour = 10
const minute = 12
const second = 59
const nanosecond = 0
const offsetHours = 13
const offsetMinutes = 30
// Month in JS is 0-based, so expected will be month minus 1

const jsDateExpected = new Date(year, month - 1, day, hour, minute, second, nanosecond)
jsDateExpected.setHours(jsDateExpected.getHours() - env.commonTestFns.getTimezoneOffsetInHours(year, month, day))
jsDateExpected.setHours(jsDateExpected.getHours() - offsetHours)
jsDateExpected.setMinutes(jsDateExpected.getMinutes() - offsetMinutes)

const testdata2Expected = `2001-04-10 10:12:59.1234567 +${offsetHours}:${offsetMinutes}`
const testdata2TsqlInsert = `'${testdata2Expected}'`

const proxy = env.makeTestFnProxy(tablename, testcolumnname)
await proxy.create(testcolumntype)
await proxy.insert(testdata1)
await proxy.insert(testdata2TsqlInsert)
await proxy.verifyData_Datetime(rowWithNullData, jsDateExpected, testname)
await t05(0, 59)
}) // end of it()

it('test 006_a - insert valid data into datetimeoffset(7) via TSQL, fetch as date UTC', async function handler () {
Expand Down
10 changes: 5 additions & 5 deletions test/dates.js
Expand Up @@ -193,7 +193,7 @@ describe('dates', function () {
await promises.query(ih.insertHoursQuery)
const expectedHour = 0
const results = await promises.query(tt.selectSql, [], { raw: true })
const expectedDate1 = env.timeHelper.makeUTCJan1900HH(expectedHour)
const expectedDate1 = env.timeHelper.makeUTCJan1970HH(expectedHour)
expectedDate1.nanosecondsDelta = 0
expect(results.first[0][0]).to.deep.equal(expectedDate1)
}
Expand All @@ -204,7 +204,7 @@ describe('dates', function () {
await promises.query(ih.insertMinutesSql)
const results = await promises.query(tt.selectSql, [], { raw: true })
results.first.forEach(r => {
const expectedDate = env.timeHelper.makeUTCJan1900HHMM(ih.randomHour, expectedMinute)
const expectedDate = env.timeHelper.makeUTCJan1970HHMM(ih.randomHour, expectedMinute)
expectedDate.nanosecondsDelta = 0
expect(r[0]).to.deep.equal(expectedDate)
++expectedMinute
Expand All @@ -218,7 +218,7 @@ describe('dates', function () {
await promises.query(ih.insertSecondsSql)
const results = await promises.query(tt.selectSql, [], { raw: true })
results.first.forEach(r => {
const expectedDate = env.timeHelper.makeUTCJan1900HHMMSS(ih.randomHour, ih.randomMinute, expectedSecond)
const expectedDate = env.timeHelper.makeUTCJan1970HHMMSS(ih.randomHour, ih.randomMinute, expectedSecond)
expectedDate.nanosecondsDelta = 0
expect(r[0]).to.deep.equal(expectedDate)
++expectedSecond
Expand All @@ -232,7 +232,7 @@ describe('dates', function () {
let msCount = 0
const results = await promises.query(tt.selectSql, [], { raw: true })
results.first.forEach(r => {
const expectedDate = env.timeHelper.makeUTCJan1900HHMMSSMS(ih.randomHour, ih.randomMinute, ih.randomSecond, ih.randomMs[msCount])
const expectedDate = env.timeHelper.makeUTCJan1970HHMMSSMS(ih.randomHour, ih.randomMinute, ih.randomSecond, ih.randomMs[msCount])
expectedDate.nanosecondsDelta = 0
expect(r[0]).to.deep.equal(expectedDate)
++msCount
Expand All @@ -246,7 +246,7 @@ describe('dates', function () {
let nsCount = 0
const results = await promises.query(tt.selectSql, [], { raw: true })
results.first.forEach(r => {
const expectedDate = env.timeHelper.makeUTCJan1900HHMMSSMS(ih.randomHour, ih.randomMinute, ih.randomSecond, ih.nanoseconds[nsCount] * 1000)
const expectedDate = env.timeHelper.makeUTCJan1970HHMMSSMS(ih.randomHour, ih.randomMinute, ih.randomSecond, ih.nanoseconds[nsCount] * 1000)
expectedDate.nanosecondsDelta = ih.nanosecondsDeltaExpected[nsCount]
expect(r[0]).to.deep.equal(expectedDate)
++nsCount
Expand Down
40 changes: 40 additions & 0 deletions test/env/time-helper.js
Expand Up @@ -55,18 +55,34 @@ class TimeHelper {
0))
}

makeUTCJan1970HH (expectedHour) {
return new Date(Date.UTC(1970, 0, 1, expectedHour, 0, 0, 0))
}

makeUTCJan1900HH (expectedHour) {
return new Date(Date.UTC(1900, 0, 1, expectedHour, 0, 0, 0))
}

makeUTCJan1970HHMM (expectedHour, expectedMinute) {
return new Date(Date.UTC(1970, 0, 1, expectedHour, expectedMinute, 0, 0))
}

makeUTCJan1900HHMM (expectedHour, expectedMinute) {
return new Date(Date.UTC(1900, 0, 1, expectedHour, expectedMinute, 0, 0))
}

makeUTCJan1970HHMMSS (expectedHour, expectedMinute, expectedSecond) {
return new Date(Date.UTC(1970, 0, 1, expectedHour, expectedMinute, expectedSecond, 0))
}

makeUTCJan1900HHMMSS (expectedHour, expectedMinute, expectedSecond) {
return new Date(Date.UTC(1900, 0, 1, expectedHour, expectedMinute, expectedSecond, 0))
}

makeUTCJan1970HHMMSSMS (expectedHour, expectedMinute, expectedSecond, expectedms) {
return new Date(Date.UTC(1970, 0, 1, expectedHour, expectedMinute, expectedSecond, expectedms))
}

makeUTCJan1900HHMMSSMS (expectedHour, expectedMinute, expectedSecond, expectedms) {
return new Date(Date.UTC(1900, 0, 1, expectedHour, expectedMinute, expectedSecond, expectedms))
}
Expand Down Expand Up @@ -121,6 +137,30 @@ class TimeHelper {
0))
}

getUTCTime1970HHMMSSMS (a) {
a = a || new Date()
return new Date(Date.UTC(
1970,
0,
1,
a.getUTCHours(),
a.getMinutes(),
a.getSeconds(),
a.getUTCMilliseconds()))
}

getUTCTime1970HHMMSS (a) {
a = a || new Date()
return new Date(Date.UTC(
1970,
0,
1,
a.getUTCHours(),
a.getMinutes(),
a.getSeconds(),
0))
}

getUTCTime (a) {
const localDate = new Date()
return Date.UTC(
Expand Down

0 comments on commit 2c31575

Please sign in to comment.