export function parseTimestampInput(value, type) { const timestampStr = value.trim() if (!timestampStr) return { error: 'empty' } if (type === 'nanoseconds') { try { const timestampNs = BigInt(timestampStr) const timestampMs = Number(timestampNs / BigInt(1000000)) const nanoseconds = Number(timestampNs % BigInt(1000000)) return { timestampMs, nanoseconds } } catch { return { error: 'invalid_nanoseconds' } } } const timestamp = parseInt(timestampStr, 10) if (isNaN(timestamp)) return { error: 'invalid_number' } if (type === 'seconds') { const timestampMs = timestamp.toString().length <= 10 ? timestamp * 1000 : timestamp return { timestampMs, nanoseconds: 0 } } return { timestampMs: timestamp, nanoseconds: 0 } } export function formatTimestampMs(timestampMs, type, nanoseconds = 0) { const date = new Date(timestampMs) if (isNaN(date.getTime())) return { error: 'invalid_date' } const year = date.getFullYear() const month = String(date.getMonth() + 1).padStart(2, '0') const day = String(date.getDate()).padStart(2, '0') const hours = String(date.getHours()).padStart(2, '0') const minutes = String(date.getMinutes()).padStart(2, '0') const seconds = String(date.getSeconds()).padStart(2, '0') const milliseconds = String(date.getMilliseconds()).padStart(3, '0') if (type === 'seconds') { return { value: `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` } } if (type === 'milliseconds') { return { value: `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}` } } const nanosecondsStr = String(nanoseconds).padStart(6, '0') return { value: `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}${nanosecondsStr}` } } export function parseDateString(dateStr) { const trimmed = dateStr.trim() if (!trimmed) return { error: 'empty' } let date = null if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(trimmed)) { date = new Date(trimmed.replace(' ', 'T')) } else if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+$/.test(trimmed)) { date = new Date(trimmed.replace(' ', 'T')) } else if (/^\d{4}\/\d{2}\/\d{2}/.test(trimmed)) { date = new Date(trimmed) } else { date = new Date(trimmed) } if (!date || isNaN(date.getTime())) return { error: 'invalid_date' } return { date } } export function dateToTimestamp(date, type) { const ms = date.getTime() if (type === 'seconds') return { value: String(Math.floor(ms / 1000)) } if (type === 'milliseconds') return { value: String(ms) } return { value: String(BigInt(ms) * BigInt(1000000)) } }