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 rounding when handling negative timestamps #580

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/mixins/well_known.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ abstract class TimestampMixin {
/// Time zone information will not be preserved.
static void setFromDateTime(TimestampMixin target, DateTime dateTime) {
var micros = dateTime.microsecondsSinceEpoch;
target.seconds = Int64(micros ~/ Duration.microsecondsPerSecond);
target.seconds = Int64((micros / Duration.microsecondsPerSecond).floor());
target.nanos = (micros % Duration.microsecondsPerSecond).toInt() * 1000;
}

Expand Down
11 changes: 11 additions & 0 deletions protoc_plugin/test/timestamp_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ void main() {
expect(fromProto, dateTime);
});

test('negative Timestamp', () {
final secondBeforeEpoch = Timestamp(seconds: Int64(-1), nanos: 1000000);
var dateTime = DateTime.fromMillisecondsSinceEpoch(-999, isUtc: true);

expect(secondBeforeEpoch.toDateTime().millisecondsSinceEpoch,
dateTime.millisecondsSinceEpoch);
expect(secondBeforeEpoch.toDateTime(), dateTime);
expect(Timestamp.fromDateTime(dateTime).nanos, 1000000);
expect(Timestamp.fromDateTime(dateTime).seconds, Int64(-1));
});

test('local datetime -> timestamp -> datetime', () {
var dateTime = DateTime(2019, 02, 15, 10, 21, 25, 5, 5);
var fromProto = Timestamp.fromDateTime(dateTime).toDateTime();
Expand Down