Jump to content

Module:Auto date formatter

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 16:09, 16 March 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require ('strict');
local get_args = require ('Module:Arguments').getArgs;

local global_df = mw.loadData ('Module:Citation/CS1/Configuration').global_df;	-- fetch global date format specified by {{use xxx dates}} template

local lang_obj = mw.language.getContentLanguage();								-- language object for date formatting


--[[--------------------------< F O R M A T T E R >------------------------------------------------------------

local function to format <date> according to <format> (dmy or mdy) and <style> (long, short, year initial) as
specified by the |cs1-dates= parameter of the {{use xxx dates}} template.

valid |cs1-dates= format character strings are:
	l, ll, all	– default; long-form publication and access- / archive-dates; 'all' when |cs1-dates= omitted or empty
	ls			– long-form publication dates; abbreviated access- / archive-dates
	ly			– long-form publication dates; year-initial numeric access- / archive-dates (ymd)
	s, ss		– abbreviated publication and access- / archive-dates
	sy			– abbreviated publication dates; year-initial numeric access- / archive-dates (ymd)
	y, yy		– year-initial numeric publication, access- and archive-dates (ymd); overrides base format

note: 'all' is added by get_date_format() in Module:Citation/CS1/Configuration when |cs1-dates= is omitted or empty

]]

local function formatter (date, format, style)
	local function do_format (format_str, date)									-- local function to do the actual formatting
		local good, new_date = pcall (lang_obj.formatDate, lang_obj, format_str, date);
		return (good and new_date) or date;										-- when good is boolean true, return <new_date>; return <date> else
	end

	if 'l' == style then														-- long month-name format
		if 'dmy' == format then
			return do_format ('j F Y', date);
		else																	-- must be mdy
			return do_format ('F j, Y', date);
		end

	elseif 's' == style then													-- abbreviated month-name format
		if 'dmy' == format then
			return do_format ('j M Y', date);
		else																	-- must be mdy
			return do_format ('M j, Y', date);
		end

	elseif 'y' == style then													-- year-initial numeric format; overrides dmy/mdy in <format>
		return do_format ('Y-m-d', date);
	end
end


--[[--------------------------< P U B _ D A T E _ F O R M A T >------------------------------------------------

For publication dates |date=, |publication-date=, etc

	{{#invoke:Sandbox/Trappist the monk/df|pub_date_format|16 March 2025}}

]]

local function pub_date_format (frame)
	if not global_df then
		return date;															-- when article does not have {{use xxx dates}}, abandon
	end

	local date = frame.args[1];													-- <args[1]> is date to be formatted
	
	local base_format = global_df:match ('^(%a%a%a)');							-- publication date format: 'dmy' or 'mdy'
	local date_style = global_df:match ('^%a%a%a%-(.+)');						-- |cs1-dates= style modifiers

	local good, new_date;
	if ({['all'] = true, ['ll'] = true, ['l'] = true, ['ls'] = true, ['ly'] = true})[date_style] then	-- default long-form style base dates
		return formatter (date, base_format, 'l');
	elseif ({['s'] = true, ['ss'] = true, ['sy'] = true})[date_style] then		-- abbrviated style publication dates
		return formatter (date, base_format, 's');
	else																		-- must be year-initial style publication dates; overrides base format in <base_format>
		return formatter (date, base_format, 'y');
	end
end


--[[--------------------------< A C C E S S _ A R C H I V E _ F O R M A T >------------------------------------

For access and archive dates |access-date=, |accessdate=, archive-date=, archivedate=

	{{#invoke:Sandbox/Trappist the monk/df|access_archive_format|16 March 2025}}

]]

local function access_archive_format (frame)
	if not global_df then
		return date;															-- when article does not have {{use xxx dates}}, abandon
	end

	local date = frame.args[1];													-- <args[1]> is date to be formatted

	local base_format = global_df:match ('^(%a%a%a)');							-- get {{use xxx dates}} base date format: 'dmy' or 'mdy'
	local date_style = global_df:match ('^%a%a%a%-(.+)');						-- |cs1-dates= style modifiers

	if ({['all'] = true, ['ll'] = true, ['l'] = true})[date_style] then			-- default long-form style access- / archive-dates
		return formatter (date, base_format, 'l');
	elseif ({['ls'] = true, ['s'] = true, ['ss'] = true})[date_style] then		-- abbrviated style access- / archive-dates
		return formatter (date, base_format, 's');
	else																		-- must be year-initial style access- / archive-dates; overrides base format in <base_format>
		return formatter (date, base_format, 'y');
	end
end


--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]

return {
	pub_date_format = pub_date_format,
	access_archive_format = access_archive_format,
	}