@capillarytech/cap-ui-utils 3.0.9-alpha.1 → 3.0.9-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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "3.0.9-alpha.1",
3
+ "version": "3.0.9-alpha.2",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -105,4 +105,10 @@ export const TMPL_SANITIZE_FORBIDDEN_JS_TAGS = [
105
105
  "vbscript:",
106
106
  "livescript:",
107
107
  "xss:",
108
- ];
108
+ ];
109
+
110
+ /**
111
+ * Constants for timezone feature
112
+ */
113
+ export const TIMEZONE_ENABLED = "TIMEZONE_ENABLED";
114
+ export const DATE_FORMAT = "DD MMM YYYY h:mm A";
package/utils/timezone.js CHANGED
@@ -1,62 +1,62 @@
1
1
  import moment from "moment-timezone";
2
2
  import * as utilsLocalStorageApi from "./utilsLocalStorageApi";
3
+ import { DATE_FORMAT,TIMEZONE_ENABLED } from "./constants";
4
+ import hasFeatureAccess from "../auth/hasFeatureAccess";
3
5
 
4
- export const DATE_FORMAT = "DD MMM YYYY h:mm A";
6
+ const getTimezoneFormattedDate = (date,orgTZ = false,serverTZ = false,customTZ = null) => {
7
+ const orgTimezone = utilsLocalStorageApi.loadItem("orgTZ") || "Asia/Kolkata";
8
+ const serverTimezone = utilsLocalStorageApi.loadItem("serverTZ") || "Asia/Kolkata";
9
+ const targetTimezone = customTZ || (serverTZ ? serverTimezone : orgTZ ? orgTimezone : orgTimezone);
10
+ let momentDate;
11
+
12
+ // If date is epoch (number or numeric string)
13
+ if (typeof date === "number" || (typeof date === "string" && /^\d+$/.test(date))) {
14
+ const timestamp = typeof date === "string" ? parseInt(date, 10) : date;
15
+ const timestampMs = timestamp < 1e12 ? timestamp * 1000 : timestamp;
16
+ momentDate = moment.utc(timestampMs).tz(targetTimezone);
17
+ } else {
18
+ momentDate = moment.tz(date, targetTimezone);
19
+ }
20
+
21
+ return momentDate;
22
+ }
5
23
 
6
24
  /**
7
25
  * Formats a date into "DD MMM YYYY h:mm A <TZ>" format.
8
- * Handles ISO strings and epoch timestamps.
26
+ * Handles epoch timestamps and ISO date strings with timezone offsets.
27
+ *
28
+ * Timezone Selection Priority:
29
+ * 1. customTZ (if provided) - overrides all others
30
+ * 2. serverTZ=true → uses server timezone from localStorage
31
+ * 3. orgTZ=true → uses organization timezone from localStorage
32
+ * 4. Default → uses organization timezone from localStorage
9
33
  *
10
- * - ISO input:
11
- * - orgTZ=false keep ISO timezone info
12
- * - orgTZ=true → convert to org timezone
13
- * - Epoch input:
14
- * - Always converts to org timezone
34
+ * Input Handling:
35
+ * - Epoch input (number or numeric string):
36
+ * - Treated as UTC timestamp (seconds or milliseconds)
37
+ * - Always converts to target timezone
38
+ * - ISO input (e.g., "2025-01-09T00:00:00+05:30"):
39
+ * - Parses timezone offset correctly
40
+ * - Converts to target timezone (doesn't preserve original timezone)
41
+ * - Date object:
42
+ * - Converts to target timezone
15
43
  *
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 '-'.
44
+ * @param {string|number|Date} date - Epoch timestamp, ISO string with timezone, or Date object.
45
+ * @param {boolean} orgTZ - Whether to force conversion into organization timezone.
46
+ * @param {boolean} serverTZ - Whether to force conversion into server timezone.
47
+ * @param {string|null} customTZ - Custom timezone override (e.g., "Asia/Kolkata").
48
+ * @returns {string} Formatted date string with timezone abbreviation or '-'.
19
49
  */
20
- export const formatDateWithTimezone = (date, orgTZ = false, onlyAbbr = false) => {
21
- try{
50
+ export const formatDateWithTimezone = (date, orgTZ = false, serverTZ = false, customTZ = null) => {
51
+ try {
22
52
  if (!date) return "-";
23
53
 
24
- let momentDate;
25
- const orgTimezone = utilsLocalStorageApi.loadItem("orgTZ") || "Asia/Kolkata";
26
-
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
- }
39
- }
40
-
54
+ const momentDate = getTimezoneFormattedDate(date,orgTZ,serverTZ,customTZ);
41
55
  if (!momentDate.isValid()) return "-";
42
56
 
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
- }
51
-
52
- if(onlyAbbr){
53
- return tzAbbr;
54
- }
55
- else{
56
- return `${momentDate.format(DATE_FORMAT)} ${tzAbbr}`;
57
- }
58
- }
59
- catch(error){
57
+ const tzAbbr = momentDate.format("z"); // e.g., IST, PST
58
+ return `${momentDate.format(DATE_FORMAT)} ${tzAbbr}`;
59
+ } catch (error) {
60
60
  console.error(error);
61
61
  return "-";
62
62
  }
@@ -65,38 +65,51 @@ export const formatDateWithTimezone = (date, orgTZ = false, onlyAbbr = false) =>
65
65
  /**
66
66
  * Returns a tooltip string with the timezone and offset for a given date.
67
67
  *
68
- * - Epoch input:
69
- * - Always uses orgTZ
70
- * - ISO input:
71
- * - orgTZ=falsekeep ISO timezone info
72
- * - orgTZ=true convert to org timezone (orgTZ)
68
+ * Timezone Selection Priority:
69
+ * 1. customTZ (if provided) - overrides all others
70
+ * 2. serverTZ=true uses server timezone from localStorage
71
+ * 3. orgTZ=trueuses organization timezone from localStorage
72
+ * 4. Default uses organization timezone from localStorage
73
+ *
74
+ * Input Handling:
75
+ * - Epoch input (number or numeric string):
76
+ * - Treated as UTC timestamp (seconds or milliseconds)
77
+ * - Returns timezone info for that specific date/time
78
+ * - ISO input (e.g., "2025-01-09T00:00:00+05:30"):
79
+ * - Parses timezone offset correctly
80
+ * - Returns timezone info for that specific date/time
81
+ * - Date object:
82
+ * - Returns timezone info for that specific date/time
73
83
  *
74
- * @param {string|number|Date} date - ISO string, epoch (sec/ms), or Date object.
75
- * @param {boolean} orgTZ - Whether to force conversion into org timezone.
76
- * @returns {string} Tooltip string with timezone and offset.
84
+ * @param {string|number|Date} date - Epoch timestamp, ISO string with timezone, or Date object.
85
+ * @param {boolean} orgTZ - Whether to force conversion into organization timezone.
86
+ * @param {boolean} serverTZ - Whether to force conversion into server timezone.
87
+ * @param {string|null} customTZ - Custom timezone override (e.g., "Asia/Kolkata").
88
+ * @returns {string} Tooltip string with timezone abbreviation and UTC offset for the specific date.
77
89
  */
78
- export const getTimezoneTooltip = (date, orgTZ = false) => {
79
- const orgTimezone = utilsLocalStorageApi.loadItem("orgTZ") || "Asia/Kolkata";
80
- const serverTimezone = utilsLocalStorageApi.loadItem("serverTZ") || "Asia/Kolkata";
90
+ export const getTimezoneTooltip = (date, orgTZ = false, serverTZ = false, customTZ = null) => {
91
+ try {
92
+ if (!date) return "-";
81
93
 
82
- if(orgTZ){
83
- return `${orgTimezone} (UTC${moment.tz(orgTimezone).format("Z")})`;
84
- }
85
- else{
86
- // case 1: epoch number or numeric string
87
- if(typeof date === "number" || (typeof date === "string" && /^\d+$/.test(date))){
88
- return `${orgTimezone} (UTC${moment.tz(orgTimezone).format("Z")})`;
89
- }
90
- // case 2: ISO string or Date object
91
- else{
92
- let m = moment.parseZone(date); // handles ISO strings like 2025-08-12T23:59:59.000Z or 2025-08-12T23:59:59+05:30
93
- if(!m.isValid()){
94
- m = moment.tz(date, "DD MMM YYYY h:mm A z"); // handles strings like 17 Sep 2025 6:00 AM EDT
95
- }
96
- if(!m.isValid() || m.isUTC()){
97
- return `${orgTimezone} (UTC${moment.tz(orgTimezone).format("Z")})`; // fallback to orgTZ
98
- }
99
- return `${serverTimezone} (UTC${m.format("Z")})`; // return the timezone and offset
100
- }
94
+ const orgTimezone = utilsLocalStorageApi.loadItem("orgTZ") || "Asia/Kolkata";
95
+ const serverTimezone = utilsLocalStorageApi.loadItem("serverTZ") || "Asia/Kolkata";
96
+ const targetTimezone = customTZ || (serverTZ ? serverTimezone : orgTZ ? orgTimezone : orgTimezone);
97
+ const momentDate = getTimezoneFormattedDate(date,orgTZ,serverTZ,customTZ);
98
+ if (!momentDate.isValid()) return "-";
99
+
100
+ const tzAbbr = momentDate.format("z"); // e.g., IST, PST, EDT
101
+ const utcOffset = momentDate.format("Z"); // e.g., +05:30, -04:00
102
+ return `${targetTimezone} (${tzAbbr},UTC${utcOffset})`;
103
+ } catch (error) {
104
+ console.error(error);
105
+ return "-";
101
106
  }
102
107
  };
108
+
109
+ /**
110
+ * checks if Timezone feature is enabled
111
+ * @returns {boolean}
112
+ */
113
+ export const hasTimezoneFeatureAccess = () => {
114
+ return hasFeatureAccess(TIMEZONE_ENABLED);
115
+ };