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

[Bug]: Issue with Default session_start_time timezone in mock_NWBFile #2013

Open
3 tasks done
alessandratrapani opened this issue Dec 19, 2024 · 2 comments
Open
3 tasks done
Assignees
Labels
category: bug errors in the code or code behavior priority: medium non-critical problem and/or affecting only a small set of NWB users

Comments

@alessandratrapani
Copy link

What happened?

Description

A potential bug occurs when using a mock function for creating NWBFile objects. The function set session_start_time to datetime(1970, 1, 1, tzinfo=tzlocal()). This results in an OSError: [Errno 22] Invalid argument when attempting to write the file using pynwb.NWBHDF5IO. The issue seems to be caused by the interaction between tzlocal() and early datetime values (e.g., the epoch time 1970-01-01), particularly in systems located in time zones earlier than UTC, where the local time would fall outside the valid range for the epoch.

Steps to Reproduce

from pynwb.testing.mock.file import mock_NWBFile

# Create NWBFile object using default session_start_time 
# 
nwbfile = mock_NWBFile()

# Attempt to write the NWB file
with NWBHDF5IO('test.nwb', mode='w') as io:
    io.write(nwbfile)  # This triggers the error

Traceback

...
    return x.isoformat()  # method works for both date and datetime
..…\dateutil\tz\tz.py:291: in _naive_is_dst
    dstval = self._naive_is_dst(dt)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = tzlocal(), dt = datetime.datetime(1970, 1, 1, 0, 0, tzinfo=tzlocal())

    def _naive_is_dst(self, dt):
        timestamp = _datetime_to_timestamp(dt)
>       return time.localtime(timestamp + time.timezone).tm_isdst
E       OSError: [Errno 22] Invalid argument

Operating System

Windows

Python Executable

Conda

Python Version

3.11

Package Versions

No response

Code of Conduct

@stephprince
Copy link
Contributor

stephprince commented Dec 19, 2024

Thanks for pointing that out @alessandratrapani!

It looks like this may be a Windows specific error so I'm not able to reproduce locally, but if I understand correctly I think we should update the default session_start_time in mock_NWBFile to use anything later than datetime(1970, 1, 1, 12, tzinfo=tzutc()) to fix this issue.

@stephprince stephprince added category: bug errors in the code or code behavior priority: medium non-critical problem and/or affecting only a small set of NWB users labels Dec 19, 2024
@stephprince stephprince self-assigned this Dec 19, 2024
@alessandratrapani
Copy link
Author

I don't know if it is Windows specific, it seems to me it is "timezone" specific 😆, maybe that's why it is difficult to reproduce.
But simply passing a session_start_time of datetime(2000, 1, 1, tzinfo=UTC) did fix the issue on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: bug errors in the code or code behavior priority: medium non-critical problem and/or affecting only a small set of NWB users
Projects
None yet
Development

No branches or pull requests

2 participants