Skip to content

Calendar TimeRange has an off-by-one error in nanosecond calculation #5646

@chipkent

Description

@chipkent

Looking at TimeRange::nanos:

    /**
     * Length of the range in nanoseconds.
     *
     * @return length of the range in nanoseconds
     */
    public long nanos() {
        return start.until(end, ChronoUnit.NANOS) - (inclusiveEnd ? 0 : 1);
    }

this seems incorrect.

For example, I would expect that

[ofEpochMilli(0), ofEpochMilli(1)) = 1,000,000 nanos
[ofEpochMilli(0), ofEpochMilli(1)] = 1,000,001 nanos

This makes me think a lot of the testing and calculations are off by 1.

but it seems like the code as written is providing

[ofEpochMilli(0), ofEpochMilli(1)) = 999,999 nanos
[ofEpochMilli(0), ofEpochMilli(1)] = 1,000,000 nanos

Originially identified in #5378 (comment).

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtriage

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions