2025-01-30 18:36:45 -05:00
|
|
|
import { dateFormats } from '$lib/constants';
|
2024-04-05 21:19:26 +02:00
|
|
|
import { locale } from '$lib/stores/preferences.store';
|
2024-02-24 21:23:30 +01:00
|
|
|
import { DateTime, Duration } from 'luxon';
|
2024-04-05 21:19:26 +02:00
|
|
|
import { get } from 'svelte/store';
|
2023-03-27 05:53:35 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert time like `01:02:03.456` to seconds.
|
|
|
|
|
*/
|
|
|
|
|
export function timeToSeconds(time: string) {
|
2025-08-23 22:42:37 +02:00
|
|
|
if (!time || time === '0') {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-03-27 05:53:35 +02:00
|
|
|
|
2025-08-23 22:42:37 +02:00
|
|
|
const seconds = Duration.fromISOTime(time).as('seconds');
|
2023-03-27 05:53:35 +02:00
|
|
|
|
2025-08-23 22:42:37 +02:00
|
|
|
return Number.isNaN(seconds) ? 0 : seconds;
|
2023-03-27 05:53:35 +02:00
|
|
|
}
|
2024-02-24 21:23:30 +01:00
|
|
|
export function parseUtcDate(date: string) {
|
|
|
|
|
return DateTime.fromISO(date, { zone: 'UTC' }).toUTC();
|
|
|
|
|
}
|
2024-04-05 21:19:26 +02:00
|
|
|
|
|
|
|
|
export const getShortDateRange = (startDate: string | Date, endDate: string | Date) => {
|
|
|
|
|
startDate = startDate instanceof Date ? startDate : new Date(startDate);
|
|
|
|
|
endDate = endDate instanceof Date ? endDate : new Date(endDate);
|
|
|
|
|
|
|
|
|
|
const userLocale = get(locale);
|
|
|
|
|
const endDateLocalized = endDate.toLocaleString(userLocale, {
|
|
|
|
|
month: 'short',
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (startDate.getFullYear() === endDate.getFullYear()) {
|
|
|
|
|
if (startDate.getMonth() === endDate.getMonth()) {
|
|
|
|
|
// Same year and month.
|
|
|
|
|
// e.g.: aug. 2024
|
|
|
|
|
return endDateLocalized;
|
|
|
|
|
} else {
|
|
|
|
|
// Same year but different month.
|
|
|
|
|
// e.g.: jul. - sept. 2024
|
|
|
|
|
const startMonthLocalized = startDate.toLocaleString(userLocale, {
|
|
|
|
|
month: 'short',
|
|
|
|
|
});
|
|
|
|
|
return `${startMonthLocalized} - ${endDateLocalized}`;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Different year.
|
|
|
|
|
// e.g.: feb. 2021 - sept. 2024
|
|
|
|
|
const startDateLocalized = startDate.toLocaleString(userLocale, {
|
|
|
|
|
month: 'short',
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
});
|
|
|
|
|
return `${startDateLocalized} - ${endDateLocalized}`;
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-01-30 18:36:45 -05:00
|
|
|
|
|
|
|
|
const formatDate = (date?: string) => {
|
|
|
|
|
if (!date) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// without timezone
|
|
|
|
|
const localDate = date.replace(/Z$/, '').replace(/\+.+$/, '');
|
|
|
|
|
return localDate ? new Date(localDate).toLocaleDateString(get(locale), dateFormats.album) : undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getAlbumDateRange = (album: { startDate?: string; endDate?: string }) => {
|
|
|
|
|
const start = formatDate(album.startDate);
|
|
|
|
|
const end = formatDate(album.endDate);
|
|
|
|
|
|
|
|
|
|
if (start && end && start !== end) {
|
|
|
|
|
return `${start} - ${end}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (start) {
|
|
|
|
|
return start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
};
|
2025-02-21 13:31:37 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use this to convert from "5pm EST" to "5pm UTC"
|
|
|
|
|
*
|
|
|
|
|
* Useful with some APIs where you want to query by "today", but the values in the database are stored as UTC
|
|
|
|
|
*/
|
|
|
|
|
export const asLocalTimeISO = (date: DateTime<true>) =>
|
|
|
|
|
(date.setZone('utc', { keepLocalTime: true }) as DateTime<true>).toISO();
|