| 1 |
# Date and Time Types |
| 2 |
|
| 3 |
::: dirty_equals.IsDatetime |
| 4 |
|
| 5 |
### Timezones |
| 6 |
|
| 7 |
Timezones are hard, anyone who claims otherwise is either a genius, a liar, or an idiot. |
| 8 |
|
| 9 |
`IsDatetime` and its subtypes (e.g. [`IsNow`][dirty_equals.IsNow]) can be used in two modes, |
| 10 |
based on the `enforce_tz` parameter: |
| 11 |
|
| 12 |
* `enforce_tz=True` (the default): |
| 13 |
* if the datetime wrapped by `IsDatetime` is timezone naive, the compared value must also be timezone naive. |
| 14 |
* if the datetime wrapped by `IsDatetime` has a timezone, the compared value must have a |
| 15 |
timezone with the same offset. |
| 16 |
* `enforce_tz=False`: |
| 17 |
* if the datetime wrapped by `IsDatetime` is timezone naive, the compared value can either be naive or have a |
| 18 |
timezone all that matters is the datetime values match. |
| 19 |
* if the datetime wrapped by `IsDatetime` has a timezone, the compared value needs to represent the same point in |
| 20 |
time - either way it must have a timezone. |
| 21 |
|
| 22 |
Example |
| 23 |
|
| 24 |
```py title="IsDatetime & timezones" requires="3.9" |
| 25 |
from datetime import datetime |
| 26 |
from zoneinfo import ZoneInfo |
| 27 |
|
| 28 |
from dirty_equals import IsDatetime |
| 29 |
|
| 30 |
tz_london = ZoneInfo('Europe/London') |
| 31 |
new_year_london = datetime(2000, 1, 1, tzinfo=tz_london) |
| 32 |
|
| 33 |
tz_nyc = ZoneInfo('America/New_York') |
| 34 |
new_year_eve_nyc = datetime(1999, 12, 31, 19, 0, 0, tzinfo=tz_nyc) |
| 35 |
|
| 36 |
assert new_year_eve_nyc == IsDatetime(approx=new_year_london, enforce_tz=False) |
| 37 |
assert new_year_eve_nyc != IsDatetime(approx=new_year_london, enforce_tz=True) |
| 38 |
|
| 39 |
new_year_naive = datetime(2000, 1, 1) |
| 40 |
|
| 41 |
assert new_year_naive != IsDatetime(approx=new_year_london, enforce_tz=False) |
| 42 |
assert new_year_naive != IsDatetime(approx=new_year_eve_nyc, enforce_tz=False) |
| 43 |
assert new_year_london == IsDatetime(approx=new_year_naive, enforce_tz=False) |
| 44 |
assert new_year_eve_nyc != IsDatetime(approx=new_year_naive, enforce_tz=False) |
| 45 |
``` |
| 46 |
|
| 47 |
::: dirty_equals.IsNow |
| 48 |
|
| 49 |
::: dirty_equals.IsDate |
| 50 |
|
| 51 |
::: dirty_equals.IsToday |