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 19:43, 15 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.
/*
 * 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.
 */

// <nowiki>
$(function()
{
	////////////////
	// NAMESPACES //
	////////////////
	
	switch(mw.config.get("wgCanonicalNamespace"))
	{
		case "Special":
		
			switch(mw.config.get("wgCanonicalSpecialPageName"))
			{
				case "Contributions":
					$(".mw-changeslist-date").each(function()
					{
						$(this).html(rxReplace($(this).html()));
					});
					break;
					
				case "Log":
				case "Userrights":
				case "AbuseLog":
					$("ul").children().each(function()
					{	
						$(this).html(rxReplace($(this).html()));
					});
					break;
					
				case "Listusers":
					$("ul").children().each(function()
					{
						$(this).html($(this).html().replace(/(on \d{1,2} \w+ \d{4} at )(\d\d:\d\d)()/g, rxPartition));
					});
					break;
					
				case "Project":
					// TODO
					break;
			}
			break;
			
		case "File":
			// Modify time in file history table.
			$("td > a").each(function()
			{
				$(this).html($(this).html().replace(/()(\d\d:\d\d)(, \d{1,2} \w+ \d{4})/g, rxPartition));
			});
			break;
			
		case "User":
			// TODO
			break;
	}
	
	/////////////
	// ACTIONS //
	/////////////
	
	switch(mw.config.get("wgAction"))
	{
		case "history":
			$(".mw-changeslist-date").each(function()
			{
					$(this).text(rxReplace($(this).text()));
			});
			break;
			
		case "view":
		
			if(document.title.indexOf("Difference between revisions") > -1)
			{
				$("#mw-diff-ntitle1 > strong > a").html(rxReplace($("#mw-diff-ntitle1 > strong > a").html()));
				$("#mw-diff-otitle1 > strong > a").html(rxReplace($("#mw-diff-otitle1 > strong > a").html()));
				$(".diff-currentversion-title").text(rxReplace($(".diff-currentversion-title").text()));
			}
			
			// Modify time in permalink pages.
			if($("div").hasClass("mw-revision"))
			{
				$("#mw-revision-date").html(rxReplace($("#mw-revision-date").html()));
			}
			
			// Modify user signatures.
			//$("#mw-content-text").html($("#mw-content-text").html().replace(/( )(\d\d:\d\d)(, \d{1,2} \w+ \d{4} )/g, rxPartition));
			break;
			
		case "edit":
			// TODO
			break;
	}
	
	///////////
	// OTHER //
	///////////
	
	// 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(rxReplace($(this).html()));
		});
	}

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

/*
 * Used when it is unlikely non-times will be matched.
 */
function rxReplace(html)
{
	return html.replace(/(\d\d:\d\d)/g, convert);
}

/*
 * Used when a very specific match needs to be made and
 * only a substring of the match should be modified.
 */
function rxPartition(match, p1, p2, p3)
{
	return (p1 + convert(p2) + p3);
}

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");
}
// </nowiki>