[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
This commit is contained in:
Dan Untenzu 2017-02-23 12:05:23 +01:00 committed by Jerome Jutteau
parent ea78557b0c
commit ce571122a5
2 changed files with 59 additions and 6 deletions

View File

@ -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 = '<span class="datetime">'
+ dateToUtcString(date) + ' (GMT)'
+ '</span>';
document.getElementById('validity').style.display = '';
}

View File

@ -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 = '<span class="datetime" data-datetime="' . strftime('%Y-%m-%d %H:%M', $timestamp) . '">'
. strftime('%Y-%m-%d %H:%M', $timestamp) . ' (GMT)</span>';
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 '</td>';
echo '<td>' . $l['mime_type'] . '</td>';
echo '<td>' . jirafeau_human_size($l['file_size']) . '</td>';
echo '<td>' . ($l['time'] == -1 ? '' : strftime('%c', $l['time'])) .
'</td>';
echo '<td>' . ($l['time'] == -1 ? '∞' : jirafeau_get_datetimefield($l['time'])) . '</td>';
echo '<td>';
if ($l['onetime'] == 'O') {
echo 'Y';
@ -608,7 +615,7 @@ function jirafeau_admin_list($name, $file_hash, $link_hash)
echo 'N';
}
echo '</td>';
echo '<td>' . strftime('%c', $l['upload_date']) . '</td>';
echo '<td>' . jirafeau_get_datetimefield($l['upload_date']) . '</td>';
echo '<td>' . $l['ip'] . '</td>';
echo '<td>' .
'<form method="post">' .