@@ -110,7 +110,7 @@ def _timezone(cls, zone):
110110
111111 def __new__ (cls , year , month , day ,
112112 hour = 0 , minute = 0 , second = 0 , microsecond = 0 ,
113- tzinfo = None ):
113+ tzinfo = None , fold = None ):
114114 """
115115 Constructor.
116116
@@ -125,7 +125,7 @@ def __new__(cls, year, month, day,
125125
126126 def __init__ (self , year , month , day ,
127127 hour = 0 , minute = 0 , second = 0 , microsecond = 0 ,
128- tzinfo = UTC ):
128+ tzinfo = UTC , fold = None ):
129129 # If a TimezoneInfo is passed we do not convert
130130 if isinstance (tzinfo , TimezoneInfo ):
131131 self ._tz = tzinfo .tz
@@ -139,6 +139,15 @@ def __init__(self, year, month, day,
139139 self ._microsecond = microsecond
140140 self ._tzinfo = tzinfo
141141
142+ if fold is None :
143+ # Converting rule to fold value
144+ if self ._TRANSITION_RULE == Timezone .POST_TRANSITION :
145+ fold = 1
146+ else :
147+ fold = 0
148+
149+ self ._fold = fold
150+
142151 dt = datetime .datetime (
143152 year , month , day ,
144153 hour , minute , second , microsecond ,
@@ -147,10 +156,24 @@ def __init__(self, year, month, day,
147156 else :
148157 self ._tz = self ._safe_create_datetime_zone (tzinfo )
149158
159+ # Support for explicit fold attribute
160+ if fold is None :
161+ transition_rule = self ._TRANSITION_RULE
162+
163+ # Converting rule to fold value
164+ if self ._TRANSITION_RULE == Timezone .POST_TRANSITION :
165+ fold = 1
166+ else :
167+ fold = 0
168+ elif fold == 1 :
169+ transition_rule = Timezone .POST_TRANSITION
170+ else :
171+ transition_rule = Timezone .PRE_TRANSITION
172+
150173 dt = self ._tz .convert (datetime .datetime (
151174 year , month , day ,
152175 hour , minute , second , microsecond
153- ), dst_rule = self . _TRANSITION_RULE )
176+ ), dst_rule = transition_rule )
154177
155178 self ._year = dt .year
156179 self ._month = dt .month
@@ -160,6 +183,7 @@ def __init__(self, year, month, day,
160183 self ._second = dt .second
161184 self ._microsecond = dt .microsecond
162185 self ._tzinfo = dt .tzinfo
186+ self ._fold = getattr (dt , 'fold' , fold )
163187
164188 self ._timestamp = None
165189 self ._int_timestamp = None
@@ -337,7 +361,7 @@ def create(cls, year=None, month=None, day=None,
337361 now = cls .get_test_now ().in_tz (tz )
338362 else :
339363 now = datetime .datetime .utcnow ().replace (tzinfo = UTC )
340- now = tz .convert (now , dst_rule = cls . _TRANSITION_RULE )
364+ now = tz .convert (now )
341365
342366 if year is None :
343367 year = now .year
@@ -510,6 +534,10 @@ def microsecond(self):
510534 def tzinfo (self ):
511535 return self ._tzinfo
512536
537+ @property
538+ def fold (self ):
539+ return self ._fold
540+
513541 @property
514542 def timestamp (self ):
515543 if self ._timestamp is None :
@@ -1108,7 +1136,10 @@ def add(self, years=0, months=0, weeks=0, days=0,
11081136 if any ([years , months , weeks , days ]):
11091137 # If we specified any of years, months, weeks or days
11101138 # we will not apply the transition (if any)
1111- dt = self ._tz .convert (dt .replace (tzinfo = None ))
1139+ dt = self ._tz .convert (
1140+ dt .replace (tzinfo = None ),
1141+ dst_rule = Timezone .POST_TRANSITION
1142+ )
11121143 else :
11131144 # Else, we need to apply the transition properly (if any)
11141145 dt = self ._tz .convert (dt )
@@ -1956,7 +1987,7 @@ def __hash__(self):
19561987 def __getnewargs__ (self ):
19571988 return (self , )
19581989
1959- def _getstate (self ):
1990+ def _getstate (self , protocol = 3 ):
19601991 tz = self .timezone_name
19611992
19621993 # Fix for fixed timezones not being properly unpickled
@@ -1966,11 +1997,15 @@ def _getstate(self):
19661997 return (
19671998 self .year , self .month , self .day ,
19681999 self .hour , self .minute , self .second , self .microsecond ,
1969- tz
2000+ tz ,
2001+ self .fold
19702002 )
19712003
19722004 def __reduce__ (self ):
1973- return self .__class__ , self ._getstate ()
2005+ return self .__reduce_ex__ (2 )
2006+
2007+ def __reduce_ex__ (self , protocol ):
2008+ return self .__class__ , self ._getstate (protocol )
19742009
19752010Pendulum .min = Pendulum .instance (datetime .datetime .min .replace (tzinfo = UTC ))
19762011Pendulum .max = Pendulum .instance (datetime .datetime .max .replace (tzinfo = UTC ))
0 commit comments