Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lua/wikis/commons/Date/Ext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,33 @@ function DateExt.daysToSeconds(days)
return days * SECONDS_PER_DAY
end

---@class DateRecord
---@field year integer
---@field month integer?
---@field day integer?
---@field timestamp integer?

--- This function parses fuzzy dates into a structured format.
---@param dateRecord string? # date in the format of `YYYY-MM-DD`, with `-MM-DD` optional.
---@return DateRecord?
function DateExt.parseDateRecord(dateRecord)
if not dateRecord then
return nil
end
if dateRecord == DateExt.defaultDate then
return nil
end
local year, month, day = dateRecord:match('^(%d%d%d%d)%-?(%d?%d?)%-?(%d?%d?)')
year, month, day = tonumber(year), tonumber(month), tonumber(day)

if not year then
return
end

local dt = {year = year, month = month or 12, day = day or 31, hour = 0}
local timestamp = os.time(dt)

return {year = year, month = month, day = day, timestamp = timestamp}
end

return DateExt
33 changes: 2 additions & 31 deletions lua/wikis/commons/Tournament.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ end
---@return StandardTournament
function Tournament.tournamentFromRecord(record)
local extradata = record.extradata or {}
local startDate = Tournament.parseDateRecord(Logic.nilOr(extradata.startdatetext, record.startdate))
local endDate = Tournament.parseDateRecord(Logic.nilOr(extradata.enddatetext, record.sortdate, record.enddate))
local startDate = DateExt.parseDateRecord(Logic.nilOr(extradata.startdatetext, record.startdate))
local endDate = DateExt.parseDateRecord(Logic.nilOr(extradata.enddatetext, record.sortdate, record.enddate))
local tier, tierType, tierOptions = Tier.parseFromQueryData(record)

local tournament = {
Expand Down Expand Up @@ -218,35 +218,6 @@ function Tournament.calculatePhase(tournament)
return TOURNAMENT_PHASE.FINISHED
end

---@class DateRecord
---@field year integer
---@field month integer?
---@field day integer?
---@field timestamp integer?

--- This function parses fuzzy dates into a structured format.
---@param dateRecord string? # date in the format of `YYYY-MM-DD`, with `-MM-DD` optional.
---@return DateRecord?
function Tournament.parseDateRecord(dateRecord)
if not dateRecord then
return nil
end
if dateRecord == DateExt.defaultDate then
return nil
end
local year, month, day = dateRecord:match('^(%d%d%d%d)%-?(%d?%d?)%-?(%d?%d?)')
year, month, day = tonumber(year), tonumber(month), tonumber(day)

if not year then
return
end

local dt = {year = year, month = month or 12, day = day or 31, hour = 0}
local timestamp = os.time(dt)

return {year = year, month = month, day = day, timestamp = timestamp}
end

--- Determines if a tournament is featured.
---@param record StandardTournament
---@return boolean
Expand Down
6 changes: 3 additions & 3 deletions lua/wikis/commons/Widget/Misc/DateRange.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ local function determineTranslateString(startDate, endDate, showYear)
end
end

---@param props {startDate: string|osdateparam?, endDate: string|osdateparam?, showYear: boolean?}
---@param props {startDate: string|DateRecord|osdateparam?, endDate: string|DateRecord|osdateparam?, showYear: boolean?}
---@return string
local function DateRange(props)
local startDate, endDate = props.startDate, props.endDate
if type(startDate) ~= 'table' then
startDate = DateExt.parseIsoDate(startDate --[[ @as string? ]])
startDate = DateExt.parseDateRecord(startDate --[[ @as string? ]])
end
if type(endDate) ~= 'table' then
endDate = DateExt.parseIsoDate(endDate --[[ @as string? ]])
endDate = DateExt.parseDateRecord(endDate --[[ @as string? ]])
end

---@type osdateparam?
Expand Down
Loading