diff --git a/lib/src/huami2021_fetch.dart b/lib/src/huami2021_fetch.dart index 8859c08..7c22c8e 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 && roundStart.year <= 2100; bool get hasData => ok && expected > 0; } @@ -105,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 f4f0cba..eef85de 100644 --- a/test/huami2021_fetch_test.dart +++ b/test/huami2021_fetch_test.dart @@ -72,7 +72,19 @@ 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('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', () {