|
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,63 @@ 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___utc_datetime_with_femtoseconds___convert_to_timestamp_with_dst___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 | + assert roundtrip_dt.microsecond == expected_la_time.microsecond |
| 134 | + assert roundtrip_dt.femtosecond == expected_la_time.femtosecond |
| 135 | + # The yoctosecond field may contain up to 1 NI-BTF tick of rounding error. |
| 136 | + assert roundtrip_dt.yoctosecond <= 54210 |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.parametrize( |
| 140 | + "base_dt, yoctosecond, subseconds", |
| 141 | + [ |
| 142 | + (ht_datetime(2023, 3, 12, tzinfo=timezone.utc), 0, 0), |
| 143 | + (ht_datetime(2023, 3, 12, tzinfo=timezone.utc), 54210, 1), |
| 144 | + (ht_datetime(2023, 6, 1, tzinfo=timezone.utc), 0, 0), |
| 145 | + (ht_datetime(2023, 6, 1, tzinfo=timezone.utc), 54210, 1), |
| 146 | + (ht_datetime(2023, 11, 5, tzinfo=timezone.utc), 0, 0), |
| 147 | + (ht_datetime(2023, 11, 5, tzinfo=timezone.utc), 54210, 1), |
| 148 | + ], |
| 149 | +) |
| 150 | +def test___utc_datetime_with_yoctoseconds___convert_to_timestamp_with_dst___is_reversible( |
| 151 | + base_dt, yoctosecond, subseconds |
| 152 | +): |
| 153 | + target_timezone = ZoneInfo("America/Los_Angeles") |
| 154 | + from_dt = base_dt.replace(yoctosecond=yoctosecond) |
| 155 | + expected_la_time = _convert_to_desired_timezone(from_dt, target_timezone) |
| 156 | + |
| 157 | + ts = LibTimestamp.from_datetime(from_dt) |
| 158 | + roundtrip_dt = ts.to_datetime(tzinfo=target_timezone) |
| 159 | + |
| 160 | + assert ts.msb == LibTimestamp.from_datetime(expected_la_time).msb |
| 161 | + assert ts.lsb == subseconds |
| 162 | + assert roundtrip_dt.microsecond == expected_la_time.microsecond |
| 163 | + assert roundtrip_dt.femtosecond == expected_la_time.femtosecond |
| 164 | + assert roundtrip_dt.yoctosecond == expected_la_time.yoctosecond |
| 165 | + |
| 166 | + |
109 | 167 | @pytest.mark.parametrize( |
110 | 168 | "datetime_cls, tzinfo, expected_offset", |
111 | 169 | [ |
|
0 commit comments