A fun side project I’ve been helping out on has me digging into quite a bit of Lua recently. It’s been been interesting to dig into, not withstanding the general headscratching (😠 arrays that start at 1…twitch). Since so many new games are developing their plugins and UI components in Lua, it’s a nice language to have in the toolkit.

A recent challenge revolved around checking if an the current moment fell within an “event” timer–a month, day, and duration range.

Caveats

Exact start date and duration are knowns and vital; exact end dates are irrelevant as the duration is all that matters.

No two events will overlap (hah, they say that now… I’ll read this post in a few months to remind myself of this pain).

This sanitized list of calendar events provides an example of the data–events have fixed start dates and durations, but the years are left off so that events can easily repeat year-over-year, matching the requirements.

ns.event.getCurrentEvent =function()
local month = os.date("%m")
local day = os.date("%d")
for _, evt in ipairs(events) do-- things should go hereendend

The first assumption would to just convert everything into timestamps and compare start <= now <= end; however, timestamps factor in the year, so cross-year events, such as the Winter Festival get screwed up.

Here’s the solution I came up with, focusing on building mini-calendars of the date ranges and comparing the current month and day to it. The year is included to create the os.time table; however, only the month and day are compared.

About

I focus on the people and products involved with technology and technology's role within the enterprise; not as a replacement for individuals, but as a tool to empower users, build relationships, and drive business strategy.