Clinical Quality Language Release 1 STU 3 (1.3)

This is Clinical Quality Language STU3 (1.3) in it's permanent home (it will always be available at this URL). It has been superseded by 1.4.
For a full list of available versions, see the Directory of published versions .

Clinical Decision Support Work GroupMaturity Level: 4Ballot Status: STU 3

Appendix H – Time Interval Calculation Examples

To determine the length of time between two dates, CQL provides two different approaches, duration, the number of whole periods between two dates, and difference, the number of period boundaries crossed between two dates.

The first approach, calculating the duration, determines the number of whole periods that occur between the two dates. Conceptually, the calculation is performed by considering the two dates on a timeline, and counting the number of whole periods that fit on that timeline between the two dates. For example:

Date 1: 2012-03-10
Date 2: 2013-03-10
Duration In Years: years between Date1 and Date2

The Duration In Years expression gives one year, because an entire year has passed between the two dates. Note that time is considered for the purposes of calculating the number of years:

DateTime 1: 2012-03-10 10:20:00
DateTime 2: 2013-03-10 09:20:00
Duration in Years: years between DateTime1 and DateTime2

This expression gives zero years, because the year has not passed until 10:20:00 on the day in the following year. To calculate the number of years, ignoring the time, extract the date from the date/time value:

DateTime 1: 2012-03-10 10:20:00
DateTime 2: 2013-03-10 09:20:00
Duration In Years: years between (date from DateTime1) and (date from DateTime2)

The second approach, calculating the difference, determines the number of boundaries crossed between two dates. To illustrate the difference, consider the following example:

Date 1: 2012-12-31
Date 2: 2013-01-01
Duration In Years: years between Date1 and Date2
Difference In Years: difference in years between Date1 and Date2

The Duration In Years expression returns zero because a full year has not passed between the two dates. However, the Difference In Years expression returns 1 because one year boundary was crossed between the two dates.

1. Calculating Duration in Years

1.1. Definition

In CQL, a year is defined as the duration of any time interval which starts at a certain time of day at a certain calendar date of the calendar year and ends at:

  • The same time of day on the same calendar date of the next calendar year, if it exists

  • The same time of day on the immediately following calendar date of the next calendar year, if the same calendar date of the next calendar year does not exist.

Note: When in the next calendar year the same calendar date does not exist, the ISO states that the ending calendar day has to be agreed upon. The above convention is used in CQL as a resolution to this issue.

1.2. Examples

  1. Month (date 2) < month (date 1): Duration (years) = year (date 2) - year (date 1) - 1

Example 1:
Date 1: 2012-03-10 22:05:09
Date 2: 2013-02-18 19:10:03
Duration = year (date 2) - year (date 1) - 1 = 2013 - 2012 - 1 = 0 years

  1. Month (date 2) = month (date 1) and day (date 2) >= day (date 1)
    Duration (years) = year (date 2) - year (date 1)

Example 2.a: day (date 1) = day (date 2)
Date 1: 2012-03-10 22:05:09
Date 2: 2013-03-10 22:05:09
Duration = year (date 2) - year (date 1) = 2013 - 2012 = 1 year

Note: Time of day is important in this calculation. If the time of day of Date 2 were less than the time of day for Date 1, the duration of the time interval would be 0 years according to the definition.

Example 2.b: day (date 2) > day (date 1)
Date 1: 2012-03-10 22:05:09
Date 2: 2013-03-20 04:01:30
Duration = year (date 2) - year (date 1) = 2013 - 2012 = 1 year

  1. Month (date 2) = month (date 1) and day (date 2) < day (date 1)
    Duration (years) = year (date 2) - year (date 1) - 1

Example 3.a:
Date 1: 2012-02-29
Date 2: 2014-02-28
Duration = year (date 2) - year (date 1) - 1 = 2014 - 2012 - 1 = 1 year

  1. Month (date 2) > month (date 1)
    Duration (years) = year (date 2) - year (date 1)

Example 4.a:
Date 1: 2012-03-10 11:16:02
Date 2: 2013-08-15 21:34:16
Duration = year (date 2) - year (date 1) = 2013 - 2012 - 1 year

Example 4.b:
Date 1: 2012-02-29 10:18:56
Date 2: 2014-03-01 19:02:34
Duration = year (date 2) - year (date 1) = 2014 - 2012 = 2 years

Note: Because there is no February 29 in 2014, the number of years can only change when the date reaches March 1, the first date in 2014 that surpasses the month and day of date 1 (Feburary 29).

2. Calculating Duration in Months

2.1. Definition

A month in CQL is defined as the duration of any time interval which starts at a certain time of day at a certain calendar day of the calendar month and ends at:

  • The same time of day at the same calendar day of the ending calendar month, if it exists

  • The same time of day at the immediately following calendar date of the ending calendar month, if the same calendar date of the ending month in the ending year does not exist.

Notes: When in the next calendar year the same calendar date does not exist, the ISO states that the ending calendar day has to be agreed upon. The above convention is used in CQL as a resolution to this issue.

2.2. Examples

  1. Day (date 2) >= day (date 1)
    Duration (months) = (year (date 2) - year (date 1)) * 12 + (month (date 2) - month (date 1))

Example 1.a:
Date 1: 2012-03-01 14:05:45
Date 2: 2012-03-31 23:01:49
Duration = (year (date 2) - year (date 1)) 12 + (month (date 2) - (month (date 1))
= (2012 - 2012) * 12 + (3 - 3) = *0 months

Example 1.b:
Date 1: 2012-03-10 22:05:09
Date 2: 2013-06-30 13:00:23
Duration = (year (date 2) - year (date 1)) 12 + (month (date 2) - (month date 1))
= (2013 - 2012) * 12 + (6 - 3) = 12 + 3 = *15 months

  1. Day (day 2) < day (date 1)
    Duration (months) = (year (date 2) - year (date 1)) * 12 + (month (date 2) - month (date 1)) - 1

Example 2:
Date 1: 2012-03-10 22:05:09
Date 2: 2013-01-09 07:19:33
Duration = (year (date 2) - year (date 1)) 12 + (month (date 2) - month (date 1)) - 1
= (2013 - 2012) * 12 + (1 - 3) - 1 = 12 - 2 - 1 = *9 months

3. Calculating Duration in Weeks

3.1. Definition

In CQL, a week is defined as a duration of any time interval which starts at a certain time of day at a certain calendar day at a certain calendar week and ends at the same time of day at the same calendar day of the ending calendar week. In other words, a complete week is always seven days long.

3.2. Examples

  1. Duration = - date 1 (days)] / 7

Example 1:
Date 1: 2012-03-10 22:05:09
Date 2: 2012-03-20 07:19:33
Duration = # days (month (date 1)) - day (date 1) + # days (month (date 1) + 1) + # days (month (date 1) + 2) + …​ + # days (month (date 2) - 1) + day (date 2)] / 7
= (20 - 10) / 7 = 10 / 7 = 1 week

4. Calculating Duration in Days

4.1. Definition

In CQL, a day is defined as a duration of any time interval which starts at a certain calendar day and ends at the next calendar day (1 second to 23 hours, 59 minutes, and 59 seconds).

The duration in days between two dates will generally be given by subtracting the start calendar date from the end calendar date, respecting the time of day between the two dates.

4.2. Examples

  1. Time (date 2) < time (date 1)
    Duration = - date 1 (days)] - 1

Example 1:
Date 1: 2012-01-31 12:30:00
Date 2: 2012-02-01 09:00:00
Duration = 02-01 - 01-31 - 1 = 0 days

  1. Time (date 2) >= time (date 1)
    Duration = date 2 - date 1 (days)

Example 2:
Date 1: 2012-01-31 12:30:00
Date 2: 2012-02-01 14:00:00
Duration = 02-01 - 01-31 = 1 day

5. Calculating Duration in Hours

5.1. Definition

In CQL, an hour is defined as 60 minutes. The duration in hours between two dates is the number of minutes between the two dates, divided by 60. The result is truncated to the unit.

5.2. Examples

  1. Example 1:
    Date 1: 2012-03-01 03:10:00
    Date 2: 2012-03-01 05:09:00
    Duration = 1 hour

  2. Example 2:
    Date 1: 2012-02-29 23:10:00
    Date 2: 2012-03-01 00:10:00
    Duration = 1 hour

  3. Example 3:
    Date 1: 2012-03-01 03:10
    Date 2: 2012-03-01 04:00
    Duration = 0 hours

6. Calculating Duration in Minutes

6.1. Definition

In CQL, a minute is defined as 60 seconds. The duration in minutes between two dates is the number of seconds between the two dates, divided by 60. The result is truncated to the unit.

6.2. Examples

  1. Example 1:
    Date 1: 2012-03-01 03:10:00
    Date 2: 2012-03-01 05:20:00
    Duration = 130 minutes

  2. Example 2:
    Date 1: 2012-02-29 23:10:00
    Date 2: 2012-03-01 00:20:00
    Duration = 70 minutes

7. Difference Calculations

Difference calculations are performed by truncating the date/time values at the next precision, and then performing the corresponding duration calculation on the truncated values.

Implementations need to consider the calculation of the "difference between" two date/times with different timezone offsets. There are multiple use cases when this might occur, but the most prevalent is likely due to Daylight Saving Time (DST).

To support the expected results in all cases, normalization of date/time values may be needed, depending on the precision used in the difference calculation. Normalization refers to adjustment of one or both of the two date/time values used in the difference calculation, if the timezone offsets are not the same, to a common timezone offset (namely, the timezone offset of the evaluation request timestamp).

When difference is calculated for hours or finer units, timezone offsets should be normalized prior to truncation to correctly consider real (actual elapsed) time. When difference is calculated for days or coarser units, however, the time components (including timezone offset) should be truncated without normalization to correctly reflect the difference in calendar days, months, and years.

7.1. Examples

  1. Example 1:
    Date 1: 2012-03-01 03:10:00
    Date 2: 2012-12-31 10:10:00
    Difference (years) = Duration (years) between 2012-01-01 00:00:00 and 2012-01-01 00:00:00
    Difference (years) = 0

  2. Example 2:
    Date 1: 2012-12-31 03:10:00
    Date 2: 2013-01-01 10:10:00
    Difference (years) = Duration (years) between 2012-01-01 00:00:00 and 2013-01-01 00:00:00
    Difference (years) = 1

  3. Example 3:
    Date 1: 2016-10-10 09:00:00
    Date 2: 2016-10-11 11:59:00
    Difference (days) = Duration (days) between 2016-10-10 00:00:00 and 2016-10-11 00:00:00
    Difference (days) = 1

  4. Example 4:
    Date 1: 2016-10-10 09:00:00
    Date 2: 2016-10-12 00:00:00
    Difference (days) = Duration (days) between 2016-10-10 00:00:00 and 2016-10-12 00:00:00
    Difference (days) = 2

  5. Example 5:
    Date 1: 2017-03-12 01:12:05.1 (-05:00)
    Date 2: 2017-03-12 03:22:27.6 (-04:00)
    Date 2 (normalized): 2017-03-12 02:22:27.6 (-05:00)
    Difference (hours) = Duration (hours) between 2017-03-12 01:00:00.0 (-05:00) and 2017-03-12 02:00:00.0 (-05:00)
    On the day that DST goes into effect, one might compare
    1:00am Eastern Standard Time (EST) 2017-03-12 01:00:00.0 (-05:00) to
    3:00am Eastern Daylight Time (EDT) 2017-03-12 03:00:00.0 (-04:00)
    Since time "springs forward" at 2:00am, only one hour of real time has elapsed.
    To calculate the "difference in hours" as 1, the second time (3:00am EDT) is normalized to the first time’s offset (making it 2:00am EST).
    The difference between 1:00am EST and 2:00am EST is clearly one hour.
    In a clinical setting, recognizing this as only one hour (and not two, as the clock might suggest) is important.

  6. Example 6:
    Date 1: 2017-11-05 01:30:00.0 (-04:00)
    Date 2: 2017-11-05 01:15:00.0 (-05:00)
    Date 2 (normalized): 2017-11-05 02:15:00.0 (-04:00)
    Difference (minutes) = Duration (minutes) between 2017-11-05 01:30:00.0 (-04:00) and 2017-11-05 02:15:00.0 (-04:00)
    Similarly, when DST ends, one might compare 1:30am EDT to 1:15am EST.
    If we did not respect timezone offsets, the difference would appear to be -15 minutes.
    Again, to calculate the "difference in minutes" as 45, the second time (1:15am EST) is normalized to the first time’s offset (making it 2:15am EDT).
    The difference between 1:30am EDT and 2:15am EDT is clearly 45 minutes.

  7. Example 7:
    Date 1: 2017-03-12 00:00:00.0 (-05:00)
    Date 2: 2017-03-13 00:00:00.0 (-04:00)
    Without normalization of the dates:
    Difference (days) = Duration (days) between 2017-03-12 00:00:00.0 and 2017-03-13 00:00:00.0
    In some cases, normalization prior to truncation may give unexpected results. Consider the "difference in days" between midnight (EST) on the day that DST goes into effect and midnight (EDT) the next day. One would expect that since the second date is the "next day" on the calendar, difference in days should be 1.
    With normalization prior to truncation, the second date would become 11:00pm EST on the same day as the first date.
    The difference in days would be 0, which is not the expected result.