@osovitny/anatoly 3.17.4 → 3.17.6

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.
@@ -13,7 +13,6 @@ import { BrowserUtils, EventType, InteractionStatus, InteractionType, Interactio
13
13
  import * as i1$2 from '@angular/common';
14
14
  import { LOCATION_INITIALIZED, DOCUMENT, CommonModule } from '@angular/common';
15
15
  import { format, formatDistance, formatDistanceToNow } from 'date-fns';
16
- import { utcToZonedTime } from 'date-fns-tz';
17
16
  import enUS from 'date-fns/locale/en-US';
18
17
  import * as i1$3 from '@ngx-translate/core';
19
18
  import { TranslateService, TranslateModule, TranslateLoader } from '@ngx-translate/core';
@@ -237,30 +236,6 @@ class DateConvert {
237
236
  return new Date(NaN);
238
237
  }
239
238
  }
240
- static localToUtcDate(value) {
241
- if (value) {
242
- return new Date(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate());
243
- }
244
- return null;
245
- }
246
- static localToUtcDateTime(value) {
247
- if (value) {
248
- return new Date(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate(), value.getUTCHours(), value.getUTCMinutes(), value.getUTCSeconds(), value.getUTCMilliseconds());
249
- }
250
- return null;
251
- }
252
- static utcToLocalDate(value) {
253
- if (value) {
254
- return new Date(Date.UTC(value.getFullYear(), value.getMonth(), value.getDate()));
255
- }
256
- return null;
257
- }
258
- static utcToLocalDateTime(value) {
259
- if (value) {
260
- return new Date(Date.UTC(value.getFullYear(), value.getMonth(), value.getDate(), value.getHours(), value.getMinutes(), value.getSeconds(), value.getMilliseconds()));
261
- }
262
- return null;
263
- }
264
239
  }
265
240
 
266
241
  /*
@@ -1655,6 +1630,7 @@ class Subs {
1655
1630
  09 Feb 2024
1656
1631
 
1657
1632
  Sources:
1633
+ https://playcode.io/javascript
1658
1634
  https://github.com/date-fns/date-fns/blob/main/src/isDate/index.ts
1659
1635
  https://github.com/date-fns/date-fns/blob/main/src/isValid/index.ts
1660
1636
 
@@ -1740,6 +1716,258 @@ is = {
1740
1716
  };
1741
1717
  */
1742
1718
 
1719
+ /*
1720
+ <file>
1721
+ Project:
1722
+ @osovitny/anatoly
1723
+
1724
+ Authors:
1725
+ Vadim Osovitny vadim@osovitny.com
1726
+ Anatoly Osovitny anatoly@osovitny.com
1727
+
1728
+ Created:
1729
+ 10 Feb 2024
1730
+
1731
+ Copyright (c) 2016-2022 Osovitny Inc. All rights reserved.
1732
+ </file>
1733
+ */
1734
+ /**
1735
+ * Returns the [year, month, day, hour, minute, seconds] tokens of the provided
1736
+ * `date` as it will be rendered in the `timeZone`.
1737
+ */
1738
+ function tzTokenizeDate(date, timeZone) {
1739
+ var dtf = getDateTimeFormat(timeZone);
1740
+ return dtf.formatToParts ? partsOffset(dtf, date) : hackyOffset(dtf, date);
1741
+ }
1742
+ var typeToPos = {
1743
+ year: 0,
1744
+ month: 1,
1745
+ day: 2,
1746
+ hour: 3,
1747
+ minute: 4,
1748
+ second: 5,
1749
+ };
1750
+ function partsOffset(dtf, date) {
1751
+ try {
1752
+ var formatted = dtf.formatToParts(date);
1753
+ var filled = [];
1754
+ for (var i = 0; i < formatted.length; i++) {
1755
+ var pos = typeToPos[formatted[i].type];
1756
+ if (pos >= 0) {
1757
+ filled[pos] = parseInt(formatted[i].value, 10);
1758
+ }
1759
+ }
1760
+ return filled;
1761
+ }
1762
+ catch (error) {
1763
+ if (error instanceof RangeError) {
1764
+ return [NaN];
1765
+ }
1766
+ throw error;
1767
+ }
1768
+ }
1769
+ function hackyOffset(dtf, date) {
1770
+ var formatted = dtf.format(date).replace(/\u200E/g, '');
1771
+ var parsed = /(\d+)\/(\d+)\/(\d+),? (\d+):(\d+):(\d+)/.exec(formatted);
1772
+ // var [, fMonth, fDay, fYear, fHour, fMinute, fSecond] = parsed
1773
+ // return [fYear, fMonth, fDay, fHour, fMinute, fSecond]
1774
+ return [parsed[3], parsed[1], parsed[2], parsed[4], parsed[5], parsed[6]];
1775
+ }
1776
+ // Get a cached Intl.DateTimeFormat instance for the IANA `timeZone`. This can be used
1777
+ // to get deterministic local date/time output according to the `en-US` locale which
1778
+ // can be used to extract local time parts as necessary.
1779
+ var dtfCache = {};
1780
+ function getDateTimeFormat(timeZone) {
1781
+ if (!dtfCache[timeZone]) {
1782
+ // New browsers use `hourCycle`, IE and Chrome <73 does not support it and uses `hour12`
1783
+ var testDateFormatted = new Intl.DateTimeFormat('en-US', {
1784
+ hour12: false,
1785
+ timeZone: 'America/New_York',
1786
+ year: 'numeric',
1787
+ month: 'numeric',
1788
+ day: '2-digit',
1789
+ hour: '2-digit',
1790
+ minute: '2-digit',
1791
+ second: '2-digit',
1792
+ }).format(new Date('2014-06-25T04:00:00.123Z'));
1793
+ var hourCycleSupported = testDateFormatted === '06/25/2014, 00:00:00' ||
1794
+ testDateFormatted === '06/25/2014 00:00:00';
1795
+ dtfCache[timeZone] = hourCycleSupported
1796
+ ? new Intl.DateTimeFormat('en-US', {
1797
+ hour12: false,
1798
+ timeZone: timeZone,
1799
+ year: 'numeric',
1800
+ month: 'numeric',
1801
+ day: '2-digit',
1802
+ hour: '2-digit',
1803
+ minute: '2-digit',
1804
+ second: '2-digit',
1805
+ })
1806
+ : new Intl.DateTimeFormat('en-US', {
1807
+ hourCycle: 'h23',
1808
+ timeZone: timeZone,
1809
+ year: 'numeric',
1810
+ month: 'numeric',
1811
+ day: '2-digit',
1812
+ hour: '2-digit',
1813
+ minute: '2-digit',
1814
+ second: '2-digit',
1815
+ });
1816
+ }
1817
+ return dtfCache[timeZone];
1818
+ }
1819
+
1820
+ /*
1821
+ <file>
1822
+ Project:
1823
+ @osovitny/anatoly
1824
+
1825
+ Authors:
1826
+ Vadim Osovitny vadim@osovitny.com
1827
+ Anatoly Osovitny anatoly@osovitny.com
1828
+
1829
+ Created:
1830
+ 10 Feb 2024
1831
+
1832
+ Copyright (c) 2016-2022 Osovitny Inc. All rights reserved.
1833
+ </file>
1834
+ */
1835
+ function tzParseTimezone(timezoneString, date, isUtcDate) {
1836
+ var token;
1837
+ var absoluteOffset;
1838
+ // Empty string
1839
+ if (!timezoneString) {
1840
+ return 0;
1841
+ }
1842
+ // Z
1843
+ token = patterns.timezoneZ.exec(timezoneString);
1844
+ if (token) {
1845
+ return 0;
1846
+ }
1847
+ var hours;
1848
+ // ±hh
1849
+ token = patterns.timezoneHH.exec(timezoneString);
1850
+ if (token) {
1851
+ hours = parseInt(token[1], 10);
1852
+ if (!validateTimezone(hours)) {
1853
+ return NaN;
1854
+ }
1855
+ return -(hours * MILLISECONDS_IN_HOUR);
1856
+ }
1857
+ // ±hh:mm or ±hhmm
1858
+ token = patterns.timezoneHHMM.exec(timezoneString);
1859
+ if (token) {
1860
+ hours = parseInt(token[1], 10);
1861
+ var minutes = parseInt(token[2], 10);
1862
+ if (!validateTimezone(hours, minutes)) {
1863
+ return NaN;
1864
+ }
1865
+ absoluteOffset = Math.abs(hours) * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE;
1866
+ return hours > 0 ? -absoluteOffset : absoluteOffset;
1867
+ }
1868
+ // IANA time zone
1869
+ if (isValidTimezoneIANAString(timezoneString)) {
1870
+ date = new Date(date || Date.now());
1871
+ var utcDate = isUtcDate ? date : toUtcDate(date);
1872
+ var offset = calcOffset(utcDate, timezoneString);
1873
+ var fixedOffset = isUtcDate ? offset : fixOffset(date, offset, timezoneString);
1874
+ return -fixedOffset;
1875
+ }
1876
+ return NaN;
1877
+ }
1878
+ var MILLISECONDS_IN_HOUR = 3600000;
1879
+ var MILLISECONDS_IN_MINUTE = 60000;
1880
+ var patterns = {
1881
+ timezone: /([Z+-].*)$/,
1882
+ timezoneZ: /^(Z)$/,
1883
+ timezoneHH: /^([+-]\d{2})$/,
1884
+ timezoneHHMM: /^([+-]\d{2}):?(\d{2})$/,
1885
+ };
1886
+ function newDateUTC(fullYear, month, day, hour, minute, second, millisecond) {
1887
+ var utcDate = new Date(0);
1888
+ utcDate.setUTCFullYear(fullYear, month, day);
1889
+ utcDate.setUTCHours(hour, minute, second, millisecond);
1890
+ return utcDate;
1891
+ }
1892
+ function toUtcDate(date) {
1893
+ return newDateUTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
1894
+ }
1895
+ function calcOffset(date, timezoneString) {
1896
+ var tokens = tzTokenizeDate(date, timezoneString);
1897
+ // ms dropped because it's not provided by tzTokenizeDate
1898
+ var asUTC = newDateUTC(tokens[0], tokens[1] - 1, tokens[2], tokens[3] % 24, tokens[4], tokens[5], 0).getTime();
1899
+ var asTS = date.getTime();
1900
+ var over = asTS % 1000;
1901
+ asTS -= over >= 0 ? over : 1000 + over;
1902
+ return asUTC - asTS;
1903
+ }
1904
+ function fixOffset(date, offset, timezoneString) {
1905
+ var localTS = date.getTime();
1906
+ // Our UTC time is just a guess because our offset is just a guess
1907
+ var utcGuess = localTS - offset;
1908
+ // Test whether the zone matches the offset for this ts
1909
+ var o2 = calcOffset(new Date(utcGuess), timezoneString);
1910
+ // If so, offset didn't change, and we're done
1911
+ if (offset === o2) {
1912
+ return offset;
1913
+ }
1914
+ // If not, change the ts by the difference in the offset
1915
+ utcGuess -= o2 - offset;
1916
+ // If that gives us the local time we want, we're done
1917
+ var o3 = calcOffset(new Date(utcGuess), timezoneString);
1918
+ if (o2 === o3) {
1919
+ return o2;
1920
+ }
1921
+ // If it's different, we're in a hole time. The offset has changed, but we don't adjust the time
1922
+ return Math.max(o2, o3);
1923
+ }
1924
+ function validateTimezone(hours, minutes = null) {
1925
+ return -23 <= hours && hours <= 23 && (minutes == null || (0 <= minutes && minutes <= 59));
1926
+ }
1927
+ var validIANATimezoneCache = {};
1928
+ function isValidTimezoneIANAString(timeZoneString) {
1929
+ if (validIANATimezoneCache[timeZoneString])
1930
+ return true;
1931
+ try {
1932
+ new Intl.DateTimeFormat(undefined, { timeZone: timeZoneString });
1933
+ validIANATimezoneCache[timeZoneString] = true;
1934
+ return true;
1935
+ }
1936
+ catch (error) {
1937
+ return false;
1938
+ }
1939
+ }
1940
+
1941
+ /*
1942
+ <file>
1943
+ Project:
1944
+ @osovitny/anatoly
1945
+
1946
+ Authors:
1947
+ Vadim Osovitny vadim@osovitny.com
1948
+ Anatoly Osovitny anatoly@osovitny.com
1949
+
1950
+ Created:
1951
+ 10 Feb 2024
1952
+
1953
+ Source:
1954
+ https://www.npmjs.com/package/date-fns-tz
1955
+ https://github.com/marnusw/date-fns-tz/blob/master/src/utcToZonedTime/index.js
1956
+ https://github.com/marnusw/date-fns-tz/blob/master/src/_lib/tzParseTimezone/index.js
1957
+
1958
+ Copyright (c) 2016-2022 Osovitny Inc. All rights reserved.
1959
+ </file>
1960
+ */
1961
+ function utcToZonedTime(dirtyDate, timeZone) {
1962
+ let date = DateConvert.toDate(dirtyDate);
1963
+ let offsetMilliseconds = tzParseTimezone(timeZone, date, true);
1964
+ let d = new Date(date.getTime() - offsetMilliseconds);
1965
+ let resultDate = new Date(0);
1966
+ resultDate.setFullYear(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
1967
+ resultDate.setHours(d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
1968
+ return resultDate;
1969
+ }
1970
+
1743
1971
  /*
1744
1972
  <file>
1745
1973
  Project: