Skip to content

Commit 98f3937

Browse files
jbrockmendeljreback
authored andcommitted
remove unused calendar options from period_helper (#19534)
1 parent 074d881 commit 98f3937

File tree

3 files changed

+43
-81
lines changed

3 files changed

+43
-81
lines changed

pandas/_libs/src/period_helper.c

+43-76
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ static int days_in_month[2][12] = {
4747
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
4848
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
4949

50-
/* Return 1/0 iff year points to a leap year in calendar. */
51-
static int dInfoCalc_Leapyear(npy_int64 year, int calendar) {
52-
if (calendar == GREGORIAN_CALENDAR) {
53-
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
54-
} else {
55-
return (year % 4 == 0);
56-
}
50+
/* Return 1/0 iff year points to a leap year.
51+
* Assumes GREGORIAN_CALENDAR */
52+
static int dInfoCalc_Leapyear(npy_int64 year) {
53+
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
5754
}
5855

5956
/* Return the day of the week for the given absolute date. */
@@ -71,40 +68,33 @@ static int dInfoCalc_DayOfWeek(npy_int64 absdate) {
7168
static int monthToQuarter(int month) { return ((month - 1) / 3) + 1; }
7269

7370
/* Return the year offset, that is the absolute date of the day
74-
31.12.(year-1) in the given calendar.
71+
31.12.(year-1)
72+
73+
Assumes GREGORIAN_CALENDAR
74+
75+
This is equivalent to:
76+
77+
(datetime(year, 1, 1) - datetime(1970, 1, 1)).days
7578
7679
Note:
7780
For the Julian calendar we shift the absdate (which is measured
7881
using the Gregorian Epoch) value by two days because the Epoch
7982
(0001-01-01) in the Julian calendar lies 2 days before the Epoch in
8083
the Gregorian calendar. */
81-
static int dInfoCalc_YearOffset(npy_int64 year, int calendar) {
84+
static int dInfoCalc_YearOffset(npy_int64 year) {
8285
year--;
83-
if (calendar == GREGORIAN_CALENDAR) {
84-
if (year >= 0 || -1 / 4 == -1)
85-
return year * 365 + year / 4 - year / 100 + year / 400;
86-
else
87-
return year * 365 + (year - 3) / 4 - (year - 99) / 100 +
86+
if (year >= 0 || -1 / 4 == -1)
87+
return year * 365 + year / 4 - year / 100 + year / 400;
88+
else
89+
return year * 365 + (year - 3) / 4 - (year - 99) / 100 +
8890
(year - 399) / 400;
89-
} else if (calendar == JULIAN_CALENDAR) {
90-
if (year >= 0 || -1 / 4 == -1)
91-
return year * 365 + year / 4 - 2;
92-
else
93-
return year * 365 + (year - 3) / 4 - 2;
94-
}
95-
Py_Error(PyExc_ValueError, "unknown calendar");
96-
onError:
97-
return INT_ERR_CODE;
9891
}
9992

100-
/* Set the instance's value using the given date and time. calendar may be set
101-
* to the flags: GREGORIAN_CALENDAR, JULIAN_CALENDAR to indicate the calendar
102-
* to be used. */
103-
93+
/* Set the instance's value using the given date and time.
94+
* Assumes GREGORIAN_CALENDAR */
10495
static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
10596
int month, int day, int hour,
106-
int minute, double second,
107-
int calendar) {
97+
int minute, double second) {
10898
/* Calculate the absolute date */
10999
{
110100
int leap;
@@ -116,7 +106,7 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
116106
PyExc_ValueError, "year out of range: %i", year);
117107

118108
/* Is it a leap year ? */
119-
leap = dInfoCalc_Leapyear(year, calendar);
109+
leap = dInfoCalc_Leapyear(year);
120110

121111
/* Negative month values indicate months relative to the years end */
122112
if (month < 0) month += 13;
@@ -128,7 +118,7 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
128118
Py_AssertWithArg(day >= 1 && day <= days_in_month[leap][month - 1],
129119
PyExc_ValueError, "day out of range: %i", day);
130120

131-
yearoffset = dInfoCalc_YearOffset(year, calendar);
121+
yearoffset = dInfoCalc_YearOffset(year);
132122
if (yearoffset == INT_ERR_CODE) goto onError;
133123

134124
absdate = day + month_offset[leap][month - 1] + yearoffset;
@@ -142,8 +132,6 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
142132

143133
dinfo->day_of_week = dInfoCalc_DayOfWeek(absdate);
144134
dinfo->day_of_year = (short)(absdate - yearoffset);
145-
146-
dinfo->calendar = calendar;
147135
}
148136

149137
/* Calculate the absolute time */
@@ -171,33 +159,27 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
171159
return INT_ERR_CODE;
172160
}
173161

174-
/* Sets the date part of the date_info struct using the indicated
175-
calendar.
162+
/* Sets the date part of the date_info struct
163+
Assumes GREGORIAN_CALENDAR
176164
177165
XXX This could also be done using some integer arithmetics rather
178166
than with this iterative approach... */
179167
static int dInfoCalc_SetFromAbsDate(register struct date_info *dinfo,
180-
npy_int64 absdate, int calendar) {
168+
npy_int64 absdate) {
181169
register npy_int64 year;
182170
npy_int64 yearoffset;
183171
int leap, dayoffset;
184172
int *monthoffset;
185173

186174
/* Approximate year */
187-
if (calendar == GREGORIAN_CALENDAR) {
188-
year = (npy_int64)(((double)absdate) / 365.2425);
189-
} else if (calendar == JULIAN_CALENDAR) {
190-
year = (npy_int64)(((double)absdate) / 365.25);
191-
} else {
192-
Py_Error(PyExc_ValueError, "unknown calendar");
193-
}
175+
year = (npy_int64)(((double)absdate) / 365.2425);
194176

195177
if (absdate > 0) year++;
196178

197179
/* Apply corrections to reach the correct year */
198180
while (1) {
199181
/* Calculate the year offset */
200-
yearoffset = dInfoCalc_YearOffset(year, calendar);
182+
yearoffset = dInfoCalc_YearOffset(year);
201183
if (yearoffset == INT_ERR_CODE) goto onError;
202184

203185
/* Backward correction: absdate must be greater than the
@@ -208,7 +190,7 @@ static int dInfoCalc_SetFromAbsDate(register struct date_info *dinfo,
208190
}
209191

210192
dayoffset = absdate - yearoffset;
211-
leap = dInfoCalc_Leapyear(year, calendar);
193+
leap = dInfoCalc_Leapyear(year);
212194

213195
/* Forward correction: non leap years only have 365 days */
214196
if (dayoffset > 365 && !leap) {
@@ -219,7 +201,6 @@ static int dInfoCalc_SetFromAbsDate(register struct date_info *dinfo,
219201
}
220202

221203
dinfo->year = year;
222-
dinfo->calendar = calendar;
223204

224205
/* Now iterate to find the month */
225206
monthoffset = month_offset[leap];
@@ -410,8 +391,7 @@ static npy_int64 DtoB_WeekendToFriday(npy_int64 absdate, int day_of_week) {
410391

411392
static npy_int64 absdate_from_ymd(int y, int m, int d) {
412393
struct date_info tempDate;
413-
if (dInfoCalc_SetFromDateAndTime(&tempDate, y, m, d, 0, 0, 0,
414-
GREGORIAN_CALENDAR)) {
394+
if (dInfoCalc_SetFromDateAndTime(&tempDate, y, m, d, 0, 0, 0)) {
415395
return INT_ERR_CODE;
416396
}
417397
return tempDate.absdate;
@@ -423,8 +403,7 @@ static npy_int64 asfreq_DTtoA(npy_int64 ordinal, char relation,
423403
asfreq_info *af_info) {
424404
struct date_info dinfo;
425405
ordinal = downsample_daytime(ordinal, af_info, 0);
426-
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
427-
GREGORIAN_CALENDAR))
406+
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET))
428407
return INT_ERR_CODE;
429408
if (dinfo.month > af_info->to_a_year_end) {
430409
return (npy_int64)(dinfo.year + 1 - BASE_YEAR);
@@ -436,8 +415,7 @@ static npy_int64 asfreq_DTtoA(npy_int64 ordinal, char relation,
436415
static npy_int64 DtoQ_yq(npy_int64 ordinal, asfreq_info *af_info, int *year,
437416
int *quarter) {
438417
struct date_info dinfo;
439-
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
440-
GREGORIAN_CALENDAR))
418+
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET))
441419
return INT_ERR_CODE;
442420
if (af_info->to_q_year_end != 12) {
443421
dinfo.month -= af_info->to_q_year_end;
@@ -474,8 +452,7 @@ static npy_int64 asfreq_DTtoM(npy_int64 ordinal, char relation,
474452

475453
ordinal = downsample_daytime(ordinal, af_info, 0);
476454

477-
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
478-
GREGORIAN_CALENDAR))
455+
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET))
479456
return INT_ERR_CODE;
480457
return (npy_int64)((dinfo.year - BASE_YEAR) * 12 + dinfo.month - 1);
481458
}
@@ -493,8 +470,7 @@ static npy_int64 asfreq_DTtoB(npy_int64 ordinal, char relation,
493470

494471
ordinal = downsample_daytime(ordinal, af_info, 0);
495472

496-
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
497-
GREGORIAN_CALENDAR))
473+
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET))
498474
return INT_ERR_CODE;
499475

500476
if (relation == 'S') {
@@ -595,8 +571,7 @@ static npy_int64 asfreq_WtoB(npy_int64 ordinal, char relation,
595571
asfreq_info *af_info) {
596572
struct date_info dinfo;
597573
if (dInfoCalc_SetFromAbsDate(
598-
&dinfo, asfreq_WtoDT(ordinal, relation, af_info) + ORD_OFFSET,
599-
GREGORIAN_CALENDAR))
574+
&dinfo, asfreq_WtoDT(ordinal, relation, af_info) + ORD_OFFSET))
600575
return INT_ERR_CODE;
601576

602577
if (relation == 'S') {
@@ -655,8 +630,7 @@ static npy_int64 asfreq_MtoB(npy_int64 ordinal, char relation,
655630
struct date_info dinfo;
656631

657632
if (dInfoCalc_SetFromAbsDate(
658-
&dinfo, asfreq_MtoDT(ordinal, relation, af_info) + ORD_OFFSET,
659-
GREGORIAN_CALENDAR))
633+
&dinfo, asfreq_MtoDT(ordinal, relation, af_info) + ORD_OFFSET))
660634
return INT_ERR_CODE;
661635

662636
if (relation == 'S') {
@@ -731,8 +705,7 @@ static npy_int64 asfreq_QtoB(npy_int64 ordinal, char relation,
731705
asfreq_info *af_info) {
732706
struct date_info dinfo;
733707
if (dInfoCalc_SetFromAbsDate(
734-
&dinfo, asfreq_QtoDT(ordinal, relation, af_info) + ORD_OFFSET,
735-
GREGORIAN_CALENDAR))
708+
&dinfo, asfreq_QtoDT(ordinal, relation, af_info) + ORD_OFFSET))
736709
return INT_ERR_CODE;
737710

738711
if (relation == 'S') {
@@ -803,8 +776,7 @@ static npy_int64 asfreq_AtoB(npy_int64 ordinal, char relation,
803776
asfreq_info *af_info) {
804777
struct date_info dinfo;
805778
if (dInfoCalc_SetFromAbsDate(
806-
&dinfo, asfreq_AtoDT(ordinal, relation, af_info) + ORD_OFFSET,
807-
GREGORIAN_CALENDAR))
779+
&dinfo, asfreq_AtoDT(ordinal, relation, af_info) + ORD_OFFSET))
808780
return INT_ERR_CODE;
809781

810782
if (relation == 'S') {
@@ -1096,19 +1068,17 @@ static int dInfoCalc_SetFromAbsTime(struct date_info *dinfo, double abstime) {
10961068
return 0;
10971069
}
10981070

1099-
/* Set the instance's value using the given date and time. calendar
1100-
may be set to the flags: GREGORIAN_CALENDAR, JULIAN_CALENDAR to
1101-
indicate the calendar to be used. */
1071+
/* Set the instance's value using the given date and time.
1072+
Assumes GREGORIAN_CALENDAR. */
11021073
static int dInfoCalc_SetFromAbsDateTime(struct date_info *dinfo,
1103-
npy_int64 absdate, double abstime,
1104-
int calendar) {
1074+
npy_int64 absdate, double abstime) {
11051075
/* Bounds check */
11061076
Py_AssertWithArg(abstime >= 0.0 && abstime <= SECONDS_PER_DAY,
11071077
PyExc_ValueError,
11081078
"abstime out of range (0.0 - 86400.0): %f", abstime);
11091079

11101080
/* Calculate the date */
1111-
if (dInfoCalc_SetFromAbsDate(dinfo, absdate, calendar)) goto onError;
1081+
if (dInfoCalc_SetFromAbsDate(dinfo, absdate)) goto onError;
11121082

11131083
/* Calculate the time */
11141084
if (dInfoCalc_SetFromAbsTime(dinfo, abstime)) goto onError;
@@ -1356,8 +1326,7 @@ static int _ISOWeek(struct date_info *dinfo) {
13561326
/* Verify */
13571327
if (week < 0) {
13581328
/* The day lies in last week of the previous year */
1359-
if ((week > -2) || (week == -2 && dInfoCalc_Leapyear(dinfo->year - 1,
1360-
dinfo->calendar)))
1329+
if ((week > -2) || (week == -2 && dInfoCalc_Leapyear(dinfo->year - 1)))
13611330
week = 53;
13621331
else
13631332
week = 52;
@@ -1384,8 +1353,7 @@ int get_date_info(npy_int64 ordinal, int freq, struct date_info *dinfo) {
13841353
absdate += 1;
13851354
}
13861355

1387-
if (dInfoCalc_SetFromAbsDateTime(dinfo, absdate, abstime,
1388-
GREGORIAN_CALENDAR))
1356+
if (dInfoCalc_SetFromAbsDateTime(dinfo, absdate, abstime))
13891357
return INT_ERR_CODE;
13901358

13911359
return 0;
@@ -1480,7 +1448,6 @@ int pdays_in_month(npy_int64 ordinal, int freq) {
14801448
if (get_date_info(ordinal, freq, &dinfo) == INT_ERR_CODE)
14811449
return INT_ERR_CODE;
14821450

1483-
days = days_in_month[dInfoCalc_Leapyear(dinfo.year, dinfo.calendar)]
1484-
[dinfo.month - 1];
1451+
days = days_in_month[dInfoCalc_Leapyear(dinfo.year)][dinfo.month - 1];
14851452
return days;
14861453
}

pandas/_libs/src/period_helper.h

-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ frequency conversion routines.
2424
* declarations from period here
2525
*/
2626

27-
#define GREGORIAN_CALENDAR 0
28-
#define JULIAN_CALENDAR 1
29-
3027
#define SECONDS_PER_DAY ((double)86400.0)
3128

3229
#define Py_AssertWithArg(x, errortype, errorstr, a1) \
@@ -138,7 +135,6 @@ typedef struct date_info {
138135
int year;
139136
int day_of_week;
140137
int day_of_year;
141-
int calendar;
142138
} date_info;
143139

144140
typedef npy_int64 (*freq_conv_func)(npy_int64, char, asfreq_info *);

pandas/_libs/tslibs/period.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ cdef extern from "period_helper.h":
5959
int year
6060
int day_of_week
6161
int day_of_year
62-
int calendar
6362

6463
ctypedef struct asfreq_info:
6564
int from_week_end

0 commit comments

Comments
 (0)