I have date time data in the following format:
2434.5 14:20
Which is for: year 2024, 34th week of the year (August), 5th day of the week (Friday), 14:20 o’clock.
I am struggeling to parse the calendar week and day of week.
Looking at the documentation of Dates, it is possible to print the dayofweek from a given date but not the other way around.
So I would like to parse “2434.5 14:20” → “2024-08-23T14:20:00”
Does anyone has suggestions?
Worst case if that is not possible, I will have to parse it the other way around with a lot of manual steps for calculating a delta time etc. which I’d like not to do.
There are other issues too. For example, the number of weeks per year is not constant, so one should probably validate that, and weeks also overflow to following years, so one should validate the day in a week too.
Working with date and time is always tricky, especially when parsing a custom format. That’s why I tried to emphasize that the implementation needs to be tested carefully. Dates are complicated in standard notation and even more so when stored in a special format
There’s lots of funny edge cases here! Under the assumption that the year, week and both hour & minutes are always specified as two numbers, your code appears to be correct. The format unfortunately doesn’t allow us to distinguish between 1970 and 2070, and so "7001.4 00:00" is an example where the result is ambiguous:
julia> using Supposition
julia> function corporate_time(dt::DateTime)
y = lpad(year(dt) % 100, 2, '0')
w = lpad(week(dt), 2, '0')
dw = dayofweek(dt)
h = lpad(hour(dt), 2, '0')
m = lpad(minute(dt), 2, '0')
"$y$w.$dw $h:$m"
end
corporate_time (generic function with 5 methods)
# generate a random `DateTime` by simply offsetting from 1970-1-1
julia> datetime = map(Data.Integers(0, 100_000), Data.Integers(0, 23), Data.Integers(0,59)) do dayoffset, h, m
DateTime(1970,1,1) + Day(dayoffset) + Hour(h) + Minute(m)
end;
julia> example(datetime)
2171-08-31T12:26:00
julia> @check function weekdate_correct(dt=datetime)
ct = corporate_time(dt)
event!("Produced string", ct)
weekdate = weekdate_to_datetime(ct)
event!("Parsed corporate date", weekdate)
weekdate == dt
end
Found counterexample
Context: weekdate_correct
Arguments:
dt::DateTime = DateTime("1970-01-01T00:00:00")
Events:
Produced string
"7001.4 00:00"
Parsed corporate date
DateTime("2070-01-02T00:00:00")
I’d recommend moving away from this data format as soon as possible, to prevent weird problems in the future.
That being said, if we limit ourselves to dayofweek, your function is accurate
julia> @check function weekdate_correct(dt=datetime)
ct = corporate_time(dt)
event!("Produced string", ct)
weekdate = weekdate_to_datetime(ct)
event!("Parsed corporate date", weekdate)
dayofweek(weekdate) == dayofweek(dt)
end
Test passed
Context: weekdate_correct