@capillarytech/cap-ui-utils 3.0.7-alpha.1 → 3.0.7-alpha.2

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/index.js CHANGED
@@ -11,3 +11,4 @@ export { default as compileHandlebars } from './utils/compileHandlebars';
11
11
  export { default as sanitizeTemplateWithRegexp } from "./utils/contentSanitizationHelper";
12
12
  export { default as decompressJsonObject } from "./utils/zlibDataDecompress";
13
13
  export { default as getEnvVariable } from "./utils/getEnvVariables";
14
+ export { formatDateWithTimezone, getTimezoneTooltip } from "./utils/timezone";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "3.0.7-alpha.1",
3
+ "version": "3.0.7-alpha.2",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,91 @@
1
+ import moment from "moment-timezone";
2
+ import * as utilsLocalStorageApi from "./utilsLocalStorageApi";
3
+
4
+ export const DATE_FORMAT = "DD MMM YYYY h:mm A";
5
+
6
+ /**
7
+ * Formats a date into "DD MMM YYYY h:mm A <TZ>" format.
8
+ * Handles ISO strings and epoch timestamps.
9
+ *
10
+ * - ISO input:
11
+ * - orgTZ=false → keep ISO timezone info
12
+ * - orgTZ=true → convert to org timezone (serverTZ)
13
+ * - Epoch input:
14
+ * - Always converts to org timezone (serverTZ)
15
+ *
16
+ * @param {string|number|Date} date - ISO string, epoch (sec/ms), or Date object.
17
+ * @param {boolean} orgTZ - Whether to force conversion into org timezone.
18
+ * @returns {string} Formatted date string with strict TZ abbreviation or '-'.
19
+ */
20
+ export const formatDateWithTimezone = (date, orgTZ = false) => {
21
+ if (!date) return "-";
22
+
23
+ let momentDate;
24
+ const orgTimezone = utilsLocalStorageApi.loadItem("serverTZ") || "Asia/Kolkata";
25
+
26
+ // Epoch (number or numeric string)
27
+ if (typeof date === "number" || (typeof date === "string" && /^\d+$/.test(date))) {
28
+ const timestamp = typeof date === "string" ? parseInt(date, 10) : date;
29
+ const timestampMs = timestamp < 1e12 ? timestamp * 1000 : timestamp;
30
+ momentDate = moment.tz(timestampMs, orgTimezone);
31
+ }
32
+ // ISO string or Date object
33
+ else {
34
+ momentDate = moment(date);
35
+ if (orgTZ) {
36
+ momentDate = momentDate.tz(orgTimezone);
37
+ }
38
+ }
39
+
40
+ if (!momentDate.isValid()) return "-";
41
+
42
+ // Get strict timezone abbreviation (no offsets like GMT+05:30)
43
+ let tzAbbr = momentDate.format("z");
44
+ if (/GMT[+-]\d+/.test(tzAbbr)) {
45
+ const tz = momentDate.tz() || orgTimezone || "UTC";
46
+ tzAbbr = moment.tz.zone(tz)?.abbr(momentDate.valueOf()) || "UTC";
47
+ }
48
+
49
+ return `${momentDate.format(DATE_FORMAT)} ${tzAbbr}`;
50
+ }
51
+
52
+ /**
53
+ * Returns a tooltip string with the timezone and offset for a given date.
54
+ *
55
+ * - Epoch input:
56
+ * - Always uses serverTZ
57
+ * - ISO input:
58
+ * - orgTZ=false → keep ISO timezone info
59
+ * - orgTZ=true → convert to org timezone (serverTZ)
60
+ *
61
+ * @param {string|number|Date} date - ISO string, epoch (sec/ms), or Date object.
62
+ * @param {boolean} orgTZ - Whether to force conversion into org timezone.
63
+ * @returns {string} Tooltip string with timezone and offset.
64
+ */
65
+ export const getTimezoneTooltip = (date, orgTZ = false) => {
66
+ const serverTZ = utilsLocalStorageApi.loadItem("serverTZ") || "Asia/Kolkata";
67
+
68
+ // Case 1: Epoch number or numeric string
69
+ if (typeof date === "number" || (typeof date === "string" && /^\d+$/.test(date))) {
70
+ return `Timezone: ${serverTZ} (UTC${moment.tz(serverTZ).format("Z")})`;
71
+ }
72
+
73
+ // Case 2: orgTZ = true → always use serverTZ
74
+ if (orgTZ) {
75
+ return `Timezone: ${serverTZ} (UTC${moment.tz(serverTZ).format("Z")})`;
76
+ }
77
+
78
+ // Case 3: Try parsing ISO or custom string
79
+ let m = moment.parseZone(date); // handles ISO strings like 2025-08-12T23:59:59.000Z
80
+
81
+ if (!m.isValid()) {
82
+ m = moment.tz(date, "DD MMM YYYY h:mm A z");
83
+ }
84
+
85
+ // If still invalid → fallback to serverTZ
86
+ if (!m.isValid()) {
87
+ return `Timezone: ${serverTZ} (UTC${moment.tz(serverTZ).format("Z")})`;
88
+ }
89
+
90
+ return `Timezone: ${m.tz() || serverTZ} (UTC${m.format("Z")})`;
91
+ };