Skip to content

Commit 1eed9ce

Browse files
andrew-colemanmattbaileyuk
authored andcommitted
Implement timezone support for $toMillis
Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
1 parent ca3070f commit 1eed9ce

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/datetime.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,41 @@ const dateTime = (function () {
947947
var res = {};
948948
if (part.type === 'literal') {
949949
res.regex = part.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
950+
} else if (part.component === 'Z' || part.component === 'z') {
951+
// timezone
952+
let separator;
953+
if (!Array.isArray(part.integerFormat.groupingSeparators)) {
954+
separator = part.integerFormat.groupingSeparators;
955+
}
956+
res.regex = '';
957+
if (part.component === 'z') {
958+
res.regex = 'GMT';
959+
}
960+
res.regex += '[-+][0-9]+';
961+
if (separator) {
962+
res.regex += separator.character + '[0-9]+';
963+
}
964+
res.parse = function(value) {
965+
if (part.component === 'z') {
966+
value = value.substring(3); // remove the leading GMT
967+
}
968+
let offsetHours = 0, offsetMinutes = 0;
969+
if (separator) {
970+
offsetHours = Number.parseInt(value.substring(0, value.indexOf(separator.character)));
971+
offsetMinutes = Number.parseInt(value.substring(value.indexOf(separator.character) + 1));
972+
} else {
973+
// depends on number of digits
974+
const numdigits = value.length - 1;
975+
if (numdigits <= 2) {
976+
// just hour offset
977+
offsetHours = Number.parseInt(value);
978+
} else {
979+
offsetHours = Number.parseInt(value.substring(0, 3));
980+
offsetMinutes = Number.parseInt(value.substring(3));
981+
}
982+
}
983+
return offsetHours * 60 + offsetMinutes;
984+
};
950985
} else if (part.integerFormat) {
951986
res = generateRegex(part.integerFormat);
952987
} else {
@@ -1240,6 +1275,10 @@ const dateTime = (function () {
12401275
}
12411276

12421277
var millis = Date.UTC(components.Y, components.M, components.D, components.H, components.m, components.s, components.f);
1278+
if(components.Z || components.z) {
1279+
// adjust for timezone
1280+
millis -= (components.Z || components.z) * 60 * 1000;
1281+
}
12431282
return millis;
12441283
}
12451284
}

test/test-suite/groups/function-tomillis/parseDateTime.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,45 @@
326326
"expr": "$toMillis('2018-32-5', '[X]-[W]-[F1]')",
327327
"data": {},
328328
"code": "D3136"
329+
},
330+
{
331+
"function": "#toMillis",
332+
"category": "timezones",
333+
"description": "should parse a date containing timezones",
334+
"expr": "$toMillis('2020-09-09 08:00:00 +02:00', '[Y0001]-[M01]-[D01] [H01]:[m01]:[s01] [Z]') ~> $fromMillis() ",
335+
"data": {},
336+
"result": "2020-09-09T06:00:00.000Z"
337+
},
338+
{
339+
"function": "#toMillis",
340+
"category": "timezones",
341+
"description": "should parse a date containing timezones",
342+
"expr": "$toMillis('2020-09-09 08:00:00 GMT-05:00', '[Y0001]-[M01]-[D01] [H01]:[m01]:[s01] [z]') ~> $fromMillis() ",
343+
"data": {},
344+
"result": "2020-09-09T13:00:00.000Z"
345+
},
346+
{
347+
"function": "#toMillis",
348+
"category": "timezones",
349+
"description": "should parse a date containing timezones",
350+
"expr": "$toMillis('2020-09-09 12:00:00 +05:30', '[Y0001]-[M01]-[D01] [H01]:[m01]:[s01] [Z]') ~> $fromMillis() ",
351+
"data": {},
352+
"result": "2020-09-09T06:30:00.000Z"
353+
},
354+
{
355+
"function": "#toMillis",
356+
"category": "timezones",
357+
"description": "should parse a date containing timezones",
358+
"expr": "$toMillis('2020-09-09 12:00:00 GMT-5', '[Y0001]-[M01]-[D01] [H01]:[m01]:[s01] [z01]') ~> $fromMillis() ",
359+
"data": {},
360+
"result": "2020-09-09T17:00:00.000Z"
361+
},
362+
{
363+
"function": "#toMillis",
364+
"category": "timezones",
365+
"description": "should parse a date containing timezones",
366+
"expr": "$toMillis('2020-09-09 12:00:00 +0530', '[Y0001]-[M01]-[D01] [H01]:[m01]:[s01] [Z0001]') ~> $fromMillis() ",
367+
"data": {},
368+
"result": "2020-09-09T06:30:00.000Z"
329369
}
330370
]

0 commit comments

Comments
 (0)