1from datetime import timedelta
2from typing import Optional
3
4from .transition_type import TransitionType
5
6
7class Transition:
8    def __init__(
9        self,
10        at,  # type: int
11        ttype,  # type: TransitionType
12        previous,  # type: Optional[Transition]
13    ):
14        self._at = at
15
16        if previous:
17            self._local = at + previous.ttype.offset
18        else:
19            self._local = at + ttype.offset
20
21        self._ttype = ttype
22        self._previous = previous
23
24        if self.previous:
25            self._fix = self._ttype.offset - self.previous.ttype.offset
26        else:
27            self._fix = 0
28
29        self._to = self._local + self._fix
30        self._to_utc = self._at + self._fix
31        self._utcoffset = timedelta(seconds=ttype.offset)
32
33    @property
34    def at(self):  # type: () -> int
35        return self._at
36
37    @property
38    def local(self):  # type: () -> int
39        return self._local
40
41    @property
42    def to(self):  # type: () -> int
43        return self._to
44
45    @property
46    def to_utc(self):  # type: () -> int
47        return self._to
48
49    @property
50    def ttype(self):  # type: () -> TransitionType
51        return self._ttype
52
53    @property
54    def previous(self):  # type: () -> Optional[Transition]
55        return self._previous
56
57    @property
58    def fix(self):  # type: () -> int
59        return self._fix
60
61    def is_ambiguous(self, stamp):  # type: (int) -> bool
62        return self._to <= stamp < self._local
63
64    def is_missing(self, stamp):  # type: (int) -> bool
65        return self._local <= stamp < self._to
66
67    def utcoffset(self):  # type: () -> timedelta
68        return self._utcoffset
69
70    def __contains__(self, stamp):  # type: (int) -> bool
71        if self.previous is None:
72            return stamp < self.local
73
74        return self.previous.local <= stamp < self.local
75
76    def __repr__(self):  # type: () -> str
77        return "Transition({} -> {}, {})".format(self._local, self._to, self._ttype)
78