zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# cython: boundscheck=False
"""
Cython implementations of functions resembling the stdlib calendar module
"""
 
cimport cython
from numpy cimport (
    int32_t,
    int64_t,
)
 
# ----------------------------------------------------------------------
# Constants
 
# Slightly more performant cython lookups than a 2D table
# The first 12 entries correspond to month lengths for non-leap years.
# The remaining 12 entries give month lengths for leap years
cdef int32_t* days_per_month_array = [
    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
    31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 
cdef int* sakamoto_arr = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
 
# The first 13 entries give the month days elapsed as of the first of month N
# (or the total number of days in the year for N=13) in non-leap years.
# The remaining 13 entries give the days elapsed in leap years.
cdef int32_t* month_offset = [
    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365,
    0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
 
# Canonical location for other modules to find name constants
MONTHS = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL",
          "AUG", "SEP", "OCT", "NOV", "DEC"]
# The first blank line is consistent with calendar.month_name in the calendar
# standard library
MONTHS_FULL = ["", "January", "February", "March", "April", "May", "June",
               "July", "August", "September", "October", "November",
               "December"]
MONTH_NUMBERS = {name: num for num, name in enumerate(MONTHS)}
cdef dict c_MONTH_NUMBERS = MONTH_NUMBERS
MONTH_ALIASES = {(num + 1): name for num, name in enumerate(MONTHS)}
MONTH_TO_CAL_NUM = {name: num + 1 for num, name in enumerate(MONTHS)}
 
DAYS = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
DAYS_FULL = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
             "Saturday", "Sunday"]
int_to_weekday = {num: name for num, name in enumerate(DAYS)}
weekday_to_int = {int_to_weekday[key]: key for key in int_to_weekday}
 
 
# ----------------------------------------------------------------------
 
 
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil:
    """
    Return the number of days in the given month of the given year.
 
    Parameters
    ----------
    year : int
    month : int
 
    Returns
    -------
    days_in_month : int
 
    Notes
    -----
    Assumes that the arguments are valid.  Passing a month not between 1 and 12
    risks a segfault.
    """
    return days_per_month_array[12 * is_leapyear(year) + month - 1]
 
 
@cython.wraparound(False)
@cython.boundscheck(False)
@cython.cdivision
cdef int dayofweek(int y, int m, int d) nogil:
    """
    Find the day of week for the date described by the Y/M/D triple y, m, d
    using Sakamoto's method, from wikipedia.
 
    0 represents Monday.  See [1]_.
 
    Parameters
    ----------
    y : int
    m : int
    d : int
 
    Returns
    -------
    weekday : int
 
    Notes
    -----
    Assumes that y, m, d, represents a valid date.
 
    See Also
    --------
    [1] https://docs.python.org/3/library/calendar.html#calendar.weekday
 
    [2] https://en.wikipedia.org/wiki/\
    Determination_of_the_day_of_the_week#Sakamoto.27s_methods
    """
    cdef:
        int day
 
    y -= m < 3
    day = (y + y / 4 - y / 100 + y / 400 + sakamoto_arr[m - 1] + d) % 7
    # convert to python day
    return (day + 6) % 7
 
 
cdef bint is_leapyear(int64_t year) nogil:
    """
    Returns 1 if the given year is a leap year, 0 otherwise.
 
    Parameters
    ----------
    year : int
 
    Returns
    -------
    is_leap : bool
    """
    return ((year & 0x3) == 0 and  # year % 4 == 0
            ((year % 100) != 0 or (year % 400) == 0))
 
 
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef int32_t get_week_of_year(int year, int month, int day) nogil:
    """
    Return the ordinal week-of-year for the given day.
 
    Parameters
    ----------
    year : int
    month : int
    day : int
 
    Returns
    -------
    week_of_year : int32_t
 
    Notes
    -----
    Assumes the inputs describe a valid date.
    """
    return get_iso_calendar(year, month, day)[1]
 
 
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil:
    """
    Return the year, week, and day of year corresponding to ISO 8601
 
    Parameters
    ----------
    year : int
    month : int
    day : int
 
    Returns
    -------
    year : int32_t
    week : int32_t
    day : int32_t
 
    Notes
    -----
    Assumes the inputs describe a valid date.
    """
    cdef:
        int32_t doy, dow
        int32_t iso_year, iso_week
 
    doy = get_day_of_year(year, month, day)
    dow = dayofweek(year, month, day)
 
    # estimate
    iso_week = (doy - 1) - dow + 3
    if iso_week >= 0:
        iso_week = iso_week // 7 + 1
 
    # verify
    if iso_week < 0:
        if (iso_week > -2) or (iso_week == -2 and is_leapyear(year - 1)):
            iso_week = 53
        else:
            iso_week = 52
    elif iso_week == 53:
        if 31 - day + dow < 3:
            iso_week = 1
 
    iso_year = year
    if iso_week == 1 and month == 12:
        iso_year += 1
 
    elif iso_week >= 52 and month == 1:
        iso_year -= 1
 
    return iso_year, iso_week, dow + 1
 
 
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef int32_t get_day_of_year(int year, int month, int day) nogil:
    """
    Return the ordinal day-of-year for the given day.
 
    Parameters
    ----------
    year : int
    month : int
    day : int
 
    Returns
    -------
    day_of_year : int32_t
 
    Notes
    -----
    Assumes the inputs describe a valid date.
    """
    cdef:
        bint isleap
        int32_t mo_off
        int day_of_year
 
    isleap = is_leapyear(year)
 
    mo_off = month_offset[isleap * 13 + month - 1]
 
    day_of_year = mo_off + day
    return day_of_year
 
 
# ---------------------------------------------------------------------
# Business Helpers
 
cpdef int get_lastbday(int year, int month) nogil:
    """
    Find the last day of the month that is a business day.
 
    Parameters
    ----------
    year : int
    month : int
 
    Returns
    -------
    last_bday : int
    """
    cdef:
        int wkday, days_in_month
 
    wkday = dayofweek(year, month, 1)
    days_in_month = get_days_in_month(year, month)
    return days_in_month - max(((wkday + days_in_month - 1) % 7) - 4, 0)
 
 
cpdef int get_firstbday(int year, int month) nogil:
    """
    Find the first day of the month that is a business day.
 
    Parameters
    ----------
    year : int
    month : int
 
    Returns
    -------
    first_bday : int
    """
    cdef:
        int first, wkday
 
    wkday = dayofweek(year, month, 1)
    first = 1
    if wkday == 5:  # on Saturday
        first = 3
    elif wkday == 6:  # on Sunday
        first = 2
    return first