Module:DateThings: Difference between revisions

From JJSWiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(19 intermediate revisions by the same user not shown)
Line 13: Line 13:
end
end


 
function trim (s)
function p.datesplit(frame)
    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
   
function p.datesplitsuccinct(frame)
InputSeparator = ","
InputSeparator = ","
OutputSeparator = "・"
OutputSeparator = "・"
Line 22: Line 25:
OutputText = ""
OutputText = ""
OldYear = 0
OldMonth = 0
OldDay = 0
for i, v in ipairs(DateList) do
for i, v in ipairs(DateList) do
if i>1 then
if i>1 then
OutputText = OutputText .. OutputSeparator
OutputText = OutputText .. OutputSeparator
end
end
OutputText = OutputText .. DateList[i]
DateBits = mysplit(trim(DateList[i]),"-")
Year = DateBits[1]
Month = DateBits[2]
Day = DateBits[3]
ThisDate = os.time{ year = Year, month = Month, day = Day }
tempdate = os.date ("*t", ThisDate)
Day = tempdate.day
--OutputText = OutputText .. trim(DateList[i])
--OutputText = OutputText .. Year .. OutputSeparator .. Month .. OutputSeparator .. Day
if Year==OldYear then
if Month==OldMonth then
OutputText = OutputText .. os.date("[[%B ", ThisDate) .. Day .. "|" .. Day .. "]]" --Only the day is different
else
OutputText = OutputText .. os.date("[[%B ", ThisDate) .. Day .. "]]" --Day and month are different
end
else
OutputText = OutputText .. os.date("[[%Y]] [[%B ", ThisDate) .. Day .. "]]" --All three are different
end
OldYear = Year
OldMonth = Month
end
end



Latest revision as of 22:55, 1 December 2024

Documentation for this module may be created at Module:DateThings/doc

local p = {}

--Copied from https://stackoverflow.com/questions/1426954/split-string-in-lua
function mysplit(inputstr, sep)
  if sep == nil then
    sep = "%s"
  end
  local t = {}
  for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
    table.insert(t, str)
  end
  return t
end

function trim (s)
    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
    
function p.datesplitsuccinct(frame)
	InputSeparator = ","
	OutputSeparator = "・"
	InputText = frame.args[1]
	
	DateList = mysplit(InputText,",")
	
	OutputText = ""
	OldYear = 0
	OldMonth = 0
	OldDay = 0
	
	for i, v in ipairs(DateList) do
		if i>1 then
			OutputText = OutputText .. OutputSeparator
		end
		
		DateBits = mysplit(trim(DateList[i]),"-")
		Year = DateBits[1]
		Month = DateBits[2]
		Day = DateBits[3]
		
		ThisDate = os.time{ year = Year, month = Month, day = Day }
		
		tempdate = os.date ("*t", ThisDate)
		Day = tempdate.day
		
		--OutputText = OutputText .. trim(DateList[i])
		--OutputText = OutputText .. Year .. OutputSeparator .. Month .. OutputSeparator .. Day
		
		if Year==OldYear then
			if Month==OldMonth then
				OutputText = OutputText .. os.date("[[%B ", ThisDate) .. Day .. "|" .. Day .. "]]" --Only the day is different
			else
				OutputText = OutputText .. os.date("[[%B ", ThisDate) .. Day .. "]]" --Day and month are different
			end
		else
			OutputText = OutputText .. os.date("[[%Y]] [[%B ", ThisDate) .. Day .. "]]" --All three are different
		end
		
		OldYear = Year
		OldMonth = Month
	end

	--OutputText = Separator .. InputText .. Separator
	return OutputText
end





return p