64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { useSettingsStore } from '@/store/settingsStore'
|
|||
|
|
import { lastPrice } from '@/utils/price'
|
||
|
|
|
||
|
|
export interface StoreTradingHours {
|
||
|
|
start: string
|
||
|
|
end: string
|
||
|
|
closePriceEnabled: boolean
|
||
|
|
}
|
||
|
|
|
||
|
|
function parseTimeToSeconds(time: string): number {
|
||
|
|
const parts = time.trim().split(':').map(Number)
|
||
|
|
const [hours = 0, minutes = 0, seconds = 0] = parts
|
||
|
|
return hours * 3600 + minutes * 60 + seconds
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatTradingTimeDisplay(time: string): string {
|
||
|
|
const parts = time.trim().split(':')
|
||
|
|
if (parts.length >= 3) {
|
||
|
|
return `${parts[0]}:${parts[1]}:${parts[2]}`
|
||
|
|
}
|
||
|
|
if (parts.length >= 2) {
|
||
|
|
return `${parts[0]}:${parts[1]}:00`
|
||
|
|
}
|
||
|
|
return time
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getStoreTradingHours(): StoreTradingHours {
|
||
|
|
const settings = useSettingsStore()
|
||
|
|
const closeRaw = settings.getSettingByKey('close_price')
|
||
|
|
const closePriceEnabled =
|
||
|
|
closeRaw === true ||
|
||
|
|
closeRaw === 1 ||
|
||
|
|
String(closeRaw ?? 'false').trim().toLowerCase() === 'true'
|
||
|
|
|
||
|
|
return {
|
||
|
|
start: String(settings.getSettingByKey('startDay') ?? '00:00'),
|
||
|
|
end: String(settings.getSettingByKey('endDay') ?? '23:59'),
|
||
|
|
closePriceEnabled,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isStoreClosedBySchedule(now = new Date()): boolean {
|
||
|
|
const { start, end, closePriceEnabled } = getStoreTradingHours()
|
||
|
|
if (!closePriceEnabled) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
const currentSeconds = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds()
|
||
|
|
const openSeconds = parseTimeToSeconds(start)
|
||
|
|
const closeSeconds = parseTimeToSeconds(end)
|
||
|
|
|
||
|
|
return currentSeconds < openSeconds || currentSeconds > closeSeconds
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isStoreClosed(): boolean {
|
||
|
|
if (lastPrice.value?.closed === true) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
return isStoreClosedBySchedule()
|
||
|
|
}
|
||
|
|
|
||
|
|
export const STORE_CLOSED_MESSAGE = 'فروشگاه در حال حاضر بسته است.'
|