-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tryit.py
54 lines (42 loc) · 1.52 KB
/
tryit.py
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
52
53
54
from typing import cast
from datetime import datetime, timezone, date, time
from zoneinfo import ZoneInfo
from datetype import (
Date,
DateTime,
naive,
aware,
date_only,
NaiveDateTime,
AwareDateTime,
NaiveTime,
AwareTime,
)
x: NaiveDateTime = naive(datetime.now()) # ok
y: AwareDateTime = aware(datetime.now(timezone.utc)) # ok
x < x # ok
y > y # ok
x < y # error: we can't compare naive and aware
a: NaiveDateTime = aware( # error: it's aware and we want naive
datetime.now(timezone.utc)
)
b: AwareDateTime = naive( # error: it's naive and we want aware
datetime.now(timezone.utc)
)
adate: Date = date_only(date.today()) # success
bdate: Date = date_only(datetime.now(timezone.utc)) # runtime error only, sadly
cdate: Date = a # error because datetimes aren't dates
ddate: Date = b # error for aware too
naive_time: NaiveTime = naive(time(0))
aware_time: AwareTime = aware(time(0))
combined_naive = DateTime.combine(cdate, naive_time)
combined_aware = DateTime.combine(cdate, aware_time)
x < combined_naive # ok; both naive
y < combined_aware # ok; aware
x < combined_aware # error; naive/aware
y < combined_naive # error; aware/naive
y.tzinfo.key # error; no attribute defined
specific: DateTime[ZoneInfo] = DateTime.now(ZoneInfo("America/Los_Angeles"))
specific.tzinfo.key # ok; ZoneInfo has 'key' attribute
not_naive: NaiveDateTime = a.astimezone()
is_aware: DateTime[timezone] = a.astimezone(None)