Module:SetDateRange: Difference between revisions

From Seven Sages of Rome
No edit summary
Tag: Reverted
No edit summary
Tags: Manual revert Reverted
Line 3: Line 3:
local function isStringSet(s)
local function isStringSet(s)
return s ~= nil and s ~= ''
return s ~= nil and s ~= ''
end
local function tableLength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
local function splitString (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
local function formatDate(d)
local dateParts = splitString(d, "-")
local datePartsNumber = tablelength(dateParts)
if datePartsNumber == 1 then
return d
elseif datePartsNumber == 2 then
local time = os.time{year=1970, month=1, day=1}
return os.date("%B %Y", time)
elseif datePartsNumber == 3 then
local time = os.time{year=1970, month=1, day=1}
return os.date("%d %B %Y", time)
end
local convertedTimestamp = os.time({year = runyear, month = runmonth, day = runday, hour = runhour, min = runminute, sec = runseconds})
return convertedTimestamp
end
end


Line 12: Line 47:
local args = f.args
local args = f.args
local start_date = args.start_date:gsub("-", "/")
local start_date = args.start_date
local end_date = args.end_date:gsub("-", "/")
local end_date = args.end_date
local date_range = ""
local date_range = ""
Line 21: Line 56:
if isStringSet(start_date) and isStringSet(end_date) then
if isStringSet(start_date) and isStringSet(end_date) then
date_range = start_date .. " - " .. end_date
date_range = start_date .. "-" .. end_date
elseif isStringSet(start_date) then
elseif isStringSet(start_date) then
date_range = start_date
date_range = start_date
Line 39: Line 74:
return date_range
return date_range
end
p.test = function( f )
return "test"
end
end


return p
return p

Revision as of 14:05, 17 June 2024

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

local p = {}; 

local function isStringSet(s)
	return s ~= nil and s ~= ''
end

local function tableLength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

local function splitString (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

local function formatDate(d)
	local dateParts = splitString(d, "-")
	local datePartsNumber = tablelength(dateParts)
	
	if datePartsNumber == 1 then
		return d
	elseif datePartsNumber == 2 then
		local time = os.time{year=1970, month=1, day=1}
		return os.date("%B %Y", time)
	elseif datePartsNumber == 3 then
		local time = os.time{year=1970, month=1, day=1}
		return os.date("%d %B %Y", time)
	end

	local convertedTimestamp = os.time({year = runyear, month = runmonth, day = runday, hour = runhour, min = runminute, sec = runseconds})
	return convertedTimestamp
end

p.set = function( f )
	if not mw.smw then
        return "mw.smw module not found"
	end

	local args = f.args
	
	local start_date = args.start_date
	local end_date = args.end_date
	
	local date_range = ""
	
	local date_range_property = args.date_range_property
	local sort_date_property = args.sort_date_property
	
	if isStringSet(start_date) and isStringSet(end_date) then
		date_range = start_date .. "-" .. end_date
	elseif isStringSet(start_date) then
		date_range = start_date
	elseif isStringSet(end_date) then
		date_range = end_date
	end
	
	local property = {}
	property[date_range_property] = date_range
	property[sort_date_property] = start_date
	
	local result = mw.smw.set( property )
	
	if result == false then
		print("Error: Couldn't set date range")
	end
	
	return date_range
end

p.test = function( f )
	return "test"
end

return p