All files / lib/analytics date-helpers.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                      4x   4x 4x   4x   4x                 22x   22x     44x 22x    
/**
 * Date Helpers for Analytics
 * Functions for handling IST timezone conversions for analytics queries
 */
 
/**
 * Get yesterday's date in IST timezone
 * IST is UTC+5:30
 * Returns date in YYYY-MM-DD format
 */
export function getYesterdayIST(): string {
	const now = new Date();
	// Convert to IST (UTC+5:30)
	const istOffset = 5.5 * 60 * 60 * 1000;
	const istDate = new Date(now.getTime() + istOffset);
	// Go back one day
	istDate.setDate(istDate.getDate() - 1);
	// Format as YYYY-MM-DD
	return istDate.toISOString().split("T")[0];
}
 
/**
 * Convert YYYY-MM-DD date to ISO datetime strings for Analytics Engine queries
 * Returns [startDateTime, endDateTime] in UTC
 */
export function getDateRangeUTC(dateIST: string): [string, string] {
	// Start: YYYY-MM-DDT00:00:00 IST = YYYY-MM-DDT-5:30 UTC
	const startIST = new Date(`${dateIST}T00:00:00+05:30`);
	// End: YYYY-MM-DDT23:59:59 IST
	const endIST = new Date(`${dateIST}T23:59:59+05:30`);
 
	// Format as 'YYYY-MM-DD HH:MM:SS' for Analytics Engine toDateTime()
	const fmt = (d: Date) => d.toISOString().replace("T", " ").replace(/\.\d{3}Z$/, "");
	return [fmt(startIST), fmt(endIST)];
}