Skip to content

fix(format): parse schema.org durations into real ISO 8601 - #741

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/705-schema-org-durations
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/705-schema-org-durations

Conversation

@drawmeanelephant

@drawmeanelephant drawmeanelephant commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #705

format_iso_duration concatenated every digit in the string and checked contains("hour") first, so 1 hour 30 minutes became PT130H, 1.5 hours became PT15H and 2 days fell through to PT2M.

The hand-rolled parser is now gone: add_time_fields reads recipe.metadata.time(converter) — the same parser the human output uses. That handles the prep_time alias, a time: 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 false PT0M; whole minutes are decomposed canonically (P2D, PT1H30M).

Test plan

  • minutes_decompose_into_iso_8601 covers PT1H30M, PT2H, P2D, PT0M
  • schema_times_come_from_the_shared_parser — prep time: 10 min + cook time: 30 (integer) give PT10M/PT30M/PT40M; time: 1h30m gives totalTime PT1H30M; about an hour emits nothing
  • cargo test -p cooklang-format green

@LeMyst LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@drawmeanelephant
drawmeanelephant force-pushed the fix/705-schema-org-durations branch from 78f4d57 to 3fe1aaf Compare October 13, 2026 20:52
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Reworked per review: the hand-rolled duration parser is gone and add_time_fields reads recipe.metadata.time(converter), so schema.org and human output now agree — including the prep_time alias, the time: key and YAML integers, which previously gave null. Unreadable durations are omitted instead of emitting PT0M, and minutes decompose canonically (P2D, PT1H30M). As you noted, sub-minute precision is dropped.

@LeMyst

LeMyst commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

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 1h30m → PT1H30M, 2 days → P2D, time: 45, cook time: 30 and time: soon → omitted. Two small things:

  1. A correction to my earlier note: cooklang's metadata.time doesn't support the prep_time alias. StdKey::from_str accepts it, but metadata.get(StdKey::PrepTime) only looks up prep time, so prep_time: 10 min still gives no time in either output. That's on cooklang's side, not this PR, but the comment at crates/format/src/schema.rs:145 should drop "the prep_time alias".
  2. Optional: prep + cook at schema.rs:163 overflows on prep time: 4294967295 + cook time: 1. It panics in debug and gives totalTime PT0M in release. prep.saturating_add(cook) avoids it. Human output has the same issue inside cooklang's RecipeTime::total(), so it's not a blocker.

🤖 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>
@drawmeanelephant
drawmeanelephant force-pushed the fix/705-schema-org-durations branch from 3fe1aaf to dd01fd9 Compare October 15, 2026 22:42
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Both on dd01fd9:

  1. Comment fixed — dropped the prep_time alias claim; the list now names only what metadata.get(StdKey::PrepTime) actually reads (prep time, plus time:/cook time/integers).
  2. prep.saturating_add(cook) for totalTime — prep time: 4294967295 + cook time: 1 now saturates to u32::MAX minutes instead of panicking (debug) or wrapping to PT0M (release).

@LeMyst
LeMyst merged commit 6262693 into cooklang:main Oct 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

schema.org format parses durations incorrectly

2 participants