@@ -0,0 +1,50 @@
|
||||
import { toFaDigits } from '@/utils/persian'
|
||||
import { toJalaali } from '@/utils/date-utils'
|
||||
|
||||
const WEEKDAY_NAMES = ['شنبه', 'یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه']
|
||||
const MONTH_NAMES = [
|
||||
'فروردین',
|
||||
'اردیبهشت',
|
||||
'خرداد',
|
||||
'تیر',
|
||||
'مرداد',
|
||||
'شهریور',
|
||||
'مهر',
|
||||
'آبان',
|
||||
'آذر',
|
||||
'دی',
|
||||
'بهمن',
|
||||
'اسفند',
|
||||
]
|
||||
const GREGORIAN_WEEKDAYS = ['Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri']
|
||||
const DAY_MS = 24 * 60 * 60 * 1000
|
||||
|
||||
/**
|
||||
* Builds the current Jalaali week (Saturday → Friday) anchored to the Asia/Tehran
|
||||
* timezone, independent of the viewer's local clock. The day matching Tehran's
|
||||
* "today" is flagged `selected`. Returns `{ month, days }` for DashboardCalendar.
|
||||
*/
|
||||
export const useTehranCalendar = () => {
|
||||
const parts = new Intl.DateTimeFormat('en-US', {
|
||||
timeZone: 'Asia/Tehran',
|
||||
year: 'numeric',
|
||||
month: 'numeric',
|
||||
day: 'numeric',
|
||||
weekday: 'short',
|
||||
}).formatToParts(new Date())
|
||||
const part = (type) => parts.find((p) => p.type === type)?.value
|
||||
const todayIndex = GREGORIAN_WEEKDAYS.indexOf(part('weekday'))
|
||||
const todayUtc = Date.UTC(Number(part('year')), Number(part('month')) - 1, Number(part('day')))
|
||||
const weekStartUtc = todayUtc - todayIndex * DAY_MS
|
||||
|
||||
const today = new Date(todayUtc)
|
||||
const { jm } = toJalaali(today.getUTCFullYear(), today.getUTCMonth() + 1, today.getUTCDate())
|
||||
|
||||
const days = WEEKDAY_NAMES.map((weekday, i) => {
|
||||
const date = new Date(weekStartUtc + i * DAY_MS)
|
||||
const { jd } = toJalaali(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate())
|
||||
return { weekday, day: toFaDigits(jd), selected: i === todayIndex }
|
||||
})
|
||||
|
||||
return { month: `${MONTH_NAMES[jm - 1]} ماه`, days }
|
||||
}
|
||||
Reference in New Issue
Block a user