diff options
author | Derick Rethans <github@derickrethans.nl> | 2017-07-11 13:58:56 +0100 |
---|---|---|
committer | Derick Rethans <github@derickrethans.nl> | 2017-07-21 09:52:28 +0100 |
commit | edc9f4c74a57db45e82c93f714b093336e88744b (patch) | |
tree | 3b05de860247db847a1f3c8c4adb5f9484229aea /jstests/aggregation/expressions | |
parent | d55deb04ad02b9b8b05b61a992166c337387b04a (diff) | |
download | mongo-edc9f4c74a57db45e82c93f714b093336e88744b.tar.gz |
SERVER-29209 Add timezone support to $dateFromString
Diffstat (limited to 'jstests/aggregation/expressions')
-rw-r--r-- | jstests/aggregation/expressions/date_from_string.js | 218 |
1 files changed, 217 insertions, 1 deletions
diff --git a/jstests/aggregation/expressions/date_from_string.js b/jstests/aggregation/expressions/date_from_string.js index a6fc7b9b09b..7dc34f78f24 100644 --- a/jstests/aggregation/expressions/date_from_string.js +++ b/jstests/aggregation/expressions/date_from_string.js @@ -31,6 +31,33 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode }); /* --------------------------------------------------------------------------------------- */ + /* Normal format tests with timezone. */ + + coll.drop(); + assert.writeOK(coll.insert({_id: 0})); + + testCases = [ + {expect: "2017-07-04T10:56:02Z", inputString: "2017-07-04T11:56.02"}, + {expect: "2017-07-04T10:56:02.813Z", inputString: "2017-07-04T11:56.02.813"}, + {expect: "2017-07-04T10:56:02.810Z", inputString: "2017-07-04T11:56.02.81"}, + {expect: "2017-07-04T10:56:02.800Z", inputString: "2017-07-04T11:56.02.8"}, + ]; + testCases.forEach(function(testCase) { + assert.eq( + [{_id: 0, date: ISODate(testCase.expect)}], + coll.aggregate({ + $project: { + date: { + $dateFromString: + {dateString: testCase.inputString, timezone: "Europe/London"} + } + } + }) + .toArray(), + tojson(testCase)); + }); + + /* --------------------------------------------------------------------------------------- */ /* Normal format tests from data. */ coll.drop(); @@ -75,6 +102,89 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode .toArray()); /* --------------------------------------------------------------------------------------- */ + /* Normal format tests from data, with time zone. */ + + coll.drop(); + assert.writeOK(coll.insert([ + {_id: 0, dateString: "2017-07-06T12:35:37.513", timezone: "GMT"}, + {_id: 1, dateString: "2017-07-06T12:35:37.513", timezone: "UTC"}, + {_id: 2, dateString: "1960-07-10T12:35:37.513", timezone: "America/New_York"}, + {_id: 3, dateString: "1960-07-10T12:35:37.513", timezone: "Europe/London"}, + {_id: 4, dateString: "2017-07-06T12:35:37.513", timezone: "America/Los_Angeles"}, + {_id: 5, dateString: "2017-07-06T12:35:37.513", timezone: "Europe/Paris"}, + ])); + + assert.eq( + [ + {"_id": 0, "date": ISODate("2017-07-06T12:35:37.513Z")}, + {"_id": 1, "date": ISODate("2017-07-06T12:35:37.513Z")}, + {"_id": 2, "date": ISODate("1960-07-10T16:35:37.513Z")}, + {"_id": 3, "date": ISODate("1960-07-10T11:35:37.513Z")}, + {"_id": 4, "date": ISODate("2017-07-06T19:35:37.513Z")}, + {"_id": 5, "date": ISODate("2017-07-06T10:35:37.513Z")}, + ], + coll.aggregate([ + { + $project: { + date: + {'$dateFromString': {dateString: "$dateString", timezone: "$timezone"}} + }, + }, + {$sort: {_id: 1}} + ]) + .toArray()); + + /* --------------------------------------------------------------------------------------- */ + /* dateString from data with timezone as constant */ + + coll.drop(); + assert.writeOK(coll.insert([ + {_id: 0, dateString: "2017-07-06T12:35:37"}, + ])); + + assert.eq( + [ + {"_id": 0, "date": ISODate("2017-07-06T03:35:37Z")}, + ], + coll.aggregate([ + { + $project: { + date: + {'$dateFromString': {dateString: "$dateString", timezone: "Asia/Tokyo"}} + }, + }, + {$sort: {_id: 1}} + ]) + .toArray()); + + /* --------------------------------------------------------------------------------------- */ + /* dateString from constant with timezone from data */ + + coll.drop(); + assert.writeOK(coll.insert([ + {_id: 0, timezone: "Europe/London"}, + {_id: 1, timezone: "America/New_York"}, + ])); + + assert.eq( + [ + {"_id": 0, "date": ISODate("2017-07-19T17:52:35.199Z")}, + {"_id": 1, "date": ISODate("2017-07-19T22:52:35.199Z")}, + ], + coll.aggregate([ + { + $project: { + date: { + '$dateFromString': + {dateString: "2017-07-19T18:52:35.199", timezone: "$timezone"} + } + }, + }, + {$sort: {_id: 1}} + ]) + .toArray()); + + /* --------------------------------------------------------------------------------------- */ /* BI format tests. */ coll.drop(); @@ -117,6 +227,43 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode }); /* --------------------------------------------------------------------------------------- */ + /* Tests with additional timezone information . */ + + coll.drop(); + assert.writeOK(coll.insert({_id: 0})); + + testCases = [ + // GMT based variants + {expect: "2017-07-14T12:02:44.771Z", inputString: "2017-07-14T12:02:44.771 GMT"}, + {expect: "2017-07-14T12:02:44.771Z", inputString: "2017-07-14T12:02:44.771 GMT+00"}, + {expect: "2017-07-14T12:02:44.771Z", inputString: "2017-07-14T12:02:44.771 GMT+00:00"}, + {expect: "2017-07-14T10:02:44.771Z", inputString: "2017-07-14T12:02:44.771 GMT+02"}, + {expect: "2017-07-14T10:02:44.771Z", inputString: "2017-07-14T12:02:44.771 GMT+02:00"}, + {expect: "2017-07-14T09:02:44.771Z", inputString: "2017-07-14T12:02:44.771+03"}, + {expect: "2017-07-14T08:32:44.771Z", inputString: "2017-07-14T12:02:44.771+0330"}, + {expect: "2017-07-14T08:32:44.771Z", inputString: "2017-07-14T12:02:44.771+03:30"}, + // With timezone abbreviations + {expect: "2017-07-14T12:02:44.771Z", inputString: "2017-07-14T12:02:44.771 UTC"}, + {expect: "2017-07-14T10:02:44.771Z", inputString: "2017-07-14T12:02:44.771 CEST"}, + {expect: "2017-07-14T17:02:44.771Z", inputString: "2017-07-14T12:02:44.771 EST"}, + {expect: "2017-07-14T19:02:44.771Z", inputString: "2017-07-14T12:02:44.771 PDT"}, + // A-I,K-Z are military time zones: + // https://en.wikipedia.org/wiki/List_of_military_time_zones + {expect: "2017-07-14T11:02:44.771Z", inputString: "2017-07-14T12:02:44.771 A"}, + {expect: "2017-07-14T01:02:44.771Z", inputString: "2017-07-14T12:02:44.771 L"}, + {expect: "2017-07-14T15:02:44.771Z", inputString: "2017-07-14T12:02:44.771 P"}, + {expect: "2017-07-14T12:02:44.771Z", inputString: "2017-07-14T12:02:44.771 Z"}, + ]; + testCases.forEach(function(testCase) { + assert.eq( + [{_id: 0, date: ISODate(testCase.expect)}], + coll.aggregate( + {$project: {date: {'$dateFromString': {"dateString": testCase.inputString}}}}) + .toArray(), + tojson(testCase)); + }); + + /* --------------------------------------------------------------------------------------- */ /* BI format tests from data. */ coll.drop(); @@ -195,7 +342,7 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode pipelines = [ [{'$project': {date: {'$dateFromString': {dateString: "July 4th"}}}}], - [{'$project': {date: {'$dateFromString': {dateString: "2017, 12:50:53"}}}}], + [{'$project': {date: {'$dateFromString': {dateString: "12:50:53"}}}}], ]; pipelines.forEach(function(pipeline) { @@ -203,6 +350,24 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode }); /* --------------------------------------------------------------------------------------- */ + /* Testing whether it throws the right assert for broken date/time strings. */ + + coll.drop(); + + assert.writeOK(coll.insert([ + {_id: 0}, + ])); + + pipelines = [ + [{'$project': {date: {'$dateFromString': {dateString: "2017, 12:50:53"}}}}], + [{'$project': {date: {'$dateFromString': {dateString: "60.Monday1770/06:59"}}}}], + ]; + + pipelines.forEach(function(pipeline) { + assertErrorCode(coll, pipeline, 40553, tojson(pipeline)); + }); + + /* --------------------------------------------------------------------------------------- */ /* NULL returns. */ coll.drop(); @@ -214,6 +379,16 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode pipelines = [ [{$project: {date: {'$dateFromString': {"dateString": "$tz"}}}}, {$sort: {_id: 1}}], + [ + { + $project: { + date: { + '$dateFromString': {"dateString": "2017-07-11T17:05:19Z", "timezone": "$tz"} + } + } + }, + {$sort: {_id: 1}} + ], ]; pipelines.forEach(function(pipeline) { assert.eq([{_id: 0, date: null}, {_id: 1, date: null}, {_id: 2, date: null}], @@ -233,4 +408,45 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode pipeline = {$project: {date: {'$dateFromString': {"dateString": 5}}}}; assertErrorCode(coll, pipeline, 40543); + /* --------------------------------------------------------------------------------------- */ + /* Passing in time zone with date/time string. */ + + pipeline = { + $project: { + date: { + '$dateFromString': + {dateString: "2017-07-12T22:23:55 GMT+02:00", timezone: "Europe/Amsterdam"} + } + } + }; + assertErrorCode(coll, pipeline, 40554); + + pipeline = { + $project: { + date: { + '$dateFromString': + {dateString: "2017-07-12T22:23:55Z", timezone: "Europe/Amsterdam"} + } + } + }; + assertErrorCode(coll, pipeline, 40551); + + pipeline = { + $project: { + date: { + '$dateFromString': { + dateString: "2017-07-12T22:23:55 America/New_York", + timezone: "Europe/Amsterdam" + } + } + } + }; + assertErrorCode(coll, pipeline, 40553); + + pipeline = { + $project: + {date: {'$dateFromString': {dateString: "2017-07-12T22:23:55 Europe/Amsterdam"}}} + }; + assertErrorCode(coll, pipeline, 40553); + })(); |