From: Dan Untenzu Date: Thu, 23 Feb 2017 11:05:23 +0000 (+0100) Subject: [FEATURE] Show readable date format X-Git-Tag: 3.0.0~7 X-Git-Url: https://git.p6c8.net/jirafeau_project.git/commitdiff_plain/ce571122a5b6866835a9313e599bcad57bc790ff?ds=inline [FEATURE] Show readable date format Whenever date objects in PHP or JavaScript are converted to strings, it depends on the servers/clients settings and timezone which locale and format is used. This may lead to situation where the date is in english, but all other texts of Jirafeau service are in another language. Depending on unser language it may be not possible to localise the date, since we would need to have a list of all existing date formats. In both PHP and JavaScript. To avoid these translation obstacles we may use the following fallback: * Always use UTC timezone in PHP and render dates with UTC only * Use the rather international format »YYYY-MM-DD hh:mm« Refs #59 #106 #73 --- diff --git a/lib/functions.js.php b/lib/functions.js.php index 3b39c6d..1a56186 100644 --- a/lib/functions.js.php +++ b/lib/functions.js.php @@ -39,6 +39,50 @@ function isEmpty(str) { return (!str || 0 === str.length); } +// Extend date object with format method +Date.prototype.format = function(format) { + format = format || 'YYYY-MM-DD hh:mm'; + + var zeropad = function(number, length) { + number = number.toString(); + length = length || 2; + while(number.length < length) + number = '0' + number; + return number; + }, + formats = { + YYYY: this.getFullYear(), + MM: zeropad(this.getMonth() + 1), + DD: zeropad(this.getDate()), + hh: zeropad(this.getHours()), + mm: zeropad(this.getMinutes()), + O: (function() { + localDate = new Date; + sign = (localDate.getTimezoneOffset() > 0) ? '-' : '+'; + offset = Math.abs(localDate.getTimezoneOffset()); + hours = zeropad(Math.floor(offset / 60)); + minutes = zeropad(offset % 60); + return sign + hours + ":" + minutes; + })() + }, + pattern = '(' + Object.keys(formats).join(')|(') + ')'; + + return format.replace(new RegExp(pattern, 'g'), function(match) { + return formats[match]; + }); +}; + +function dateToUtcString(datelocal) { + return new Date( + datelocal.getUTCFullYear(), + datelocal.getUTCMonth(), + datelocal.getUTCDate(), + datelocal.getUTCHours(), + datelocal.getUTCMinutes(), + datelocal.getUTCSeconds() + ).format(); +} + function show_link (url, reference, delete_code, crypt_key, date) { // Upload finished @@ -67,7 +111,7 @@ function show_link (url, reference, delete_code, crypt_key, date) b += encodeURIComponent(download_link_href) + "%0D"; if (false == isEmpty(date)) { - b += "%0D" + encodeURIComponent("This file will be available until " + date.toString()) + "%0D"; + b += "%0D" + encodeURIComponent("This file will be available until " + dateToUtcString(date)) + "%0D"; document.getElementById('upload_link_email').href = "mailto:?body=" + b + "&subject=" + encodeURIComponent(filename); } @@ -83,7 +127,9 @@ function show_link (url, reference, delete_code, crypt_key, date) document.getElementById('validity').style.display = 'none'; } else { - document.getElementById('date').innerHTML = date.toString(); + document.getElementById('date').innerHTML = '' + + dateToUtcString(date) + ' (GMT)' + + ''; document.getElementById('validity').style.display = ''; } diff --git a/lib/functions.php b/lib/functions.php index bac444b..50ab345 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -120,6 +120,14 @@ function jirafeau_human_size($octets) return round($o, 1) . $u[$p]; } +// Convert UTC timestamp to a datetime field +function jirafeau_get_datetimefield($timestamp) +{ + $content = '' + . strftime('%Y-%m-%d %H:%M', $timestamp) . ' (GMT)'; + return $content; +} + function jirafeau_clean_rm_link($link) { $p = s2p("$link"); @@ -599,8 +607,7 @@ function jirafeau_admin_list($name, $file_hash, $link_hash) echo ''; echo '' . $l['mime_type'] . ''; echo '' . jirafeau_human_size($l['file_size']) . ''; - echo '' . ($l['time'] == -1 ? '' : strftime('%c', $l['time'])) . - ''; + echo '' . ($l['time'] == -1 ? '∞' : jirafeau_get_datetimefield($l['time'])) . ''; echo ''; if ($l['onetime'] == 'O') { echo 'Y'; @@ -608,7 +615,7 @@ function jirafeau_admin_list($name, $file_hash, $link_hash) echo 'N'; } echo ''; - echo '' . strftime('%c', $l['upload_date']) . ''; + echo '' . jirafeau_get_datetimefield($l['upload_date']) . ''; echo '' . $l['ip'] . ''; echo '' . '
' . @@ -1109,7 +1116,7 @@ function jirafeau_challenge_upload ($cfg, $ip, $password) if (!jirafeau_has_upload_password($cfg)) { return false; } - + foreach ($cfg['upload_password'] as $p) { if ($password == $p) { return true;