Skip to content

Timestamp.fromMillisecondsSinceEpoch() throws when calculating negative milliseconds. #3545

@marcglasberg

Description

@marcglasberg

Currently, this will throw an error:

Timestamp.fromMillisecondsSinceEpoch(-1);

I suggest the above code should be add as a test to the package.

It's a simple fix. The seconds/nanoseconds calculation is wrong:

    int milliseconds = -1;
    var _kThousand = 1000;
    var _kMillion = 1000000;

    // What is should be:
    int seconds = (milliseconds / _kThousand).floor();
    int nanoseconds = (milliseconds - seconds * _kThousand) * _kMillion;
    print('seconds = $seconds');
    print('nanoseconds = $nanoseconds');

    // Flutterfire's current code:
    seconds = (milliseconds ~/ _kThousand).floor();
    nanoseconds = (milliseconds - seconds * _kThousand) * _kMillion;
    print('seconds = $seconds');
    print('nanoseconds = $nanoseconds');

This is the same as flutter/flutter#34929 and #1222 which was not fixed after all.

This is a serious issue with an easy fix. Please, give it a high priority and don't let it stand for long. Thanks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions