Skip to content

duration.between() borrows a hard-coded 29 days when it has to take a day out of the month component, so the returned duration added back to the start does not land on the end #8386

Closed

Description

@ruispereira

Summary

TemporalUtil.durationBetween splits the interval into a calendar part (Period between the two
dates) and a clock part (Duration between the calendar-shifted start and the end). When the clock
part comes out negative while the calendar part is positive, it has to borrow. Borrowing from days
is exact - a LocalDateTime day is always 86400 seconds - and that branch is right. Borrowing from
months is not: a month is 28, 29, 30 or 31 days depending on which month is being borrowed, and the
code hard-codes 29.

if ((seconds < 0 || (seconds == 0 && nanos < 0)) && (days > 0 || months > 0)) {
  if (days > 0) {
    days--;
    seconds += 86400;
  } else {
    months--;
    days += 29;      // <- a month is not always 30 days (29 + the one day converted to seconds)
    seconds += 86400;
  }
}

days += 29 together with seconds += 86400 gives back 30 days for the month that was just removed.
The month-borrow branch is reached whenever the two datetimes land on the same day-of-month (so
Period.getDays() == 0) and the end time-of-day is earlier than the start time-of-day - an entirely
ordinary pair of values, e.g. 2024-03-15T10:00 to 2024-04-15T08:00.

The contract this breaks is the one the rest of the engine relies on: ArithmeticExpression adds a
duration to a datetime as plusMonths(months).plusDays(days).plusSeconds(seconds), so
from + duration.between(from, to) has to be to. For any borrowed month that is not 30 days long it
is not: it is one day short for a 31-day month, one day long for a 29-day February and two days long
for a 28-day February.

Where (HEAD 8b84c3d)

engine/src/main/java/com/arcadedb/query/opencypher/temporal/TemporalUtil.java:322

    // Normalize: ensure days and seconds have the same sign (or one is zero).
    // If clock (seconds) is negative but calendar (days) is positive, borrow from days.
    if ((seconds < 0 || (seconds == 0 && nanos < 0)) && (days > 0 || months > 0)) {
      if (days > 0) {
        days--;
        seconds += 86400;
      } else {
        months--;
        days += 29;
        seconds += 86400;
      }
    }

The consumer of the result, engine/src/main/java/com/arcadedb/query/opencypher/ast/ArithmeticExpression.java:327:

    if (leftValue instanceof CypherLocalDateTime cldt && rightValue instanceof CypherDuration dur) {
      LocalDateTime dt = cldt.getValue()
          .plusMonths(op == Operator.ADD ? dur.getMonths() : -dur.getMonths())
          .plusDays(op == Operator.ADD ? dur.getDays() : -dur.getDays())
          .plusSeconds(op == Operator.ADD ? dur.getSeconds() : -dur.getSeconds())
          .plusNanos(op == Operator.ADD ? dur.getNanosAdjustment() : -dur.getNanosAdjustment());

Repro

Against the compiled engine classes at HEAD, durationBetween on two CypherLocalDateTime values,
then the same add ArithmeticExpression performs:

from=2024-03-15T10:00 to=2024-04-15T08:00  between=P29DT22H (M=0 D=29 S=79200)  from+between=2024-04-14T08:00  *** MISMATCH ***
from=2024-01-31T12:00 to=2024-02-29T06:00  between=P28DT18H (M=0 D=28 S=64800)  from+between=2024-02-29T06:00  OK
from=2023-01-15T12:00 to=2023-02-15T06:00  between=P29DT18H (M=0 D=29 S=64800)  from+between=2023-02-14T06:00  *** MISMATCH ***
from=2024-01-15T12:00 to=2024-02-15T06:00  between=P29DT18H (M=0 D=29 S=64800)  from+between=2024-02-14T06:00  *** MISMATCH ***
from=2024-04-15T10:00 to=2024-05-15T08:00  between=P29DT22H (M=0 D=29 S=79200)  from+between=2024-05-15T08:00  OK
from=2024-01-10T10:00 to=2024-03-10T08:00  between=P1M29DT22H (M=1 D=29 S=79200)  from+between=2024-03-11T08:00  *** MISMATCH ***
from=2024-03-15T10:00 to=2024-04-20T08:00  between=P1M4DT22H (M=1 D=4 S=79200)  from+between=2024-04-20T08:00  OK
from=2024-03-15T10:00 to=2024-03-20T08:00  between=P4DT22H (M=0 D=4 S=79200)  from+between=2024-03-20T08:00  OK

The two OK lines that go through the month-borrow branch are exactly the ones where the borrowed
month has 30 days (April 2024, and the Jan 31 case which borrows from days instead). Every other
line that reaches the branch is off by a day or two. 2023-01-15 -> 2023-02-15 (a 31-day January)
and 2024-01-15 -> 2024-02-15 (also 31 days) both come back a day short; 2024-01-10 -> 2024-03-10
borrows a 29-day February and comes back a day long.

Suggested fix

Do not fabricate a day count. Once months has been decremented, recompute the remainder from the
reduced calendar shift instead of patching days with a constant:

      } else {
        months--;
        final LocalDateTime afterMonths = fromDT.plusMonths(months);
        days = ChronoUnit.DAYS.between(afterMonths.toLocalDate(), toDT.toLocalDate());
        final Duration rest = Duration.between(afterMonths.plusDays(days), toDT);
        if (rest.isNegative()) {
          days--;
          seconds = rest.getSeconds() + 86400;
        } else {
          seconds = rest.getSeconds();
        }
        nanos = ...; // from the same Duration
      }

i.e. the same two-step split the method already does at the top, re-run with one month fewer. A
regression test should pin from + duration.between(from, to) == to over a sweep of month lengths
(28/29/30/31) rather than a single hand-picked pair, since a single pair in April or a 30-day month
passes today.

Scope / confidence

Reproduced against the compiled engine classes at HEAD, by calling the public
TemporalUtil.durationBetween and reapplying the duration exactly as ArithmeticExpression does.

What I tried to refute it with: (a) I checked that durationInDays and durationInMonths, which have
their own sign-adjustment loops, are correct - they adjust the count by one and never fabricate a
length, so they are not affected; (b) I checked the negative-direction analogue (months < 0,
days == 0, positive clock) and it round-trips correctly, because that case is deliberately left
un-normalized rather than borrowed; (c) I checked the days > 0 borrow branch and the
seconds >= 86400 carry branch and both are exact, since LocalDateTime.plusDays is exactly 86400
seconds. Only the month borrow is wrong. The zoned (CypherDateTime) path reaches the same code via
resolveDateTime, so the defect is not specific to local datetimes.

Activity

  1. added
    severity:major
    on Sep 25, 2026
  2. added
    in progress
    on Sep 25, 2026
  3. self-assigned this
    on Sep 25, 2026
  4. added this to the 26.10.1 milestone on Sep 25, 2026
  5. added a commit that references this issue on Sep 25, 2026
  6. lvca commented on Sep 25, 2026

    @lvca
    Member

    Fixed in #8396 (26.10.1). duration.between() now counts months, then days, then time, the same way Neo4j does, so start + duration.between(start, end) always equals end, whatever the month length. For example, 2024-03-15T10:00 to 2024-04-15T08:00 is now P30DT22H.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. removed
    in progress
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugopencypherseverity:major

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions