-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathtimestamp_test.dart
46 lines (37 loc) · 1.67 KB
/
timestamp_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:fixnum/fixnum.dart';
import 'package:test/test.dart';
import '../out/protos/google/protobuf/timestamp.pb.dart';
void main() {
test('timestamp -> datetime -> timestamp', () {
final timestamp = Timestamp()
..seconds = Int64(1550225928)
..nanos = 12345000;
expect(Timestamp.fromDateTime(timestamp.toDateTime()), timestamp);
});
test('utc datetime -> timestamp -> datetime', () {
final dateTime = DateTime.utc(2019, 02, 15, 10, 21, 25, 5, 5);
final fromProto = Timestamp.fromDateTime(dateTime).toDateTime();
expect(fromProto.isUtc, true, reason: '$fromProto is not a UTC time.');
expect(fromProto, dateTime);
});
test('negative Timestamp', () {
final secondBeforeEpoch = Timestamp()
..seconds = Int64(-1)
..nanos = 1000000;
final 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', () {
final dateTime = DateTime(2019, 02, 15, 10, 21, 25, 5, 5);
final fromProto = Timestamp.fromDateTime(dateTime).toDateTime();
expect(fromProto.isUtc, true, reason: '$fromProto is not a UTC time.');
expect(fromProto, dateTime.toUtc());
});
}