Jump to content

User:Guywan/Scripts/12HourFormat.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Guywan (talk | contribs) at 13:15, 2 February 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*
 * Install with:
 * <code><nowiki>{{subst:Iusc|User:Guywan/Scripts/12HourFormat.js}}</nowiki></code>
 * or:
 * <code><nowiki>importScript("User:Guywan/Scripts/12HourFormat.js"); // Backling: [[User:Guywan/Scripts/12HoursFormat.js]]</nowiki></code>
 *
 * This script was originally authored by User:Bility, [[User:Bility/convert24hourtime.js]].
 * Considerable contributions by User:DannyS712, [[User:DannyS712/12Hours.js]], whose version I have forked.
 * I have mainly streamlined the code and introduced more functionality.
 */

var config =
{
	name 	: "[[User:Guywan/Scripts/12HourFormat]]",
	version : 3.0,
	debug	: false
};

$(function()
{
	if(mw.config.get("wgCanonicalNamespace") == "Special")
	{
		switch(mw.config.get("wgCanonicalSpecialPageName"))
		{
			case "Contributions":
				$(".mw-contributions-list").children().each(function()
				{
					$(this).children().first().text(partition($(this).children().first().text()));
				});
				break;
				
			case "Log":
			case "Userrights":
				$("ul").children().each(function()
				{	
					$(this).html(regexpReplace($(this).html(), 1, 5));
				});
				break;
				
			case "Project":
				// TODO
				break;
		}
	}
	else if(mw.config.get("wgCanonicalNamespace") == "File")
	{
		// Modify time in file history table.
		$("td > a").each(function()
		{
			var text = $(this).text();
			
			var index = text.indexOf(":");
			if(index > -1)
			{
				var time = text.substr(index - 2, 6);
				
				// Will convert anything in a table matching the pattern "dd:dd," where d is a digit.
				if(time.match(/\d\d:\d\d,/))
				{
					$(this).text(partition(text));
				}
			}
		});
	}
	else if(mw.config.get("wgCanonicalNamespace") == "User")
	{
		// TODO
	}
	
	switch(mw.config.get("wgAction"))
	{
		case "history":
			$("span.mw-history-histlinks ~ a").each(function()
			{
				if($(this).attr("class") == "mw-changeslist-date")
				{
					$(this).text(partition($(this).text()));
				}
			});
			break;
			
		case "view":
			if(document.title.indexOf("Difference between revisions") > -1)
			{
				var comp = "#mw-diff-ntitle1 > strong > a";
				var temp;
				for(i = 0; i < 2; i++)
				{
					temp = ($(comp).html());
					
					$(comp).html(temp.replace(/(\d\d:\d\d)/g, convert(temp.substr(temp.indexOf(":") - 2, 5))));
					
					comp = "#mw-diff-otitle1 > strong > a";
				}
				
				$(".diff-currentversion-title").text(partition($(".diff-currentversion-title").text()));
			}
			break;
			
		case "edit":
			// TODO
			break;
	}
	
	// Modify time in warning banners.
	if($("div").hasClass("mw-warning-with-logexcerpt mw-content-ltr"))
	{
		$(".mw-warning-with-logexcerpt.mw-content-ltr > ul").children().each(function()
		{
			$(this).html(regexpReplace($(this).html(), 1, 5));
		});
	}

	// Modify the time in the footer of most pages.
	if($("#footer-info-lastmod").length)
	{
		$("#footer-info-lastmod").text(partition($("#footer-info-lastmod").text()));
	}
});

function partition(text)
{
	var front = text.substr(0, text.indexOf(":") - 2);
	var time = convert(text.substr(text.indexOf(":") - 2, 5));
	var end = text.substr(text.indexOf(":") + 3, text.length);
	
	return (front + time + end);
}

function regexpReplace(html, start, num)
{	
	return html.replace(/(\d\d:\d\d)/g, convert(html.substr(start, num)));
}

function convert(time)
{
	var hour = parseFloat(time.substr(0, 2));
	
	if(hour >= 12)
	{
		if(hour != 12)
		{
			hour = hour - 12;
		}
		
		return (hour + time.substr(2, 5) + " PM");
	}
	
	if(hour === 0)
	{
		hour = 12;
	}
	
	return (hour + time.substr(2, 5) + " AM");
}