btw this Wiki is not meant for a wider audience yet

Module:DateNav

From HFGCS Wiki
Jump to navigation Jump to search

local p = {}

-- Function to add or subtract days from a YYMMDD date string function p.addDays(frame)

   local dateStr = frame.args[1] or frame.args.date or ""
   local days = tonumber(frame.args[2] or frame.args.days or 0)
   -- Validate input
   if #dateStr ~= 6 then
       return 'Invalid date format (expected YYMMDD)'
   end
   -- Parse YYMMDD
   local yy = tonumber(dateStr:sub(1, 2))
   local mm = tonumber(dateStr:sub(3, 4))
   local dd = tonumber(dateStr:sub(5, 6))
   if not yy or not mm or not dd then
       return 'Invalid date'
   end
   -- Convert YY to full year (assuming 2000s)
   local year = 2000 + yy
   -- Create timestamp using os.time
   local timestamp = os.time({year = year, month = mm, day = dd, hour = 12})
   if not timestamp then
       return 'Invalid date'
   end
   -- Add/subtract days (86400 seconds per day)
   local newTimestamp = timestamp + (days * 86400)
   -- Convert back to date
   local newDate = os.date("*t", newTimestamp)
   -- Format as YYMMDD
   local newYY = string.format("%02d", newDate.year - 2000)
   local newMM = string.format("%02d", newDate.month)
   local newDD = string.format("%02d", newDate.day)
   return newYY .. newMM .. newDD

end

-- Function to get previous day function p.prev(frame)

   return p.addDays({args = {frame.args[1] or frame.args.date, -1}})

end

-- Function to get next day function p.next(frame)

   return p.addDays({args = {frame.args[1] or frame.args.date, 1}})

end

return p