This commit implements comprehensive timezone and time format customization:
## Backend Changes
- Added timeFormat field ('12h' | '24h') to user preferences JSONB in user entity
- Timezone field already existed in user entity, now actively used
- Backend ready to accept timezone on registration
## Frontend Components (2 new)
- TimeZoneSelector: Dropdown with timezones grouped by region (Americas, Europe, Asia, Pacific, Africa)
* Auto-detect button to detect browser timezone
* Save functionality with success/error feedback
* Integrated into Settings > Preferences section
- TimeFormatSelector: Radio buttons to choose 12h vs 24h format
* Live preview showing current time in selected format
* Save functionality with user feedback
* Integrated into Settings > Preferences section
## Timezone Auto-Detection
- Register function now auto-detects user's timezone via Intl.DateTimeFormat()
- Detected timezone sent to backend during registration
- Timezone stored in user profile for persistent preference
## Enhanced useLocalizedDate Hook
- Added useAuth integration to access user timezone and timeFormat preferences
- Installed and integrated date-fns-tz for timezone conversion
- New format() function with timezone support via useTimezone option
- New formatTime() function respecting user's 12h/24h preference
- New formatDateTime() function combining date, time, and timezone
- All formatting now respects user's:
* Language (existing: en, es, fr, pt-BR, zh-CN)
* Timezone (user-selected or auto-detected)
* Time format (12h with AM/PM or 24h)
## Settings Page Updates
- Added TimeZoneSelector to Preferences card
- Added TimeFormatSelector to Preferences card
- Visual separators (Dividers) between preference sections
- Settings now show: Language | Units | Timezone | Time Format
## Translations
- Enhanced settings.json with timezone and time format keys:
* preferences.timezone, autoDetectTimezone, timezoneUpdated
* preferences.12hour, 24hour, timeFormatUpdated
## User Experience Flow
1. User registers → timezone auto-detected and saved
2. User can change timezone in Settings > Preferences > Time Zone
3. User can change time format in Settings > Preferences > Time Format
4. All dates/times throughout app respect these preferences
5. Changes persist across sessions
Files changed: 10 files
New dependencies: date-fns-tz
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
151 lines
4.5 KiB
TypeScript
151 lines
4.5 KiB
TypeScript
import { useTranslation } from './useTranslation';
|
|
import { useAuth } from '@/lib/auth/AuthContext';
|
|
import {
|
|
format as dateFnsFormat,
|
|
formatDistanceToNow as dateFnsFormatDistanceToNow,
|
|
formatDistance as dateFnsFormatDistance,
|
|
formatRelative as dateFnsFormatRelative,
|
|
toZonedTime,
|
|
} from 'date-fns';
|
|
import { formatInTimeZone } from 'date-fns-tz';
|
|
import { enUS, es, fr, ptBR, zhCN } from 'date-fns/locale';
|
|
|
|
/**
|
|
* Map of i18next language codes to date-fns locales
|
|
*/
|
|
const localeMap: Record<string, Locale> = {
|
|
'en': enUS,
|
|
'en-US': enUS,
|
|
'es': es,
|
|
'es-ES': es,
|
|
'fr': fr,
|
|
'fr-FR': fr,
|
|
'pt': ptBR,
|
|
'pt-BR': ptBR,
|
|
'zh': zhCN,
|
|
'zh-CN': zhCN,
|
|
};
|
|
|
|
/**
|
|
* Custom hook for localized date formatting using date-fns
|
|
* Automatically applies the correct locale, timezone, and time format based on user preferences
|
|
*/
|
|
export function useLocalizedDate() {
|
|
const { language } = useTranslation();
|
|
const { user } = useAuth();
|
|
|
|
// Get the locale for the current language, fallback to enUS
|
|
const locale = localeMap[language] || enUS;
|
|
|
|
// Get timezone from user preferences, fallback to UTC or browser timezone
|
|
const timezone = user?.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC';
|
|
|
|
// Get time format preference (12h or 24h)
|
|
const timeFormat = user?.preferences?.timeFormat || '12h';
|
|
|
|
/**
|
|
* Format a date using date-fns with the current locale and timezone
|
|
* @param date - The date to format
|
|
* @param formatStr - The format string (e.g., 'PPP', 'MM/dd/yyyy', 'p' for time)
|
|
* @param options - Optional formatting options
|
|
*/
|
|
const format = (
|
|
date: Date | number | string,
|
|
formatStr: string,
|
|
options?: { useTimezone?: boolean }
|
|
): string => {
|
|
const dateObj = typeof date === 'string' ? new Date(date) : date;
|
|
|
|
// Replace time format tokens with 12h/24h based on preference
|
|
let adjustedFormat = formatStr;
|
|
if (timeFormat === '24h') {
|
|
// Convert 12h format to 24h format
|
|
adjustedFormat = formatStr.replace(/h:mm a/gi, 'HH:mm').replace(/p/g, 'HH:mm');
|
|
}
|
|
|
|
// Use timezone if requested
|
|
if (options?.useTimezone && timezone) {
|
|
return formatInTimeZone(dateObj, timezone, adjustedFormat, { locale });
|
|
}
|
|
|
|
return dateFnsFormat(dateObj, adjustedFormat, { locale });
|
|
};
|
|
|
|
/**
|
|
* Format a date as a distance to now (e.g., "2 hours ago")
|
|
* @param date - The date to format
|
|
* @param options - Optional options for formatDistanceToNow
|
|
*/
|
|
const formatDistanceToNow = (
|
|
date: Date | number | string,
|
|
options?: { addSuffix?: boolean; includeSeconds?: boolean }
|
|
): string => {
|
|
const dateObj = typeof date === 'string' ? new Date(date) : date;
|
|
return dateFnsFormatDistanceToNow(dateObj, { ...options, locale });
|
|
};
|
|
|
|
/**
|
|
* Format the distance between two dates
|
|
* @param date - The later date
|
|
* @param baseDate - The earlier date
|
|
* @param options - Optional options for formatDistance
|
|
*/
|
|
const formatDistance = (
|
|
date: Date | number,
|
|
baseDate: Date | number,
|
|
options?: { addSuffix?: boolean; includeSeconds?: boolean }
|
|
): string => {
|
|
return dateFnsFormatDistance(date, baseDate, { ...options, locale });
|
|
};
|
|
|
|
/**
|
|
* Format a date relative to now (e.g., "today at 5:00 PM")
|
|
* @param date - The date to format
|
|
* @param baseDate - Optional base date (defaults to now)
|
|
*/
|
|
const formatRelative = (
|
|
date: Date | number,
|
|
baseDate?: Date | number
|
|
): string => {
|
|
return dateFnsFormatRelative(date, baseDate || new Date(), { locale });
|
|
};
|
|
|
|
/**
|
|
* Format a time string with the user's preferred time format
|
|
* @param date - The date/time to format
|
|
*/
|
|
const formatTime = (date: Date | number | string): string => {
|
|
const dateObj = typeof date === 'string' ? new Date(date) : date;
|
|
const formatStr = timeFormat === '24h' ? 'HH:mm' : 'h:mm a';
|
|
return dateFnsFormat(dateObj, formatStr, { locale });
|
|
};
|
|
|
|
/**
|
|
* Format a date and time with the user's timezone and time format
|
|
* @param date - The date/time to format
|
|
*/
|
|
const formatDateTime = (date: Date | number | string): string => {
|
|
const dateObj = typeof date === 'string' ? new Date(date) : date;
|
|
const timeFormatStr = timeFormat === '24h' ? 'HH:mm' : 'h:mm a';
|
|
const fullFormat = `PPP ${timeFormatStr}`;
|
|
|
|
if (timezone) {
|
|
return formatInTimeZone(dateObj, timezone, fullFormat, { locale });
|
|
}
|
|
|
|
return dateFnsFormat(dateObj, fullFormat, { locale });
|
|
};
|
|
|
|
return {
|
|
format,
|
|
formatDistanceToNow,
|
|
formatDistance,
|
|
formatRelative,
|
|
formatTime,
|
|
formatDateTime,
|
|
locale,
|
|
timezone,
|
|
timeFormat,
|
|
};
|
|
}
|