#1509·CoreNLP

SUTime: Time.intersect gives different answers depending on operand order

Author: AngledLuffaCreated Sep 22, 2026Updated Sep 22, 2026

SUTime: Time.intersect gives different answers depending on operand order

Summary

a.intersect(b) and b.intersect(a) disagree for 28 of the 55 pairs drawn from a small set of ordinary SUTime constants. One direction usually returns the right answer and the other returns null; three pairs throw ClassCastException. A related symptom is that a month combined with a day of the week loses the month on the way out.

Found while auditing SUTime ahead of replacing joda-time. None of it is caused by joda, and all of it can be fixed independently of that work.

The asymmetry

Intersecting every pair from {JUNE, MONDAY, MORNING, SUMMER, WEEKEND, NOON, WINTER, FRIDAY, NIGHT, IsoDate(2017,6,15), IsoDate(2017,-1,-1)}, in both orders:

  JUNE    ^ MORNING  = XXXX-06TMO          reversed = null
  JUNE    ^ SUMMER   = XXXX-06              reversed = null
  MONDAY  ^ MORNING  = XXXX-WXX-1TMO       reversed = null
  MONDAY  ^ SUMMER   = XXXX-WXX-1-SU       reversed = null
  MORNING ^ NOON     = null                 reversed = T12:00
  MORNING ^ FRIDAY   = null                 reversed = XXXX-WXX-5TMO
  SUMMER  ^ FRIDAY   = null                 reversed = XXXX-WXX-5-WI
  WINTER  ^ YEAR2017 = null                 reversed = 2017-WI
  MORNING ^ WEEKEND  = null                 reversed = ClassCastException
  SUMMER  ^ WEEKEND  = null                 reversed = ClassCastException
  WEEKEND ^ WINTER   = ClassCastException   reversed = null

  asymmetric pairs: 28 of 55

SUMMER.intersect(MONDAY) returning null also means callers get a NullPointerException if they go on to resolve the result.

Cause

PartialTime.intersect (SUTime.java:3142) dispatches on the runtime class of its argument, and the classes do not line up with the concepts:

constant class what intersect does with it
MONDAY PartialTime merged by JodaTimeUtils.combine (line 3154)
WEEKEND TimeWithRange falls through to makeComposite
SUMMER, MORNING InexactTime falls through to makeComposite

PartialTime delegates to the other operand where it cannot handle a case itself (return t.intersect(this) at 3153, 3168, 3170, 3172, 3179), so a PartialTime on the left generally works. InexactTime and TimeWithRange have no matching delegation, so they return null or throw when they are on the left.

makeComposite (SUTime.java:1560) also switches on StandardTemporalType and handles DAYS_OF_WEEK but not DAY_OF_WEEK. Those are different constants: DAYS_OF_WEEK is WEEKDAY and WEEKEND, while DAY_OF_WEEK is MONDAY through SUNDAY. The singular case looks simply to have been missed, though it is not reachable today for two PartialTime operands, which take the combine branch first.

The month-plus-weekday symptom

JUNE.intersect(MONDAY) builds the right data and then cannot print it:

partial              = {monthOfYear=6, dayOfWeek=1}      both constraints present
isCompatible         = true
toISOString          = XXXX-WXX-1                        the month is gone

appendDateFormats (SUTime.java:2745) picks one calendar style:

java
if (monthOfYear && dayOfMonth)         appendMonthDay = true;
else if (weekOfWeekyear || dayOfWeek)  appendWeekDay  = true;   // <- taken
else if (monthOfYear || dayOfMonth)    appendMonthDay = true;   // unreachable for this shape

A partial with monthOfYear and dayOfWeek but no dayOfMonth takes the week branch, and the month is never appended. ISO 8601 genuinely cannot mix -06 with -W24-1 in one value, so the restriction is right; silently dropping one of the two constraints is not.

Resolution has a second gap. The day of week is only pinned to a day of month when the reference already falls inside the month:

ref 2017-06-15 -> {year=2017, monthOfYear=6, dayOfMonth=12, dayOfWeek=1}   2017-06-12
ref 2017-01-15 -> {year=2017, monthOfYear=6, dayOfWeek=1}                  2017-WXX-1
ref 2017-09-20 -> {year=2017, monthOfYear=6, dayOfWeek=1}                  2017-WXX-1

resolveDowToDay works from the reference's own week, and a January Monday conflicts with month 6, so the day of week stays unresolved. Leaving it unresolved is defensible. Printing the result as 2017-WXX-1, which claims "some Monday in 2017", is not.

Through the rules, phrases without an ordinal are affected:

"Monday in June"              -> 2009-WXX-1     month lost
"Mondays in June"             -> 2009-WXX-1     month lost
"Wednesday in November"       -> 2009-WXX-3     month lost
"a Monday in June 2011"       -> 2011-WXX-1     month lost, year kept
"June Mondays"                -> 2009-WXX-1     month lost

Ordinal-qualified phrases are fine, because they resolve to a single day:

"the first Monday in June"    -> 2009-06-01
"3rd wednesday in november"   -> 2009-11-18
"the second Tuesday of March" -> 2009-03-10
"the last Friday in November" -> alt_value = PREV_IMMEDIATE XXXX-WXX-5 INTERSECT XXXX-11

That last one is the existing idiom for a combination TIMEX cannot express in a value: a temporal function carrying the constraint in alt_value. It keeps November.

Suggested direction

Two changes, separable, neither small enough to make without a decision:

  1. Make intersect commutative. Give InexactTime and TimeWithRange the delegation that PartialTime already has. Needs care against mutual recursion, since both sides would then delegate to each other. Fixes the nulls and the ClassCastExceptions.
  2. Represent month-plus-weekday as a function rather than a lossy value, in the same shape as "the last Friday in November". This is a semantic decision about what "Monday in June" should emit, not a formatting change, and would alter output for the phrases listed above.

Adding the missing DAY_OF_WEEK case to makeComposite is a smaller change but has no effect on its own, since two PartialTime operands never reach it.

Regression cover already in place

SUTimeRuleBehaviorTest pins the correct behaviour for the ordinal forms and for the "last Friday in November" function form, so a fix for the above cannot quietly break them. The broken cases are deliberately not pinned, to avoid blessing them.