mirror of https://github.com/therootcompany/tz.js
make Prettier
This commit is contained in:
parent
aaa31a25b9
commit
ff9e266052
|
@ -0,0 +1 @@
|
||||||
|
*.min.js
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"printWidth": 80,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"singleQuote": false,
|
||||||
|
"bracketSpacing": true,
|
||||||
|
"semi": true
|
||||||
|
}
|
14
README.md
14
README.md
|
@ -1,8 +1,7 @@
|
||||||
# [xtz.js](https://github.com/therootcompany/tz.js)
|
# [xtz.js](https://github.com/therootcompany/tz.js)
|
||||||
|
|
||||||
A fast, lightweight, zero-dependency library to
|
A fast, lightweight, zero-dependency library to translate between Time Zones and UTC with native `Intl.DateTimeFormat`
|
||||||
translate between Time Zones and UTC with native
|
in ~100 LoC. For Node.js & Browsers.
|
||||||
`Intl.DateTimeFormat` in ~100 LoC. For Node.js & Browsers.
|
|
||||||
|
|
||||||
XTZ is a poor man's Temporal polyfill, but just for time zones.
|
XTZ is a poor man's Temporal polyfill, but just for time zones.
|
||||||
|
|
||||||
|
@ -155,7 +154,8 @@ Or our bespoke date object:
|
||||||
var utcDate = TZ.toUTC("2021-11-07 03:15:59.000", "America/New_York");
|
var utcDate = TZ.toUTC("2021-11-07 03:15:59.000", "America/New_York");
|
||||||
```
|
```
|
||||||
|
|
||||||
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).
|
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
|
```js
|
||||||
var utcDate = TZ.toUTC(
|
var utcDate = TZ.toUTC(
|
||||||
|
@ -180,7 +180,8 @@ utcDate.toISOString();
|
||||||
|
|
||||||
Q: What happens in March when 2am is skipped?
|
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.
|
- 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
|
```js
|
||||||
var utcDate = TZ.toUTC("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();
|
utcDate.toISOString();
|
||||||
|
@ -190,7 +191,8 @@ Q: What happens in March when 2am is skipped?
|
||||||
|
|
||||||
Q: What happens in November when 2am happens twice?
|
Q: What happens in November when 2am happens twice?
|
||||||
|
|
||||||
- A: Although both 2ams are distinguishable with ISO offset times, only the first can be resolved from a local time with this library.
|
- A: Although both 2ams are distinguishable with ISO offset times, only the first can be resolved from a local time
|
||||||
|
with this library.
|
||||||
```js
|
```js
|
||||||
var utcDate = TZ.toUTC("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();
|
utcDate.toISOString();
|
||||||
|
|
16
examples.js
16
examples.js
|
@ -22,9 +22,7 @@ var XTZ;
|
||||||
console.info();
|
console.info();
|
||||||
|
|
||||||
console.info("\t// during daylight savings");
|
console.info("\t// during daylight savings");
|
||||||
console.info(
|
console.info(`\tXTZ.toUTC("2021-03-14 08:15:59.000", "America/New_York")`);
|
||||||
`\tXTZ.toUTC("2021-03-14 08:15:59.000", "America/New_York")`
|
|
||||||
);
|
|
||||||
console.info(`\ttzDate.toISOString()`);
|
console.info(`\ttzDate.toISOString()`);
|
||||||
tzDate = XTZ.toUTC("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(
|
console.info(
|
||||||
|
@ -35,9 +33,7 @@ var XTZ;
|
||||||
console.info();
|
console.info();
|
||||||
|
|
||||||
console.info("\t// during standard time");
|
console.info("\t// during standard time");
|
||||||
console.info(
|
console.info(`\tXTZ.toUTC("2021-11-07 08:15:59.000", "America/New_York")`);
|
||||||
`\tXTZ.toUTC("2021-11-07 08:15:59.000", "America/New_York")`
|
|
||||||
);
|
|
||||||
console.info(`\ttzDate.toISOString()`);
|
console.info(`\ttzDate.toISOString()`);
|
||||||
tzDate = XTZ.toUTC("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(
|
console.info(
|
||||||
|
@ -52,9 +48,7 @@ var XTZ;
|
||||||
// Time Zone-relative time translated to UTC
|
// Time Zone-relative time translated to UTC
|
||||||
//
|
//
|
||||||
function demo2() {
|
function demo2() {
|
||||||
console.info(
|
console.info("What time is it in New York at 8:15am on March 14th UTC?");
|
||||||
"What time is it in New York at 8:15am on March 14th UTC?"
|
|
||||||
);
|
|
||||||
console.info();
|
console.info();
|
||||||
|
|
||||||
console.info("\t// during daylight savings");
|
console.info("\t// during daylight savings");
|
||||||
|
@ -71,9 +65,7 @@ var XTZ;
|
||||||
console.info();
|
console.info();
|
||||||
|
|
||||||
console.info("\t// during standard time");
|
console.info("\t// during standard time");
|
||||||
console.info(
|
console.info(`\tXTZ.toUTC("2021-11-07T08:15:59.000Z", "America/New_York")`);
|
||||||
`\tXTZ.toUTC("2021-11-07T08:15:59.000Z", "America/New_York")`
|
|
||||||
);
|
|
||||||
console.info(`\ttzDate.toISOString()`);
|
console.info(`\ttzDate.toISOString()`);
|
||||||
tzDate = XTZ.toUTC("2021-11-07T08:15:59.000Z", "America/New_York");
|
tzDate = XTZ.toUTC("2021-11-07T08:15:59.000Z", "America/New_York");
|
||||||
console.info(
|
console.info(
|
||||||
|
|
9
xtz.js
9
xtz.js
|
@ -70,8 +70,7 @@ var XTZ;
|
||||||
|
|
||||||
function getOffset(utcDate, tzD2) {
|
function getOffset(utcDate, tzD2) {
|
||||||
var tzDate = new Date(toOffsetISOString(tzD2));
|
var tzDate = new Date(toOffsetISOString(tzD2));
|
||||||
var diff =
|
var diff = Math.round(tzDate.valueOf() - utcDate.valueOf()) / (60 * 1000);
|
||||||
Math.round(tzDate.valueOf() - utcDate.valueOf()) / (60 * 1000);
|
|
||||||
return diff;
|
return diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,9 +98,7 @@ var XTZ;
|
||||||
|
|
||||||
// +0500, -0730
|
// +0500, -0730
|
||||||
return (
|
return (
|
||||||
offset +
|
offset + h.toString().padStart(2, "0") + m.toString().padStart(2, "0")
|
||||||
h.toString().padStart(2, "0") +
|
|
||||||
m.toString().padStart(2, "0")
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,4 +176,4 @@ var XTZ;
|
||||||
if ("undefined" != typeof module && module.exports) {
|
if ("undefined" != typeof module && module.exports) {
|
||||||
module.exports = XTZ;
|
module.exports = XTZ;
|
||||||
}
|
}
|
||||||
}());
|
})();
|
||||||
|
|
Loading…
Reference in New Issue