fix(pwa+timezone)
This commit is contained in:
66
frontend/src/utils/dateUtils.js
Normal file
66
frontend/src/utils/dateUtils.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { format, formatDistanceToNow } from 'date-fns'
|
||||
import { fr } from 'date-fns/locale'
|
||||
import { zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz'
|
||||
|
||||
// Fuseau horaire français
|
||||
const FRENCH_TIMEZONE = 'Europe/Paris'
|
||||
|
||||
/**
|
||||
* Convertit une date UTC en date du fuseau horaire français
|
||||
*/
|
||||
function toFrenchTimezone(date) {
|
||||
if (!date) return null
|
||||
const dateObj = date instanceof Date ? date : new Date(date)
|
||||
return utcToZonedTime(dateObj, FRENCH_TIMEZONE)
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate une date dans le fuseau horaire français
|
||||
*/
|
||||
export function formatDateInFrenchTimezone(date, formatStr = 'dd MMM à HH:mm') {
|
||||
if (!date) return ''
|
||||
const frenchDate = toFrenchTimezone(date)
|
||||
return format(frenchDate, formatStr, { locale: fr })
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate une date relative dans le fuseau horaire français
|
||||
*/
|
||||
export function formatRelativeDateInFrenchTimezone(date) {
|
||||
if (!date) return ''
|
||||
const frenchDate = toFrenchTimezone(date)
|
||||
return formatDistanceToNow(frenchDate, { addSuffix: true, locale: fr })
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate une date complète dans le fuseau horaire français
|
||||
*/
|
||||
export function formatFullDateInFrenchTimezone(date) {
|
||||
return formatDateInFrenchTimezone(date, 'EEEE d MMMM yyyy à HH:mm')
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate une date courte dans le fuseau horaire français
|
||||
*/
|
||||
export function formatShortDateInFrenchTimezone(date) {
|
||||
return formatDateInFrenchTimezone(date, 'dd/MM/yyyy')
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate une date pour un input datetime-local dans le fuseau horaire français
|
||||
*/
|
||||
export function formatDateForInputInFrenchTimezone(date) {
|
||||
if (!date) return ''
|
||||
const frenchDate = toFrenchTimezone(date)
|
||||
return format(frenchDate, "yyyy-MM-dd'T'HH:mm", { locale: fr })
|
||||
}
|
||||
|
||||
/**
|
||||
* Convertit une date du fuseau horaire français vers UTC pour l'envoyer au backend
|
||||
*/
|
||||
export function convertFrenchTimezoneToUTC(date) {
|
||||
if (!date) return null
|
||||
const dateObj = date instanceof Date ? date : new Date(date)
|
||||
return zonedTimeToUtc(dateObj, FRENCH_TIMEZONE)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user