fp/services/next/app/lib/dates.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-01-20 16:16:14 +00:00
import { parse } from 'date-fns';
import { format } from 'date-fns-tz'
2024-11-05 19:48:21 +00:00
import { toZonedTime, fromZonedTime } from 'date-fns-tz'
2024-01-20 16:16:14 +00:00
const safeDateFormatString: string = "yyyyMMdd'T'HHmmss'Z'"
const localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
export function getSafeDate(date: string | Date): string {
let dateString: string;
if (typeof date === 'string') {
2024-11-05 19:48:21 +00:00
const dateObject = toZonedTime(date, 'UTC');
2024-01-20 16:16:14 +00:00
dateString = format(dateObject, safeDateFormatString, { timeZone: 'UTC' });
} else {
dateString = format(date, safeDateFormatString, { timeZone: 'UTC' });
}
return dateString;
}
export function getDateFromSafeDate(safeDate: string): Date {
const date = parse(safeDate, safeDateFormatString, new Date())
2024-11-05 19:48:21 +00:00
const utcDate = fromZonedTime(date, 'UTC')
2024-01-20 16:16:14 +00:00
return utcDate;
}
export function formatTimestamp(seconds: number = 0): string {
return new Date(seconds * 1000).toISOString().slice(11, 19);
}
export function formatUrlTimestamp(timestampInSeconds: number): string {
const hours = Math.floor(timestampInSeconds / 3600);
const minutes = Math.floor((timestampInSeconds % 3600) / 60);
const seconds = timestampInSeconds % 60;
return `${hours}h${minutes}m${seconds}s`;
}
export function parseUrlTimestamp(timestamp: string): number | null {
// Regular expression to match the "XhYmZs" format
const regex = /^(\d+)h(\d+)m(\d+)s$/;
const match = timestamp.match(regex);
if (match) {
const hours = parseInt(match[1], 10);
const minutes = parseInt(match[2], 10);
const seconds = parseInt(match[3], 10);
if (!isNaN(hours) && !isNaN(minutes) && !isNaN(seconds)) {
return hours * 3600 + minutes * 60 + seconds;
}
}
// If the format doesn't match or parsing fails, return null
return null;
}