|
7 | 7 | from hightime import datetime as ht_datetime |
8 | 8 |
|
9 | 9 | from nidaqmx._lib_time import AbsoluteTime as LibTimestamp |
| 10 | +from nidaqmx._time import _convert_to_desired_timezone |
10 | 11 | from tests.unit._time_utils import ( |
11 | 12 | JAN_01_1850_DATETIME, |
12 | 13 | JAN_01_1850_HIGHTIME, |
@@ -106,6 +107,37 @@ def test___utc_datetime___convert_to_timestamp_with_dst___is_reversible(date): |
106 | 107 | assert astimezone_date == roundtrip_dt |
107 | 108 |
|
108 | 109 |
|
| 110 | +@pytest.mark.parametrize( |
| 111 | + "base_dt, femtosecond, subseconds", |
| 112 | + [ |
| 113 | + (ht_datetime(2023, 3, 12, tzinfo=timezone.utc), 0, 0), |
| 114 | + (ht_datetime(2023, 3, 12, tzinfo=timezone.utc), 1, 0x480F), |
| 115 | + (ht_datetime(2023, 6, 1, tzinfo=timezone.utc), 0, 0), |
| 116 | + (ht_datetime(2023, 6, 1, tzinfo=timezone.utc), 1, 0x480F), |
| 117 | + (ht_datetime(2023, 11, 5, tzinfo=timezone.utc), 0, 0), |
| 118 | + (ht_datetime(2023, 11, 5, tzinfo=timezone.utc), 1, 0x480F), |
| 119 | + ], |
| 120 | +) |
| 121 | +def test___datetime_with_dst_and_femtoseconds___convert_to_timestamp___is_reversible( |
| 122 | + base_dt, femtosecond, subseconds |
| 123 | +): |
| 124 | + target_timezone = ZoneInfo("America/Los_Angeles") |
| 125 | + from_dt = base_dt.replace(femtosecond=femtosecond) |
| 126 | + expected_la_time = _convert_to_desired_timezone(from_dt, target_timezone) |
| 127 | + |
| 128 | + ts = LibTimestamp.from_datetime(from_dt) |
| 129 | + roundtrip_dt = ts.to_datetime(tzinfo=target_timezone) |
| 130 | + |
| 131 | + assert ts.msb == LibTimestamp.from_datetime(expected_la_time).msb |
| 132 | + assert ts.lsb == subseconds |
| 133 | + # comparison is tricky since imprecision in the conversion to NI-BTF are |
| 134 | + # caught by the higher precision values in hightime, so we round here. |
| 135 | + roundtrip_dt_femtosecond = roundtrip_dt.femtosecond |
| 136 | + if roundtrip_dt.yoctosecond > LibTimestamp.MAX_YS / 2: |
| 137 | + roundtrip_dt_femtosecond += 1 |
| 138 | + assert roundtrip_dt_femtosecond == femtosecond |
| 139 | + |
| 140 | + |
109 | 141 | @pytest.mark.parametrize( |
110 | 142 | "datetime_cls, tzinfo, expected_offset", |
111 | 143 | [ |
|
0 commit comments