Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

9.1 KB
Raw Blame
import { DATE_TIME_FORMATS } from 'constants/dateTimeFormats';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import duration from 'dayjs/plugin/duration';
import relativeTime from 'dayjs/plugin/relativeTime';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);
dayjs.extend(customParseFormat);
dayjs.extend(duration);
dayjs.extend(timezone);
dayjs.extend(relativeTime);

export function toUTCEpoch(time: number): number {
	const x = new Date();
	return time + x.getTimezoneOffset() * 60 * 1000;
}

export const getFormattedDate = (epochTimestamp: number): string => {
	// Convert epoch timestamp to a date
	const date = dayjs.unix(epochTimestamp);

	// Format the date as "18 Nov 2013"
	return date.format(DATE_TIME_FORMATS.MONTH_DATE_SHORT);
};

export const getFormattedDateWithMinutes = (epochTimestamp: number): string => {
	// Convert epoch timestamp to a date
	const date = dayjs.unix(epochTimestamp);

	// Format the date as "18 Nov 2013"
	return date.format(DATE_TIME_FORMATS.MONTH_DATETIME_SHORT);
};

export const getRemainingDays = (billingEndDate: number): number => {
	// Convert Epoch timestamps to Date objects
	const startDate = new Date(); // Convert seconds to milliseconds
	const endDate = new Date(billingEndDate * 1000); // Convert seconds to milliseconds

	// Calculate the time difference in milliseconds
	// @ts-expect-error
	const timeDifference = endDate - startDate;

	return Math.ceil(timeDifference / (1000 * 60 * 60 * 24));
};

/**
 * Calculates the duration from the given epoch timestamp to the current time.
 *
 *
 * @param {number} epochTimestamp
 * @returns {string} - human readable string representing the duration from the given epoch timestamp to the current time e.g. "3d 14h"
 */
export const getDurationFromNow = (epochTimestamp: number): string => {
	const now = dayjs();
	const inputTime = dayjs(epochTimestamp);
	const duration = dayjs.duration(now.diff(inputTime));

	const days = duration.days();
	const hours = duration.hours();
	const minutes = duration.minutes();
	const seconds = duration.seconds();

	let result = '';
	if (days > 0) {
		result += `${days}d `;
	}
	if (hours > 0) {
		result += `${hours}h `;
	}
	if (minutes > 0) {
		result += `${minutes}m `;
	}
	if (seconds > 0) {
		result += `${seconds}s`;
	}

	return result.trim();
};

/**
 * Formats an epoch timestamp into a human-readable date and time string.
 *
 * @param {number} epoch - The epoch timestamp to format.
 * @returns {string} - The formatted date and time string in the format "MMM D, YYYY ⎯ HH:MM:SS".
 */
export function formatEpochTimestamp(epoch: number): string {
	const date = new Date(epoch);

	const optionsDate: Intl.DateTimeFormatOptions = {
		month: 'short',
		day: 'numeric',
		year: 'numeric',
	};

	const optionsTime: Intl.DateTimeFormatOptions = {
		hour: '2-digit',
		minute: '2-digit',
		second: '2-digit',
		hour12: false,
	};

	const formattedDate = date.toLocaleDateString('en-US', optionsDate);
	const formattedTime = date.toLocaleTimeString('en-US', optionsTime);

	return `${formattedDate} ⎯ ${formattedTime}`;
}

/**
 * Converts a given number of seconds into a human-readable format.
 * @param {number} seconds The number of seconds to convert.
 * @returns  {string} The formatted time string, either in days (e.g., "1.2d"), hours (e.g., "1.2h"), minutes (e.g., "~7m"), or seconds (e.g., "~45s").
 */

export function formatTime(seconds: number): string {
	seconds = +seconds;

	if (Number.isNaN(seconds)) {
		return '-';
	}

	const days = seconds / 86400;

	if (days >= 1) {
		return `${days.toFixed(1)}d`;
	}

	const hours = seconds / 3600;
	if (hours >= 1) {
		return `${hours.toFixed(1)}h`;
	}

	const minutes = seconds / 60;
	if (minutes >= 1) {
		return `${minutes.toFixed(1)}m`;
	}

	return `${seconds.toFixed(1)}s`;
}

export const nanoToMilli = (nanoseconds: number): number =>
	nanoseconds / 1_000_000;

export const secondsToMilliseconds = (seconds: number): number =>
	seconds * 1_000;

export const epochToTimeString = (epochMs: number): string => {
	const date = new Date(epochMs);
	const options: Intl.DateTimeFormatOptions = {
		hour: '2-digit',
		minute: '2-digit',
		hour12: false,
	};
	return date.toLocaleTimeString('en-US', options);
};

/**
 * Converts nanoseconds to milliseconds
 * @param timestamp - The timestamp to convert
 * @returns The timestamp in milliseconds
 */
export const normalizeTimeToMs = (timestamp: number | string): number => {
	let ts = timestamp;
	if (typeof timestamp === 'string') {
		ts = Math.trunc(parseInt(timestamp, 10));
	}
	ts = Number(ts);

	// Check if timestamp is in nanoseconds (19+ digits)
	const isNanoSeconds = ts.toString().length >= 19;

	return isNanoSeconds ? Math.floor(ts / 1_000_000) : ts;
};

/**
 * Calculates milliseconds elapsed since the given timestamp.
 * Returns 0 for undefined/invalid timestamps.
 */
export function getElapsedMs(startsAt: Date | string | undefined): number {
	if (!startsAt) {
		return 0;
	}
	const timestamp =
		typeof startsAt === 'string'
			? new Date(startsAt).getTime()
			: startsAt.getTime();
	if (Number.isNaN(timestamp)) {
		return 0;
	}
	return Date.now() - timestamp;
}

export const hasDatePassed = (expiresAt: string): boolean => {
	const date = dayjs(expiresAt);

	if (!date.isValid()) {
		return false;
	}

	return date.isBefore(dayjs(), 'day');
};

export const getDaysUntilExpiry = (expiresAt: string): number => {
	const date = dayjs(expiresAt);
	if (!date.isValid()) {
		return 0;
	}
	return date.diff(dayjs(), 'day');
};

export interface TimeRangeValidationResult {
	isValid: boolean;
	errorDetails?: {
		message: string;
		code: string;
		description: string;
	};
	startTimeMs?: number;
	endTimeMs?: number;
}

/**
 * Validates a start and end datetime string.
 *
 * Validation rules:
 * 1. Both start and end must be valid date-time values
 * 2. End time must be after start time
 * 3. End time must not be in the future
 *
 * Assumptions:
 * - Input values follow the provided date-time format
 * - All comparisons are performed in epoch milliseconds
 *
 * @param startTime - Start datetime string
 * @param endTime - End datetime string
 * @param format - Expected date-time format (e.g. DD/MM/YYYY HH:mm:ss)
 * @returns Validation result with parsed epoch milliseconds
 */
export const validateTimeRange = (
	startTime: string,
	endTime: string,
	format: string,
	timezone: string,
): TimeRangeValidationResult => {
	const start = dayjs.tz(startTime, format, timezone);
	const end = dayjs.tz(endTime, format, timezone);
	const now = dayjs().tz(timezone);
	const startTimeMs = start.valueOf();
	const endTimeMs = end.valueOf();

	// Invalid format or parsing failure
	if (!start.isValid() || !end.isValid()) {
		return {
			isValid: false,
			errorDetails: {
				message: 'Invalid date/time format',
				code: 'INVALID_DATE_TIME_FORMAT',
				description: `
Enter a valid date/time. e.g. 


Range:
${now.subtract(1, 'hour').format(format)} - ${now.format(format)}

Shortcuts:
15m, 2h, 2d, 2w
`,
			},
		};
	}

	// dates must not be in the future
	if (start.isAfter(now) || end.isAfter(now)) {
		return {
			isValid: false,
			errorDetails: {
				message: 'Dates in the future',
				code: 'DATES_IN_THE_FUTURE',
				description:
					'Dates must not be in the future. Enter a past or current date/time.',
			},
		};
	}

	// start time must be before end time
	if (startTimeMs >= endTimeMs) {
		return {
			isValid: false,
			errorDetails: {
				message: 'Start time after end time',
				code: 'START_TIME_AFTER_END_TIME',
				description:
					'Start time must be before end time. Change the start or end so the range is chronological.',
			},
		};
	}

	return {
		isValid: true,
		startTimeMs,
		endTimeMs,
	};
};

/**
 * Formats age in milliseconds to a human-readable string (e.g., "5d 3h", "2h 30m", "45s")
 */
export function formatAge(ms: number): string {
	if (ms < 0 || Number.isNaN(ms) || !Number.isFinite(ms)) {
		return '-';
	}

	const seconds = Math.floor(ms / 1000);
	const minutes = Math.floor(seconds / 60);
	const hours = Math.floor(minutes / 60);
	const days = Math.floor(hours / 24);

	if (days > 0) {
		const remainingHours = hours % 24;
		return remainingHours > 0 ? `${days}d ${remainingHours}h` : `${days}d`;
	}
	if (hours > 0) {
		const remainingMinutes = minutes % 60;
		return remainingMinutes > 0 ? `${hours}h ${remainingMinutes}m` : `${hours}h`;
	}
	if (minutes > 0) {
		return `${minutes}m`;
	}
	return `${seconds}s`;
}

/**
 * Formats a nanosecond duration (as a string) as milliseconds with two
 * decimals, e.g. `'12345678' -> '12.35'`.
 */
export const getMs = (value: string): string =>
	parseFloat(
		dayjs
			.duration({
				milliseconds: parseInt(value, 10) / 1000000,
			})
			.format('SSS'),
	).toFixed(2);

/** `overrideFormat`, when given, wins over the same-day check. */
export const formatTimestampOmittingTodaysDate = (
	timestampMs: number,
	timezone: string,
	overrideFormat?: string,
): string => {
	const time = dayjs(timestampMs).tz(timezone);

	if (overrideFormat) {
		return time.format(overrideFormat);
	}

	return time.format(
		time.isSame(dayjs().tz(timezone), 'day')
			? DATE_TIME_FORMATS.TIME_SECONDS
			: DATE_TIME_FORMATS.MONTH_DATETIME_SECONDS,
	);
};