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

fix: prefer use ISO8601 then parse due to milliseconds digits #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

husainazkas
Copy link

DateTime.fromMillisecondsSinceEpoch constructor can cause not equal between two DateTime(s) since it will not fill microseconds value.

For example,

void main() {
  final now = DateTime.now();
  final foo = Foo(now);
  final newFoo = Foo.fromMap(foo.toMap());

  print(foo == newFoo); // false
  print(foo.createdAt == newFoo.createdAt); // false
  print(foo.createdAt == now); // true
  print(newFoo.createdAt == now); // false
  print(foo.createdAt); // yyyy-mm-dd HH:mm:ss.xxxxxx (contains microseconds)
  print(newFoo.createdAt); // yyyy-mm-dd HH:mm:ss.xxx (missing microseconds)
}

class Foo {
  final DateTime createdAt;

  Foo(this.createdAt);

  factory Foo.fromMap(Map<String, dynamic> map) {
    return Foo(DateTime.fromMillisecondsSinceEpoch(map['createdAt']));
  }

  Map<String, dynamic> toMap() {
    return {
      'createdAt': createdAt.millisecondsSinceEpoch,
    };
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is Foo && other.createdAt == createdAt;
  }

  @override
  int get hashCode {
    return createdAt.hashCode;
  }
}

DateTime.fromMillisecondsSinceEpoch constructor can cause not equal between two DateTime(s) since it will not fill microseconds value.
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

Successfully merging this pull request may close these issues.

1 participant