22
22
_int_to_weekday , _weekday_to_int ,
23
23
_determine_offset ,
24
24
apply_index_wraps ,
25
+ roll_yearday ,
25
26
shift_month ,
26
27
BeginMixin , EndMixin ,
27
28
BaseOffset )
@@ -1943,49 +1944,12 @@ class YearEnd(EndMixin, YearOffset):
1943
1944
1944
1945
@apply_wraps
1945
1946
def apply (self , other ):
1946
- def _increment (date ):
1947
- if date .month == self .month :
1948
- _ , days_in_month = tslib .monthrange (date .year , self .month )
1949
- if date .day != days_in_month :
1950
- year = date .year
1951
- else :
1952
- year = date .year + 1
1953
- elif date .month < self .month :
1954
- year = date .year
1955
- else :
1956
- year = date .year + 1
1957
- _ , days_in_month = tslib .monthrange (year , self .month )
1958
- return datetime (year , self .month , days_in_month ,
1959
- date .hour , date .minute , date .second ,
1960
- date .microsecond )
1961
-
1962
- def _decrement (date ):
1963
- year = date .year if date .month > self .month else date .year - 1
1964
- _ , days_in_month = tslib .monthrange (year , self .month )
1965
- return datetime (year , self .month , days_in_month ,
1966
- date .hour , date .minute , date .second ,
1967
- date .microsecond )
1968
-
1969
- def _rollf (date ):
1970
- if date .month != self .month or \
1971
- date .day < tslib .monthrange (date .year , date .month )[1 ]:
1972
- date = _increment (date )
1973
- return date
1974
-
1975
- n = self .n
1976
- result = other
1977
- if n > 0 :
1978
- while n > 0 :
1979
- result = _increment (result )
1980
- n -= 1
1981
- elif n < 0 :
1982
- while n < 0 :
1983
- result = _decrement (result )
1984
- n += 1
1985
- else :
1986
- # n == 0, roll forward
1987
- result = _rollf (result )
1988
- return result
1947
+ n = roll_yearday (other , self .n , self .month , 'end' )
1948
+ year = other .year + n
1949
+ days_in_month = tslib .monthrange (year , self .month )[1 ]
1950
+ return datetime (year , self .month , days_in_month ,
1951
+ other .hour , other .minute , other .second ,
1952
+ other .microsecond )
1989
1953
1990
1954
@apply_index_wraps
1991
1955
def apply_index (self , i ):
@@ -2006,36 +1970,9 @@ class YearBegin(BeginMixin, YearOffset):
2006
1970
2007
1971
@apply_wraps
2008
1972
def apply (self , other ):
2009
- def _increment (date , n ):
2010
- year = date .year + n - 1
2011
- if date .month >= self .month :
2012
- year += 1
2013
- return datetime (year , self .month , 1 , date .hour , date .minute ,
2014
- date .second , date .microsecond )
2015
-
2016
- def _decrement (date , n ):
2017
- year = date .year + n + 1
2018
- if date .month < self .month or (date .month == self .month and
2019
- date .day == 1 ):
2020
- year -= 1
2021
- return datetime (year , self .month , 1 , date .hour , date .minute ,
2022
- date .second , date .microsecond )
2023
-
2024
- def _rollf (date ):
2025
- if (date .month != self .month ) or date .day > 1 :
2026
- date = _increment (date , 1 )
2027
- return date
2028
-
2029
- n = self .n
2030
- result = other
2031
- if n > 0 :
2032
- result = _increment (result , n )
2033
- elif n < 0 :
2034
- result = _decrement (result , n )
2035
- else :
2036
- # n == 0, roll forward
2037
- result = _rollf (result )
2038
- return result
1973
+ n = roll_yearday (other , self .n , self .month , 'start' )
1974
+ year = other .year + n
1975
+ return other .replace (year = year , month = self .month , day = 1 )
2039
1976
2040
1977
@apply_index_wraps
2041
1978
def apply_index (self , i ):
0 commit comments