Parsing corporate DateTime format with calendar week

I’m worried the solution is wrong. Let me explain.

2025-01-01 started on a Wednesday, so it should be called 2501.3 right? That means we need to offset the final date with 3 days, not 1.

We can get this value with dayofweek(Date(Year(2025)), which returns 3.

(You can check your implementation is false, dayofweek(myparse("2505.1 13:30")) returns 3 instead of 1)

So I think the solution is:

using Dates
function weekdate_to_datetime(str::AbstractString)
    m = match(r"(?<year>\d\d)(?<week>\d\d)\.(?<day>\d)\s(?<hour>\d\d):(?<minute>\d\d)", str)
    if isnothing(m) || length(m.captures) != 5
        error("incorrect weekday format, expected \"yymm.dd hh:mm\"")
    end
    year = 2000 + parse(Int, m[:year])
    week = parse(Int, m[:week])
    day = parse(Int, m[:day])
    hour = parse(Int, m[:hour])
    minute = parse(Int, m[:minute])

    day_offset = dayofweek(DateTime(year))
    return (DateTime(year) + Week(week - 1) + Day(day) - Day(day_offset) +
            Hour(hour) + Minute(minute))
end

Note the following funny result due to the week date definition we use here:

julia> weekdate_to_datetime("2453.1 13:30")
2024-12-30T13:30:00

julia> weekdate_to_datetime("2501.1 13:30")
2024-12-30T13:30:00

Maybe you want to throw an error if someone inputs a week date of a year that starts in the previous year like "2501.1" ?

Also note that there may be funny ISO definitions as hinted at in this discussion: Determine date range given year and week number - General Usage - Julia Programming Language

3 Likes