Compare commits

...

2 Commits

Author SHA1 Message Date
AJ ONeal f03e75b898
1.3.2 2022-03-11 21:02:29 -07:00
AJ ONeal fd0f3a265b
fix: add timeZone() and fix toOffsetISOString() 2022-03-11 21:01:43 -07:00
3 changed files with 29 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "xtz", "name": "xtz",
"version": "1.3.1", "version": "1.3.2",
"description": "A fast, lightweight, zero-dependency library to translate between Time Zones and UTC with native Intl.DateTimeFormat in ~100 LoC. For Node.js & Browsers.", "description": "A fast, lightweight, zero-dependency library to translate between Time Zones and UTC with native Intl.DateTimeFormat in ~100 LoC. For Node.js & Browsers.",
"main": "xtz.js", "main": "xtz.js",
"files": [ "files": [

25
test.js
View File

@ -4,7 +4,8 @@ var TZ = require("./");
function testUtcToTz(t) { function testUtcToTz(t) {
var result = TZ.toTimeZone.apply(TZ, t.inputs).toISOString(); 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( throw new Error(
`Invalid UTC/ISO+Offset to TZ conversion for ${t.desc}:\n` + `Invalid UTC/ISO+Offset to TZ conversion for ${t.desc}:\n` +
`\tExpected: ${t.result}\n` + `\tExpected: ${t.result}\n` +
@ -14,10 +15,11 @@ function testUtcToTz(t) {
} }
function testTzToUtc(t) { function testTzToUtc(t) {
var result = TZ.toUTC.apply(TZ, t.inputs); var result = TZ.fromTimeZone.apply(TZ, t.inputs).toISOString();
var result2 = TZ.fromTimeZone.apply(TZ, t.inputs); var result2 = TZ.toOffsetISOString.apply(TZ, t.inputs);
if (t.result !== result.toISOString() || t.result !== result2.toISOString()) { var result3 = TZ.toUTC.apply(TZ, t.inputs).toISOString();
console.log(result); if (t.result !== result || t.result !== result2 || t.result !== result3) {
console.error(result);
throw new Error( throw new Error(
`Invalid TZ to UTC conversion for ${t.desc}:\n` + `Invalid TZ to UTC conversion for ${t.desc}:\n` +
`\tExpected: ${t.result}\n` + `\tExpected: ${t.result}\n` +
@ -317,3 +319,16 @@ console.info(
}, },
].forEach(testTzToUtc); ].forEach(testTzToUtc);
console.info("Pass: TZ to UTC for America/New_York and Asia/Colombo"); 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
View File

@ -103,7 +103,7 @@
} }
function toOffsetISOString(date, timeZone) { function toOffsetISOString(date, timeZone) {
if ("offset" in date && "year" in date) { if ("object" === typeof date && "offset" in date && "year" in date) {
return formatAsOffsetISOString(date); return formatAsOffsetISOString(date);
} }
@ -179,6 +179,10 @@
return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${sss}${offset}`; return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${sss}${offset}`;
} }
function getTimeZone() {
return new Intl.DateTimeFormat().resolvedOptions().timeZone;
}
exports.XTZ = { exports.XTZ = {
// bespoke date => // bespoke date =>
// 2021-11-07T3:15:59-0500 // 2021-11-07T3:15:59-0500
@ -190,6 +194,9 @@
// -240 => -0400 // -240 => -0400
formatOffset: formatOffset, formatOffset: formatOffset,
// "America/New_York"
timeZone: getTimeZone,
// "2021-11-07T03:15:59-0500"
toLocalISOString: toLocalISOString, toLocalISOString: toLocalISOString,
// [ "2021-11-07T08:15:59Z", "America/New_York" ] // [ "2021-11-07T08:15:59Z", "America/New_York" ]