Skip to content

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

Open
husainazkas wants to merge 1 commit intoricardoemerson:mainfrom
husainazkas:main
Open

fix: prefer use ISO8601 then parse due to milliseconds digits#18
husainazkas wants to merge 1 commit intoricardoemerson:mainfrom
husainazkas:main

Conversation

@husainazkas
Copy link
Copy Markdown

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