Repository navigation
fix(format): parse schema.org durations into real ISO 8601 - #741
Conversation
LeMyst
left a comment
There was a problem hiding this comment.
Thanks. This fixes the three repros (PT1H30M, PT1.5H, P2D). Other inputs still give wrong or invalid durations:
| Input | Output | Problem |
|---|---|---|
1.333 hours + 45 seconds |
totalTime PT1H20M43.80000000000018S |
float noise: format_seconds_iso (schema.rs:279, :283) never rounds |
10-15 minutes |
PT25M |
the range is summed |
1 1/2 hours |
PT2H2M |
the fraction is misread |
1:30 |
PT31M |
misread |
2 months, 2 Stunden |
PT2M |
an unknown unit falls back to minutes (schema.rs:234) |
½ hour, about an hour |
PT0M |
a wrong claim; omitting the field would be better |
1 week 2 days |
P1W2D |
weeks can't be combined with other components in ISO 8601:2004 (schema.rs:252); fold them into days |
1.5 hours 30 minutes |
PT1.5H30M |
invalid: only the smallest component may be fractional |
Values like PT36H and PT90M are valid ISO 8601, so keeping them as written is fine.
Design question: human.rs:287 already uses recipe.metadata.time(converter), cooklang's own time parser, and print_schema already receives the converter. That parser handles the prep_time alias, the time: key and YAML integers. On both this branch and main, prep_time: 10 min and cook time: 30 (an integer) give null times here. It also rejects ranges and unknown units instead of guessing. Reusing it would make the schema and human output agree, at the cost of seconds precision. Would that work for you?
The tests are unit-level and cover only the happy paths. A few of the cases above would be worth adding.
🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.
78f4d57 to
3fe1aaf
Compare
|
Reworked per review: the hand-rolled duration parser is gone and |
|
Thanks, this works well. I reran the inputs from the earlier table: every one now gives a valid duration or is omitted, and schema and human output agree. That includes
🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
The schema formatter ran its own duration parser that summed ranges (10-15 min -> PT25M), misread "1:30" and "1 1/2 hours", treated unknown units as minutes and emitted PT0M for unreadable text. recipe.metadata .time(converter) — the same parser human output uses — already handles the prep_time alias, the time: total and YAML integers, and refuses what it cannot read, at the cost of sub-minute precision. Durations are now ISO 8601 decompositions of whole minutes, and unreadable fields are omitted instead of claiming PT0M. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
3fe1aaf to
dd01fd9
Compare
|
Both on dd01fd9:
|
Closes #705
format_iso_durationconcatenated every digit in the string and checkedcontains("hour")first, so1 hour 30 minutesbecamePT130H,1.5 hoursbecamePT15Hand2 daysfell through toPT2M.The hand-rolled parser is now gone:
add_time_fieldsreadsrecipe.metadata.time(converter)— the same parser the human output uses. That handles theprep_timealias, atime:total and YAML integers (which previously emitted nothing, since only string values were read), rejects ranges and unknown units instead of guessing, and drops sub-minute precision. Unreadable durations are omitted rather than emitted as a falsePT0M; whole minutes are decomposed canonically (P2D,PT1H30M).Test plan
minutes_decompose_into_iso_8601coversPT1H30M,PT2H,P2D,PT0Mschema_times_come_from_the_shared_parser—prep time: 10 min+cook time: 30(integer) givePT10M/PT30M/PT40M;time: 1h30mgivestotalTime PT1H30M;about an houremits nothingcargo test -p cooklang-formatgreen