Skip to content

Commit 53108e9

Browse files
committed
fix: chat timestamps — UTC ISO → browser local timezone (EAT)
1 parent f7c0bfe commit 53108e9

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ def get_messages(conv_id):
23082308
"sender": m.sender.full_name,
23092309
"is_mine": m.sender_id == current_user.id,
23102310
"is_read": m.is_read,
2311-
"sent_at": m.sent_at.strftime("%d %b %Y, %H:%M"),
2311+
"sent_at": m.sent_at.strftime("%Y-%m-%dT%H:%M:%SZ"), # UTC ISO
23122312
}
23132313
for m in conv.messages
23142314
]

templates/messages/thread.html

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
return `<div class="msg-bubble ${cls}" data-id="${m.id}" data-body="${escAttr(m.body)}" data-mine="${m.is_mine}" data-deleted="${m.is_deleted}">
248248
${replyHtml}
249249
<div class="msg-text${deletedCls}">${escHtml(m.body)}</div>
250-
<div class="msg-meta">${escHtml(m.sent_at)} ${ticks}</div>
250+
<div class="msg-meta">${fmtTime(m.sent_at)} ${ticks}</div>
251251
</div>`;
252252
}).join('');
253253

@@ -259,6 +259,31 @@
259259
if(!str) return '';
260260
return String(str).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
261261
}
262+
function fmtTime(iso){
263+
if(!iso) return '';
264+
const d = new Date(iso);
265+
const now = new Date();
266+
const diffMs = now - d;
267+
const diffMins = Math.floor(diffMs / 60000);
268+
const diffHours = Math.floor(diffMs / 3600000);
269+
const diffDays = Math.floor(diffMs / 86400000);
270+
// Leo
271+
if(diffDays === 0){
272+
return d.toLocaleTimeString([], {hour:'2-digit', minute:'2-digit'});
273+
}
274+
// Jana
275+
if(diffDays === 1){
276+
return 'Jana ' + d.toLocaleTimeString([], {hour:'2-digit', minute:'2-digit'});
277+
}
278+
// Wiki hii
279+
if(diffDays < 7){
280+
const days = ['Jumapili','Jumatatu','Jumanne','Jumatano','Alhamisi','Ijumaa','Jumamosi'];
281+
return days[d.getDay()] + ' ' + d.toLocaleTimeString([], {hour:'2-digit', minute:'2-digit'});
282+
}
283+
// Zamani
284+
return d.toLocaleDateString('sw-TZ', {day:'numeric', month:'short', year:'numeric'}) +
285+
', ' + d.toLocaleTimeString([], {hour:'2-digit', minute:'2-digit'});
286+
}
262287
function escAttr(str){
263288
if(!str) return '';
264289
return String(str).replace(/"/g,'&quot;').replace(/'/g,'&#39;');

0 commit comments

Comments
 (0)