From a9d95c6da4f845a2a553cba519c90d275b5fbaa0 Mon Sep 17 00:00:00 2001 From: SyntacticSalt Date: Tue, 25 Aug 2026 10:04:55 +0200 Subject: [PATCH 1/2] refactor: Align parsing of date string in DateRange --- lua/wikis/commons/Date/Ext.lua | 29 ++++++++++++++++++ lua/wikis/commons/Tournament.lua | 33 ++------------------- lua/wikis/commons/Widget/Misc/DateRange.lua | 4 +-- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lua/wikis/commons/Date/Ext.lua b/lua/wikis/commons/Date/Ext.lua index 9cbe6534758..f20d71ef25d 100644 --- a/lua/wikis/commons/Date/Ext.lua +++ b/lua/wikis/commons/Date/Ext.lua @@ -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 diff --git a/lua/wikis/commons/Tournament.lua b/lua/wikis/commons/Tournament.lua index 660db121983..0576a63f386 100644 --- a/lua/wikis/commons/Tournament.lua +++ b/lua/wikis/commons/Tournament.lua @@ -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 = { @@ -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 diff --git a/lua/wikis/commons/Widget/Misc/DateRange.lua b/lua/wikis/commons/Widget/Misc/DateRange.lua index b1f3577e386..5dbc9bfa9b2 100644 --- a/lua/wikis/commons/Widget/Misc/DateRange.lua +++ b/lua/wikis/commons/Widget/Misc/DateRange.lua @@ -94,10 +94,10 @@ end 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? From 59e5c1688f2c77f09dbf5555a0f50752872a2e0f Mon Sep 17 00:00:00 2001 From: SyntacticSalt Date: Tue, 25 Aug 2026 10:15:59 +0200 Subject: [PATCH 2/2] Update DateRange.lua --- lua/wikis/commons/Widget/Misc/DateRange.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/wikis/commons/Widget/Misc/DateRange.lua b/lua/wikis/commons/Widget/Misc/DateRange.lua index 5dbc9bfa9b2..2b600340a6b 100644 --- a/lua/wikis/commons/Widget/Misc/DateRange.lua +++ b/lua/wikis/commons/Widget/Misc/DateRange.lua @@ -89,7 +89,7 @@ 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