Skip to content

Commit

Permalink
Render post dates with client-requested timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
lalcmellkmal committed Jan 26, 2014
1 parent b73a0bd commit ce9cbb0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
8 changes: 7 additions & 1 deletion common.js
Expand Up @@ -557,7 +557,13 @@ function pad(n) {
}

OS.readable_time = function (time) {
var d = new Date(time - new Date().getTimezoneOffset() * 60000);
var h = this.tz_offset;
var offset;
if (h || h == 0)
offset = h * 60 * 60 * 1000;
else /* would be nice not to construct new Dates all the time */
offset = new Date().getTimezoneOffset() * -60 * 1000;
var d = new Date(time + offset);
var k = "日月火水木金土"[d.getUTCDay()];
return (d.getUTCFullYear() + '/' + pad(d.getUTCMonth()+1) + '/' +
pad(d.getUTCDate()) + ' (' + k + ') ' +
Expand Down
1 change: 1 addition & 0 deletions deps.js
Expand Up @@ -66,6 +66,7 @@ exports.SERVER_DEPS = [
'server/server.js',
'server/state.js',
'server/web.js',
'time/server.js',
'tripcode/tripcode.cc',
'voice/server.js',
];
Expand Down
1 change: 1 addition & 0 deletions server/render.js
Expand Up @@ -19,6 +19,7 @@ function tamashii(num) {

exports.write_thread_html = function (reader, req, out, opts) {
var oneeSama = new common.OneeSama(tamashii);
oneeSama.tz_offset = req.tz_offset;

opts.ident = req.ident;
caps.augment_oneesama(oneeSama, opts);
Expand Down
14 changes: 14 additions & 0 deletions server/server.js
Expand Up @@ -28,6 +28,7 @@ try {
if (reportConfig.RECAPTCHA_PUBLIC_KEY)
require('../report/server');
} catch (e) {}
require('../time/server');
if (config.VOICE_PATH)
require('../voice/server');

Expand Down Expand Up @@ -325,6 +326,10 @@ web.resource(/^\/(\w+)\/$/, function (req, params, cb) {
if (!caps.can_ever_access_board(req.ident, board))
return cb(404);

// we don't do board etags yet
var info = {etag: 'dummy', req: req};
hooks.trigger_sync('buildETag', info);

cb(null, 'ok', {board: board});
},
function (req, resp) {
Expand Down Expand Up @@ -383,6 +388,10 @@ web.resource(/^\/(\w+)\/page(\d+)$/, function (req, params, cb) {
yaku.disconnect();
});
yaku.once('begin', function (threadCount) {
// we don't do board etags yet
var info = {etag: 'dummy', req: req};
hooks.trigger_sync('buildETag', info);

cb(null, 'ok', {
board: board, page: page, yaku: yaku,
threadCount: threadCount,
Expand Down Expand Up @@ -504,6 +513,11 @@ web.resource(/^\/(\w+)\/(\d+)$/, function (req, params, cb) {
etag += '-locked';
if (req.ident.auth)
etag += '-auth';

var info = {etag: etag, req: req};
hooks.trigger_sync('buildETag', info);
etag = info.etag;

if (req.headers['if-none-match'] === etag) {
yaku.disconnect();
return cb(null, 304);
Expand Down
6 changes: 6 additions & 0 deletions time/client.js
Expand Up @@ -25,6 +25,12 @@ function is_skewed() {

if (is_skewed()) {
adjust_all_times();

setTimeout(function () {
// next request, have the server render the right times
var tz = -new Date().getTimezoneOffset() / 60;
$.cookie('timezone', tz, { expires: 90 });
}, 3000);
}

})();
18 changes: 18 additions & 0 deletions time/server.js
@@ -0,0 +1,18 @@
var hooks = require('../hooks');

function parse_timezone(tz) {
if (!tz && tz != 0)
return null;
tz = parseInt(tz, 10);
if (isNaN(tz) || tz < -24 || tz > 24)
return null;
return tz;
}

hooks.hook_sync('buildETag', function (info) {
var tz = parse_timezone(info.req.cookies.timezone);
if (tz) {
info.req.tz_offset = tz;
info.etag += '-tz' + tz;
}
});

0 comments on commit ce9cbb0

Please sign in to comment.