diff --git a/src/lib/i18n/store.js b/src/lib/i18n/store.js index 28b2d40..046e72a 100644 --- a/src/lib/i18n/store.js +++ b/src/lib/i18n/store.js @@ -85,3 +85,20 @@ export function formatMoney(dirams, lang = 'en') { const s = n.toFixed(2); return lang === 'tg' ? s.replace('.', ',') : s; } + +export function formatTs(utcStr) { + if (!utcStr) return ''; + + const normalized = String(utcStr).trim().replace(' ', 'T'); + const utcDate = new Date(`${normalized}Z`); + if (Number.isNaN(utcDate.getTime())) return utcStr; + + const tajikDate = new Date(utcDate.getTime() + 5 * 60 * 60 * 1000); + const pad = (n) => String(n).padStart(2, '0'); + + return [ + pad(tajikDate.getUTCDate()), + pad(tajikDate.getUTCMonth() + 1), + tajikDate.getUTCFullYear() + ].join('.') + ` ${pad(tajikDate.getUTCHours())}:${pad(tajikDate.getUTCMinutes())}`; +} diff --git a/src/lib/server/reports.js b/src/lib/server/reports.js index 5897b1e..4eca19b 100644 --- a/src/lib/server/reports.js +++ b/src/lib/server/reports.js @@ -1,6 +1,6 @@ import { getDb } from './db.js'; -// All time windows are computed in local time using SQLite's `datetime('now', 'localtime')`. +// All time windows are computed in Tajikistan time (UTC+5) while timestamps are stored as UTC. // Cost of goods (COG) and profit are computed against each part's current cost_price — // the schema does not snapshot cost at sale time, so historical cost changes are not // reflected. Custom (non-inventory) lines contribute to sale revenue but have zero COG. @@ -29,9 +29,9 @@ function windowStats(dateClause) { export function salesSummary() { return { all_time: windowStats(''), - today: windowStats(`date(saved_at, 'localtime') = date('now', 'localtime')`), - week: windowStats(`date(saved_at, 'localtime') >= date('now', 'localtime', '-6 days')`), - month: windowStats(`strftime('%Y-%m', saved_at, 'localtime') = strftime('%Y-%m', 'now', 'localtime')`) + today: windowStats(`date(saved_at, '+5 hours') = date('now', '+5 hours')`), + week: windowStats(`date(saved_at, '+5 hours') >= date('now', '+5 hours', '-6 days')`), + month: windowStats(`strftime('%Y-%m', saved_at, '+5 hours') = strftime('%Y-%m', 'now', '+5 hours')`) }; } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 31c624b..a899500 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,5 +1,5 @@