@zajno/common 3.0.11 → 3.0.12

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,42 @@
1
+ /** This is experimental stuff, so no tests provided, see comments in the code */
2
+ export var Timezones;
3
+ (function (Timezones) {
4
+ Timezones.Current = Intl.DateTimeFormat().resolvedOptions().timeZone;
5
+ Timezones.CurrentOffset = new Date().getTimezoneOffset();
6
+ // https://stackoverflow.com/a/64262840/9053142
7
+ // tested locally, but it's hard to write tests since they are based on the local timezone, browser implementation and whether DST is active
8
+ // it's recommended to wrap with try/catch and don't rely it will always work
9
+ Timezones.getOffset = (timeZone) => {
10
+ const timeZoneName = Intl.DateTimeFormat('ia', {
11
+ timeZoneName: 'short',
12
+ timeZone,
13
+ })
14
+ .formatToParts()
15
+ .find((i) => i.type === 'timeZoneName').value;
16
+ const offset = timeZoneName.slice(3);
17
+ if (!offset) {
18
+ return 0;
19
+ }
20
+ const matchData = offset.match(/([+-])(\d+)(?::(\d+))?/);
21
+ if (!matchData) {
22
+ throw `cannot parse timezone name: ${timeZoneName}`;
23
+ }
24
+ const [, sign, hour, minute] = matchData;
25
+ let result = parseInt(hour, 10) * 60;
26
+ if (sign === '+') {
27
+ result *= -1;
28
+ }
29
+ if (minute) {
30
+ result += parseInt(minute, 10);
31
+ }
32
+ return result;
33
+ };
34
+ // Tests may look like this:
35
+ /*
36
+ expect(Timezones.getOffset('America/New_York')).toBe(240);
37
+ expect(Timezones.getOffset('Europe/Paris')).toBe(-120);
38
+ expect(Timezones.getOffset('Europe/Kiev')).toBe(-180);
39
+ expect(Timezones.getOffset('Asia/Kolkata')).toBe(-270);
40
+ */
41
+ })(Timezones || (Timezones = {}));
42
+ //# sourceMappingURL=tz.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tz.js","sourceRoot":"","sources":["../../src/dates/tz.ts"],"names":[],"mappings":"AACA,iFAAiF;AACjF,MAAM,KAAW,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,KAAT,SAAS,QA8CzB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zajno/common",
3
- "version": "3.0.11",
3
+ "version": "3.0.12",
4
4
  "description": "Zajno's re-usable utilities for JS/TS projects",
5
5
  "repository": {
6
6
  "type": "git",