52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { storageService } from './storage/indexedDB'
|
|
|
|
const LAST_SYNC_DATE_KEY = 'last_sync_date'
|
|
const SYNC_STORAGE_SECRET = 'sync_storage_secret'
|
|
|
|
/**
|
|
* Get the last synchronization date
|
|
* Returns MIN_EVENT_DATE if no date is stored
|
|
*/
|
|
export async function getLastSyncDate(): Promise<number> {
|
|
try {
|
|
const stored = await storageService.get<number>(LAST_SYNC_DATE_KEY, SYNC_STORAGE_SECRET)
|
|
if (stored !== null && typeof stored === 'number') {
|
|
return stored
|
|
}
|
|
} catch (error) {
|
|
console.error('Error getting last sync date:', error)
|
|
}
|
|
// Return MIN_EVENT_DATE if no date is stored
|
|
const { MIN_EVENT_DATE } = await import('./platformConfig')
|
|
return MIN_EVENT_DATE
|
|
}
|
|
|
|
/**
|
|
* Store the last synchronization date
|
|
*/
|
|
export async function setLastSyncDate(timestamp: number): Promise<void> {
|
|
try {
|
|
await storageService.set(LAST_SYNC_DATE_KEY, timestamp, SYNC_STORAGE_SECRET)
|
|
} catch (error) {
|
|
console.error('Error setting last sync date:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Calculate the number of days between two timestamps
|
|
*/
|
|
export function calculateDaysBetween(startTimestamp: number, endTimestamp: number): number {
|
|
const startDate = new Date(startTimestamp * 1000)
|
|
const endDate = new Date(endTimestamp * 1000)
|
|
const diffTime = endDate.getTime() - startDate.getTime()
|
|
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
|
|
return Math.max(0, diffDays)
|
|
}
|
|
|
|
/**
|
|
* Get the current date as Unix timestamp
|
|
*/
|
|
export function getCurrentTimestamp(): number {
|
|
return Math.floor(Date.now() / 1000)
|
|
}
|