From 9e8635e6e1f0555da5332180431128d8ecc1e580 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 5 Dec 2026 08:13:59 +0530 Subject: [PATCH 1/3] huami fetch start: bad date isn't ok, so a 1970 round never stamps samples --- lib/src/huami2021_fetch.dart | 4 +++- test/huami2021_fetch_test.dart | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/src/huami2021_fetch.dart b/lib/src/huami2021_fetch.dart index 8859c08..14241fe 100644 --- a/lib/src/huami2021_fetch.dart +++ b/lib/src/huami2021_fetch.dart @@ -73,7 +73,9 @@ class Huami2021FetchStart { final int expected; final DateTime roundStart; - bool get ok => status == 0x01; + /// False for a bad status or an implausible date (roundStart then is the + /// 1970 placeholder and must not stamp any samples). + bool get ok => status == 0x01 && roundStart.year >= 2000; bool get hasData => ok && expected > 0; } diff --git a/test/huami2021_fetch_test.dart b/test/huami2021_fetch_test.dart index f4f0cba..4dec9ac 100644 --- a/test/huami2021_fetch_test.dart +++ b/test/huami2021_fetch_test.dart @@ -72,7 +72,11 @@ void main() { () { final b = Uint8List.fromList( [0x10, 0x01, 0x01, 1, 0, 0, 0, 0xEA, 0x07, 13, 32, 24, 60, 60]); - expect(parseHuami2021FetchStart(b)!.roundStart, DateTime(1970)); + final r = parseHuami2021FetchStart(b)!; + expect(r.roundStart, DateTime(1970)); + // a 1970 round start must never read as a usable round + expect(r.ok, isFalse); + expect(r.hasData, isFalse); }); test('a non-success status is not hasData', () { From 3c6fe3c0517ca5063079fdcc26e0450cfdc6c0a2 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 5 Dec 2026 08:15:37 +0530 Subject: [PATCH 2/3] also reject feb 31 style dates in fetch start --- lib/src/huami2021_fetch.dart | 4 +++- test/huami2021_fetch_test.dart | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/src/huami2021_fetch.dart b/lib/src/huami2021_fetch.dart index 14241fe..80b4cd9 100644 --- a/lib/src/huami2021_fetch.dart +++ b/lib/src/huami2021_fetch.dart @@ -107,8 +107,10 @@ Huami2021FetchStart? parseHuami2021FetchStart(Uint8List b) { second <= 59 && year >= 2000 && year <= 2100; + final dt = DateTime(year, month, day, hour, minute, second); + // Feb 31 etc. pass the field checks but normalise into the next month. final roundStart = - plausible ? DateTime(year, month, day, hour, minute, second) : DateTime(1970); + plausible && dt.month == month && dt.day == day ? dt : DateTime(1970); return Huami2021FetchStart(status: status, expected: expected, roundStart: roundStart); } diff --git a/test/huami2021_fetch_test.dart b/test/huami2021_fetch_test.dart index 4dec9ac..eef85de 100644 --- a/test/huami2021_fetch_test.dart +++ b/test/huami2021_fetch_test.dart @@ -79,6 +79,14 @@ void main() { expect(r.hasData, isFalse); }); + test('feb 31 is rejected, not normalised into march', () { + final b = Uint8List.fromList( + [0x10, 0x01, 0x01, 1, 0, 0, 0, 0xEA, 0x07, 2, 31, 1, 1, 1]); + final r = parseHuami2021FetchStart(b)!; + expect(r.ok, isFalse); + expect(r.roundStart, DateTime(1970)); + }); + test('a non-success status is not hasData', () { final b = Uint8List.fromList( [0x10, 0x01, 0x02, 5, 0, 0, 0, 0xEA, 0x07, 1, 1, 0, 0, 0]); From 31afe2c81b4410b5a7bdc06b0519a5fa398257b4 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 5 Dec 2026 20:45:46 +0530 Subject: [PATCH 3/3] fetch start ok: cap year at 2100 like the parser --- lib/src/huami2021_fetch.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/huami2021_fetch.dart b/lib/src/huami2021_fetch.dart index 80b4cd9..7c22c8e 100644 --- a/lib/src/huami2021_fetch.dart +++ b/lib/src/huami2021_fetch.dart @@ -75,7 +75,7 @@ class Huami2021FetchStart { /// False for a bad status or an implausible date (roundStart then is the /// 1970 placeholder and must not stamp any samples). - bool get ok => status == 0x01 && roundStart.year >= 2000; + bool get ok => status == 0x01 && roundStart.year >= 2000 && roundStart.year <= 2100; bool get hasData => ok && expected > 0; }