diff --git a/mne/annotations.py b/mne/annotations.py index 5606ff26fad..845a8b7228f 100644 --- a/mne/annotations.py +++ b/mne/annotations.py @@ -278,12 +278,19 @@ class Annotations: def __init__(self, onset, duration, description, orig_time=None, ch_names=None): self._orig_time = _handle_meas_date(orig_time) if isinstance(orig_time, str) and self._orig_time is None: - warnings.warn( - "The format of the `orig_time` string is not recognised. It must " - "conform to the ISO8601 format with at most microsecond precision and " - "where the delimiter between date and time is ' '.", - RuntimeWarning, - ) + try: # only warn if `orig_time` is not the default '1970-01-01 00:00:00' + if _handle_meas_date(0) == datetime.strptime( + orig_time, "%Y-%m-%d %H:%M:%S" + ).replace(tzinfo=timezone.utc): + pass + except ValueError: # error if incorrect datetime format AND not the default + warn( + "The format of the `orig_time` string is not recognised. It " + "must conform to the ISO8601 format with at most microsecond " + "precision and where the delimiter between date and time is " + "' '.", + RuntimeWarning, + ) self.onset, self.duration, self.description, self.ch_names = _check_o_d_s_c( onset, duration, description, ch_names ) diff --git a/mne/tests/test_annotations.py b/mne/tests/test_annotations.py index c02d37bc92a..55f97360916 100644 --- a/mne/tests/test_annotations.py +++ b/mne/tests/test_annotations.py @@ -76,6 +76,9 @@ def windows_like_datetime(monkeypatch): def test_basics(): """Test annotation class.""" + pytest.importorskip("pandas") + import pandas as pd + raw = read_raw_fif(fif_fname) assert raw.annotations is not None assert len(raw.annotations.onset) == 0 @@ -95,6 +98,17 @@ def test_basics(): assert isinstance(annot.orig_time, datetime) assert annot.orig_time.tzinfo is timezone.utc + # Test bad format `orig_time` str -> `None` raises warning + with pytest.warns( + RuntimeWarning, match="The format of the `orig_time` string is not recognised." + ): + bad_orig_time = ( + pd.Timestamp(_ORIG_TIME) + .astimezone(None) + .isoformat(sep=" ", timespec="nanoseconds") + ) + Annotations(onset, duration, description, bad_orig_time) + pytest.raises(ValueError, Annotations, onset, duration, description[:9]) pytest.raises(ValueError, Annotations, [onset, 1], duration, description) pytest.raises(ValueError, Annotations, onset, [duration, 1], description)