Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime equality fails #29

Open
JCKodel opened this issue Jun 15, 2024 · 0 comments
Open

DateTime equality fails #29

JCKodel opened this issue Jun 15, 2024 · 0 comments

Comments

@JCKodel
Copy link

JCKodel commented Jun 15, 2024

Equal DateTimes often returns false because Dart implementation of DateTime is a huge mess (two equal dates are considered different when one is local and the other is UTC, even if they are exactly the same date and time.

DateTimes should be compared exclusively through the millisecondsSinceEpoch, since that is always in UTC, and it doesn't care about precision (most of the time, microseconds are not even considered nor supported).

So, I think the default DateTime equality comparer should be written as this:

import 'package:collection/collection.dart';

final class DateTimeEquality implements Equality<DateTime> {
  const DateTimeEquality();

  @override
  bool equals(DateTime e1, DateTime e2) {
    return e1.millisecondsSinceEpoch == e2.millisecondsSinceEpoch;
  }

  @override
  int hash(DateTime e) {
    return e.millisecondsSinceEpoch;
  }

  @override
  bool isValidKey(Object? o) {
    throw UnimplementedError();
  }
}

And that should be the default (so we don't need to use

@DataField(equality: DateTimeEquality())
final DateTime created;

)

(maybe a build option to not break anything)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant