Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Remove unused date and formatter modules that parsed server times as local time (#412 B10): utils/dateUtils.js, modules/utils/dates.js, formatters.js and their index; only their own tests used them

Murtaza Nasira3fb98fc78a6
diff --git a/static/js/modules/utils/dates.js b/static/js/modules/utils/dates.js
deleted file mode 100644
index 768e93da..00000000
--- a/static/js/modules/utils/dates.js
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- * Date comparison utility functions
- */
-
-export const isSameDay = (date1, date2) => {
- return date1.getFullYear() === date2.getFullYear() &&
- date1.getMonth() === date2.getMonth() &&
- date1.getDate() === date2.getDate();
-};
-
-export const isToday = (date) => {
- const today = new Date();
- return isSameDay(date, today);
-};
-
-export const isYesterday = (date) => {
- const yesterday = new Date();
- yesterday.setDate(yesterday.getDate() - 1);
- return isSameDay(date, yesterday);
-};
-
-export const isThisWeek = (date) => {
- const now = new Date();
- const startOfWeek = new Date(now);
- const day = now.getDay();
- const diff = now.getDate() - day + (day === 0 ? -6 : 1); // Monday as start of week
- startOfWeek.setDate(diff);
- startOfWeek.setHours(0, 0, 0, 0);
-
- const endOfWeek = new Date(startOfWeek);
- endOfWeek.setDate(startOfWeek.getDate() + 6);
- endOfWeek.setHours(23, 59, 59, 999);
-
- return date >= startOfWeek && date <= endOfWeek;
-};
-
-export const isLastWeek = (date) => {
- const now = new Date();
- const startOfLastWeek = new Date(now);
- const day = now.getDay();
- const diff = now.getDate() - day + (day === 0 ? -6 : 1) - 7; // Previous Monday
- startOfLastWeek.setDate(diff);
- startOfLastWeek.setHours(0, 0, 0, 0);
-
- const endOfLastWeek = new Date(startOfLastWeek);
- endOfLastWeek.setDate(startOfLastWeek.getDate() + 6);
- endOfLastWeek.setHours(23, 59, 59, 999);
-
- return date >= startOfLastWeek && date <= endOfLastWeek;
-};
-
-export const isThisMonth = (date) => {
- const now = new Date();
- return date.getFullYear() === now.getFullYear() && date.getMonth() === now.getMonth();
-};
-
-export const isLastMonth = (date) => {
- const now = new Date();
- const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
- return date.getFullYear() === lastMonth.getFullYear() && date.getMonth() === lastMonth.getMonth();
-};
-
-export const getDateForSorting = (recording, sortBy) => {
- const dateStr = sortBy === 'meeting_date' ? recording.meeting_date : recording.created_at;
- if (!dateStr) return null;
- return new Date(dateStr);
-};
diff --git a/static/js/modules/utils/dates.test.js b/static/js/modules/utils/dates.test.js
deleted file mode 100644
index 6f0d1f5d..00000000
--- a/static/js/modules/utils/dates.test.js
+++ /dev/null
@@ -1,164 +0,0 @@
-/**
- * Unit tests for the relative-date helpers in dates.js.
- *
- * Every helper except isSameDay/getDateForSorting reads `new Date()` for "now",
- * so we pin the system clock with fake timers to a fixed mid-month Wednesday
- * (2024-06-12, getDay()===3). All input dates are built with the local
- * `new Date(year, monthIndex, day)` constructor so the comparisons happen in
- * the same timezone as the helpers and stay deterministic in any CI timezone.
- */
-
-import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
-import {
- isSameDay,
- isToday,
- isYesterday,
- isThisWeek,
- isLastWeek,
- isThisMonth,
- isLastMonth,
- getDateForSorting,
-} from './dates.js';
-
-// Wednesday, 12 June 2024 at noon. Week (Mon-Sun) = Jun 10 - Jun 16.
-const NOW = new Date(2024, 5, 12, 12, 0, 0);
-
-describe('isSameDay', () => {
- it('is true for the same calendar day at different times', () => {
- expect(isSameDay(new Date(2024, 5, 12, 8), new Date(2024, 5, 12, 20))).toBe(true);
- });
-
- it('is false for different days', () => {
- expect(isSameDay(new Date(2024, 5, 12), new Date(2024, 5, 13))).toBe(false);
- });
-
- it('is false for the same day number in a different month', () => {
- expect(isSameDay(new Date(2024, 5, 12), new Date(2024, 6, 12))).toBe(false);
- });
-
- it('is false for the same day/month in a different year', () => {
- expect(isSameDay(new Date(2024, 5, 12), new Date(2023, 5, 12))).toBe(false);
- });
-});
-
-describe('relative-to-now helpers', () => {
- beforeEach(() => {
- vi.useFakeTimers();
- vi.setSystemTime(NOW);
- });
-
- afterEach(() => {
- vi.useRealTimers();
- });
-
- describe('isToday', () => {
- it('is true for today regardless of time of day', () => {
- expect(isToday(new Date(2024, 5, 12, 0, 0, 1))).toBe(true);
- expect(isToday(new Date(2024, 5, 12, 23, 59))).toBe(true);
- });
- it('is false for yesterday and tomorrow', () => {
- expect(isToday(new Date(2024, 5, 11))).toBe(false);
- expect(isToday(new Date(2024, 5, 13))).toBe(false);
- });
- });
-
- describe('isYesterday', () => {
- it('is true for the day before now', () => {
- expect(isYesterday(new Date(2024, 5, 11, 15))).toBe(true);
- });
- it('is false for today and two days ago', () => {
- expect(isYesterday(new Date(2024, 5, 12))).toBe(false);
- expect(isYesterday(new Date(2024, 5, 10))).toBe(false);
- });
- });
-
- describe('isThisWeek', () => {
- it('is true for the Monday and Sunday bounding the current week', () => {
- expect(isThisWeek(new Date(2024, 5, 10))).toBe(true); // Monday
- expect(isThisWeek(new Date(2024, 5, 16, 12))).toBe(true); // Sunday
- });
- it('is false just outside the week boundaries', () => {
- expect(isThisWeek(new Date(2024, 5, 9, 12))).toBe(false); // prev Sunday
- expect(isThisWeek(new Date(2024, 5, 17))).toBe(false); // next Monday
- });
- });
-
- describe('isLastWeek', () => {
- it('is true for the previous Monday-to-Sunday window (Jun 3 - Jun 9)', () => {
- expect(isLastWeek(new Date(2024, 5, 3))).toBe(true);
- expect(isLastWeek(new Date(2024, 5, 9, 12))).toBe(true);
- });
- it('is false for the current week and two weeks ago', () => {
- expect(isLastWeek(new Date(2024, 5, 10))).toBe(false);
- expect(isLastWeek(new Date(2024, 5, 2))).toBe(false);
- });
- });
-
- describe('isThisMonth', () => {
- it('is true for any day in the current month/year', () => {
- expect(isThisMonth(new Date(2024, 5, 1))).toBe(true);
- expect(isThisMonth(new Date(2024, 5, 30))).toBe(true);
- });
- it('is false for an adjacent month or the same month in another year', () => {
- expect(isThisMonth(new Date(2024, 4, 30))).toBe(false);
- expect(isThisMonth(new Date(2023, 5, 12))).toBe(false);
- });
- });
-
- describe('isLastMonth', () => {
- it('is true for any day in the previous month (May 2024)', () => {
- expect(isLastMonth(new Date(2024, 4, 1))).toBe(true);
- expect(isLastMonth(new Date(2024, 4, 31))).toBe(true);
- });
- it('is false for the current month and the same month in another year', () => {
- expect(isLastMonth(new Date(2024, 5, 1))).toBe(false);
- expect(isLastMonth(new Date(2023, 4, 15))).toBe(false);
- });
- });
-});
-
-describe('week helpers when "now" is a Sunday (day === 0 edge case)', () => {
- // Sunday, 16 June 2024. getDay()===0 exercises the special Monday-start
- // ternary (`day === 0 ? -6 : 1`) in isThisWeek/isLastWeek.
- beforeEach(() => {
- vi.useFakeTimers();
- vi.setSystemTime(new Date(2024, 5, 16, 12, 0, 0));
- });
-
- afterEach(() => {
- vi.useRealTimers();
- });
-
- it('treats Sunday as the end of the current week (Jun 10 - Jun 16)', () => {
- expect(isThisWeek(new Date(2024, 5, 10))).toBe(true); // Monday
- expect(isThisWeek(new Date(2024, 5, 16, 8))).toBe(true); // the Sunday itself
- expect(isThisWeek(new Date(2024, 5, 9))).toBe(false); // prev Sunday
- expect(isThisWeek(new Date(2024, 5, 17))).toBe(false); // next Monday
- });
-
- it('treats the prior Mon-Sun window as last week (Jun 3 - Jun 9)', () => {
- expect(isLastWeek(new Date(2024, 5, 3))).toBe(true);
- expect(isLastWeek(new Date(2024, 5, 9, 12))).toBe(true);
- expect(isLastWeek(new Date(2024, 5, 10))).toBe(false);
- });
-});
-
-describe('getDateForSorting', () => {
- it('uses meeting_date when sorting by meeting_date', () => {
- const rec = { meeting_date: '2024-06-12', created_at: '2024-01-01' };
- const result = getDateForSorting(rec, 'meeting_date');
- expect(result.getTime()).toBe(new Date('2024-06-12').getTime());
- });
-
- it('uses created_at for any other sort key', () => {
- const rec = { meeting_date: '2024-06-12', created_at: '2024-01-01' };
- const result = getDateForSorting(rec, 'created_at');
- expect(result.getTime()).toBe(new Date('2024-01-01').getTime());
- });
-
- it('returns null when the selected date field is missing', () => {
- expect(getDateForSorting({ created_at: null }, 'created_at')).toBeNull();
- expect(getDateForSorting({ meeting_date: null, created_at: '2024-01-01' }, 'meeting_date')).toBeNull();
- expect(getDateForSorting({}, 'created_at')).toBeNull();
- });
-});
diff --git a/static/js/modules/utils/formatters.js b/static/js/modules/utils/formatters.js
deleted file mode 100644
index e5e6a859..00000000
--- a/static/js/modules/utils/formatters.js
+++ /dev/null
@@ -1,139 +0,0 @@
-/**
- * Formatting utility functions
- */
-
-export const formatFileSize = (bytes) => {
- if (bytes == null || bytes === 0) return '0 Bytes';
- const k = 1024;
- const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
- if (bytes < 0) bytes = 0;
- const i = bytes === 0 ? 0 : Math.max(0, Math.floor(Math.log(bytes) / Math.log(k)));
- const size = i === 0 ? bytes : parseFloat((bytes / Math.pow(k, i)).toFixed(2));
- return size + ' ' + sizes[i];
-};
-
-export const formatDisplayDate = (dateString) => {
- if (!dateString) return '';
- try {
- let date = new Date(dateString);
-
- if (isNaN(date.getTime())) {
- if (/^\d{4}-\d{2}-\d{2}$/.test(dateString)) {
- date = new Date(dateString + 'T00:00:00');
- } else {
- return dateString;
- }
- }
-
- if (isNaN(date.getTime())) {
- return dateString;
- }
-
- return date.toLocaleDateString(undefined, { year: 'numeric', month: 'long', day: 'numeric' });
- } catch (e) {
- console.error("Error formatting date:", e);
- return dateString;
- }
-};
-
-export const formatShortDate = (dateString) => {
- if (!dateString) return '';
- try {
- let date = new Date(dateString);
-
- if (isNaN(date.getTime())) {
- if (/^\d{4}-\d{2}-\d{2}$/.test(dateString)) {
- date = new Date(dateString + 'T00:00:00');
- } else {
- return dateString;
- }
- }
-
- if (isNaN(date.getTime())) {
- return dateString;
- }
-
- const now = new Date();
- const isCurrentYear = date.getFullYear() === now.getFullYear();
-
- if (isCurrentYear) {
- return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' });
- } else {
- return date.toLocaleDateString(undefined, { year: '2-digit', month: 'short', day: 'numeric' });
- }
- } catch (e) {
- console.error("Error formatting short date:", e);
- return dateString;
- }
-};
-
-export const formatStatus = (status, t) => {
- if (!status || status === 'COMPLETED') return '';
- const statusMap = {
- 'PENDING': t('status.queued'),
- 'QUEUED': t('status.queued'),
- 'PROCESSING': t('status.processing'),
- 'TRANSCRIBING': t('status.transcribing'),
- 'SUMMARIZING': t('status.summarizing'),
- 'FAILED': t('status.failed'),
- 'UPLOADING': t('status.uploading')
- };
- return statusMap[status] || status.charAt(0).toUpperCase() + status.slice(1).toLowerCase();
-};
-
-export const getStatusClass = (status) => {
- switch(status) {
- case 'PENDING': return 'status-pending';
- case 'QUEUED': return 'status-pending';
- case 'PROCESSING': return 'status-processing';
- case 'SUMMARIZING': return 'status-summarizing';
- case 'COMPLETED': return '';
- case 'FAILED': return 'status-failed';
- default: return 'status-pending';
- }
-};
-
-export const formatTime = (seconds) => {
- const minutes = Math.floor(seconds / 60);
- const secs = seconds % 60;
- return `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
-};
-
-export const formatDuration = (totalSeconds) => {
- if (totalSeconds == null || totalSeconds < 0) return 'N/A';
-
- if (totalSeconds < 1) {
- return `${totalSeconds.toFixed(2)} seconds`;
- }
-
- totalSeconds = Math.round(totalSeconds);
-
- if (totalSeconds < 60) {
- return `${totalSeconds} sec`;
- }
-
- const hours = Math.floor(totalSeconds / 3600);
- const minutes = Math.floor((totalSeconds % 3600) / 60);
- const seconds = totalSeconds % 60;
-
- let parts = [];
- if (hours > 0) {
- parts.push(`${hours} hr`);
- }
- if (minutes > 0) {
- parts.push(`${minutes} min`);
- }
- if (hours === 0 && seconds > 0) {
- parts.push(`${seconds} sec`);
- }
-
- return parts.join(' ');
-};
-
-export const formatProcessingDuration = (seconds) => {
- if (!seconds && seconds !== 0) return null;
- if (seconds < 60) return `${seconds}s`;
- const mins = Math.floor(seconds / 60);
- const secs = seconds % 60;
- return secs > 0 ? `${mins}m ${secs}s` : `${mins}m`;
-};
diff --git a/static/js/modules/utils/formatters.test.js b/static/js/modules/utils/formatters.test.js
deleted file mode 100644
index d908388c..00000000
--- a/static/js/modules/utils/formatters.test.js
+++ /dev/null
@@ -1,219 +0,0 @@
-/**
- * Unit tests for the pure formatting helpers in formatters.js.
- *
- * Several helpers call `toLocaleDateString` with an undefined locale, so the
- * exact rendered string depends on the host locale/timezone. For those we
- * assert timezone-robust properties (presence of the year, emptiness, or
- * pass-through of invalid input) rather than an exact string, and we use
- * mid-month/mid-year dates so a UTC<->local shift can never cross a day,
- * month, or year boundary.
- */
-
-import { describe, it, expect } from 'vitest';
-import {
- formatFileSize,
- formatDisplayDate,
- formatShortDate,
- formatStatus,
- getStatusClass,
- formatTime,
- formatDuration,
- formatProcessingDuration,
-} from './formatters.js';
-
-describe('formatFileSize', () => {
- it('returns "0 Bytes" for null, undefined and zero', () => {
- expect(formatFileSize(null)).toBe('0 Bytes');
- expect(formatFileSize(undefined)).toBe('0 Bytes');
- expect(formatFileSize(0)).toBe('0 Bytes');
- });
-
- it('treats negative input as zero', () => {
- expect(formatFileSize(-1024)).toBe('0 Bytes');
- });
-
- it('formats byte-range values without a decimal', () => {
- expect(formatFileSize(500)).toBe('500 Bytes');
- expect(formatFileSize(1023)).toBe('1023 Bytes');
- });
-
- it('formats kilobytes, megabytes and gigabytes', () => {
- expect(formatFileSize(1024)).toBe('1 KB');
- expect(formatFileSize(1536)).toBe('1.5 KB');
- expect(formatFileSize(1048576)).toBe('1 MB');
- expect(formatFileSize(1073741824)).toBe('1 GB');
- expect(formatFileSize(1099511627776)).toBe('1 TB');
- });
-
- it('rounds to two decimal places', () => {
- expect(formatFileSize(1234567)).toBe('1.18 MB');
- });
-});
-
-describe('formatDisplayDate', () => {
- it('returns an empty string for falsy input', () => {
- expect(formatDisplayDate('')).toBe('');
- expect(formatDisplayDate(null)).toBe('');
- expect(formatDisplayDate(undefined)).toBe('');
- });
-
- it('returns the original string for unparseable input', () => {
- expect(formatDisplayDate('not a date')).toBe('not a date');
- });
-
- it('returns the original string for an invalid date-only string', () => {
- expect(formatDisplayDate('2024-13-45')).toBe('2024-13-45');
- });
-
- it('renders a valid date and includes the year', () => {
- const result = formatDisplayDate('2024-06-15T12:00:00');
- expect(typeof result).toBe('string');
- expect(result).toContain('2024');
- });
-
- it('handles a date-only string', () => {
- const result = formatDisplayDate('2024-06-15');
- expect(result).toContain('2024');
- });
-});
-
-describe('formatShortDate', () => {
- it('returns an empty string for falsy input', () => {
- expect(formatShortDate('')).toBe('');
- expect(formatShortDate(null)).toBe('');
- });
-
- it('returns the original string for unparseable input', () => {
- expect(formatShortDate('nonsense')).toBe('nonsense');
- });
-
- it('includes a 2-digit year for dates outside the current year', () => {
- const result = formatShortDate('2019-06-15T12:00:00');
- expect(result).toContain('19');
- });
-
- it('omits the 4-digit year for current-year dates', () => {
- const year = new Date().getFullYear();
- const result = formatShortDate(`${year}-06-15T12:00:00`);
- expect(result).not.toContain(String(year));
- expect(result.length).toBeGreaterThan(0);
- });
-});
-
-describe('formatStatus', () => {
- const t = (key) => key;
-
- it('returns an empty string for falsy status or COMPLETED', () => {
- expect(formatStatus('', t)).toBe('');
- expect(formatStatus(null, t)).toBe('');
- expect(formatStatus('COMPLETED', t)).toBe('');
- });
-
- it('maps known statuses through the translation function', () => {
- expect(formatStatus('PENDING', t)).toBe('status.queued');
- expect(formatStatus('QUEUED', t)).toBe('status.queued');
- expect(formatStatus('PROCESSING', t)).toBe('status.processing');
- expect(formatStatus('TRANSCRIBING', t)).toBe('status.transcribing');
- expect(formatStatus('SUMMARIZING', t)).toBe('status.summarizing');
- expect(formatStatus('FAILED', t)).toBe('status.failed');
- expect(formatStatus('UPLOADING', t)).toBe('status.uploading');
- });
-
- it('title-cases unknown statuses', () => {
- expect(formatStatus('WEIRD', t)).toBe('Weird');
- expect(formatStatus('custom', t)).toBe('Custom');
- });
-});
-
-describe('getStatusClass', () => {
- it('maps each known status to its CSS class', () => {
- expect(getStatusClass('PENDING')).toBe('status-pending');
- expect(getStatusClass('QUEUED')).toBe('status-pending');
- expect(getStatusClass('PROCESSING')).toBe('status-processing');
- expect(getStatusClass('SUMMARIZING')).toBe('status-summarizing');
- expect(getStatusClass('COMPLETED')).toBe('');
- expect(getStatusClass('FAILED')).toBe('status-failed');
- });
-
- it('defaults unknown statuses to status-pending', () => {
- expect(getStatusClass('SOMETHING')).toBe('status-pending');
- expect(getStatusClass(undefined)).toBe('status-pending');
- });
-});
-
-describe('formatTime', () => {
- it('formats zero as 00:00', () => {
- expect(formatTime(0)).toBe('00:00');
- });
-
- it('zero-pads minutes and seconds', () => {
- expect(formatTime(5)).toBe('00:05');
- expect(formatTime(65)).toBe('01:05');
- });
-
- it('formats whole minutes', () => {
- expect(formatTime(600)).toBe('10:00');
- expect(formatTime(3599)).toBe('59:59');
- });
-
- it('lets minutes exceed 60 (no hour rollover)', () => {
- expect(formatTime(3661)).toBe('61:01');
- });
-});
-
-describe('formatDuration', () => {
- it('returns N/A for null, undefined and negative values', () => {
- expect(formatDuration(null)).toBe('N/A');
- expect(formatDuration(undefined)).toBe('N/A');
- expect(formatDuration(-5)).toBe('N/A');
- });
-
- it('renders sub-second values with two decimals', () => {
- expect(formatDuration(0)).toBe('0.00 seconds');
- expect(formatDuration(0.5)).toBe('0.50 seconds');
- });
-
- it('renders seconds below a minute', () => {
- expect(formatDuration(30)).toBe('30 sec');
- expect(formatDuration(59)).toBe('59 sec');
- });
-
- it('rounds to the nearest whole second', () => {
- expect(formatDuration(29.4)).toBe('29 sec');
- expect(formatDuration(59.6)).toBe('1 min');
- });
-
- it('renders minutes and seconds', () => {
- expect(formatDuration(60)).toBe('1 min');
- expect(formatDuration(90)).toBe('1 min 30 sec');
- });
-
- it('renders hours and omits seconds when hours are present', () => {
- expect(formatDuration(3600)).toBe('1 hr');
- expect(formatDuration(3661)).toBe('1 hr 1 min');
- expect(formatDuration(7200)).toBe('2 hr');
- });
-});
-
-describe('formatProcessingDuration', () => {
- it('returns null for null and undefined but not for zero', () => {
- expect(formatProcessingDuration(null)).toBeNull();
- expect(formatProcessingDuration(undefined)).toBeNull();
- expect(formatProcessingDuration(0)).toBe('0s');
- });
-
- it('renders seconds below a minute', () => {
- expect(formatProcessingDuration(30)).toBe('30s');
- expect(formatProcessingDuration(59)).toBe('59s');
- });
-
- it('renders whole minutes without a seconds part', () => {
- expect(formatProcessingDuration(60)).toBe('1m');
- expect(formatProcessingDuration(120)).toBe('2m');
- });
-
- it('renders minutes and seconds', () => {
- expect(formatProcessingDuration(90)).toBe('1m 30s');
- expect(formatProcessingDuration(125)).toBe('2m 5s');
- });
-});
diff --git a/static/js/modules/utils/index.js b/static/js/modules/utils/index.js
deleted file mode 100644
index d2f0c601..00000000
--- a/static/js/modules/utils/index.js
+++ /dev/null
@@ -1,10 +0,0 @@
-/**
- * Utils module exports
- */
-
-export * from './formatters.js';
-export * from './dates.js';
-export { createApiClient } from './api.js';
-export { showToast } from './toast.js';
-export { getContrastTextColor } from './colors.js';
-export { labelPathForFilter, matchTagsByLabelName, parseLabelPath } from './label-links.js';
diff --git a/static/js/utils/dateUtils.js b/static/js/utils/dateUtils.js
deleted file mode 100644
index ec8cd8c2..00000000
--- a/static/js/utils/dateUtils.js
+++ /dev/null
@@ -1,83 +0,0 @@
-/**
- * Date utility functions for formatting and parsing dates
- */
-
-export function formatDateTime(dateString) {
- if (!dateString) return '';
- const date = new Date(dateString);
- const now = new Date();
- const diffMs = now - date;
- const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
-
- if (diffDays === 0) {
- return 'Today at ' + date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true });
- } else if (diffDays === 1) {
- return 'Yesterday at ' + date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true });
- } else if (diffDays < 7) {
- const dayName = date.toLocaleDateString('en-US', { weekday: 'long' });
- return dayName + ' at ' + date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true });
- } else {
- return date.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
- }
-}
-
-export function formatTimeAgo(dateString) {
- if (!dateString) return '';
- const date = new Date(dateString);
- const now = new Date();
- const diffMs = now - date;
- const diffMins = Math.floor(diffMs / 60000);
- const diffHours = Math.floor(diffMs / 3600000);
- const diffDays = Math.floor(diffMs / 86400000);
-
- if (diffMins < 1) return 'Just now';
- if (diffMins < 60) return `${diffMins} minute${diffMins > 1 ? 's' : ''} ago`;
- if (diffHours < 24) return `${diffHours} hour${diffHours > 1 ? 's' : ''} ago`;
- if (diffDays < 30) return `${diffDays} day${diffDays > 1 ? 's' : ''} ago`;
-
- const diffMonths = Math.floor(diffDays / 30);
- if (diffMonths < 12) return `${diffMonths} month${diffMonths > 1 ? 's' : ''} ago`;
-
- const diffYears = Math.floor(diffDays / 365);
- return `${diffYears} year${diffYears > 1 ? 's' : ''} ago`;
-}
-
-export function formatDuration(seconds) {
- if (!seconds || seconds < 0) return '0:00';
- const hrs = Math.floor(seconds / 3600);
- const mins = Math.floor((seconds % 3600) / 60);
- const secs = Math.floor(seconds % 60);
-
- if (hrs > 0) {
- return `${hrs}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
- }
- return `${mins}:${secs.toString().padStart(2, '0')}`;
-}
-
-export function parseDateRange(preset) {
- const now = new Date();
- const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
-
- switch (preset) {
- case 'today':
- return { start: today, end: new Date() };
- case 'yesterday':
- const yesterday = new Date(today);
- yesterday.setDate(yesterday.getDate() - 1);
- return { start: yesterday, end: today };
- case 'week':
- const weekStart = new Date(today);
- weekStart.setDate(weekStart.getDate() - 7);
- return { start: weekStart, end: new Date() };
- case 'month':
- const monthStart = new Date(today);
- monthStart.setMonth(monthStart.getMonth() - 1);
- return { start: monthStart, end: new Date() };
- case 'year':
- const yearStart = new Date(today);
- yearStart.setFullYear(yearStart.getFullYear() - 1);
- return { start: yearStart, end: new Date() };
- default:
- return { start: null, end: null };
- }
-}