Skip to content

Commit

Permalink
Fix nathanhi#34: Gracefully handle invalid file timestamps
Browse files Browse the repository at this point in the history
In case an invalid timestamp is encountered, an exception
is thrown instead of gracefully handling the invalid timestamp.

This particular malformed time was produced by a Hach TL2350
turbidity meter, producing a seconds value of 60 at the end of
a minute (e.g., 0x1E).

Date deserialization already properly handles this with a
fallback date of 1980-01-01; similarly fall back to 00:00:00
in case of invalid timestamps.
  • Loading branch information
beckerben authored and nathanhi committed Oct 15, 2023
1 parent 4f8bfd5 commit a9d7b34
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Added
~~~~~
* (mkfs) `PR #30 <https://github.com/nathanhi/pyfatfs/pull/30>`_: Add support for different FAT12 cluster sizes for filesystems up to 256MB by `@zurcher <https://github.com/zurcher>`_ / `@Microsoft <https://github.com/Microsoft>`_

Fixed
~~~~~

* `#34 <https://github.com/nathanhi/pyfatfs/issues/34>`_ (DosDateTime) `PR #35 <https://github.com/nathanhi/pyfatfs/pull/35>`_: Gracefully handle invalid file timestamps by `@beckerben <https://github.com/beckerben>`_

Changed
~~~~~~~

Expand Down
6 changes: 5 additions & 1 deletion pyfatfs/DosDateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ def deserialize_time(tm: int) -> time:
second = (tm & (1 << 5) - 1) * 2
minute = (tm >> 5) & ((1 << 6) - 1)
hour = (tm >> 11) & ((1 << 5) - 1)
return time(hour, minute, second)

try:
return time(hour, minute, second)
except ValueError:
return time(0, 0, 0)
7 changes: 2 additions & 5 deletions tests/test_DosDateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from datetime import datetime, time

import pytest

from pyfatfs.DosDateTime import DosDateTime


Expand Down Expand Up @@ -93,9 +91,8 @@ def test_deserialize_max():


def test_deserialize_exceed_max():
"""Deserialize invalid time value."""
with pytest.raises(ValueError, match="second must be in 0..59"):
DosDateTime.deserialize_time(0xBF7E)
"""Deserialize invalid time value to 00:00:00."""
assert DosDateTime.deserialize_time(0xBF7E) == time(0, 0, 0)


def test_deserialize_minutes_high():
Expand Down

0 comments on commit a9d7b34

Please sign in to comment.