Compare commits
No commits in common. "6417eca7f69ba0c7eabdcc92f284ea2397efec59" and "6a095b0966704f61bb1a66e544320e5557d93318" have entirely different histories.
6417eca7f6
...
6a095b0966
108
README.md
108
README.md
|
@ -9,36 +9,58 @@ XTZ is a poor man's `Temporal` polyfill, but just for time zones. \
|
|||
Demo: <https://therootcompany.github.io/tz.js/>
|
||||
|
||||
```js
|
||||
// What's the current time, in ISO+Offset format?
|
||||
// What's the current time, in ISO format?
|
||||
|
||||
TZ.toLocalISOString(new Date()); // "2021-11-07T03:15:59.000-0500"
|
||||
TZ.timeZone(); // "America/New_York"
|
||||
```
|
||||
|
||||
```js
|
||||
// What will the ISO+Offset datetime string be
|
||||
// when it's 3:15am in New York?
|
||||
//
|
||||
// (Relative New York time to Absolute ISO+Offset Time)
|
||||
> What UTC time will it be when it's 3:15am in New York?
|
||||
|
||||
TZ.toOffsetISOString("2021-11-07 03:15:59.000", "America/New_York");
|
||||
```js
|
||||
// Relative New York time to Absolute UTC Time
|
||||
TZ.toUTCISOString("2021-11-07 03:15:59.000", "America/New_York");
|
||||
// "2021-11-07T03:15:59.000-0500"
|
||||
```
|
||||
|
||||
```js
|
||||
// What time will it be in New York
|
||||
// when it's 7:15am UTC?
|
||||
//
|
||||
// (Absolute UTC Zulu time to Relative New York time)
|
||||
var tzDate = TZ.toUTC("2021-11-07 03:15:59.000", "America/New_York");
|
||||
// {
|
||||
// year: 2021, month: 10, day: 7,
|
||||
// hour: 3, minute: 15, second: 59, millisecond: 0,
|
||||
// offset: -300, timeZoneName: "Eastern Standard Time"
|
||||
// }
|
||||
|
||||
tzDate.toISOString();
|
||||
// "2021-11-07T03:15:59.000-0500"
|
||||
// (same as "2021-11-07T08:15:59.000Z")
|
||||
```
|
||||
|
||||
> What time will it be in New York when it's 7:15am UTC?
|
||||
|
||||
```js
|
||||
// Absolute UTC time to Relative New York time
|
||||
TZ.toTimeZoneISOString("2021-03-14T07:15:59.000Z", "America/New_York");
|
||||
// "2021-03-14T03:15:59.000-0400"
|
||||
```
|
||||
|
||||
```js
|
||||
var utcDate = TZ.toTimeZone("2021-03-14T07:15:59.000Z", "America/New_York");
|
||||
// {
|
||||
// year: 2021, month: 2, day: 14,
|
||||
// hour: 3, minute: 15, second: 59, millisecond: 0,
|
||||
// offset: -240, timeZoneName: "Eastern Daylight Time"
|
||||
// }
|
||||
|
||||
utcDate.toISOString();
|
||||
// "2021-03-14T03:15:59.000-0400"
|
||||
// (same as "2021-11-07T07:15:59.000Z")
|
||||
```
|
||||
|
||||
# Features
|
||||
|
||||
- [x] Translate a UTC Zulu time to a Time Zone
|
||||
- [x] Translate a Zoned time to ISO+Offset
|
||||
- [x] Translate a UTC time to a Time Zone
|
||||
- [x] Translate a Zoned time to UTC
|
||||
- [x] Handles **Daylight Savings**, Weird Time Zones, etc...
|
||||
- [x] Well-tested `npm run test`
|
||||
- [x] Lightweight (No deps)
|
||||
|
@ -86,46 +108,33 @@ https://www.youtube.com/playlist?list=PLxki0D-ilnqa6horOJ2G18WMZlJeQFlAt
|
|||
- `toLocalISOString(dateOrNull)`
|
||||
- `toTimeZone(utcDate, timeZone)`
|
||||
- `toTimeZoneISOString(isoString, timeZone)`
|
||||
- `fromTimeZone(dtString, timeZone)`
|
||||
- `toOffsetISOString(dtString, timeZone)`
|
||||
- `toUTC(dtString, timeZone)`
|
||||
- `toUTCISOString(dtString, timeZone)`
|
||||
|
||||
## `toTimeZone(utcDate, timeZone)`
|
||||
|
||||
> Convert UTC into a Target Time Zone
|
||||
|
||||
Use ISO timestamps representing the absolute UTC time (ISO with or without offset):
|
||||
Use ISO timestamps representing the absolute UTC time (with or without offset):
|
||||
|
||||
```txt
|
||||
"2021-11-07T08:15:59.000Z"
|
||||
```
|
||||
|
||||
```js
|
||||
var utcDate = TZ.toTimeZone("2021-03-14T07:15:59.000Z", "America/New_York");
|
||||
// {
|
||||
// year: 2021, month: 2, day: 14,
|
||||
// hour: 3, minute: 15, second: 59, millisecond: 0,
|
||||
// offset: -240, timeZoneName: "Eastern Daylight Time"
|
||||
// }
|
||||
|
||||
utcDate.toISOString();
|
||||
// "2021-03-14T03:15:59.000-0400"
|
||||
// (same as "2021-11-07T07:15:59.000Z")
|
||||
```
|
||||
|
||||
### Convert directly to an ISO String:
|
||||
Convert directly to an ISO String:
|
||||
|
||||
```js
|
||||
TZ.toTimeZoneISOString("2021-11-07T08:15:59.000Z", "America/New_York");
|
||||
// "2021-11-07T03:15:59.000-0500"
|
||||
```
|
||||
|
||||
### Or use our bespoke (custom) date object:
|
||||
Or use our bespoke (custom) date object:
|
||||
|
||||
```js
|
||||
var tzDate = TZ.toTimeZone("2021-11-07T08:15:59.000Z", "America/New_York");
|
||||
```
|
||||
|
||||
### You can also use a date object with an absolute ISO time:
|
||||
You can also use a date object with an absolute UTC time:
|
||||
|
||||
```js
|
||||
var tzDate = TZ.toTimeZone(
|
||||
|
@ -139,16 +148,16 @@ console.log(tzDate.toISOString());
|
|||
// "2021-11-07T03:15:59.000-0500"
|
||||
```
|
||||
|
||||
### Our ISO Strings + Offsets work with JavaScript's native Date object!!
|
||||
Our ISO Strings + Offsets work with JavaScript's native Date object!!
|
||||
|
||||
```js
|
||||
new Date("2021-11-07T03:15:59.000-0500").toISOString());
|
||||
// "2021-11-07T08:15:59.000Z"
|
||||
```
|
||||
|
||||
## `fromTimeZone(dtString, timeZone)`
|
||||
## `toUTC(dtString, timeZone)`
|
||||
|
||||
> Convert a Target Time Zone into ISO
|
||||
> Convert a Target Time Zone into UTC
|
||||
|
||||
Use ISO-like timestamps representing the _local_ time in the target time zone:
|
||||
|
||||
|
@ -156,39 +165,24 @@ Use ISO-like timestamps representing the _local_ time in the target time zone:
|
|||
"2021-11-0 03:15:59.000"
|
||||
```
|
||||
|
||||
```js
|
||||
var tzDate = TZ.fromTimeZone("2021-11-07 03:15:59.000", "America/New_York");
|
||||
// {
|
||||
// year: 2021, month: 10, day: 7,
|
||||
// hour: 3, minute: 15, second: 59, millisecond: 0,
|
||||
// offset: -300, timeZoneName: "Eastern Standard Time"
|
||||
// }
|
||||
|
||||
tzDate.toISOString();
|
||||
// "2021-11-07T03:15:59.000-0500"
|
||||
// (same as "2021-11-07T08:15:59.000Z")
|
||||
```
|
||||
|
||||
### Convert directly to an offset ISO String:
|
||||
Convert directly to an offset ISO String:
|
||||
|
||||
```js
|
||||
TZ.toOffsetISOString("2021-11-07 03:15:59.000", "America/New_York");
|
||||
TZ.toUTCISOString("2021-11-07 03:15:59.000", "America/New_York");
|
||||
// "2021-11-07T03:15:59.000-0500"
|
||||
```
|
||||
|
||||
### Or our bespoke date object:
|
||||
Or our bespoke date object:
|
||||
|
||||
```js
|
||||
var utcDate = TZ.fromTimeZone("2021-11-07 03:15:59.000", "America/New_York");
|
||||
var utcDate = TZ.toUTC("2021-11-07 03:15:59.000", "America/New_York");
|
||||
```
|
||||
|
||||
### Use a Date as a source time
|
||||
|
||||
You can also use a date object as the source time, but the date's UTC time will be treated as **_relative to time
|
||||
zone_** rather than absolute (this is a workaround for JavaScript's lack of bi-directional timezone support).
|
||||
|
||||
```js
|
||||
var utcDate = TZ.fromTimeZone(
|
||||
var utcDate = TZ.toUTC(
|
||||
new Date("2021-11-07T03:15:59.000Z"),
|
||||
"America/New_York"
|
||||
);
|
||||
|
@ -213,7 +207,7 @@ Q: What happens in March when 2am is skipped?
|
|||
- A: Although 2am is not a valid time, rather than throwing an error this library will resolve to 1am instead, which
|
||||
is an hour early in real ("tick-tock" or "monotonic") time.
|
||||
```js
|
||||
var utcDate = TZ.fromTimeZone("2021-03-14 02:15:59.000", "America/New_York");
|
||||
var utcDate = TZ.toUTC("2021-03-14 02:15:59.000", "America/New_York");
|
||||
utcDate.toISOString();
|
||||
// "2021-03-14T02:15:59.000-0400"
|
||||
// (same as "2021-03-14T01:15:59.000-0500")
|
||||
|
@ -224,7 +218,7 @@ Q: What happens in November when 1am happens twice?
|
|||
- A: Although both 1ams are distinguishable with ISO offset times, only the first can be resolved from a local time
|
||||
with this library.
|
||||
```js
|
||||
var utcDate = TZ.fromTimeZone("2021-11-07 01:15:59.000", "America/New_York");
|
||||
var utcDate = TZ.toUTC("2021-11-07 01:15:59.000", "America/New_York");
|
||||
utcDate.toISOString();
|
||||
// "2021-11-07T01:15:59.000-0400", same as "2021-11-07T05:15:59.000Z"
|
||||
// (an hour before the 2nd 1am at "2021-11-07T01:15:59.000-0500")
|
||||
|
|
12
examples.js
12
examples.js
|
@ -22,9 +22,9 @@ var XTZ;
|
|||
console.info();
|
||||
|
||||
console.info("\t// during daylight savings");
|
||||
console.info(`\tXTZ.fromTimeZone("2021-03-14 08:15:59.000", "America/New_York")`);
|
||||
console.info(`\tXTZ.toUTC("2021-03-14 08:15:59.000", "America/New_York")`);
|
||||
console.info(`\ttzDate.toISOString()`);
|
||||
tzDate = XTZ.fromTimeZone("2021-03-14 08:15:59.000", "America/New_York");
|
||||
tzDate = XTZ.toUTC("2021-03-14 08:15:59.000", "America/New_York");
|
||||
console.info(
|
||||
"\t" + tzDate.toISOString(),
|
||||
"// same as",
|
||||
|
@ -33,9 +33,9 @@ var XTZ;
|
|||
console.info();
|
||||
|
||||
console.info("\t// during standard time");
|
||||
console.info(`\tXTZ.fromTimeZone("2021-11-07 08:15:59.000", "America/New_York")`);
|
||||
console.info(`\tXTZ.toUTC("2021-11-07 08:15:59.000", "America/New_York")`);
|
||||
console.info(`\ttzDate.toISOString()`);
|
||||
tzDate = XTZ.fromTimeZone("2021-11-07 08:15:59.000", "America/New_York");
|
||||
tzDate = XTZ.toUTC("2021-11-07 08:15:59.000", "America/New_York");
|
||||
console.info(
|
||||
"\t" + tzDate.toISOString(),
|
||||
"// same as",
|
||||
|
@ -65,9 +65,9 @@ var XTZ;
|
|||
console.info();
|
||||
|
||||
console.info("\t// during standard time");
|
||||
console.info(`\tXTZ.fromTimeZone("2021-11-07T08:15:59.000Z", "America/New_York")`);
|
||||
console.info(`\tXTZ.toUTC("2021-11-07T08:15:59.000Z", "America/New_York")`);
|
||||
console.info(`\ttzDate.toISOString()`);
|
||||
tzDate = XTZ.fromTimeZone("2021-11-07T08:15:59.000Z", "America/New_York");
|
||||
tzDate = XTZ.toUTC("2021-11-07T08:15:59.000Z", "America/New_York");
|
||||
console.info(
|
||||
"\t" + tzDate.toISOString(),
|
||||
"// same as",
|
||||
|
|
18
index.html
18
index.html
|
@ -99,8 +99,8 @@ String(((new Date()).getTimezoneOffset() % 60)).padStart(2, '0')</code></pre>
|
|||
<hr />
|
||||
|
||||
<form class="js-tz2utc">
|
||||
<h3>TimeZone-Relative (Local) to Absolute (ISO+Offset) String:</h3>
|
||||
<pre><code>XTZ.fromTimeZone("<span class="js-dtx-tz">YYYY-03-14 03:15:69.000</span>", "<span class="js-tzx-tz">UTC</span>")
|
||||
<h3>Relative TimeZone to Absolute UTC:</h3>
|
||||
<pre><code>XTZ.toUTC("<span class="js-dtx-tz">YYYY-03-14 03:15:69.000</span>", "<span class="js-tzx-tz">UTC</span>")
|
||||
.toISOString()
|
||||
// <span class="js-myx-dt-tz"></span></code></pre>
|
||||
<label>
|
||||
|
@ -130,14 +130,14 @@ String(((new Date()).getTimezoneOffset() % 60)).padStart(2, '0')</code></pre>
|
|||
</label>
|
||||
<br />
|
||||
<br />
|
||||
<button type="submit">Convert to ISO+Offset!</button>
|
||||
<button type="submit">Convert to UTC!</button>
|
||||
<br />
|
||||
</form>
|
||||
|
||||
<hr />
|
||||
|
||||
<form class="js-utc2tz">
|
||||
<h3>Absolute (UTC) to TimeZone-Relative (ISO+Offset) String:</h3>
|
||||
<h3>Absolute UTC to Relative TimeZone</h3>
|
||||
<pre><code>XTZ.toTimeZone("<span class="js-dtx-utc">YYYY-03-14 03:15:69.000</span>", "<span class="js-tzx-utc">UTC</span>")
|
||||
.toISOString()
|
||||
// <span class="js-myx-dt-utc"></span></code></pre>
|
||||
|
@ -201,17 +201,17 @@ Asia/Kolkata +0530 (No DST) (30-min)</pre
|
|||
}
|
||||
|
||||
function translate(dt, tz, tz2) {
|
||||
var utc = new Date(XTZ.fromTimeZone(dt, tz).toISOString());
|
||||
var utc = new Date(XTZ.toUTC(dt, tz).toISOString());
|
||||
if (!tz2) {
|
||||
return utc;
|
||||
}
|
||||
return XTZ.toTimeZone(utc, tz2);
|
||||
}
|
||||
|
||||
function fromTimeZone() {
|
||||
function toUTC() {
|
||||
var curDt = $(".js-dt-tz").value;
|
||||
var curTz = $(".js-tz-tz").value;
|
||||
$(".js-my-dt-tz").value = XTZ.fromTimeZone(curDt, curTz).toISOString();
|
||||
$(".js-my-dt-tz").value = XTZ.toUTC(curDt, curTz).toISOString();
|
||||
|
||||
$(".js-dtx-tz").innerText = curDt;
|
||||
$(".js-tzx-tz").innerText = curTz;
|
||||
|
@ -265,10 +265,10 @@ Asia/Kolkata +0530 (No DST) (30-min)</pre
|
|||
$("form.js-tz2utc").addEventListener("submit", function (ev) {
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
fromTimeZone();
|
||||
toUTC();
|
||||
});
|
||||
|
||||
fromTimeZone();
|
||||
toUTC();
|
||||
toTZ();
|
||||
xTZ();
|
||||
</script>
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
{
|
||||
"name": "xtz",
|
||||
"version": "1.3.1",
|
||||
"version": "1.1.1",
|
||||
"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",
|
||||
"files": [
|
||||
"xtz.js",
|
||||
"tolocalisostring.js"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "node ./test.js",
|
||||
"build": "uglifyjs ./xtz.js > ./xtz.min.js ; gzip -k -f ./xtz.min.js"
|
||||
|
|
53
test.js
53
test.js
|
@ -6,7 +6,7 @@ function testUtcToTz(t) {
|
|||
var result = TZ.toTimeZone.apply(TZ, t.inputs).toISOString();
|
||||
if (t.result !== result) {
|
||||
throw new Error(
|
||||
`Invalid UTC/ISO+Offset to TZ conversion for ${t.desc}:\n` +
|
||||
`Invalid UTC to TZ conversion for ${t.desc}:\n` +
|
||||
`\tExpected: ${t.result}\n` +
|
||||
`\tActual: ${result}\n`
|
||||
);
|
||||
|
@ -15,8 +15,7 @@ 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()) {
|
||||
if (t.result !== result.toISOString()) {
|
||||
console.log(result);
|
||||
throw new Error(
|
||||
`Invalid TZ to UTC conversion for ${t.desc}:\n` +
|
||||
|
@ -45,23 +44,23 @@ function testTzToUtc(t) {
|
|||
|
||||
// 12:15am NY -0500 => -0400
|
||||
{
|
||||
desc: "UTC Zulu to 12:15am NY EST",
|
||||
desc: "UTC to 12:15am NY EST",
|
||||
inputs: ["2021-03-14T05:15:59.000Z", "America/New_York"],
|
||||
result: "2021-03-14T00:15:59.000-0500",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 12:15am NY EST (2)",
|
||||
desc: "UTC to 12:15am NY EST (2)",
|
||||
inputs: ["2021-03-14T00:15:59.000-0500", "America/New_York"],
|
||||
result: "2021-03-14T00:15:59.000-0500",
|
||||
},
|
||||
// 1:15am NY (non-DST)
|
||||
{
|
||||
desc: "UTC Zulu to 1:15am NY EST",
|
||||
desc: "UTC to 1:15am NY EST",
|
||||
inputs: ["2021-03-14T06:15:59.000Z", "America/New_York"],
|
||||
result: "2021-03-14T01:15:59.000-0500",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 1:15am NY EST (2)",
|
||||
desc: "UTC to 1:15am NY EST (2)",
|
||||
inputs: ["2021-03-14T01:15:59.000-0500", "America/New_York"],
|
||||
result: "2021-03-14T01:15:59.000-0500",
|
||||
},
|
||||
|
@ -70,23 +69,23 @@ function testTzToUtc(t) {
|
|||
|
||||
// 3:15am NY (DST)
|
||||
{
|
||||
desc: "UTC Zulu to 3:15am NY EDT",
|
||||
desc: "UTC to 3:15am NY EDT",
|
||||
inputs: ["2021-03-14T07:15:59.000Z", "America/New_York"],
|
||||
result: "2021-03-14T03:15:59.000-0400",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 3:15am NY EDT (2)",
|
||||
desc: "UTC to 3:15am NY EDT (2)",
|
||||
inputs: ["2021-03-14T03:15:59.000-0400", "America/New_York"],
|
||||
result: "2021-03-14T03:15:59.000-0400",
|
||||
},
|
||||
// 4:15am NY
|
||||
{
|
||||
desc: "UTC Zulu to 4:15am NY EDT",
|
||||
desc: "UTC to 4:15am NY EDT",
|
||||
inputs: ["2021-03-14T08:15:59.000Z", "America/New_York"],
|
||||
result: "2021-03-14T04:15:59.000-0400",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 4:15am NY EDT (2)",
|
||||
desc: "UTC to 4:15am NY EDT (2)",
|
||||
inputs: ["2021-03-14T04:15:59.000-0400", "America/New_York"],
|
||||
result: "2021-03-14T04:15:59.000-0400",
|
||||
},
|
||||
|
@ -107,57 +106,57 @@ function testTzToUtc(t) {
|
|||
|
||||
// 12:15am NY -0400 => -0500
|
||||
{
|
||||
desc: "UTC Zulu to 2021 Nov 7, 12:15am NY EDT",
|
||||
desc: "UTC to 2021 Nov 7, 12:15am NY EDT",
|
||||
inputs: ["2021-11-07T04:15:59.000Z", "America/New_York"],
|
||||
result: "2021-11-07T00:15:59.000-0400",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 2021 Nov 7, 12:15am NY EDT (2)",
|
||||
desc: "UTC to 2021 Nov 7, 12:15am NY EDT (2)",
|
||||
inputs: ["2021-11-07T00:15:59.000-0400", "America/New_York"],
|
||||
result: "2021-11-07T00:15:59.000-0400",
|
||||
},
|
||||
// 1:15am NY (DST) -0400
|
||||
// NOTE: 1:15am happens TWICE (with different offsets)
|
||||
{
|
||||
desc: "UTC Zulu to 2021 Nov 7, 1:15am NY EDT",
|
||||
desc: "UTC to 2021 Nov 7, 1:15am NY EDT",
|
||||
inputs: ["2021-11-07T05:15:59.000Z", "America/New_York"],
|
||||
result: "2021-11-07T01:15:59.000-0400",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 2021 Nov 7, 1:15am NY EDT (2)",
|
||||
desc: "UTC to 2021 Nov 7, 1:15am NY EDT (2)",
|
||||
inputs: ["2021-11-07T01:15:59.000-0400", "America/New_York"],
|
||||
result: "2021-11-07T01:15:59.000-0400",
|
||||
},
|
||||
// 1:15am NY (non-DST) -0500
|
||||
{
|
||||
desc: "UTC Zulu to 2021 Nov 7, 1:15am NY EST",
|
||||
desc: "UTC to 2021 Nov 7, 1:15am NY EST",
|
||||
inputs: ["2021-11-07T06:15:59.000Z", "America/New_York"],
|
||||
result: "2021-11-07T01:15:59.000-0500",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 2021 Nov 7, 1:15am NY EST (2)",
|
||||
desc: "UTC to 2021 Nov 7, 1:15am NY EST (2)",
|
||||
inputs: ["2021-11-07T01:15:59.000-0500", "America/New_York"],
|
||||
result: "2021-11-07T01:15:59.000-0500",
|
||||
},
|
||||
// 2:15am NY -0500
|
||||
{
|
||||
desc: "UTC Zulu to 2021 Nov 7, 2:15am NY EST",
|
||||
desc: "UTC to 2021 Nov 7, 2:15am NY EST",
|
||||
inputs: ["2021-11-07T07:15:59.000Z", "America/New_York"],
|
||||
result: "2021-11-07T02:15:59.000-0500",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 2021 Nov 7, 2:15am NY EST (2)",
|
||||
desc: "UTC to 2021 Nov 7, 2:15am NY EST (2)",
|
||||
inputs: ["2021-11-07T02:15:59.000-0500", "America/New_York"],
|
||||
result: "2021-11-07T02:15:59.000-0500",
|
||||
},
|
||||
// 3:15am NY
|
||||
{
|
||||
desc: "UTC Zulu to 2021 Nov 7, 3:15am NY EST",
|
||||
desc: "UTC to 2021 Nov 7, 3:15am NY EST",
|
||||
inputs: ["2021-11-07T08:15:59.000Z", "America/New_York"],
|
||||
result: "2021-11-07T03:15:59.000-0500",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to 2021 Nov 7, 3:15am NY EST (2)",
|
||||
desc: "UTC to 2021 Nov 7, 3:15am NY EST (2)",
|
||||
inputs: ["2021-11-07T03:15:59.000-0500", "America/New_York"],
|
||||
result: "2021-11-07T03:15:59.000-0500",
|
||||
},
|
||||
|
@ -168,29 +167,27 @@ function testTzToUtc(t) {
|
|||
|
||||
// Colombo +0530 (not DST)
|
||||
{
|
||||
desc: "UTC Zulu to Asia/Colombo (1)",
|
||||
desc: "UTC to Asia/Colombo (1)",
|
||||
inputs: ["2021-03-14T08:15:59.000Z", "Asia/Colombo"],
|
||||
result: "2021-03-14T13:45:59.000+0530",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to Asia/Colombo (2)",
|
||||
desc: "UTC to Asia/Colombo (2)",
|
||||
inputs: ["2021-03-14T13:45:59.000+0530", "Asia/Colombo"],
|
||||
result: "2021-03-14T13:45:59.000+0530",
|
||||
},
|
||||
{
|
||||
desc: "UTC Zulu to Asia/Colombo (3)",
|
||||
desc: "UTC to Asia/Colombo (3)",
|
||||
inputs: ["2021-11-07T08:15:59.000Z", "Asia/Colombo"],
|
||||
result: "2021-11-07T13:45:59.000+0530",
|
||||
},
|
||||
{
|
||||
desc: "ISO+Offset to Asia/Colombo (4)",
|
||||
desc: "UTC to Asia/Colombo (4)",
|
||||
inputs: ["2021-11-07T13:45:59.000+0530", "Asia/Colombo"],
|
||||
result: "2021-11-07T13:45:59.000+0530",
|
||||
},
|
||||
].forEach(testUtcToTz);
|
||||
console.info(
|
||||
"Pass: UTC/ISO+Offset to TZ for America/New_York and Asia/Colombo"
|
||||
);
|
||||
console.info("Pass: UTC to TZ for America/New_York and Asia/Colombo");
|
||||
|
||||
[
|
||||
//
|
||||
|
|
49
xtz.js
49
xtz.js
|
@ -1,4 +1,6 @@
|
|||
(function (exports) {
|
||||
var XTZ;
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
function toTimeZone(date, timeZone) {
|
||||
|
@ -60,16 +62,15 @@
|
|||
|
||||
function toTimeZoneISOString(date, timeZone) {
|
||||
var whole = toTimeZone(date, timeZone);
|
||||
return formatAsOffsetISOString(whole);
|
||||
return toOffsetISOString(whole);
|
||||
}
|
||||
|
||||
function _toOffsetISOString() {
|
||||
/* jshint validthis: true */
|
||||
return formatAsOffsetISOString(this);
|
||||
return toOffsetISOString(this);
|
||||
}
|
||||
|
||||
function getOffset(utcDate, tzD2) {
|
||||
var tzDate = new Date(formatAsOffsetISOString(tzD2));
|
||||
var tzDate = new Date(toOffsetISOString(tzD2));
|
||||
var diff = Math.round((tzDate.valueOf() - utcDate.valueOf()) / (60 * 1000));
|
||||
return diff;
|
||||
}
|
||||
|
@ -97,21 +98,10 @@
|
|||
}
|
||||
|
||||
// +0500, -0730
|
||||
return (
|
||||
offset + h.toString().padStart(2, "0") + m.toString().padStart(2, "0")
|
||||
);
|
||||
return offset + p2(h) + p2(m);
|
||||
}
|
||||
|
||||
function toOffsetISOString(date, timeZone) {
|
||||
if ("offset" in date && "year" in date) {
|
||||
return formatAsOffsetISOString(date);
|
||||
}
|
||||
|
||||
var whole = fromTimeZone(date, timeZone);
|
||||
return formatAsOffsetISOString(whole);
|
||||
}
|
||||
|
||||
function formatAsOffsetISOString(d) {
|
||||
function toOffsetISOString(d) {
|
||||
var offset = formatOffset(d.offset);
|
||||
return (
|
||||
`${d.year}-${p2(d.month + 1)}-${p2(d.day)}` +
|
||||
|
@ -121,7 +111,7 @@
|
|||
);
|
||||
}
|
||||
|
||||
function fromTimeZone(dt, tz) {
|
||||
function toUTC(dt, tz) {
|
||||
if ("string" === typeof dt) {
|
||||
// Either of these formats should work:
|
||||
// 2021-03-14 01:15:59
|
||||
|
@ -158,6 +148,11 @@
|
|||
return tzD3;
|
||||
}
|
||||
|
||||
function toUTCISOString(date, timeZone) {
|
||||
var whole = toUTC(date, timeZone);
|
||||
return toOffsetISOString(whole);
|
||||
}
|
||||
|
||||
function toLocalISOString(dateOrStr) {
|
||||
var d;
|
||||
if (dateOrStr) {
|
||||
|
@ -179,17 +174,15 @@
|
|||
return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${sss}${offset}`;
|
||||
}
|
||||
|
||||
exports.XTZ = {
|
||||
XTZ = {
|
||||
// bespoke date =>
|
||||
// 2021-11-07T3:15:59-0500
|
||||
// (todo?)
|
||||
// xtzToISOString: formatAsOffsetISOString,
|
||||
// (deprecated)
|
||||
toOffsetISOString: toOffsetISOString,
|
||||
|
||||
// -240 => -0400
|
||||
formatOffset: formatOffset,
|
||||
|
||||
// new Date() => "2021-11-07T03:15:59-0500"
|
||||
toLocalISOString: toLocalISOString,
|
||||
|
||||
// [ "2021-11-07T08:15:59Z", "America/New_York" ]
|
||||
|
@ -199,13 +192,11 @@
|
|||
|
||||
// [ "2021-11-07 03:15:59", "America/New_York" ]
|
||||
// => "2021-11-07T03:15:59-0500" // 2021-11-07T08:15:59Z
|
||||
toUTC: fromTimeZone,
|
||||
fromTimeZone: fromTimeZone,
|
||||
// deprecated
|
||||
toUTCISOString: toOffsetISOString,
|
||||
toUTC: toUTC,
|
||||
toUTCISOString: toUTCISOString,
|
||||
};
|
||||
|
||||
if ("undefined" != typeof module && module.exports) {
|
||||
module.exports = exports.XTZ;
|
||||
module.exports = XTZ;
|
||||
}
|
||||
})(("undefined" === typeof module && window) || exports);
|
||||
})();
|
||||
|
|
Loading…
Reference in New Issue