コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

利用者:Lemonsquash/local time.js

お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。

多くの WindowsLinux のブラウザ

  • Ctrl を押しながら F5 を押す。

Mac における Safari

  • Shift を押しながら、更新ボタン をクリックする。

Mac における ChromeFirefox

  • Cmd Shift を押しながら R を押す。

詳細についてはWikipedia:キャッシュを消すをご覧ください。

/*
	Documentation: [[Wikipedia:Comments in Local Time]]
*/

$(function()
{
	// wgCanonicalNamespace = unsafeWindow.wgCanonicalNamespace
	
	/*
		Language
	*/
	var language = 
		{
			// relative terms
			'Today'		: 	'今日',
			'Yesterday'	: 	'昨日',
			'Tomorrow'	: 	'明日',
			
			'last'		: 	'前の',
			'this'		: 	'次の',
			
			// days of the week
			'Sunday'	: 	'日曜',
			'Monday'	: 	'月曜',
			'Tuesday'	: 	'火曜',
			'Wednesday'	: 	'水曜',
			'Thursday'	: 	'木曜',
			'Friday'	: 	'金曜',
			'Saturday'	: 	'土曜',
			
			// months of the year
			'January'	: 	'1月',
			'February'	: 	'2月',
			'March'		: 	'3月',
			'April'		: 	'4月',
			'May'		: 	'5月',
			'June'		: 	'6月',
			'July'		: 	'7月',
			'August'	: 	'8月',
			'September'	: 	'9月',
			'October'	: 	'10月',
			'November'	: 	'11月',
			'December'	: 	'12月',
			
			// difference words
			'ago'		: 	'前',
			'from now'	: 	'後',
			
			// date phrases
			'year'		: 	'年',
			'years'		: 	'年',
			'month'		: 	'ヶ月',
			'months'	: 	'ヶ月',
			'day'		: 	'日',
			'days'		: 	'日'
		}
	
	/*
		Settings
	*/
	if (typeof(LocalComments) == 'undefined')
		LocalComments = {};

	if (typeof(LocalComments.dateFormat) == 'undefined')
	{
		// Deprecated: LocalizeConfig
		if (typeof(LocalizeConfig) != 'undefined' && typeof(LocalizeConfig.dateFormat) != 'undefined' && LocalizeConfig.dateFormat != '')
			LocalComments.dateFormat = LocalizeConfig.dateFormat;
		else
			LocalComments.dateFormat = 'ymd';
	}
 
	if (typeof(LocalComments.timeFirst) == 'undefined')
		LocalComments.timeFirst = false;
		
	if (typeof(LocalComments.dateDifference) == 'undefined')
		LocalComments.dateDifference = true;
	/*
		End Settings
	*/
 
	if (wgCanonicalNamespace == '' || wgCanonicalNamespace == 'MediaWiki' || wgCanonicalNamespace == 'Special')
		return;
 
	var disabled_urls = new Array('action=history'), unique_url = false, wikiPreview = new Array('action=edit', 'action=submit');
	for (var i = 0; i < disabled_urls.length; i++)
	{
		if (document.location.href.indexOf(disabled_urls[i]) != -1)
			return;
	}
 
	for (var i = 0; i < wikiPreview.length; i++)
	{
		if (document.location.href.indexOf(wikiPreview[i]) != -1)
			unique_url = 'wikiPreview';
	}
 
	var element_id = unique_url ? unique_url : 'bodyContent';
//日本語対応
//	replace_text(document.getElementById(element_id), /(\d\d):(\d\d), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/g, adjust_time);
	replace_text(document.getElementById(element_id), /(\d{4})年(\d{1,2})月(\d{1,2})日 \([月火水木金土日]\) (\d\d):(\d\d) \(UTC\)/g, adjust_time);

function add_leading_zero(number)
{
	if (number < 10)
		number = '0' + number;
	return number;
}

//日本語対応
//function adjust_time(original_timestamp, old_hour, old_minute, old_day, old_month, old_year, offset)
function adjust_time(original_timestamp, old_year, old_month, old_day, old_hour, old_minute, offset)
{
	var today = new Date(), yesterday = new Date(), tomorrow = new Date();
	yesterday.setDate(yesterday.getDate() - 1);
	tomorrow.setDate(tomorrow.getDate() + 1);
	
	// set the date entered
	var time = new Date();
//日本語対応
//	time.setUTCFullYear(old_year, convert_month_to_number(old_month), old_day);
	time.setUTCFullYear(old_year, old_month-1, old_day);
	time.setUTCHours(old_hour);
	time.setUTCMinutes(old_minute);
	
	// determine the time offset
	var utc_offset = -1 * time.getTimezoneOffset() / 60;
	if (utc_offset >= 0)
		utc_offset = '+' + utc_offset;
	else
		utc_offset = '−' + Math.abs(utc_offset);
	
	// set the date bits to output
	var year = time.getFullYear(), month = add_leading_zero(time.getMonth() + 1);
	var day = time.getDate();
	var hour = parseInt(time.getHours()), minute = add_leading_zero(time.getMinutes());

	// output am or pm depending on the date
//日本語対応
	var ampm = '午前';
	if (hour > 11) ampm = '午後';
	if (hour > 12) hour -= 12;
	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())
		var date = language['Today'];
	else if (year == yesterday.getFullYear() && month == add_leading_zero(yesterday.getMonth() + 1) && day == yesterday.getDate())
		var date = language['Yesterday'];
	else if (year == tomorrow.getFullYear() && month == add_leading_zero(tomorrow.getMonth() + 1) && day == tomorrow.getDate())
		var date = language['Tomorrow'];
	else
	{
		// calculate day of week
		day_names = new Array(language['Sunday'], language['Monday'], language['Tuesday'], language['Wednesday'], language['Thursday'], language['Friday'], language['Saturday']);
		day_of_the_week = day_names[time.getDay()];
		
		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());
		
			milliseconds_ago = today.getTime() - time.getTime();
			days_ago = 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)
					last = language['last'] + ' ';
			}
			else
			{
				difference = new Date(time.valueOf() - today.valueOf());
				difference_word = language['from now'];
				if (days_ago >= -7)
					last = language['this'] + ' ';
			}
			var descriptive_difference = [];

			if (difference.getYear() - 70 > 0)
			{
//日本語対応
//				var years_ago = (difference.getYear() - 70) + ' ' + pluralize(language['year'], difference.getYear() - 70, language['years']);
				var years_ago = (difference.getYear() - 70) + pluralize(language['year'], difference.getYear() - 70, language['years']);
				descriptive_difference.push(years_ago);
			}
			if (difference.getMonth() > 0)
			{
//日本語対応
//				var months_ago = difference.getMonth() + ' ' + pluralize(language['month'], difference.getMonth(), language['months']);
				var months_ago = difference.getMonth() + pluralize(language['month'], difference.getMonth(), language['months']);
				descriptive_difference.push(months_ago);
			}
			if (difference.getDate() > 0)
			{
//日本語対応
//				var new_days_ago = difference.getDate() + ' ' + pluralize(language['day'], difference.getDate(), language['days']);
				var new_days_ago = difference.getDate() + pluralize(language['day'], difference.getDate(), language['days']);
				descriptive_difference.push(new_days_ago);
			}
			
//日本語対応
//			descriptive_difference = ' (' + descriptive_difference.join(', ') + ' ' + difference_word + ')';
			descriptive_difference = ' (' + descriptive_difference.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())
		{
			case 'dmy':
//日本語対応
//				formatted_date = day + ' ' + month_name + ' ' + year;
				formatted_date = day + '日' + month_name + year + '年';
				break;
			case 'mdy':
//日本語対応
//				formatted_date = month_name + ' ' + day + ', ' + year;
				formatted_date = month_name + day + '日' + year + '年';
				break;
			default:
//日本語対応
//				formatted_date = year + '-' + month + '-' + add_leading_zero(day);
				formatted_date = year + '年' + month_name + day + '日' ;
		}
		
		var date = formatted_date + ', ' + last + day_of_the_week + descriptive_difference;
	}
	
//日本語対応
//	var time = hour + ':' + minute + ' ' + ampm;
	var time = ampm + hour + '時' + minute + '分';
	
	if (LocalComments.timeFirst)
//日本語対応
//		var return_date = time + ', ' + date + ' (UTC' + utc_offset + ')';
		var return_date = time + ' ' + date + ' (UTC' + utc_offset + ')';
	else
//日本語対応
//		var return_date = date + ', ' + time + ' (UTC' + utc_offset + ')';
		var return_date = date + ' ' + time + ' (UTC' + utc_offset + ')';

	return return_date;
}

function convert_month_to_number(month)
{
   var output = new Date(month + ' 1, 2001');
   return output.getMonth();
}

function convert_number_to_month(number)
{
	var month = new Array(language['January'], language['February'], language['March'], language['April'], language['May'], language['June'], language['July'], language['August'], language['September'], language['October'], language['November'], language['December']);
	return month[number];
}

function pluralize(term, count, plural)
{
  if (plural == null)
    plural = term + 's';

  return (count == 1 ? term : plural)	
}

function replace_text(node, search, replace)
{
	var length = 0;

	if (node.nodeType == 3)
	{
		value = node.nodeValue;
		matches = value.match(search);
		
		if (matches != null)
		{
			node_parent_node = node.parentNode;
			old_node = node;
			// old_node_list = node.parentNode.childNodes;
			
			for (match = 0; match < matches.length; match++)
			{
				if (after_match != null && length != null)
					position = after_match.search(search) + before_match.length + length;
				else
					position = value.search(search);
				length = matches[match].toString().length;
				before_match = value.substring(0, position);
				var after_match = value.substring(position + length);
				
				first_span = document.createElement('span');			
				first_span.setAttribute('style', 'font-size: 95%; white-space: nowrap;');
				
				second_span = document.createElement('span');
				second_span.setAttribute('class', 'localcomments');
				second_span.setAttribute('title', matches[match]);
				second_span.appendChild(document.createTextNode(matches[match].toString().replace(search, replace)));
				
				first_span.appendChild(second_span);
				
				new_node = document.createDocumentFragment();
				new_node.appendChild(document.createTextNode(before_match));
				new_node.appendChild(first_span);
				new_node.appendChild(document.createTextNode(after_match));

				// new_node_list = new_node.childNodes;
				node_parent_node.replaceChild(new_node, old_node);
				// old_node_list = new_node_list;
				
				break;
			}
		}
	}
	else
	{
		var children = [], child = node.firstChild;
		while (child)
		{
			children[children.length] = child;
			child = child.nextSibling;
		}
		
		for (var child = 0; child < children.length; child++)
			replace_text(children[child], search, replace);
	}
}

});