2021-08-22 20:16:04 +02:00
|
|
|
import {
|
|
|
|
addDays,
|
|
|
|
addMonths,
|
|
|
|
addYears,
|
2021-09-05 17:43:14 +02:00
|
|
|
endOfMonth,
|
|
|
|
endOfWeek,
|
2021-09-22 10:39:25 +02:00
|
|
|
format,
|
2021-08-22 20:16:04 +02:00
|
|
|
startOfMonth,
|
|
|
|
startOfWeek,
|
|
|
|
startOfYear,
|
|
|
|
} from 'date-fns'
|
2021-09-05 17:43:14 +02:00
|
|
|
import { utcToZonedTime } from 'date-fns-tz'
|
2021-08-22 20:16:04 +02:00
|
|
|
|
2022-11-01 10:43:47 +01:00
|
|
|
import createI18n from '@/i18n'
|
|
|
|
import { localeFromLanguage } from '@/utils/locales'
|
|
|
|
|
|
|
|
const { locale } = createI18n.global
|
|
|
|
|
2021-10-23 15:44:25 +02:00
|
|
|
export const getStartDate = (
|
2021-08-22 20:16:04 +02:00
|
|
|
duration: string,
|
|
|
|
day: Date,
|
|
|
|
weekStartingMonday: boolean
|
|
|
|
): Date => {
|
|
|
|
switch (duration) {
|
|
|
|
case 'week':
|
|
|
|
return startOfWeek(day, { weekStartsOn: weekStartingMonday ? 1 : 0 })
|
|
|
|
case 'year':
|
|
|
|
return startOfYear(day)
|
|
|
|
case 'month':
|
|
|
|
return startOfMonth(day)
|
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
`Invalid duration, expected: "week", "month", "year", got: "${duration}"`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const incrementDate = (duration: string, day: Date): Date => {
|
|
|
|
switch (duration) {
|
|
|
|
case 'week':
|
|
|
|
return addDays(day, 7)
|
|
|
|
case 'year':
|
|
|
|
return addYears(day, 1)
|
|
|
|
case 'month':
|
|
|
|
return addMonths(day, 1)
|
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
`Invalid duration, expected: "week", "month", "year", got: "${duration}"`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 17:43:14 +02:00
|
|
|
|
|
|
|
export const getDateWithTZ = (dateInUTC: string, tz: string): Date => {
|
|
|
|
return utcToZonedTime(new Date(dateInUTC), tz)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getCalendarStartAndEnd = (
|
|
|
|
date: Date,
|
|
|
|
weekStartingMonday: boolean
|
|
|
|
): Record<string, Date> => {
|
|
|
|
const monthStart = startOfMonth(date)
|
|
|
|
const monthEnd = endOfMonth(date)
|
|
|
|
const weekStartsOn = weekStartingMonday ? 1 : 0
|
|
|
|
return {
|
|
|
|
start: startOfWeek(monthStart, { weekStartsOn }),
|
2021-10-01 17:40:58 +02:00
|
|
|
end: endOfWeek(monthEnd, { weekStartsOn }),
|
2021-09-05 17:43:14 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-22 10:39:25 +02:00
|
|
|
|
|
|
|
export const formatWorkoutDate = (
|
|
|
|
dateTime: Date,
|
|
|
|
dateFormat: string | null = null,
|
|
|
|
timeFormat: string | null = null
|
|
|
|
): Record<string, string> => {
|
|
|
|
if (!dateFormat) {
|
|
|
|
dateFormat = 'yyyy/MM/dd'
|
|
|
|
}
|
2022-11-01 10:43:47 +01:00
|
|
|
dateFormat = getDateFormat(dateFormat, locale.value)
|
2021-09-22 10:39:25 +02:00
|
|
|
if (!timeFormat) {
|
|
|
|
timeFormat = 'HH:mm'
|
|
|
|
}
|
|
|
|
return {
|
2022-11-01 10:43:47 +01:00
|
|
|
workout_date: format(dateTime, dateFormat, {
|
|
|
|
locale: localeFromLanguage[locale.value],
|
|
|
|
}),
|
2021-09-22 10:39:25 +02:00
|
|
|
workout_time: format(dateTime, timeFormat),
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 08:03:47 +01:00
|
|
|
|
2022-11-01 10:43:47 +01:00
|
|
|
const availableDateFormats = [
|
|
|
|
'MM/dd/yyyy',
|
2022-11-01 14:24:08 +01:00
|
|
|
'dd/MM/yyyy',
|
2022-11-01 10:43:47 +01:00
|
|
|
'yyyy-MM-dd',
|
|
|
|
'date_string',
|
|
|
|
]
|
2023-02-26 18:25:25 +01:00
|
|
|
export const dateStringFormats: Record<string, string> = {
|
2022-11-01 12:17:20 +01:00
|
|
|
de: 'do MMM yyyy',
|
2022-11-01 10:43:47 +01:00
|
|
|
en: 'MMM. do, yyyy',
|
|
|
|
fr: 'd MMM yyyy',
|
2022-12-14 12:23:35 +01:00
|
|
|
it: 'd MMM yyyy',
|
2022-11-01 15:05:28 +01:00
|
|
|
// nb: 'do MMM yyyy',
|
2022-11-30 10:31:53 +01:00
|
|
|
nl: 'd MMM yyyy',
|
2022-11-01 10:43:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDateFormat = (dateFormat: string, language: string): string => {
|
|
|
|
return dateFormat === 'date_string' ? dateStringFormats[language] : dateFormat
|
|
|
|
}
|
|
|
|
|
2022-11-01 08:03:47 +01:00
|
|
|
export const formatDate = (
|
|
|
|
dateString: string,
|
|
|
|
timezone: string,
|
|
|
|
dateFormat: string,
|
2022-11-01 10:43:47 +01:00
|
|
|
withTime = true,
|
2023-03-01 21:16:25 +01:00
|
|
|
language: string | null = null,
|
|
|
|
withSeconds = false
|
2022-11-01 10:43:47 +01:00
|
|
|
): string => {
|
|
|
|
if (!language) {
|
|
|
|
language = locale.value
|
|
|
|
}
|
2023-03-01 21:16:25 +01:00
|
|
|
const timeFormat = withTime ? (withSeconds ? ' HH:mm:ss' : ' HH:mm') : ''
|
2022-11-01 10:43:47 +01:00
|
|
|
return format(
|
2022-11-01 08:03:47 +01:00
|
|
|
getDateWithTZ(dateString, timezone),
|
2023-03-01 21:16:25 +01:00
|
|
|
`${getDateFormat(dateFormat, language)}${timeFormat}`,
|
2022-11-01 10:43:47 +01:00
|
|
|
{ locale: localeFromLanguage[language] }
|
2022-11-01 08:03:47 +01:00
|
|
|
)
|
2022-11-01 10:43:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const availableDateFormatOptions = (
|
|
|
|
inputDate: string,
|
|
|
|
timezone: string,
|
|
|
|
language: string | null = null
|
|
|
|
) => {
|
|
|
|
const l: string = language ? language : locale.value
|
|
|
|
const options: Record<string, string>[] = []
|
|
|
|
availableDateFormats.map((df) => {
|
|
|
|
const dateFormat = getDateFormat(df, l)
|
|
|
|
options.push({
|
|
|
|
label: `${dateFormat} - ${formatDate(
|
|
|
|
inputDate,
|
|
|
|
timezone,
|
|
|
|
dateFormat,
|
|
|
|
false,
|
|
|
|
l
|
|
|
|
)}`,
|
|
|
|
value: df,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return options
|
|
|
|
}
|