Langbahn Team – Weltmeisterschaft

User:Dudemanfellabra/diffs.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function DateSub(Date1,Date2) {
    var timeDifference = Date2 - Date1;
    var minutesAgo = Math.round(timeDifference / 1000 / 60);
    var hoursAgo = 0
    var daysAgo = 0
    var hoursText = 'hour'
    var daysText = 'day'

    while (minutesAgo >= 60) {
        minutesAgo = minutesAgo-60;
        hoursAgo++;
    }

    while (hoursAgo >= 24) {
        hoursAgo = hoursAgo-24;
        daysAgo++;
    }

    if (hoursAgo!=1)
        hoursText = hoursText + 's';

    if (daysAgo!=1)
        daysText = daysText + 's';

    if (daysAgo > 0) {
        if (hoursAgo > 0) {
            if (minutesAgo <=1 ) {
                minutesAgo = daysAgo + ' ' + daysText + ', ' + hoursAgo + ' ' + hoursText;
            }
            else {
                minutesAgo = daysAgo + ' ' + daysText + ', ' + hoursAgo + ' ' + hoursText + ', ' + minutesAgo + ' minutes';
            }
        }
        else {
            if (minutesAgo <=1 ) {
                minutesAgo = daysAgo + ' ' + daysText;
            }
            else {
                minutesAgo = daysAgo + ' ' + daysText + ', ' + minutesAgo + ' minutes';
            }
        }
    }
    else {
        if (hoursAgo > 0) {
            if (minutesAgo <=1 ) {
                minutesAgo = hoursAgo + ' ' + hoursText;
            }
            else {
                minutesAgo = hoursAgo + ' ' + hoursText + ', ' + minutesAgo + ' minutes';
            }
        }
        else {
            if (minutesAgo <=1 ) {
                minutesAgo = 'Less than a minute';
            }
            else {
                minutesAgo = minutesAgo + ' minutes';
            }
        }
    }
    return minutesAgo;
}

if (document.getElementById('mw-diff-otitle1') || document.getElementById('mw-diff-ntitle1')) {

function convertTimestampStringToDate(id) {
    var timestamp = document.getElementById(id).firstChild.firstChild.firstChild.nodeValue;
    var str = /evision as of /g
    str.lastIndex = 0;
    str.test(timestamp)
    var date = timestamp.substring(str.lastIndex)
/*

All this following bogus crap is because Safari
doesn't recognize Wikipedia's date format.
Every other browser can just use the date string
object above, but Safari is retarded

*/
    var str1 = /[A-z]+/
    var str2 = /\d+/g
    var monthtext = date.match(str1);
    var testdate = date.match(str2);
    var hours = testdate[0];
    var minutes = testdate[1];
    var day = testdate[2];
    var year = testdate[3];
    var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    for (i in months) {
        if (monthtext==months[i]) {
            var month = i;
            break;
        }
        i++
    }
/*

End Safari madness

*/
    date = new Date(year,month,day,hours,minutes);
    return date;
}

var leftNode = document.getElementById('mw-diff-otitle1');
var rightNode = document.getElementById('mw-diff-ntitle1');
var firstDate = convertTimestampStringToDate('mw-diff-otitle1');
var secondDate = convertTimestampStringToDate('mw-diff-ntitle1');

function TimeBetweenDiffs() {

    var minutesAgo = DateSub(firstDate,secondDate);

    var newNode = document.createElement('span');
    newNode.setAttribute('style', 'font-weight:bold; color:red;')
    newNode.appendChild(document.createTextNode('(' + minutesAgo + ' later)'));
    newNode.appendChild(document.createElement('br'));

    rightNode.insertBefore(newNode, rightNode.firstChild);
}

function TimeSinceDiff() {
    var DateNow = new Date();

    var minutesAgo1 = DateSub(firstDate,DateNow);
    var minutesAgo2 = DateSub(secondDate,DateNow);

    document.getElementById('mw-diff-otitle1').title=minutesAgo1 + ' ago';
    document.getElementById('mw-diff-ntitle1').title=minutesAgo2 + ' ago';

    var t = setTimeout("TimeSinceDiff()",60000);
}

$(window).load(TimeSinceDiff);                                  // Adds time since displayed diffs as tooltip
$(window).load(TimeBetweenDiffs);                               // Adds time between diffs

}