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..2b600340a6b 100644 --- a/lua/wikis/commons/Widget/Misc/DateRange.lua +++ b/lua/wikis/commons/Widget/Misc/DateRange.lua @@ -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?