mirror of https://github.com/therootcompany/tz.js
fix: add timeZone() and fix toOffsetISOString()
This commit is contained in:
parent
6417eca7f6
commit
fd0f3a265b
25
test.js
25
test.js
|
@ -4,7 +4,8 @@ var TZ = require("./");
|
|||
|
||||
function testUtcToTz(t) {
|
||||
var result = TZ.toTimeZone.apply(TZ, t.inputs).toISOString();
|
||||
if (t.result !== result) {
|
||||
var result2 = TZ.toTimeZoneISOString.apply(TZ, t.inputs);
|
||||
if (result !== result2 || t.result !== result) {
|
||||
throw new Error(
|
||||
`Invalid UTC/ISO+Offset to TZ conversion for ${t.desc}:\n` +
|
||||
`\tExpected: ${t.result}\n` +
|
||||
|
@ -14,10 +15,11 @@ function testUtcToTz(t) {
|
|||
}
|
||||
|
||||
function testTzToUtc(t) {
|
||||
var result = TZ.toUTC.apply(TZ, t.inputs);
|
||||
var result2 = TZ.fromTimeZone.apply(TZ, t.inputs);
|
||||
if (t.result !== result.toISOString() || t.result !== result2.toISOString()) {
|
||||
console.log(result);
|
||||
var result = TZ.fromTimeZone.apply(TZ, t.inputs).toISOString();
|
||||
var result2 = TZ.toOffsetISOString.apply(TZ, t.inputs);
|
||||
var result3 = TZ.toUTC.apply(TZ, t.inputs).toISOString();
|
||||
if (t.result !== result || t.result !== result2 || t.result !== result3) {
|
||||
console.error(result);
|
||||
throw new Error(
|
||||
`Invalid TZ to UTC conversion for ${t.desc}:\n` +
|
||||
`\tExpected: ${t.result}\n` +
|
||||
|
@ -317,3 +319,16 @@ console.info(
|
|||
},
|
||||
].forEach(testTzToUtc);
|
||||
console.info("Pass: TZ to UTC for America/New_York and Asia/Colombo");
|
||||
|
||||
var localISOString = TZ.toLocalISOString();
|
||||
var reISOString = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d$/;
|
||||
if (!reISOString.test(localISOString)) {
|
||||
throw new Error("Couldn't get local time as iso+offset");
|
||||
}
|
||||
console.info("Pass: can get local time as ISO+Offset");
|
||||
|
||||
var tzName = TZ.timeZone();
|
||||
if (!/^[A-Z]\w+\/[A-Z]\w+$/.test(tzName)) {
|
||||
throw new Error("Couldn't get local Time Zone");
|
||||
}
|
||||
console.info("Pass: can get local timezone");
|
||||
|
|
9
xtz.js
9
xtz.js
|
@ -103,7 +103,7 @@
|
|||
}
|
||||
|
||||
function toOffsetISOString(date, timeZone) {
|
||||
if ("offset" in date && "year" in date) {
|
||||
if ("object" === typeof date && "offset" in date && "year" in date) {
|
||||
return formatAsOffsetISOString(date);
|
||||
}
|
||||
|
||||
|
@ -179,6 +179,10 @@
|
|||
return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${sss}${offset}`;
|
||||
}
|
||||
|
||||
function getTimeZone() {
|
||||
return new Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
}
|
||||
|
||||
exports.XTZ = {
|
||||
// bespoke date =>
|
||||
// 2021-11-07T3:15:59-0500
|
||||
|
@ -190,6 +194,9 @@
|
|||
// -240 => -0400
|
||||
formatOffset: formatOffset,
|
||||
|
||||
// "America/New_York"
|
||||
timeZone: getTimeZone,
|
||||
// "2021-11-07T03:15:59-0500"
|
||||
toLocalISOString: toLocalISOString,
|
||||
|
||||
// [ "2021-11-07T08:15:59Z", "America/New_York" ]
|
||||
|
|
Loading…
Reference in New Issue