Skip to content

Commit 242d15a

Browse files
committed
timestamp
1 parent 6608fb5 commit 242d15a

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

Diff for: packages/firebase_data_connect/firebase_data_connect/lib/src/timestamp.dart

+15-17
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ part of firebase_data_connect;
66

77
/// Timestamp class is a custom class that allows for storing of nanoseconds.
88
class Timestamp {
9+
// ignore: use_raw_strings
10+
final regex = RegExp(
11+
r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{0,9})?(Z|[+-]\d{2}:\d{2})$');
12+
913
/// Constructor
1014
Timestamp(this.nanoseconds, this.seconds);
15+
1116
// TODO(mtewani): Fix this so that it keeps track of positional arguments so you don't have to repeatedly search the string multiple times.
1217
Timestamp.fromJson(String date) {
13-
// ignore: use_raw_strings
14-
var regex = RegExp(
15-
r'^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{0,9})?(Z|[+-]\\d{2}:\\d{2})\$');
1618
if (!regex.hasMatch(date)) {
1719
throw Exception('Invalid Date provided!');
1820
}
1921
DateTime dateTime = DateTime.parse(date);
20-
seconds = dateTime.second;
22+
seconds = dateTime.millisecondsSinceEpoch ~/ 1000;
2123
String nanoStr = '';
2224
int dotIdx = date.indexOf('.');
2325
if (dotIdx > -1) {
@@ -32,28 +34,24 @@ class Timestamp {
3234
if (nanoStr.isNotEmpty) {
3335
nanoseconds = int.parse(nanoStr.padRight(9, '0'));
3436
}
35-
// TODO(mtewani): Add offset values.
36-
if (date.contains('Z')) {
37-
return;
38-
}
39-
int addIdx = date.indexOf('+');
40-
bool isAdd = addIdx > -1;
41-
int signIdx = isAdd ? addIdx : date.indexOf('-');
42-
int timeHour = int.parse(date.substring(signIdx + 1, signIdx + 3));
43-
int timeMin = int.parse(date.substring(signIdx + 4, signIdx + 6));
44-
int timeZoneDiffer = timeHour * 3600 + timeMin * 60;
45-
seconds = seconds + (isAdd ? -timeZoneDiffer : timeZoneDiffer);
4637
}
38+
4739
String toJson() {
4840
String secondsStr =
4941
DateTime.fromMillisecondsSinceEpoch(seconds * 1000, isUtc: true)
5042
.toIso8601String();
43+
if (nanoseconds == 0) {
44+
return secondsStr;
45+
}
5146
String nanoStr = nanoseconds.toString().padRight(9, '0');
52-
return '${secondsStr.substring(0, nanoStr.length - 1)}.${nanoStr}Z';
47+
return '${secondsStr.substring(0, 19)}.${nanoStr}Z';
5348
}
5449

5550
DateTime toDateTime() {
56-
return DateTime.utc((seconds * 1000 + (nanoseconds / 1000000)).floor());
51+
final string = toJson();
52+
final date = DateTime.parse(string);
53+
return date;
54+
return DateTime.parse(toJson());
5755
}
5856

5957
/// Current nanoseconds
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import 'package:firebase_data_connect/firebase_data_connect.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('Timestamp', () {
6+
test('constructor initializes with correct nanoseconds and seconds', () {
7+
final timestamp = Timestamp(500, 864000); // Example timestamp values
8+
expect(timestamp.nanoseconds, 500);
9+
expect(timestamp.seconds, 864000);
10+
});
11+
12+
test('fromJson throws exception for invalid date format', () {
13+
expect(() => Timestamp.fromJson('invalid-date'), throwsException);
14+
});
15+
16+
test('fromJson correctly parses date with nanoseconds and UTC (Z) format',
17+
() {
18+
final timestamp = Timestamp.fromJson('1970-01-11T00:00:00.123456789Z');
19+
expect(timestamp.seconds, 864000);
20+
expect(timestamp.nanoseconds, 123456789);
21+
});
22+
23+
test('fromJson correctly parses date without nanoseconds', () {
24+
final timestamp = Timestamp.fromJson('1970-01-11T00:00:00Z');
25+
expect(timestamp.seconds, 864000);
26+
expect(timestamp.nanoseconds, 0);
27+
});
28+
29+
test('fromJson correctly handles timezones with positive offset', () {
30+
final timestamp = Timestamp.fromJson('1970-01-11T00:00:00+02:00');
31+
expect(timestamp.seconds,
32+
864000 - (2 * 3600)); // Adjusts by the positive timezone offset
33+
});
34+
35+
test('fromJson correctly handles timezones with negative offset', () {
36+
final timestamp = Timestamp.fromJson('1970-01-11T00:00:00-05:00');
37+
expect(timestamp.seconds,
38+
864000 + (5 * 3600)); // Adjusts by the negative timezone offset
39+
});
40+
41+
test('toJson correctly serializes to ISO8601 string with nanoseconds', () {
42+
final timestamp = Timestamp(123456789, 864000); // Example timestamp
43+
final json = timestamp.toJson();
44+
expect(json, '1970-01-11T00:00:00.123456789Z');
45+
});
46+
47+
test('toJson correctly serializes to ISO8601 string without nanoseconds',
48+
() {
49+
final timestamp = Timestamp(0, 864000); // No nanoseconds
50+
final json = timestamp.toJson();
51+
expect(json, '1970-01-11T00:00:00.000Z');
52+
});
53+
54+
test('toDateTime correctly converts to DateTime object', () {
55+
final timestamp = Timestamp(0, 864000); // Example timestamp
56+
final dateTime = timestamp.toDateTime();
57+
expect(dateTime, DateTime.utc(1970, 1, 11, 0, 0, 0, 0));
58+
});
59+
});
60+
}

0 commit comments

Comments
 (0)