Module:Sandbox/Safan41/Dates

.mw-parser-output .documentation,.mw-parser-output .documentation-metadata{border:1px solid var(--border-color-base,#a2a9b1);background-color:#ecfcf4;color:inhe

Module:Sandbox/Safan41/Dates
local p = {} -- p stands for package

function p.dateformat( frame )
  local date, format = frame.args.date or "", frame.args.format -- Arguments
  local mnthlst = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
  
  function dateextract(date) -- find date pattern, use os.time() to retrieve
    month, day, year = 1, 1, 1
    haha = true
    if string.match(date, "on the (%d+)%a?%a? of (%a+) in the year of our Lord (%d+)") then
    	day, month, year =  string.match(date, "on the (%d+)%a?%a? of (%a+) in the year of our Lord (%d+)")
    elseif string.match(date, "(%a+) (%d+)%a?%a?, (%d+)") then
      month, day, year = string.match(date, "(%a+) (%d+)%a?%a?, (%d+)")
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+)") then
      day, month, year = string.match(date, "(%d+)%a?%a? (%a+) (%d+)")
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) BC") or string.match(date, "(%d+)%a?%a? (%a+) (%d+) BCE") or string.match(date, "(%d+)%a?%a? (%a+) (%d+) AD") or string.match(date, "(%d+)%a?%a? (%a+) (%d+) CE") then
      day, month, year = string.match(date, "(%d+)%a?%a? (%a+) (%d+)")
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+)") then
      day, month, year = string.match(date, "(%d+)%a?%a? (%a+) (%d+)")
    elseif string.match(date, "(%d+)-(%d+)-(%d+)") then
      year, month, day = string.match(date, "(%d+)-(%d+)-(%d+)")
    elseif string.match(date, "(%d+)/(%d+)/(%d+)") then
      day, month, year = string.match(date, "(%d+)/(%d+)/(%d+)")
    elseif string.match(date, "(%a+) (%d+)") then
      month, year = string.match(date, "(%a+) (%d+)")
    elseif string.match(date, "(%d+)%a?%a? (%d+)") then
      day, year = string.match(date, "(%d+)%a?%a? (%d+)")
    elseif string.match(date, "(%d+) BC") or string.match(date, "(%d+) BCE") or string.match(date, "(%d+) AD") or string.match(date, "(%d+) CE") then
      year = string.match(date, "(%d+)")
    elseif string.match(date, "(%d+)%a?%a? (%a+)") then
      year, month  = string.match(date, "(%d+)%a?%a? (%a+)")
      haha = false
    elseif string.match(date, "(%d+)") then
      year = string.match(date, "(%d+)")
    else
      for mo in mnthlst do
        if string.match(date, "(".. mo ..")") then month = string.match(date, "(".. mo ..")") end
      end
      year = string.match(date, "(%4d)")
      day = string.match(date, "(%2d)") or string.match(date, "(%2d)th")
    end
    if type(month) == "string" then 
      for k,v in ipairs(mnthlst) do 
        if string.upper(v) == string.upper(month) or string.upper(string.sub(v, 1, 3)) == string.sub(string.upper(month), 1, 3) then 
        	month = k
        	haha = true
        	do break end
        end
      end
    end
    if not haha then
		return os.time{year=year, month=1, day=day}, haha
	else
		return os.time{year=year, month=month, day=day}, haha
	end
  end
  a, b, c = pcall(dateextract, date)
  if a == false then 
  	return "Invalid entry" 
  end
  yeet = b --date information
  
  if format then -- if there is a format, find format date using os.date() as specified
    if format == "mdy" then
      text = os.date("%B %d, %Y", yeet)
    elseif format == "dmy" then
      text = os.date("%d %B %Y", yeet)
    elseif format == "iso" then
      text = os.date("%Y-%m-%d", yeet)
    elseif format == "year" then
      text = os.date("%Y", yeet)
    elseif format == "monthandyear" then
      text = os.date("%B %Y", yeet)
  else -- no format, go off of pattern
    if string.match(date, "(%a+) (%d+)%a?%a?, (%d+)") then
      text = os.date("%B %d, %Y", yeet)
    
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) AD") then
      text = os.date("%d %B %Y", yeet) .. " AD"
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) BCE") then
      text = os.date("%d %B %Y", yeet) .. " BCE"
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) CE") then
      text = os.date("%d %B %Y", yeet) .. " CE"
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) BC") then 
      text = os.date("%d %B %Y", yeet) .. " BC"
      
    elseif string.match(date, "(%d+) AD") then
      text = os.date("%Y", yeet) .. " AD"
    elseif string.match(date, "(%d+) BCE") then
      text = os.date("%Y", yeet) .. " BCE"
    elseif string.match(date, "(%d+) CE") then
      text = os.date("%Y", yeet) .. " CE"
    elseif string.match(date, "(%d+) BC") then 
      text = os.date("%Y", yeet) .. " BC"
    
    elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+)") or string.match(date, "on the (%d+)%a?%a? of (%a+) in the year of our Lord (%d+)") then
      text = os.date("%d %B %Y", yeet)
    elseif string.match(date, "(%d+)-(%d+)-(%d+)") or string.match(date, "(%d+)/(%d+)/(%d+)") then
      text = os.date("%Y-%m-%d", yeet)
    elseif string.match(date, "(%d+) (%d+)") then
        text = os.date("%Y", yeet)
    elseif string.match(date, "(%d+)%a?%a? (%a+)") then
        if not c then
        	text = os.date("%Y", yeet)
        else
        	text = os.date("%Y %B", yeet)
        end
    elseif string.match(date, "(%a+) (%d+)") then
      if not pcall(os.date("%B %Y", yeet)) then
        text = string.match(date, "(%d+)")
      else
        text = os.date("%B %Y", yeet)
      end
    elseif string.match(date, "(%d+)") then
      if not pcall(os.date("%Y", yeet)) then --If os.date() can't validate any numbers, return the numbers only
        text = string.match(date, "(%d+)")
      else
        text = os.date("%Y", yeet)
      end
    end
	end
end
	if string.match(date, "^sometime around ") or string.match(date, " %(uncertain%)$") then
		text = "circa " .. text
	end
	text = text:gsub('^0', ''):gsub(' 0', ' ')
    return text
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.