User:Gary/comments in local time.js: Difference between revisions

Content deleted Content added
new func
Line 6:
 
TODO Make lines with more than one timestamp format all timestamps on the line.
TODO All instances of "new Date(today.getYear()" should be changed to "new Date(today.getFullYear()"
*/
 
Line 77 ⟶ 76:
'days' : 'days'
};
 
/*
Settings
Line 86 ⟶ 85:
if (typeof(LocalComments.dateDifference) == 'undefined')
LocalComments.dateDifference = true;
 
if (typeof(LocalComments.dropDays) == 'undefined')
LocalComments.dropDays = 0;
 
if (typeof(LocalComments.dropMonths) == 'undefined')
LocalComments.dropMonths = 0;
 
if (typeof(LocalComments.dateFormat) == 'undefined')
{
Line 95 ⟶ 100:
LocalComments.dateFormat = 'dmy';
}
 
if (typeof(LocalComments.dayOfWeek) == 'undefined')
LocalComments.dayOfWeek = true;
 
if (typeof(LocalComments.timeFirst) == 'undefined')
LocalComments.timeFirst = true;
 
if (typeof(LocalComments.twentyFourHours) == 'undefined')
LocalComments.twentyFourHours = false;
Line 104 ⟶ 112:
End Settings
*/
 
if (wgCanonicalNamespace == '' || wgCanonicalNamespace == 'MediaWiki' || wgCanonicalNamespace == 'Special' || wgAction == 'history')
return false;
 
replace_text(document.getElementById((wgAction == 'edit' || wgAction == 'submit') ? 'wikiPreview' : 'bodyContent'), /(\d\d):(\d\d), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/g, adjust_time);
}
Line 123 ⟶ 131:
yesterday.setDate(yesterday.getDate() - 1);
tomorrow.setDate(tomorrow.getDate() + 1);
 
// set the date entered
var time = new Date();
Line 129 ⟶ 137:
time.setUTCHours(old_hour);
time.setUTCMinutes(old_minute);
 
// determine the time offset
var utc_offset = -1 * time.getTimezoneOffset() / 60;
Line 136 ⟶ 144:
else
utc_offset = '−' + Math.abs(utc_offset);
 
// set the date bits to output
var year = time.getFullYear(),;
var month = add_leading_zero(time.getMonth() + 1);
var day = time.getDate();
var hour = parseInt(time.getHours()),;
var minute = add_leading_zero(time.getMinutes());
 
// output am or pm depending on the date
var ampm = '';
if (!LocalComments.twentyFourHours)
hour = add_leading_zero(hour);
else
{
ampm = ' am';
Line 151 ⟶ 163:
if (hour == '00') hour = 12;
}
 
// return 'today' or 'yesterday' if that is the case
if (year == today.getFullYear() && month == add_leading_zero(today.getMonth() + 1) && day == today.getDate())
Line 164 ⟶ 176:
var day_names = new Array(language['Sunday'], language['Monday'], language['Tuesday'], language['Wednesday'], language['Thursday'], language['Friday'], language['Saturday']);
var day_of_the_week = day_names[time.getDay()];
 
var descriptive_difference = '';
var last = '';
 
if (LocalComments.dateDifference)
{
// calculate time difference from today and the timestamp
 
today = new Date(today.getYear(), today.getMonth(), today.getDate());
time = new Date(time.getYear(), time.getMonth(), time.getDate());
var milliseconds_ago = today.getTime() - time.getTime();
var days_ago = Math.abs(Math.round(milliseconds_ago / 1000 / 60 / 60 / 24));
 
var difference, difference_word = '', last = '';
if (today.valueOf() >= time.valueOf())
{
difference = new Date(today.valueOf() - time.valueOf());
difference_word = language['ago'];
if (days_ago <= 7)
Line 184 ⟶ 196:
else
{
difference = new Date(time.valueOf() - today.valueOf());
difference_word = language['from now'];
if (days_ago ><= -7)
last = language['this'] + ' ';
}
var descriptive_difference = [];
 
// This method of computing the years & months is not exact.
if (difference.getYear() - 70 > 0)
// However, it's better than the previous method that used
// 1 January + delta days. That was usually quite off because
// it mapped the second delta month to February, which has only 28 days.
// This method is usually not more than one day off.
 
var months_ago = Math.floor(days_ago / 365 * 12); // close enough
var total_months_ago = months_ago;
var years_ago = Math.floor(months_ago / 12);
 
if (months_ago < LocalComments.dropMonths)
years_ago = 0;
else if (LocalComments.dropMonths > 0)
months_ago = 0;
else
months_ago = months_ago - years_ago * 12;
 
if (days_ago < LocalComments.dropDays)
{
months_ago = 0;
var years_ago = (difference.getYear() - 70) + ' ' + pluralize(language['year'], difference.getYear() - 70, language['years']);
descriptive_difference.push(years_ago) = 0;
}
else if (differenceLocalComments.getMonth()dropDays > 0)
days_ago = 0;
else
days_ago = days_ago - Math.floor(total_months_ago * 365 / 12);
 
var descriptive_parts = [];
 
if (years_ago > 0)
{
var months_agofmt_years = difference.getMonth()years_ago + ' ' + pluralize(language['monthyear'], difference.getMonth()years_ago, language['monthsyears']);
descriptive_differencedescriptive_parts.push(months_agofmt_years);
}
if (difference.getDate()months_ago > 0)
{
var new_days_agofmt_months = difference.getDate()months_ago + ' ' + pluralize(language['daymonth'], difference.getDate()months_ago, language['daysmonths']);
descriptive_differencedescriptive_parts.push(new_days_agofmt_months);
}
if (days_ago > 0)
{
descriptive_difference = ' (' + descriptive_difference.join(', ') + ' ' + difference_word + ')';
var fmt_days = days_ago + ' ' + pluralize(language['day'], days_ago, language['days']);
descriptive_parts.push(fmt_days);
}
 
descriptive_difference = ' (' + descriptive_parts.join(', ') + ' ' + difference_word + ')';
}
 
else
{
descriptive_difference = '';
last = '';
}
// format the date according to user preferences
var formatted_date = '', month_name = convert_number_to_month(time.getMonth());
 
switch (LocalComments.dateFormat.toLowerCase())
{
Line 229 ⟶ 263:
formatted_date = year + '-' + month + '-' + add_leading_zero(day);
}
 
var formatted_day_of_the_week = '';
var date = formatted_date + ', ' + last + day_of_the_week + descriptive_difference;
if (LocalComments.dayOfWeek)
formatted_day_of_the_week = ', ' + last + day_of_the_week;
 
var date = formatted_date + formatted_day_of_the_week + descriptive_difference;
}
 
var time = hour + ':' + minute + ampm;
 
if (LocalComments.timeFirst)
var return_date = time + ', ' + date + ' (UTC' + utc_offset + ')';
Line 269 ⟶ 307:
var value = node.nodeValue;
var matches = value.match(search);
 
if (matches != null)
{
Line 275 ⟶ 313:
var old_node = node;
// old_node_list = node.parentNode.childNodes;
 
for (match = 0; match < matches.length; match++)
{
// Create <span class="localcomments" style="font-size: 95%; white-space: nowrap;" title="MATCHES[MATCH]">MATCHES[MATCH]</span>
 
var position;
 
if (after_match != null && length != null)
position = after_match.search(search) + before_match.length + length;
else
position = value.search(search);
 
var length = matches[match].toString().length;
var before_match = value.substring(0, position);
var after_match = value.substring(position + length);
 
var span = document.createElement('span');
span.setAttribute('class', 'localcomments');
Line 297 ⟶ 335:
span.setAttribute('title', matches[match]);
span.appendChild(document.createTextNode(matches[match].toString().replace(search, replace)));
 
var new_node = document.createDocumentFragment();
new_node.appendChild(document.createTextNode(before_match));
Line 304 ⟶ 342:
 
node_parent_node.replaceChild(new_node, old_node);
 
break;
}
Line 317 ⟶ 355:
child = child.nextSibling;
}
 
for (var child = 0; child < children.length; child++)
replace_text(children[child], search, replace);