Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/src/datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ cdef extern from "datetime/np_datetime.h":
void pandas_datetime_to_datetimestruct(npy_datetime val,
PANDAS_DATETIMEUNIT fr,
pandas_datetimestruct *result)
int _days_per_month_table[2][12]
int days_per_month_table[2][12]

int dayofweek(int y, int m, int d)
int is_leapyear(int64_t year)
Expand Down
2 changes: 1 addition & 1 deletion pandas/src/datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ def monthrange(int64_t year, int64_t month):
if month < 1 or month > 12:
raise ValueError("bad month number 0; must be 1-12")

days = _days_per_month_table[is_leapyear(year)][month-1]
days = days_per_month_table[is_leapyear(year)][month-1]

return (dayofweek(year, month, 1), days)

Expand Down
24 changes: 15 additions & 9 deletions pandas/src/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
#endif

const int days_per_month_table[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};

/*
* Returns 1 if the given year is a leap year, 0 otherwise.
*/
Expand All @@ -52,7 +57,7 @@ int is_leapyear(npy_int64 year)
int dayofweek(int y, int m, int d)
{
int day;
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
static const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
day = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
// convert to python day
Expand Down Expand Up @@ -97,12 +102,12 @@ add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes)
dts->month = 12;
}
isleap = is_leapyear(dts->year);
dts->day += _days_per_month_table[isleap][dts->month-1];
dts->day += days_per_month_table[isleap][dts->month-1];
}
else if (dts->day > 28) {
isleap = is_leapyear(dts->year);
if (dts->day > _days_per_month_table[isleap][dts->month-1]) {
dts->day -= _days_per_month_table[isleap][dts->month-1];
if (dts->day > days_per_month_table[isleap][dts->month-1]) {
dts->day -= days_per_month_table[isleap][dts->month-1];
dts->month++;
if (dts->month > 12) {
dts->year++;
Expand All @@ -120,7 +125,7 @@ get_datetimestruct_days(const pandas_datetimestruct *dts)
{
int i, month;
npy_int64 year, days = 0;
int *month_lengths;
const int *month_lengths;

year = dts->year - 1970;
days = year * 365;
Expand Down Expand Up @@ -160,7 +165,7 @@ get_datetimestruct_days(const pandas_datetimestruct *dts)
days += year / 400;
}

month_lengths = _days_per_month_table[is_leapyear(dts->year)];
month_lengths = days_per_month_table[is_leapyear(dts->year)];
month = dts->month - 1;

/* Add the months */
Expand Down Expand Up @@ -250,10 +255,11 @@ add_seconds_to_datetimestruct(pandas_datetimestruct *dts, int seconds)
static void
set_datetimestruct_days(npy_int64 days, pandas_datetimestruct *dts)
{
int *month_lengths, i;
const int *month_lengths;
int i;

dts->year = days_to_yearsdays(&days);
month_lengths = _days_per_month_table[is_leapyear(dts->year)];
month_lengths = days_per_month_table[is_leapyear(dts->year)];

for (i = 0; i < 12; ++i) {
if (days < month_lengths[i]) {
Expand Down Expand Up @@ -348,7 +354,7 @@ convert_pydatetime_to_datetimestruct(PyObject *obj, pandas_datetimestruct *out,
}
isleap = is_leapyear(out->year);
if (out->day < 1 ||
out->day > _days_per_month_table[isleap][out->month-1]) {
out->day > days_per_month_table[isleap][out->month-1]) {
goto invalid_date;
}

Expand Down
5 changes: 1 addition & 4 deletions pandas/src/datetime/np_datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ void pandas_datetime_to_datetimestruct(npy_datetime val, PANDAS_DATETIMEUNIT fr,

int dayofweek(int y, int m, int d);

static int _days_per_month_table[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
extern const int days_per_month_table[2][12];

// stuff numpy-derived code needs in header
// ----------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions pandas/src/datetime/np_datetime_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef time_t NPY_TIME_T;
/*// linux complains.*/
/*static void _suppress_unused_variable_warning(void)*/
/*{*/
/* int x = _days_per_month_table[0][0];*/
/* int x = days_per_month_table[0][0];*/
/* x = x;*/

/* int y = _month_offset[0][0];*/
Expand Down Expand Up @@ -229,6 +229,7 @@ convert_datetimestruct_utc_to_local(pandas_datetimestruct *out_dts_local,
return 0;
}

#if 0
/*
* Converts a datetimestruct in local time to a datetimestruct in UTC.
*
Expand Down Expand Up @@ -303,6 +304,7 @@ convert_datetimestruct_local_to_utc(pandas_datetimestruct *out_dts_utc,

return 0;
}
#endif

/* int */
/* parse_python_string(PyObject* obj, pandas_datetimestruct *dts) { */
Expand Down Expand Up @@ -593,7 +595,7 @@ parse_iso_8601_datetime(char *str, int len,
out->day = 10 * (substr[0] - '0') + (substr[1] - '0');

if (out->day < 1 ||
out->day > _days_per_month_table[year_leap][out->month-1]) {
out->day > days_per_month_table[year_leap][out->month-1]) {
PyErr_Format(PyExc_ValueError,
"Day out of range in datetime string \"%s\"", str);
goto error;
Expand Down
2 changes: 1 addition & 1 deletion pandas/src/numpy_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ infer_type(PyObject* obj) {
}

PANDAS_INLINE npy_int64
get_nat() {
get_nat(void) {
return NPY_MIN_INT64;
}

Expand Down
8 changes: 4 additions & 4 deletions pandas/src/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ cdef class MonthOffset(_Offset):
self.m -= 12
self.y += 1
self.ly = is_leapyear(self.y)
days += _days_per_month_table[self.ly][self.m]
days += days_per_month_table[self.ly][self.m]
self.m += 1

self.t += days * us_in_day
Expand All @@ -238,7 +238,7 @@ cdef class MonthOffset(_Offset):
self.m += 12
self.y -= 1
self.ly = is_leapyear(self.y)
days += _days_per_month_table[self.ly][self.m]
days += days_per_month_table[self.ly][self.m]

self.t -= days * us_in_day

Expand Down Expand Up @@ -286,7 +286,7 @@ cdef class DayOfMonthOffset(_Offset):
cdef:
int64_t tmp, days

days = _days_per_month_table[self.ly][self.m]
days = days_per_month_table[self.ly][self.m]
self.t += days * us_in_day
self.dow = (self.dow + days) % 7

Expand All @@ -300,7 +300,7 @@ cdef class DayOfMonthOffset(_Offset):
cdef:
int64_t tmp, days

days = _days_per_month_table[self.ly][(self.m - 1) % 12]
days = days_per_month_table[self.ly][(self.m - 1) % 12]
self.t -= days * us_in_day
self.dow = (self.dow - days) % 7

Expand Down
6 changes: 3 additions & 3 deletions pandas/src/period.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,10 @@ npy_int64 get_period_ordinal(int year, int month, int day,
int hour, int minute, int second,
int freq)
{
npy_int64 absdays, delta;
npy_int64 absdays, delta;
npy_int64 weeks, days;
npy_int64 adj_ordinal, ordinal, day_adj;
int freq_group, fmonth, mdiff, quarter;
npy_int64 ordinal, day_adj;
int freq_group, fmonth, mdiff;
freq_group = get_freq_group(freq);

if (freq == FR_SEC) {
Expand Down