# Cooper: DateTime.CompareTo returns non-zero for equal values when the calendar time zone is not UTC

**URL:** https://talk.remobjects.com/t/cooper-datetime-compareto-returns-non-zero-for-equal-values-when-the-calendar-time-zone-is-not-utc/34251
**Category:** Elements Bugs
**Tags:** java, bug, oxygene
**Created:** [October 30, 2026, 1:17pm UTC](https://talk.remobjects.com/t/cooper-datetime-compareto-returns-non-zero-for-equal-values-when-the-calendar-time-zone-is-not-utc/34251 "2026-10-30T13:17:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![michal.kowalczuk](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@michal.kowalczuk](https://talk.remobjects.com/u/michal.kowalczuk)
#### Post date: [October 30, 2026, 1:17pm UTC](https://talk.remobjects.com/t/cooper-datetime-compareto-returns-non-zero-for-equal-values-when-the-calendar-time-zone-is-not-utc/34251/1 "2026-10-30T13:17:35Z")

</div>

**IDE:** API  
**Version:** newest  
**Target** (If relevant): Java (Cooper), JDK 1.8, Elements RTL (`com.remobjects.elements.rtl.jar`)  
**Description** :

On Cooper, `DateTime.CompareTo` returns a non-zero value when a `DateTime` is compared with itself, if the underlying `java.util.Calendar` is in any time zone other than UTC. It also disagrees with the `DateTime` operators: `d = d` is `True` while `d.CompareTo(d)` is `-1`. Values that differ only in milliseconds compare as equal.

Cause, in `Source/RemObjects.Elements.RTL/DateTime.pas` (Cooper branch of `CompareTo`):

```pascal
result := mapped.compareTo(new DateTime(aValue.Year, aValue.Month, aValue.Day,
                                        aValue.Hour, aValue.Minute, aValue.Second));

```

- `aValue.Year … aValue.Second` read wall-clock fields in the time zone of `aValue`’s calendar (`mapped.get(Calendar.XXX)`).
- `new DateTime(y, m, d, h, n, s)` builds a calendar in `TimeZone.Utc` from them, so the argument is shifted by the zone offset of `aValue`.
- Milliseconds are dropped.
- The operators `=`, `<`, `>`, `<=`, `>=` compare `Ticks` (`TimeInMillis + TimeZone.getOffset(TimeInMillis)`) and are correct.

Impact:

- `CompareTo` and `DateTime.Compare` give different results on Java than on .NET and Island.
- A sort comparator based on `CompareTo` reports `x < x`. Java’s TimSort may then throw `IllegalArgumentException: Comparison method violates its general contract!`
- The result depends on the machine’s time zone, so the bug does not show on machines that run in UTC.

Suggested fix: compare `Ticks`, so that `CompareTo` and the operators always agree:

```pascal
{$IF COOPER}
var t1 := Ticks;
var t2 := aValue.Ticks;
result := if t1 < t2 then -1 else if t1 > t2 then 1 else 0;
{$ENDIF}

```

**Expected Behavior** :

`d.CompareTo(d)` returns `0`. In general, `CompareTo` agrees with the `=` / `<` / `>` operators, including milliseconds, regardless of the calendar’s time zone:

```auto
d.CompareTo(d) = 0
d = d = True
a.CompareTo(b) = -1
a < b = True

```

**Actual Behavior** :

```auto
d.CompareTo(d) = -1
d = d = True
a.CompareTo(b) = 0
a < b = True

```

**Steps** :

1. Use a machine (or JVM default time zone) other than UTC; we use `Europe/Belgrade`.
2. Build and run this Cooper console application:

```pascal
namespace DateTimeCompareTest;

uses
  RemObjects.Elements.RTL;

type
  Program = class
  public
    class method Main(args: array of String): Int32;
    begin
      // any default zone other than UTC
      java.util.TimeZone.Default := java.util.TimeZone.getTimeZone('Europe/Belgrade');

      var d := new DateTime(2020, 1, 15);
      java.util.Calendar(d).setTimeZone(java.util.TimeZone.Default);

      writeLn('d.CompareTo(d) = ' + d.CompareTo(d));
      writeLn('d = d = ' + (d = d));

      var a := new DateTime(2020, 1, 15, 10, 0, 0, 0);
      var b := new DateTime(2020, 1, 15, 10, 0, 0, 500);
      writeLn('a.CompareTo(b) = ' + a.CompareTo(b));
      writeLn('a < b = ' + (a < b));
    end;
  end;

end.

```

1. Compare the output with the expected values above.

We are adding a workaround on our side that compares `Ticks` instead of calling `CompareTo` on Cooper. We would prefer to remove the workaround once the RTL is fixed.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 30, 2026, 4:32pm UTC](https://talk.remobjects.com/t/cooper-datetime-compareto-returns-non-zero-for-equal-values-when-the-calendar-time-zone-is-not-utc/34251/2 "2026-10-30T16:32:23Z")

</div>

Thanx, fixed (unfortunatelty _not_ in time for today’s build .3149).
