mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
Change time render to be client-side and formatted.
This changes the time render to be done on the client-side and therefore take advantage of knowing the client’s timezone, along with being formatted in a more human-parseable way.
This commit is contained in:
committed by
Tim Abbott
parent
434317b4cc
commit
e504eaaa68
@@ -11,6 +11,10 @@ exports.set_up_attachments = function () {
|
||||
|
||||
var attachment_list = $('#attachments_list');
|
||||
_.each(page_params.attachments, function (attachment) {
|
||||
_.each(attachment.messages, function (o) {
|
||||
o.name = timerender.absolute_time(o.name);
|
||||
});
|
||||
|
||||
var li = templates.render('attachment-item', {attachment: attachment});
|
||||
attachment_list.append(li);
|
||||
});
|
||||
|
||||
@@ -150,6 +150,41 @@ exports.get_full_time = function (timestamp) {
|
||||
return full_date_str + ' ' + full_time_str;
|
||||
};
|
||||
|
||||
|
||||
// this is for rendering absolute time based off the preferences for twenty-four
|
||||
// hour time in the format of "%mmm %d, %h:%m %p".
|
||||
exports.absolute_time = (function () {
|
||||
var MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
|
||||
var fmt_time = function (date, H_24) {
|
||||
var payload = {
|
||||
hours: date.getHours(),
|
||||
minutes: date.getMinutes(),
|
||||
};
|
||||
|
||||
if (payload.hours > 12 && !H_24) {
|
||||
payload.hours -= 12;
|
||||
payload.is_pm = true;
|
||||
}
|
||||
|
||||
var str = ("0" + payload.hours).slice(-2) + ":" + ("0" + payload.minutes).slice(-2);
|
||||
|
||||
if (!H_24) {
|
||||
str += payload.is_pm ? " PM" : " AM";
|
||||
}
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
return function (timestamp) {
|
||||
var date = new Date(timestamp);
|
||||
var H_24 = page_params.twenty_four_hour_time;
|
||||
|
||||
return MONTHS[date.getMonth()] + " " + date.getDate() + ", " + fmt_time(date, H_24);
|
||||
};
|
||||
}());
|
||||
|
||||
// XDate.toLocaleDateString and XDate.toLocaleTimeString are
|
||||
// expensive, so we delay running the following code until we need
|
||||
// the full date and time strings.
|
||||
|
||||
@@ -1167,7 +1167,9 @@ class Attachment(ModelReprMixin, models.Model):
|
||||
'path_id': self.path_id,
|
||||
'messages': [{
|
||||
'id': m.id,
|
||||
'name': '{m.pub_date:%Y-%m-%d %H:%M}'.format(m=m)
|
||||
# convert to JavaScript-style UNIX timestamp so we can take
|
||||
# advantage of client timezones.
|
||||
'name': time.mktime(m.pub_date.timetuple()) * 1000
|
||||
} for m in self.messages.all()]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user