File: datetime.md

package info (click to toggle)
dirty-equals 0.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 648 kB
  • sloc: python: 2,485; makefile: 69
file content (51 lines) | stat: -rw-r--r-- 1,885 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Date and Time Types

::: dirty_equals.IsDatetime

### Timezones

Timezones are hard, anyone who claims otherwise is either a genius, a liar, or an idiot.

`IsDatetime` and its subtypes (e.g. [`IsNow`][dirty_equals.IsNow]) can be used in two modes,
based on the `enforce_tz` parameter:

* `enforce_tz=True` (the default):
    * if the datetime wrapped by `IsDatetime` is timezone naive, the compared value must also be timezone naive.
    * if the datetime wrapped by `IsDatetime` has a timezone, the compared value must have a
      timezone with the same offset.
* `enforce_tz=False`:
    * if the datetime wrapped by `IsDatetime` is timezone naive, the compared value can either be naive or have a
      timezone all that matters is the datetime values match.
    * if the datetime wrapped by `IsDatetime` has a timezone, the compared value needs to represent the same point in
      time - either way it must have a timezone.

Example

```py title="IsDatetime & timezones" requires="3.9"
from datetime import datetime
from zoneinfo import ZoneInfo

from dirty_equals import IsDatetime

tz_london = ZoneInfo('Europe/London')
new_year_london = datetime(2000, 1, 1, tzinfo=tz_london)

tz_nyc = ZoneInfo('America/New_York')
new_year_eve_nyc = datetime(1999, 12, 31, 19, 0, 0, tzinfo=tz_nyc)

assert new_year_eve_nyc == IsDatetime(approx=new_year_london, enforce_tz=False)
assert new_year_eve_nyc != IsDatetime(approx=new_year_london, enforce_tz=True)

new_year_naive = datetime(2000, 1, 1)

assert new_year_naive != IsDatetime(approx=new_year_london, enforce_tz=False)
assert new_year_naive != IsDatetime(approx=new_year_eve_nyc, enforce_tz=False)
assert new_year_london == IsDatetime(approx=new_year_naive, enforce_tz=False)
assert new_year_eve_nyc != IsDatetime(approx=new_year_naive, enforce_tz=False)
```

::: dirty_equals.IsNow

::: dirty_equals.IsDate

::: dirty_equals.IsToday