@@ -47,13 +47,10 @@ static int days_in_month[2][12] = {
47
47
{31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 },
48
48
{31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }};
49
49
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 ));
57
54
}
58
55
59
56
/* Return the day of the week for the given absolute date. */
@@ -71,40 +68,33 @@ static int dInfoCalc_DayOfWeek(npy_int64 absdate) {
71
68
static int monthToQuarter (int month ) { return ((month - 1 ) / 3 ) + 1 ; }
72
69
73
70
/* 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
75
78
76
79
Note:
77
80
For the Julian calendar we shift the absdate (which is measured
78
81
using the Gregorian Epoch) value by two days because the Epoch
79
82
(0001-01-01) in the Julian calendar lies 2 days before the Epoch in
80
83
the Gregorian calendar. */
81
- static int dInfoCalc_YearOffset (npy_int64 year , int calendar ) {
84
+ static int dInfoCalc_YearOffset (npy_int64 year ) {
82
85
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 +
88
90
(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 ;
98
91
}
99
92
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 */
104
95
static int dInfoCalc_SetFromDateAndTime (struct date_info * dinfo , int year ,
105
96
int month , int day , int hour ,
106
- int minute , double second ,
107
- int calendar ) {
97
+ int minute , double second ) {
108
98
/* Calculate the absolute date */
109
99
{
110
100
int leap ;
@@ -116,7 +106,7 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
116
106
PyExc_ValueError , "year out of range: %i" , year );
117
107
118
108
/* Is it a leap year ? */
119
- leap = dInfoCalc_Leapyear (year , calendar );
109
+ leap = dInfoCalc_Leapyear (year );
120
110
121
111
/* Negative month values indicate months relative to the years end */
122
112
if (month < 0 ) month += 13 ;
@@ -128,7 +118,7 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
128
118
Py_AssertWithArg (day >= 1 && day <= days_in_month [leap ][month - 1 ],
129
119
PyExc_ValueError , "day out of range: %i" , day );
130
120
131
- yearoffset = dInfoCalc_YearOffset (year , calendar );
121
+ yearoffset = dInfoCalc_YearOffset (year );
132
122
if (yearoffset == INT_ERR_CODE ) goto onError ;
133
123
134
124
absdate = day + month_offset [leap ][month - 1 ] + yearoffset ;
@@ -142,8 +132,6 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
142
132
143
133
dinfo -> day_of_week = dInfoCalc_DayOfWeek (absdate );
144
134
dinfo -> day_of_year = (short )(absdate - yearoffset );
145
-
146
- dinfo -> calendar = calendar ;
147
135
}
148
136
149
137
/* Calculate the absolute time */
@@ -171,33 +159,27 @@ static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
171
159
return INT_ERR_CODE ;
172
160
}
173
161
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
176
164
177
165
XXX This could also be done using some integer arithmetics rather
178
166
than with this iterative approach... */
179
167
static int dInfoCalc_SetFromAbsDate (register struct date_info * dinfo ,
180
- npy_int64 absdate , int calendar ) {
168
+ npy_int64 absdate ) {
181
169
register npy_int64 year ;
182
170
npy_int64 yearoffset ;
183
171
int leap , dayoffset ;
184
172
int * monthoffset ;
185
173
186
174
/* 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 );
194
176
195
177
if (absdate > 0 ) year ++ ;
196
178
197
179
/* Apply corrections to reach the correct year */
198
180
while (1 ) {
199
181
/* Calculate the year offset */
200
- yearoffset = dInfoCalc_YearOffset (year , calendar );
182
+ yearoffset = dInfoCalc_YearOffset (year );
201
183
if (yearoffset == INT_ERR_CODE ) goto onError ;
202
184
203
185
/* Backward correction: absdate must be greater than the
@@ -208,7 +190,7 @@ static int dInfoCalc_SetFromAbsDate(register struct date_info *dinfo,
208
190
}
209
191
210
192
dayoffset = absdate - yearoffset ;
211
- leap = dInfoCalc_Leapyear (year , calendar );
193
+ leap = dInfoCalc_Leapyear (year );
212
194
213
195
/* Forward correction: non leap years only have 365 days */
214
196
if (dayoffset > 365 && !leap ) {
@@ -219,7 +201,6 @@ static int dInfoCalc_SetFromAbsDate(register struct date_info *dinfo,
219
201
}
220
202
221
203
dinfo -> year = year ;
222
- dinfo -> calendar = calendar ;
223
204
224
205
/* Now iterate to find the month */
225
206
monthoffset = month_offset [leap ];
@@ -410,8 +391,7 @@ static npy_int64 DtoB_WeekendToFriday(npy_int64 absdate, int day_of_week) {
410
391
411
392
static npy_int64 absdate_from_ymd (int y , int m , int d ) {
412
393
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 )) {
415
395
return INT_ERR_CODE ;
416
396
}
417
397
return tempDate .absdate ;
@@ -423,8 +403,7 @@ static npy_int64 asfreq_DTtoA(npy_int64 ordinal, char relation,
423
403
asfreq_info * af_info ) {
424
404
struct date_info dinfo ;
425
405
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 ))
428
407
return INT_ERR_CODE ;
429
408
if (dinfo .month > af_info -> to_a_year_end ) {
430
409
return (npy_int64 )(dinfo .year + 1 - BASE_YEAR );
@@ -436,8 +415,7 @@ static npy_int64 asfreq_DTtoA(npy_int64 ordinal, char relation,
436
415
static npy_int64 DtoQ_yq (npy_int64 ordinal , asfreq_info * af_info , int * year ,
437
416
int * quarter ) {
438
417
struct date_info dinfo ;
439
- if (dInfoCalc_SetFromAbsDate (& dinfo , ordinal + ORD_OFFSET ,
440
- GREGORIAN_CALENDAR ))
418
+ if (dInfoCalc_SetFromAbsDate (& dinfo , ordinal + ORD_OFFSET ))
441
419
return INT_ERR_CODE ;
442
420
if (af_info -> to_q_year_end != 12 ) {
443
421
dinfo .month -= af_info -> to_q_year_end ;
@@ -474,8 +452,7 @@ static npy_int64 asfreq_DTtoM(npy_int64 ordinal, char relation,
474
452
475
453
ordinal = downsample_daytime (ordinal , af_info , 0 );
476
454
477
- if (dInfoCalc_SetFromAbsDate (& dinfo , ordinal + ORD_OFFSET ,
478
- GREGORIAN_CALENDAR ))
455
+ if (dInfoCalc_SetFromAbsDate (& dinfo , ordinal + ORD_OFFSET ))
479
456
return INT_ERR_CODE ;
480
457
return (npy_int64 )((dinfo .year - BASE_YEAR ) * 12 + dinfo .month - 1 );
481
458
}
@@ -493,8 +470,7 @@ static npy_int64 asfreq_DTtoB(npy_int64 ordinal, char relation,
493
470
494
471
ordinal = downsample_daytime (ordinal , af_info , 0 );
495
472
496
- if (dInfoCalc_SetFromAbsDate (& dinfo , ordinal + ORD_OFFSET ,
497
- GREGORIAN_CALENDAR ))
473
+ if (dInfoCalc_SetFromAbsDate (& dinfo , ordinal + ORD_OFFSET ))
498
474
return INT_ERR_CODE ;
499
475
500
476
if (relation == 'S' ) {
@@ -595,8 +571,7 @@ static npy_int64 asfreq_WtoB(npy_int64 ordinal, char relation,
595
571
asfreq_info * af_info ) {
596
572
struct date_info dinfo ;
597
573
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 ))
600
575
return INT_ERR_CODE ;
601
576
602
577
if (relation == 'S' ) {
@@ -655,8 +630,7 @@ static npy_int64 asfreq_MtoB(npy_int64 ordinal, char relation,
655
630
struct date_info dinfo ;
656
631
657
632
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 ))
660
634
return INT_ERR_CODE ;
661
635
662
636
if (relation == 'S' ) {
@@ -731,8 +705,7 @@ static npy_int64 asfreq_QtoB(npy_int64 ordinal, char relation,
731
705
asfreq_info * af_info ) {
732
706
struct date_info dinfo ;
733
707
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 ))
736
709
return INT_ERR_CODE ;
737
710
738
711
if (relation == 'S' ) {
@@ -803,8 +776,7 @@ static npy_int64 asfreq_AtoB(npy_int64 ordinal, char relation,
803
776
asfreq_info * af_info ) {
804
777
struct date_info dinfo ;
805
778
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 ))
808
780
return INT_ERR_CODE ;
809
781
810
782
if (relation == 'S' ) {
@@ -1096,19 +1068,17 @@ static int dInfoCalc_SetFromAbsTime(struct date_info *dinfo, double abstime) {
1096
1068
return 0 ;
1097
1069
}
1098
1070
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. */
1102
1073
static int dInfoCalc_SetFromAbsDateTime (struct date_info * dinfo ,
1103
- npy_int64 absdate , double abstime ,
1104
- int calendar ) {
1074
+ npy_int64 absdate , double abstime ) {
1105
1075
/* Bounds check */
1106
1076
Py_AssertWithArg (abstime >= 0.0 && abstime <= SECONDS_PER_DAY ,
1107
1077
PyExc_ValueError ,
1108
1078
"abstime out of range (0.0 - 86400.0): %f" , abstime );
1109
1079
1110
1080
/* Calculate the date */
1111
- if (dInfoCalc_SetFromAbsDate (dinfo , absdate , calendar )) goto onError ;
1081
+ if (dInfoCalc_SetFromAbsDate (dinfo , absdate )) goto onError ;
1112
1082
1113
1083
/* Calculate the time */
1114
1084
if (dInfoCalc_SetFromAbsTime (dinfo , abstime )) goto onError ;
@@ -1356,8 +1326,7 @@ static int _ISOWeek(struct date_info *dinfo) {
1356
1326
/* Verify */
1357
1327
if (week < 0 ) {
1358
1328
/* 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 )))
1361
1330
week = 53 ;
1362
1331
else
1363
1332
week = 52 ;
@@ -1384,8 +1353,7 @@ int get_date_info(npy_int64 ordinal, int freq, struct date_info *dinfo) {
1384
1353
absdate += 1 ;
1385
1354
}
1386
1355
1387
- if (dInfoCalc_SetFromAbsDateTime (dinfo , absdate , abstime ,
1388
- GREGORIAN_CALENDAR ))
1356
+ if (dInfoCalc_SetFromAbsDateTime (dinfo , absdate , abstime ))
1389
1357
return INT_ERR_CODE ;
1390
1358
1391
1359
return 0 ;
@@ -1480,7 +1448,6 @@ int pdays_in_month(npy_int64 ordinal, int freq) {
1480
1448
if (get_date_info (ordinal , freq , & dinfo ) == INT_ERR_CODE )
1481
1449
return INT_ERR_CODE ;
1482
1450
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 ];
1485
1452
return days ;
1486
1453
}
0 commit comments