Skip to content

Commit ae0a188

Browse files
committed
add comments and a unit test for the date parsing to ensure it lines up with third-party real life examples and my blog post
1 parent c1ef424 commit ae0a188

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/extensions.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ extension Concept2DateExtension on DateTime {
66
int date =
77
CsafeIntExtension.fromBytes(bytes.sublist(0, 2), endian: Endian.little);
88

9-
int month = date & 0x0F;
10-
int day = (date >> 4) & 0x1F;
11-
int year = (date >> 9) & 0x7f;
9+
int month = date & 0x0F; //lowest 4 bits of Date LO
10+
int day = (date >> 4) & 0x1F; // Next 5 bits
11+
int year = (date >> 9) & 0x7f; // Next 7 bits
1212

1313
int minutes =
1414
CsafeIntExtension.fromBytes(bytes.sublist(2, 3), endian: Endian.little);

test/extensions_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ void main() {
1919
// expect(CsafeIntExtension.fromBytes(bytes, Endian.little), 2147483648);
2020
});
2121

22+
test('converting a date from a verifiable source from bytes', () {
23+
// https://www.c2forum.com/viewtopic.php?t=93684
24+
// DateH DateL TimeH TimeL
25+
// 00011101 10010100 00010000 00001001
26+
// final bytes = Uint8List.fromList([29, 148, 16, 9]);
27+
final bytes = Uint8List.fromList([148, 29, 9, 16]);
28+
29+
expect(Concept2DateExtension.fromBytes(bytes),
30+
DateTime(2014, 4, 25, 16, 09));
31+
// 25th April 2014 @ 16:09 (4:09pm)
32+
});
33+
2234
test('converting a date in 2022 from bytes', () {
2335
final bytes = Uint8List.fromList([133, 44, 14, 14]);
2436
expect(

0 commit comments

Comments
 (0)