Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The DosDateTime.deserialize_time function produce ValueError: second must be in 0..59 #34

Closed
beckerben opened this issue Aug 22, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@beckerben
Copy link
Contributor

I'm finding at the top of every minute, it is possible to get the value error in the deserialize_time function.

You can produce the error with:

#Function from DasDateTime
def deserialize_time(tm: int) -> time:
        """Convert a DOS time format to a Python object."""
        second = (tm & (1 << 5) - 1) * 2
        minute = (tm >> 5) & ((1 << 6) - 1)
        hour = (tm >> 11) & ((1 << 5) - 1)
        return time(hour, minute, second)

#Code to show the out of range second error
python_time = deserialize_time(0x001E)
print(python_time)
@nathanhi
Copy link
Owner

nathanhi commented Oct 15, 2023

Interesting! According to the FAT specification having a second value >29 (in this case: 30) is invalid:

image

Do you mind sharing what created the file and/or the filesystem? Maybe even an example filesystem image? At least in PyFatFS there's a boundary check in place that verifies the seconds range:

>>> from pyfatfs.DosDateTime import DosDateTime
>>> DosDateTime(1980, 1, 1, 0, 0, 60)
Traceback (most recent call last):
  File "/app/extra/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
           ^^^^^^
  File "<input>", line 1, in <module>
ValueError: second must be in 0..59

Serialization of the boundary is part of the test suite and seems to work as intended:

>>> from pyfatfs.DosDateTime import DosDateTime
>>> hex(DosDateTime(1980, 1, 1, 0, 0, 58).serialize_time())
'0x1d'
>>> hex(DosDateTime(1980, 1, 1, 0, 0, 59).serialize_time())
'0x1d'

@beckerben
Copy link
Contributor Author

It was a turbidity meter (Hach TL2350) and we believe that there is actually an issue with the instrument with regard to how it is writing invalid timestamps. We worked around it by handling the invalid value to force it to return 0 rather than triggering the exception.

nathanhi pushed a commit to beckerben/pyfatfs that referenced this issue Oct 15, 2023
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.
nathanhi pushed a commit to beckerben/pyfatfs that referenced this issue Oct 15, 2023
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.
@nathanhi
Copy link
Owner

Thank you for the heads-up! I've updated you pull request and added additional information to the commit message.

@nathanhi nathanhi added the bug Something isn't working label Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants