Skip to content

Commit 9c550e1

Browse files
Fix Windows bug with local time in BIDS datasets and add tests (#1465)
* including the test * updating the read * updating the whats new file
1 parent b999865 commit 9c550e1

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/whats_new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Detailed list of changes
5656

5757
- Fixed a bug that modified the name and help message of some of the available commands, by `Alex Lopez Marquez`_ (:gh:`1441`)
5858
- Updated MEG/iEEG writers to satisfy the stricter checks in the latest BIDS validator releases: BTi/4D run folders now retain their ``.pdf`` suffix (falling back to the legacy naming when an older validator is detected), KIT marker files encode the run via the ``acq`` entity instead of ``run``, datasets lacking iEEG montages receive placeholder ``electrodes.tsv``/``coordsystem.json`` files, and the ``AssociatedEmptyRoom`` entry stores dataset-relative paths by `Bruno Aristimunha`_ (:gh:`1449`)
59+
- Fixed a bug in :func:`mne_bids.read_raw_bids` that caused it to fail when reading BIDS datasets where the acquisition time was specified in local time rather than UTC only in Windows, by `Bruno Aristimunha`_ (:gh:`1452`)
5960

6061
⚕️ Code health
6162
^^^^^^^^^^^^^^

mne_bids/read.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ def _handle_scans_reading(scans_fname, raw, bids_path):
420420
acq_time = acq_time.replace(tzinfo=timezone.utc)
421421
else:
422422
# Convert time offset to UTC
423+
if acq_time.tzinfo is None:
424+
# Windows needs an explicit local tz for naive, pre-epoch times.
425+
local_tz = datetime.now().astimezone().tzinfo or timezone.utc
426+
acq_time = acq_time.replace(tzinfo=local_tz)
423427
acq_time = acq_time.astimezone(timezone.utc)
424428

425429
logger.debug(f"Loaded {scans_fname} scans file to set acq_time as {acq_time}.")

mne_bids/tests/test_read.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,21 @@ def test_handle_scans_reading(tmp_path):
834834
new_acq_time = datetime.strptime(new_acq_time_str, date_format)
835835
assert raw_03.info["meas_date"] == new_acq_time.astimezone(timezone.utc)
836836

837+
# Regression for naive, pre-epoch acquisition times (Windows bug GH-1399)
838+
pre_epoch_str = "1950-06-15T13:45:30"
839+
scans_tsv["acq_time"][0] = pre_epoch_str
840+
_to_tsv(scans_tsv, scans_path)
841+
842+
raw_pre_epoch = read_raw_bids(bids_path)
843+
pre_epoch_naive = datetime.strptime(pre_epoch_str, "%Y-%m-%dT%H:%M:%S")
844+
local_tz = datetime.now().astimezone().tzinfo or timezone.utc
845+
expected_pre_epoch = pre_epoch_naive.replace(tzinfo=local_tz).astimezone(
846+
timezone.utc
847+
)
848+
assert raw_pre_epoch.info["meas_date"] == expected_pre_epoch
849+
if raw_pre_epoch.annotations.orig_time is not None:
850+
assert raw_pre_epoch.annotations.orig_time == expected_pre_epoch
851+
837852

838853
@pytest.mark.filterwarnings(warning_str["channel_unit_changed"])
839854
def test_handle_scans_reading_brainvision(tmp_path):

0 commit comments

Comments
 (0)