fix: use unixepoch modifier in count_posts SQL for integer timestamps

This commit is contained in:
2026-03-01 20:54:14 +01:00
parent db84129a17
commit 32aae7b694

View File

@@ -1003,11 +1003,11 @@ export class PostEngine extends EventEmitter {
for (const dim of groupBy) { for (const dim of groupBy) {
switch (dim) { switch (dim) {
case 'year': case 'year':
selectExprs.push("CAST(strftime('%Y', posts.created_at) AS INTEGER) AS g_year"); selectExprs.push("CAST(strftime('%Y', posts.created_at, 'unixepoch') AS INTEGER) AS g_year");
groupByCols.push('g_year'); groupByCols.push('g_year');
break; break;
case 'month': case 'month':
selectExprs.push("CAST(strftime('%m', posts.created_at) AS INTEGER) AS g_month"); selectExprs.push("CAST(strftime('%m', posts.created_at, 'unixepoch') AS INTEGER) AS g_month");
groupByCols.push('g_month'); groupByCols.push('g_month');
break; break;
case 'tag': case 'tag':
@@ -1034,22 +1034,22 @@ export class PostEngine extends EventEmitter {
const args: (string | number)[] = [this.currentProjectId]; const args: (string | number)[] = [this.currentProjectId];
if (filter?.year !== undefined) { if (filter?.year !== undefined) {
const start = `${filter.year}-01-01`; const startEpoch = Math.floor(new Date(`${filter.year}-01-01T00:00:00Z`).getTime() / 1000);
const end = `${filter.year + 1}-01-01`; const endEpoch = Math.floor(new Date(`${filter.year + 1}-01-01T00:00:00Z`).getTime() / 1000);
conditions.push('posts.created_at >= ?'); conditions.push('posts.created_at >= ?');
args.push(start); args.push(startEpoch);
conditions.push('posts.created_at < ?'); conditions.push('posts.created_at < ?');
args.push(end); args.push(endEpoch);
} }
if (filter?.month !== undefined && filter?.year !== undefined) { if (filter?.month !== undefined && filter?.year !== undefined) {
const start = `${filter.year}-${String(filter.month).padStart(2, '0')}-01`; const startEpoch = Math.floor(new Date(`${filter.year}-${String(filter.month).padStart(2, '0')}-01T00:00:00Z`).getTime() / 1000);
const endMonth = filter.month === 12 ? 1 : filter.month + 1; const endMonth = filter.month === 12 ? 1 : filter.month + 1;
const endYear = filter.month === 12 ? filter.year + 1 : filter.year; const endYear = filter.month === 12 ? filter.year + 1 : filter.year;
const end = `${endYear}-${String(endMonth).padStart(2, '0')}-01`; const endEpoch = Math.floor(new Date(`${endYear}-${String(endMonth).padStart(2, '0')}-01T00:00:00Z`).getTime() / 1000);
conditions.push('posts.created_at >= ?'); conditions.push('posts.created_at >= ?');
args.push(start); args.push(startEpoch);
conditions.push('posts.created_at < ?'); conditions.push('posts.created_at < ?');
args.push(end); args.push(endEpoch);
} }
if (filter?.status) { if (filter?.status) {
conditions.push('posts.status = ?'); conditions.push('posts.status = ?');