Date/time gaps: month-end durations (Appendix H definition vs formulas), invalid Date components, Today() vs Now() in CalculateAge

Open
#200 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
xml
Domain
testing

Research direction

Start with the proposed XML tests in the issue and the cited Appendix H, Appendix B, and ELM rules. The issue identifies gaps in month-end duration calculations, invalid Date/DateTime components and overflow, and which current-time function CalculateAge uses; compare those rules with the existing tests, including DateTimeAddInvalidYears and DateTimeSubtractInvalidYears. Done means the specification questions are resolved and tests cover the agreed behavior.

Written by the indexing model from the issue text.

Description

While differential testing date operators across engines (the reference engine via @cqframework/cql 5.8.0, and cql-execution 3.3.2 and 4.0.0-beta.1), we found three places where the specification is inconsistent or silent, the engines disagree, and no test in the suite settles the behaviour. Proposed tests follow each point. The points about the specification text would also need an HL7 STU comment.

(The fourth one we found, DurationInDaysA/DurationInDaysAA across offsets, is already covered by #164.)

1. Month end in years between / months between (Appendix H)

Appendix H defines a month (and a year) as ending at the same time of day on the same calendar date, or "at the last calendar date of the ending calendar month, if the same calendar date of the ending month in the ending year does not exist", and notes that the time of day matters. The scenario formulas compare only the day of the month and ignore the time. The two disagree at month end:

Expression Definition Scenario formulas Reference engine 5.8.0 cql-execution
months between @2024-01-31 and @2024-02-29 1 0 (Scenario 2) 0 1
months between @2024-02-29 and @2024-01-31 -1 0 (Scenario 4) 0 -1
years between @2012-02-29 and @2014-02-28 (YearsBetweenLeapYearDatesEquals2) 2 1 (Scenario 3) 2 2
years between @2000-02-29T12:00:00.000Z and @2001-02-28T09:00:00.000Z 0 0 1 0

For years, Example 3.b in Appendix H follows the definition although it is listed under Scenario 3, and so does the existing test. Nothing covers months. The reference engine's behaviour is reported in cqframework/clinical_quality_language#1978.

Proposed tests (values from the definition; cql-execution passes all of them today):

<test name="MonthsBetweenMonthEnd" version="1.5">
	<capability code="duration-between"/>
	<expression>months between @2024-01-31 and @2024-02-29</expression>
	<output>1</output>
</test>
<test name="MonthsBetweenMonthEnd30" version="1.5">
	<capability code="duration-between"/>
	<expression>months between @2024-03-31 and @2024-04-30</expression>
	<output>1</output>
</test>
<test name="MonthsBetweenAddOneMonth" version="1.5">
	<capability code="duration-between"/>
	<expression>months between @2024-01-31 and (@2024-01-31 + 1 month)</expression>
	<output>1</output>
</test>
<test name="MonthsBetweenMonthEndNegative" version="1.5">
	<capability code="duration-between"/>
	<expression>months between @2024-02-29 and @2024-01-31</expression>
	<output>-1</output>
</test>
<test name="MonthsBetweenMonthEndDateTime" version="1.5">
	<capability code="duration-between"/>
	<expression>months between @2024-01-31T10:00:00.000Z and @2024-02-29T10:00:00.000Z</expression>
	<output>1</output>
</test>
<test name="MonthsBetweenMonthEndEarlierTime" version="1.5">
	<capability code="duration-between"/>
	<expression>months between @2024-01-31T10:00:00.000Z and @2024-02-29T09:00:00.000Z</expression>
	<output>0</output>
</test>
<test name="YearsBetweenLeapDayEarlierTime" version="1.5">
	<capability code="duration-between"/>
	<expression>years between @2000-02-29T12:00:00.000Z and @2001-02-28T09:00:00.000Z</expression>
	<output>0</output>
</test>
<test name="YearsBetweenLeapDayNextDayEarlierTime" version="1.5">
	<capability code="duration-between"/>
	<expression>years between @2000-02-29T12:00:00.000Z and @2001-03-01T09:00:00.000Z</expression>
	<output>1</output>
</test>

The same applies to age: CalculateAgeInMonthsAt(@2024-03-31, @2024-04-30) is 1 by the definition (0 in the reference engine). This could go into the clinical operator tests proposed in #157.

2. Invalid components in the Date and DateTime selectors

Appendix B (Date, DateTime) gives the rule for missing components only. It does not say what Date(2023, 2, 29), Date(2023, 13, 1) or Date(2023, 1, 32) yield. The reference engine throws; cql-execution returns a non-existent date that later turns into NaN (cqframework/cql-execution#414). The translator rejects the equivalent literal @2023-02-29 ("Invalid date-time input").

Proposed tests, following the reference engine and the treatment of literals (an error). If the work group prefers null, the same tests with <output>null</output>:

<test name="DateInvalidLeapDay" version="1.5">
	<expression invalid="true">Date(2023, 2, 29)</expression>
</test>
<test name="DateInvalidMonth" version="1.5">
	<expression invalid="true">Date(2023, 13, 1)</expression>
</test>
<test name="DateInvalidDay" version="1.5">
	<expression invalid="true">Date(2023, 1, 32)</expression>
</test>
<test name="DateTimeInvalidLeapDay" version="1.5">
	<expression invalid="true">DateTime(2023, 2, 29, 0, 0, 0, 0, 0)</expression>
</test>

Related gap: the Author's Guide (Date/Time arithmetic) states the out-of-range error only for years, which DateTimeAddInvalidYears and DateTimeSubtractInvalidYears test. Month and day overflow (@9999-12-15 + 1 month, @9999-12-31 + 1 day, @0001-01-01 - 1 day) are covered by neither text nor tests. The reference engine throws; cql-execution returns null, also for years (cqframework/cql-execution#413).

3. Today() or Now() in CalculateAge for a DateTime birth date

The specification gives three different rules:

  • Appendix B, Age: "for AgeInYears and AgeInMonths, the birthDate is specified as a Date and Today() is used to obtain the current date; whereas with the other precisions, birthDate is specified as a DateTime, and Now() is used". The rule depends on the precision.
  • Appendix B, CalculateAge: "as of the current date", shorthand for CalculateAgeInXXXAt "passing the current date for the asOf argument". Read literally, a "current date" gives no time of day for CalculateAgeInHours(DateTime).
  • ELM, CalculateAge: "For the Date overload, the calculation is performed using Today() … For the DateTime overload, the calculation is performed using Now()". The rule depends on the type.

The engines follow different rules. The reference engine follows the type; cql-execution follows the precision and applies the Age note to CalculateAge (for years and months it truncates a DateTime birth date to a Date). Evaluated at 2025-02-28T10:00:00.000Z, CalculateAgeInMonths(@2024-02-29T23:59:59.999Z) is 11 in the reference engine and 12 in cql-execution.

A fixed expected value is impossible without a fixed evaluation time, but a relational test works under the ELM rule:

<test name="CalculateAgeInMonthsDateTimeUsesNow" version="1.5">
	<expression>CalculateAgeInMonths(@2024-02-29T23:59:59.999Z) = CalculateAgeInMonthsAt(@2024-02-29T23:59:59.999Z, Now())</expression>
	<output>true</output>
</test>

Under the precision rule, this is false only on days when the age changes, so it is a weak check. The main point is to align the three texts.

Dominant language
No language data
Stars
8
Forks
9
Avg merge
4d 2h
Merged PRs (30d)
3

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cqframework/cql-tests

All issues in cqframework/cql-tests

Similar issues

More Testing & QA issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.