@capillarytech/cap-ui-utils 3.0.7-alpha.7 → 3.0.9-alpha.1
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/package.json +1 -1
- package/utils/timezone.js +42 -31
package/package.json
CHANGED
package/utils/timezone.js
CHANGED
|
@@ -17,39 +17,50 @@ export const DATE_FORMAT = "DD MMM YYYY h:mm A";
|
|
|
17
17
|
* @param {boolean} orgTZ - Whether to force conversion into org timezone.
|
|
18
18
|
* @returns {string} Formatted date string with strict TZ abbreviation or '-'.
|
|
19
19
|
*/
|
|
20
|
-
export const formatDateWithTimezone = (date, orgTZ = false) => {
|
|
21
|
-
|
|
20
|
+
export const formatDateWithTimezone = (date, orgTZ = false, onlyAbbr = false) => {
|
|
21
|
+
try{
|
|
22
|
+
if (!date) return "-";
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
let momentDate;
|
|
25
|
+
const orgTimezone = utilsLocalStorageApi.loadItem("orgTZ") || "Asia/Kolkata";
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
// Epoch (number or numeric string)
|
|
28
|
+
if (typeof date === "number" || (typeof date === "string" && /^\d+$/.test(date))) {
|
|
29
|
+
const timestamp = typeof date === "string" ? parseInt(date, 10) : date;
|
|
30
|
+
const timestampMs = timestamp < 1e12 ? timestamp * 1000 : timestamp;
|
|
31
|
+
momentDate = moment.tz(timestampMs, orgTimezone);
|
|
32
|
+
}
|
|
33
|
+
// ISO string or Date object
|
|
34
|
+
else {
|
|
35
|
+
momentDate = moment(date);
|
|
36
|
+
if(momentDate.isUTC() || orgTZ){
|
|
37
|
+
momentDate = momentDate.tz(orgTimezone);
|
|
38
|
+
}
|
|
37
39
|
}
|
|
38
|
-
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
if (!momentDate.isValid()) return "-";
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
// Get strict timezone abbreviation (no offsets like GMT+05:30)
|
|
44
|
+
let tzAbbr;
|
|
45
|
+
if (momentDate.isValid()) {
|
|
46
|
+
const tz = momentDate.tz() || orgTimezone;
|
|
47
|
+
tzAbbr = moment.tz.zone(tz)?.abbr(momentDate.valueOf()) || "UTC";
|
|
48
|
+
} else {
|
|
49
|
+
tzAbbr = "UTC"; // fallback
|
|
50
|
+
}
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
if(onlyAbbr){
|
|
53
|
+
return tzAbbr;
|
|
54
|
+
}
|
|
55
|
+
else{
|
|
56
|
+
return `${momentDate.format(DATE_FORMAT)} ${tzAbbr}`;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch(error){
|
|
60
|
+
console.error(error);
|
|
61
|
+
return "-";
|
|
62
|
+
}
|
|
63
|
+
};
|
|
53
64
|
|
|
54
65
|
/**
|
|
55
66
|
* Returns a tooltip string with the timezone and offset for a given date.
|
|
@@ -69,12 +80,12 @@ export const getTimezoneTooltip = (date, orgTZ = false) => {
|
|
|
69
80
|
const serverTimezone = utilsLocalStorageApi.loadItem("serverTZ") || "Asia/Kolkata";
|
|
70
81
|
|
|
71
82
|
if(orgTZ){
|
|
72
|
-
return
|
|
83
|
+
return `${orgTimezone} (UTC${moment.tz(orgTimezone).format("Z")})`;
|
|
73
84
|
}
|
|
74
85
|
else{
|
|
75
86
|
// case 1: epoch number or numeric string
|
|
76
87
|
if(typeof date === "number" || (typeof date === "string" && /^\d+$/.test(date))){
|
|
77
|
-
return
|
|
88
|
+
return `${orgTimezone} (UTC${moment.tz(orgTimezone).format("Z")})`;
|
|
78
89
|
}
|
|
79
90
|
// case 2: ISO string or Date object
|
|
80
91
|
else{
|
|
@@ -83,9 +94,9 @@ export const getTimezoneTooltip = (date, orgTZ = false) => {
|
|
|
83
94
|
m = moment.tz(date, "DD MMM YYYY h:mm A z"); // handles strings like 17 Sep 2025 6:00 AM EDT
|
|
84
95
|
}
|
|
85
96
|
if(!m.isValid() || m.isUTC()){
|
|
86
|
-
return
|
|
97
|
+
return `${orgTimezone} (UTC${moment.tz(orgTimezone).format("Z")})`; // fallback to orgTZ
|
|
87
98
|
}
|
|
88
|
-
return
|
|
99
|
+
return `${serverTimezone} (UTC${m.format("Z")})`; // return the timezone and offset
|
|
89
100
|
}
|
|
90
101
|
}
|
|
91
102
|
};
|