|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# cython: profile=False |
| 3 | +# cython: boundscheck=False |
| 4 | +""" |
| 5 | +Cython implementations of functions resembling the stdlib calendar module |
| 6 | +""" |
| 7 | + |
| 8 | +cimport cython |
| 9 | +from cython cimport Py_ssize_t |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +cimport numpy as np |
| 13 | +from numpy cimport int64_t, int32_t |
| 14 | +np.import_array() |
| 15 | + |
| 16 | + |
| 17 | +# ---------------------------------------------------------------------- |
| 18 | +# Constants |
| 19 | + |
| 20 | +# Slightly more performant cython lookups than a 2D table |
| 21 | +# The first 12 entries correspond to month lengths for non-leap years. |
| 22 | +# The remaining 12 entries give month lengths for leap years |
| 23 | +cdef int32_t* days_per_month_array = [ |
| 24 | + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, |
| 25 | + 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 26 | + |
| 27 | +cdef int* sakamoto_arr = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4] |
| 28 | + |
| 29 | +# The first 13 entries give the month days elapsed as of the first of month N |
| 30 | +# (or the total number of days in the year for N=13) in non-leap years. |
| 31 | +# The remaining 13 entries give the days elapsed in leap years. |
| 32 | +cdef int32_t* _month_offset = [ |
| 33 | + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, |
| 34 | + 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] |
| 35 | + |
| 36 | +# ---------------------------------------------------------------------- |
| 37 | + |
| 38 | + |
| 39 | +@cython.wraparound(False) |
| 40 | +@cython.boundscheck(False) |
| 41 | +cpdef inline int32_t get_days_in_month(int year, Py_ssize_t month) nogil: |
| 42 | + """Return the number of days in the given month of the given year. |
| 43 | +
|
| 44 | + Parameters |
| 45 | + ---------- |
| 46 | + year : int |
| 47 | + month : int |
| 48 | +
|
| 49 | + Returns |
| 50 | + ------- |
| 51 | + days_in_month : int |
| 52 | +
|
| 53 | + Notes |
| 54 | + ----- |
| 55 | + Assumes that the arguments are valid. Passing a month not between 1 and 12 |
| 56 | + risks a segfault. |
| 57 | + """ |
| 58 | + return days_per_month_array[12 * is_leapyear(year) + month - 1] |
| 59 | + |
| 60 | + |
| 61 | +@cython.wraparound(False) |
| 62 | +@cython.boundscheck(False) |
| 63 | +@cython.cdivision |
| 64 | +cdef int dayofweek(int y, int m, int d) nogil: |
| 65 | + """Find the day of week for the date described by the Y/M/D triple y, m, d |
| 66 | + using Sakamoto's method, from wikipedia. |
| 67 | +
|
| 68 | + 0 represents Monday. See [1]_. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + y : int |
| 73 | + m : int |
| 74 | + d : int |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + weekday : int |
| 79 | +
|
| 80 | + Notes |
| 81 | + ----- |
| 82 | + Assumes that y, m, d, represents a valid date. |
| 83 | +
|
| 84 | + See Also |
| 85 | + -------- |
| 86 | + [1] https://docs.python.org/3.6/library/calendar.html#calendar.weekday |
| 87 | +
|
| 88 | + [2] https://en.wikipedia.org/wiki/\ |
| 89 | + Determination_of_the_day_of_the_week#Sakamoto.27s_methods |
| 90 | + """ |
| 91 | + cdef: |
| 92 | + int day |
| 93 | + |
| 94 | + y -= m < 3 |
| 95 | + day = (y + y / 4 - y / 100 + y / 400 + sakamoto_arr[m - 1] + d) % 7 |
| 96 | + # convert to python day |
| 97 | + return (day + 6) % 7 |
| 98 | + |
| 99 | + |
| 100 | +cdef bint is_leapyear(int64_t year) nogil: |
| 101 | + """Returns 1 if the given year is a leap year, 0 otherwise. |
| 102 | +
|
| 103 | + Parameters |
| 104 | + ---------- |
| 105 | + year : int |
| 106 | +
|
| 107 | + Returns |
| 108 | + ------- |
| 109 | + is_leap : bool |
| 110 | + """ |
| 111 | + return ((year & 0x3) == 0 and # year % 4 == 0 |
| 112 | + ((year % 100) != 0 or (year % 400) == 0)) |
| 113 | + |
| 114 | + |
| 115 | +@cython.wraparound(False) |
| 116 | +@cython.boundscheck(False) |
| 117 | +cpdef int32_t get_week_of_year(int year, int month, int day) nogil: |
| 118 | + """Return the ordinal week-of-year for the given day. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + year : int |
| 123 | + month : int |
| 124 | + day : int |
| 125 | +
|
| 126 | + Returns |
| 127 | + ------- |
| 128 | + week_of_year : int32_t |
| 129 | +
|
| 130 | + Notes |
| 131 | + ----- |
| 132 | + Assumes the inputs describe a valid date. |
| 133 | + """ |
| 134 | + cdef: |
| 135 | + bint isleap, isleap_prev |
| 136 | + int32_t mo_off |
| 137 | + int32_t doy, dow |
| 138 | + int woy |
| 139 | + |
| 140 | + isleap = is_leapyear(year) |
| 141 | + isleap_prev = is_leapyear(year - 1) |
| 142 | + |
| 143 | + mo_off = _month_offset[isleap * 13 + month - 1] |
| 144 | + |
| 145 | + doy = mo_off + day |
| 146 | + dow = dayofweek(year, month, day) |
| 147 | + |
| 148 | + # estimate |
| 149 | + woy = (doy - 1) - dow + 3 |
| 150 | + if woy >= 0: |
| 151 | + woy = woy / 7 + 1 |
| 152 | + |
| 153 | + # verify |
| 154 | + if woy < 0: |
| 155 | + if (woy > -2) or (woy == -2 and isleap_prev): |
| 156 | + woy = 53 |
| 157 | + else: |
| 158 | + woy = 52 |
| 159 | + elif woy == 53: |
| 160 | + if 31 - day + dow < 3: |
| 161 | + woy = 1 |
| 162 | + |
| 163 | + return woy |
0 commit comments