@zajno/common 2.0.19 → 2.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dates/tz.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /** This is experimental stuff, so no tests provided, see comments in the code */
2
+ export declare namespace Timezones {
3
+ const Current: string;
4
+ const CurrentOffset: number;
5
+ const getOffset: (timeZone: string) => number;
6
+ }
7
+ //# sourceMappingURL=tz.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tz.d.ts","sourceRoot":"","sources":["../../src/dates/tz.ts"],"names":[],"mappings":"AACA,iFAAiF;AACjF,yBAAiB,SAAS,CAAC;IAEhB,MAAM,OAAO,QAAmD,CAAC;IACjE,MAAM,aAAa,QAAiC,CAAC;IAKrD,MAAM,SAAS,aAAc,MAAM,WA4BzC,CAAC;CAUL"}
package/dates/tz.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Timezones = void 0;
4
+ /** This is experimental stuff, so no tests provided, see comments in the code */
5
+ var Timezones;
6
+ (function (Timezones) {
7
+ Timezones.Current = Intl.DateTimeFormat().resolvedOptions().timeZone;
8
+ Timezones.CurrentOffset = new Date().getTimezoneOffset();
9
+ // https://stackoverflow.com/a/64262840/9053142
10
+ // tested locally, but it's hard to write tests since they are based on the local timezone, browser implementation and whether DST is active
11
+ // it's recommended to wrap with try/catch and don't rely it will always work
12
+ Timezones.getOffset = (timeZone) => {
13
+ const timeZoneName = Intl.DateTimeFormat('ia', {
14
+ timeZoneName: 'short',
15
+ timeZone,
16
+ })
17
+ .formatToParts()
18
+ .find((i) => i.type === 'timeZoneName').value;
19
+ const offset = timeZoneName.slice(3);
20
+ if (!offset) {
21
+ return 0;
22
+ }
23
+ const matchData = offset.match(/([+-])(\d+)(?::(\d+))?/);
24
+ if (!matchData) {
25
+ throw `cannot parse timezone name: ${timeZoneName}`;
26
+ }
27
+ const [, sign, hour, minute] = matchData;
28
+ let result = parseInt(hour, 10) * 60;
29
+ if (sign === '+') {
30
+ result *= -1;
31
+ }
32
+ if (minute) {
33
+ result += parseInt(minute, 10);
34
+ }
35
+ return result;
36
+ };
37
+ // Tests may look like this:
38
+ /*
39
+ expect(Timezones.getOffset('America/New_York')).toBe(240);
40
+ expect(Timezones.getOffset('Europe/Paris')).toBe(-120);
41
+ expect(Timezones.getOffset('Europe/Kiev')).toBe(-180);
42
+ expect(Timezones.getOffset('Asia/Kolkata')).toBe(-270);
43
+ */
44
+ })(Timezones = exports.Timezones || (exports.Timezones = {}));
45
+ //# sourceMappingURL=tz.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tz.js","sourceRoot":"","sources":["../../src/dates/tz.ts"],"names":[],"mappings":";;;AACA,iFAAiF;AACjF,IAAiB,SAAS,CA8CzB;AA9CD,WAAiB,SAAS;IAET,iBAAO,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ,CAAC;IAC3D,uBAAa,GAAG,IAAI,IAAI,EAAE,CAAC,iBAAiB,EAAE,CAAC;IAE5D,+CAA+C;IAC/C,4IAA4I;IAC5I,6EAA6E;IAChE,mBAAS,GAAG,CAAC,QAAgB,EAAE,EAAE;QAC1C,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;YAC3C,YAAY,EAAE,OAAO;YACrB,QAAQ;SACX,CAAC;aACG,aAAa,EAAE;aACf,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,cAAc,CAAC,CAAC,KAAK,CAAC;QAElD,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACrC,IAAI,CAAC,MAAM,EAAE;YACT,OAAO,CAAC,CAAC;SACZ;QAED,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;QACzD,IAAI,CAAC,SAAS,EAAE;YACZ,MAAM,+BAA+B,YAAY,EAAE,CAAC;SACvD;QAED,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;QACzC,IAAI,MAAM,GAAG,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC;QACrC,IAAI,IAAI,KAAK,GAAG,EAAE;YACd,MAAM,IAAI,CAAC,CAAC,CAAC;SAChB;QACD,IAAI,MAAM,EAAE;YACR,MAAM,IAAI,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;SAClC;QAED,OAAO,MAAM,CAAC;IAClB,CAAC,CAAC;IAEF,4BAA4B;IAC5B;;;;;MAKE;AAEN,CAAC,EA9CgB,SAAS,GAAT,iBAAS,KAAT,iBAAS,QA8CzB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zajno/common",
3
- "version": "2.0.19",
3
+ "version": "2.0.20",
4
4
  "description": "Zajno's re-usable utilities for JS/TS projects",
5
5
  "repository": {
6
6
  "type": "git",