Skip to content

Commit 9973c0d

Browse files
committed
Refactor date/time formatting to use centralized timezone handling and update timeline label formatting
1 parent 40966e9 commit 9973c0d

File tree

4 files changed

+72
-62
lines changed

4 files changed

+72
-62
lines changed

web/static/js/core/charts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ const LogLynxCharts = {
401401
/**
402402
* Format timeline labels based on time range
403403
*/
404-
formatTimelineLabels(dataPoints, hours) {
405-
const timeZone = (window.LOGLYNX_CONFIG && window.LOGLYNX_CONFIG.timeZone) ? window.LOGLYNX_CONFIG.timeZone : undefined;
404+
formatTimelineLabels(dataPoints, hours) {
405+
const timeZone = LogLynxUtils.getTimeZone();
406406

407407
if (hours > 0 && hours <= 24) {
408408
// Hourly labels (HH:MM format)

web/static/js/core/utils.js

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -188,64 +188,72 @@ const LogLynxUtils = {
188188
/**
189189
* Format date/time
190190
*/
191-
formatDateTime: function(dateStr, options = {}) {
192-
if (!dateStr) return '-';
193-
try {
194-
const date = new Date(dateStr);
195-
const defaultOptions = {
196-
year: 'numeric',
197-
month: 'short',
198-
day: '2-digit',
199-
hour: '2-digit',
200-
minute: '2-digit',
201-
second: '2-digit',
202-
hour12: false,
203-
timeZone: window.LOGLYNX_CONFIG?.timeZone || 'UTC'
204-
};
205-
return new Intl.DateTimeFormat(undefined, { ...defaultOptions, ...options }).format(date);
206-
} catch (e) {
207-
return dateStr;
208-
}
209-
},
210-
211-
/**
212-
* Format date only
213-
*/
214-
formatDate: function(dateStr, options = {}) {
215-
if (!dateStr) return '-';
216-
try {
217-
const date = new Date(dateStr);
218-
const defaultOptions = {
219-
year: 'numeric',
220-
month: 'short',
221-
day: '2-digit',
222-
timeZone: window.LOGLYNX_CONFIG?.timeZone || 'UTC'
223-
};
224-
return new Intl.DateTimeFormat(undefined, { ...defaultOptions, ...options }).format(date);
225-
} catch (e) {
226-
return dateStr;
227-
}
228-
},
229-
230-
/**
231-
* Format time only
232-
*/
233-
formatTime: function(dateStr, options = {}) {
234-
if (!dateStr) return '-';
235-
try {
236-
const date = new Date(dateStr);
237-
const defaultOptions = {
238-
hour: '2-digit',
239-
minute: '2-digit',
240-
second: '2-digit',
241-
hour12: false,
242-
timeZone: window.LOGLYNX_CONFIG?.timeZone || 'UTC'
243-
};
244-
return new Intl.DateTimeFormat(undefined, { ...defaultOptions, ...options }).format(date);
245-
} catch (e) {
246-
return dateStr;
247-
}
248-
},
191+
getTimeZone: function() {
192+
const configuredTz = window.LOGLYNX_CONFIG?.timeZone;
193+
if (!configuredTz || configuredTz === '') {
194+
return 'UTC';
195+
}
196+
try {
197+
new Intl.DateTimeFormat(undefined, { timeZone: configuredTz });
198+
return configuredTz;
199+
} catch (e) {
200+
console.warn('Invalid timezone configured:', configuredTz, '- falling back to UTC');
201+
return 'UTC';
202+
}
203+
},
204+
205+
formatDateTime: function(dateStr, options = {}) {
206+
if (!dateStr) return '-';
207+
try {
208+
const date = new Date(dateStr);
209+
const defaultOptions = {
210+
year: 'numeric',
211+
month: 'short',
212+
day: '2-digit',
213+
hour: '2-digit',
214+
minute: '2-digit',
215+
second: '2-digit',
216+
hour12: false,
217+
timeZone: this.getTimeZone()
218+
};
219+
return new Intl.DateTimeFormat(undefined, { ...defaultOptions, ...options }).format(date);
220+
} catch (e) {
221+
return dateStr;
222+
}
223+
},
224+
225+
formatDate: function(dateStr, options = {}) {
226+
if (!dateStr) return '-';
227+
try {
228+
const date = new Date(dateStr);
229+
const defaultOptions = {
230+
year: 'numeric',
231+
month: 'short',
232+
day: '2-digit',
233+
timeZone: this.getTimeZone()
234+
};
235+
return new Intl.DateTimeFormat(undefined, { ...defaultOptions, ...options }).format(date);
236+
} catch (e) {
237+
return dateStr;
238+
}
239+
},
240+
241+
formatTime: function(dateStr, options = {}) {
242+
if (!dateStr) return '-';
243+
try {
244+
const date = new Date(dateStr);
245+
const defaultOptions = {
246+
hour: '2-digit',
247+
minute: '2-digit',
248+
second: '2-digit',
249+
hour12: false,
250+
timeZone: this.getTimeZone()
251+
};
252+
return new Intl.DateTimeFormat(undefined, { ...defaultOptions, ...options }).format(date);
253+
} catch (e) {
254+
return dateStr;
255+
}
256+
},
249257

250258
/**
251259
* Format relative time (e.g., "2 minutes ago")

web/static/js/pages/ip-detail.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ function updateTimelineChart(data) {
341341
timelineChart.destroy();
342342
}
343343

344-
const labels = data.map(d => d.hour);
344+
const hours = parseInt($('#timelineRange').val()) || 168;
345+
const labels = LogLynxCharts.formatTimelineLabels(data, hours);
345346
const requests = data.map(d => d.requests);
346347
const bandwidth = data.map(d => d.bandwidth);
347348

web/templates/pages/security.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ <h5 class="table-title">
260260
.then(data => {
261261
if (errorTimelineChartInstance) LogLynxCharts.destroyChart(errorTimelineChartInstance);
262262

263+
const timelineLabels = LogLynxCharts.formatTimelineLabels(data, currentTimeRange);
263264
errorTimelineChartInstance = LogLynxCharts.createStackedAreaChart('errorTimelineChart', {
264-
labels: data.map(item => item.hour),
265+
labels: timelineLabels,
265266
datasets: [
266267
{
267268
label: '4xx Errors',

0 commit comments

Comments
 (0)