| 123 | |
| 124 | |
| 125 | def test_remaining(): |
| 126 | # Relative |
| 127 | remaining(datetime.now(_timezone.utc), timedelta(hours=1), relative=True) |
| 128 | |
| 129 | """ |
| 130 | The upcoming cases check whether the next run is calculated correctly |
| 131 | """ |
| 132 | eastern_tz = ZoneInfo("US/Eastern") |
| 133 | tokyo_tz = ZoneInfo("Asia/Tokyo") |
| 134 | |
| 135 | # Case 1: `start` in UTC and `now` in other timezone |
| 136 | start = datetime.now(ZoneInfo("UTC")) |
| 137 | now = datetime.now(eastern_tz) |
| 138 | delta = timedelta(hours=1) |
| 139 | assert str(start.tzinfo) == str(ZoneInfo("UTC")) |
| 140 | assert str(now.tzinfo) == str(eastern_tz) |
| 141 | rem_secs = remaining(start, delta, now).total_seconds() |
| 142 | # assert remaining time is approximately equal to delta |
| 143 | assert rem_secs == pytest.approx(delta.total_seconds(), abs=1) |
| 144 | |
| 145 | # Case 2: `start` and `now` in different timezones (other than UTC) |
| 146 | start = datetime.now(eastern_tz) |
| 147 | now = datetime.now(tokyo_tz) |
| 148 | delta = timedelta(hours=1) |
| 149 | assert str(start.tzinfo) == str(eastern_tz) |
| 150 | assert str(now.tzinfo) == str(tokyo_tz) |
| 151 | rem_secs = remaining(start, delta, now).total_seconds() |
| 152 | assert rem_secs == pytest.approx(delta.total_seconds(), abs=1) |
| 153 | |
| 154 | """ |
| 155 | Case 3: DST check |
| 156 | Suppose start (which is last_run_time) is in EST while next_run is in EDT, |
| 157 | then check whether the `next_run` is actually the time specified in the |
| 158 | start (i.e. there is not an hour diff due to DST). |
| 159 | In 2019, DST starts on March 10 |
| 160 | """ |
| 161 | start = datetime( |
| 162 | month=3, day=9, year=2019, hour=10, |
| 163 | minute=0, tzinfo=eastern_tz) # EST |
| 164 | |
| 165 | now = datetime( |
| 166 | day=11, month=3, year=2019, hour=1, |
| 167 | minute=0, tzinfo=eastern_tz) # EDT |
| 168 | delta = ffwd(hour=10, year=2019, microsecond=0, minute=0, |
| 169 | second=0, day=11, weeks=0, month=3) |
| 170 | # `next_actual_time` is the next time to run (derived from delta) |
| 171 | next_actual_time = datetime( |
| 172 | day=11, month=3, year=2019, hour=10, minute=0, tzinfo=eastern_tz) # EDT |
| 173 | assert start.tzname() == "EST" |
| 174 | assert now.tzname() == "EDT" |
| 175 | assert next_actual_time.tzname() == "EDT" |
| 176 | rem_time = remaining(start, delta, now) |
| 177 | next_run = now + rem_time |
| 178 | assert next_run == next_actual_time |
| 179 | |
| 180 | """ |
| 181 | Case 4: DST check between now and next_run |
| 182 | Suppose start (which is last_run_time) and now are in EST while next_run |