Usor:AristippusSer/DateToRoman.js

E Vicipaedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.using( 'mediawiki.util', function () {
    $( document ).ready( function () { 
		var monthsFullGen = ['Ianuarii','Februarii','Martii',
		                     'Aprilis', 'Maii', 'Iunii', 'Iulii',
		                     'Augusti', 'Septembris', 'Octobris',
		                     'Novembris', 'Decembris'];
		$("span.dies-latine").each(function() {
		  var year = this.attributes["data-dies-latine-annus"].value,
		      month = this.attributes["data-dies-latine-mensis"].value,
		      day = this.attributes["data-dies-latine-dies"].value;
		  var dateWikiLink = mw.util.getUrl(day + '_' + monthsFullGen[month-1]),
		      yearWikiLink = mw.util.getUrl(year),
		      dateText = toRomanDay(year, month, day) + ' ' + toRomanMonth(month, day),
		      yearText = toRomanYear(year, month, day) + ' a.u.c.';
		  console.log(dateWikiLink + ' | ' + dateText + ' | ' + yearWikiLink + ' | ' + yearText);
		  this.innerHTML = '<a href="' + dateWikiLink + '">' + dateText +
		    '</a> <a href="' + yearWikiLink + '">' + yearText + '</a>';
		});
    });
});

// Functions for converting date into Roman calendar (AUC year)

function isNumeric(num) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

// https://stackoverflow.com/a/14326809
// License: CC-BY-SA 3.0
function toRomanNumeral(num){
    num = parseInt(num);

    if (num > 3999999) { alert('Number is too big!'); return false; }
    if (num < 1) { alert('Number is too small!'); return false; }

    var result = '',
        ref = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'],
        xis = [1000,900,500,400,100,90,50,40,10,9,5,4,1];

    if (num <= 3999999 && num >= 4000) {
        num += ''; // need to convert to string for .substring()
        result = '<label style="text-decoration: overline;">'+toRomanNumeral(num.substring(0,num.length-3))+'</label>';
        num = num.substring(num.length-3);
    }

    for (x = 0; x < ref.length; x++){
        while(num >= xis[x]){
            result += ref[x];
            num -= xis[x];
        }
    }
    return result;
}

function isLeap(year) {
  return (year % 4 === 0 && (year % 100 !== 0 || year % 400 == 0));
}

function toRomanDay(year, month, day) {
  if (day === 1) return 'Kal.';
  else if (month === 1 || month === 8 || month === 12) {
    if (day < 4) return 'a.d. ' + toRomanNumeral(6-day) + ' Non.';
    else if (day === 4) return 'pr. Non.';
    else if (day === 5) return 'Non.';
    else if (day < 12) return 'a.d. ' + toRomanNumeral(14-day) + ' Id.';
    else if (day === 12) return 'pr. Id.';
    else if (day === 13) return 'Id.';
    else if (day < 31) return 'a.d. ' + toRomanNumeral(33-day) + ' Kal.';
    else return 'pr. Kal.';
  } else if (month === 3 || month === 5 || month === 7 || month === 10) {
    if (day < 6) return 'a.d. ' + toRomanNumeral(8-day) + ' Non.';
    else if (day === 6) return 'pr. Non.';
    else if (day === 7) return 'Non.';
    else if (day < 14) return 'a.d. ' + toRomanNumeral(16-day) + ' Id.';
    else if (day === 14) return 'pr. Id.';
    else if (day === 15) return 'Id.';
    else if (day < 31) return 'a.d. ' + toRomanNumeral(33-day) + ' Kal.';
    else return 'pr. Kal.';
  } else if (month === 2 && !isLeap(year)) {
    if (day < 4) return 'a.d. ' + toRomanNumeral(6-day) + ' Non.';
    else if (day === 4) return 'pr. Non.';
    else if (day === 5) return 'Non.';
    else if (day < 12) return 'a.d. ' + toRomanNumeral(14-day) + ' Id.';
    else if (day === 12) return 'pr. Id.';
    else if (day === 13) return 'Id.';
    else if (day < 28) return 'a.d. ' + toRomanNumeral(30-day) + ' Kal.';
    else return 'pr. Kal.';
  } else if (month === 2 && isLeap(year)) {
    if (day < 4) return 'a.d. ' + toRomanNumeral(6-day) + ' Non.';
    else if (day === 4) return 'pr. Non.';
    else if (day === 5) return 'Non.';
    else if (day < 12) return 'a.d. ' + toRomanNumeral(14-day) + ' Id.';
    else if (day === 12) return 'pr. Id.';
    else if (day === 13) return 'Id.';
    else if (day === 14) return 'a.d. bis VI Kal.'; // why
    else if (day < 29) return 'a.d. ' + toRomanNumeral(31-day) + ' Kal.';
    else return 'pr. Kal.';
  } else { // month is 4, 6, 9 or 11
    if (day < 4) return 'a.d. ' + toRomanNumeral(6-day) + ' Non.';
    else if (day === 4) return 'pr. Non.';
    else if (day === 5) return 'Non.';
    else if (day < 12) return 'a.d. ' + toRomanNumeral(14-day) + ' Id.';
    else if (day === 12) return 'pr. Id.';
    else if (day === 13) return 'Id.';
    else if (day < 30) return 'a.d. ' + toRomanNumeral(32-day) + ' Kal.';
    else return 'pr. Kal.';
  }
}
  
function toRomanMonth(month, day) {
  var ides, months = ['Ian.', 'Feb.', 'Mar.', 'Apr.', 'Mai.', 'Iun.', 'Iul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'];
  if (month === 3 || month === 5 || month === 7 || month === 10) ides = 15;
  else ides = 13;
  if (day <= ides) return months[month-1];
  else if (month === 12) return months[0];
  else return months[month];  
}

function toRomanYear(year, month, day) {
  var conditas = 753;
  var thisYear = +year + +conditas; // O stultitiam JavaScripti
  if (month === 12 && day > 13) return toRomanNumeral(thisYear+1);
  else return toRomanNumeral(thisYear);
}