@@ -1162,190 +1162,54 @@ char *str_replace(const char *s, const char *old, const char *new) {
1162
1162
// function to generate a nice string representation of the period
1163
1163
// object, originally from DateObject_strftime
1164
1164
1165
- char * skts_strftime (npy_int64 ordinal , int freq , PyObject * args )
1166
- {
1167
- char * orig_fmt_str , * fmt_str ;
1168
- char * result ;
1165
+ char * c_strftime (struct date_info * tmp , char * fmt ) {
1166
+ struct tm c_date ;
1167
+ char * result ;
1168
+ struct date_info dinfo = * tmp ;
1169
+ int result_len = strlen (fmt ) + 50 ;
1170
+
1171
+ c_date .tm_sec = (int )dinfo .second ;
1172
+ c_date .tm_min = dinfo .minute ;
1173
+ c_date .tm_hour = dinfo .hour ;
1174
+ c_date .tm_mday = dinfo .day ;
1175
+ c_date .tm_mon = dinfo .month - 1 ;
1176
+ c_date .tm_year = dinfo .year - 1900 ;
1177
+ c_date .tm_wday = (dinfo .day_of_week + 1 ) % 7 ;
1178
+ c_date .tm_yday = dinfo .day_of_year - 1 ;
1179
+ c_date .tm_isdst = -1 ;
1169
1180
1170
- int num_extra_fmts = 3 ;
1181
+ result = malloc ( result_len * sizeof ( char )) ;
1171
1182
1172
- char extra_fmts [3 ][2 ][10 ] = {{"%q" , "^`AB`^" },
1173
- {"%f" , "^`CD`^" },
1174
- {"%F" , "^`EF`^" }};
1183
+ strftime (result , result_len , fmt , & c_date );
1175
1184
1176
- int extra_fmts_found [3 ] = {0 ,0 ,0 };
1177
- int extra_fmts_found_one = 0 ;
1178
- struct tm c_date ;
1179
- struct date_info tempDate ;
1180
- npy_int64 absdate , daily_ord ;
1181
- double abstime ;
1182
- int i , result_len ;
1183
- PyObject * py_result ;
1185
+ return result ;
1186
+ }
1184
1187
1185
- npy_int64 ( * toDaily )( npy_int64 , char , asfreq_info * ) = NULL ;
1188
+ int get_yq ( npy_int64 ordinal , int freq , int * quarter , int * year ) {
1186
1189
asfreq_info af_info ;
1187
-
1188
- if (! PyArg_ParseTuple ( args , "s:strftime(fmt)" , & orig_fmt_str ))
1189
- return NULL ;
1190
+ int qtr_freq ;
1191
+ npy_int64 daily_ord ;
1192
+ npy_int64 ( * toDaily )( npy_int64 , char , asfreq_info * ) = NULL ;
1190
1193
1191
1194
toDaily = get_asfreq_func (freq , FR_DAY );
1192
1195
get_asfreq_info (freq , FR_DAY , & af_info );
1193
1196
1194
1197
daily_ord = toDaily (ordinal , 'E' , & af_info );
1195
- abstime = get_abs_time (freq , daily_ord , ordinal );
1196
-
1197
- if (abstime < 0 ) {
1198
- abstime += 86400 ;
1199
- daily_ord -= 1 ;
1200
- }
1201
-
1202
- /* printf("daily_ord: %d, abstime: %f \n", (int) daily_ord, abstime); */
1203
-
1204
- if (dInfoCalc_SetFromAbsDateTime (& tempDate , daily_ord + ORD_OFFSET , abstime ,
1205
- GREGORIAN_CALENDAR )) return NULL ;
1206
-
1207
- // populate standard C date struct with info from our date_info struct
1208
- c_date .tm_sec = (int )tempDate .second ;
1209
- c_date .tm_min = tempDate .minute ;
1210
- c_date .tm_hour = tempDate .hour ;
1211
- c_date .tm_mday = tempDate .day ;
1212
- c_date .tm_mon = tempDate .month - 1 ;
1213
- c_date .tm_year = tempDate .year - 1900 ;
1214
- c_date .tm_wday = (tempDate .day_of_week + 1 ) % 7 ;
1215
- c_date .tm_yday = tempDate .day_of_year - 1 ;
1216
- c_date .tm_isdst = -1 ;
1217
-
1218
- result_len = strlen (orig_fmt_str ) + 50 ;
1219
- if ((result = PyArray_malloc (result_len * sizeof (char ))) == NULL ) {
1220
- return (char * )PyErr_NoMemory ();
1221
- }
1222
-
1223
- fmt_str = orig_fmt_str ;
1224
-
1225
- // replace any special format characters with their place holder
1226
- for (i = 0 ; i < num_extra_fmts ; i ++ ) {
1227
- char * special_loc ;
1228
- if ((special_loc = strstr (fmt_str ,extra_fmts [i ][0 ])) != NULL ) {
1229
- char * tmp_str = fmt_str ;
1230
- fmt_str = str_replace (fmt_str , extra_fmts [i ][0 ],
1231
- extra_fmts [i ][1 ]);
1232
- /* only free the previous loop value if this is not the first
1233
- special format string found */
1234
- if (extra_fmts_found_one ) { free (tmp_str ); }
1235
1198
1236
- if (fmt_str == NULL ) {return NULL ;}
1199
+ if (get_freq_group (freq ) == FR_QTR ) {
1200
+ qtr_freq = freq ;
1201
+ } else { qtr_freq = FR_QTR ; }
1202
+ get_asfreq_info (FR_DAY , qtr_freq , & af_info );
1237
1203
1238
- extra_fmts_found [i ] = 1 ;
1239
- extra_fmts_found_one = 1 ;
1240
- }
1241
- }
1204
+ if (DtoQ_yq (daily_ord , & af_info , year , quarter ) == INT_ERR_CODE )
1205
+ return -1 ;
1242
1206
1243
- strftime (result , result_len , fmt_str , & c_date );
1244
- if (extra_fmts_found_one ) { free (fmt_str ); }
1245
-
1246
- // replace any place holders with the appropriate value
1247
- for (i = 0 ; i < num_extra_fmts ; i ++ ) {
1248
- if (extra_fmts_found [i ]) {
1249
- char * tmp_str = result ;
1250
- char * extra_str ;
1251
-
1252
- if (strcmp (extra_fmts [i ][0 ], "%q" ) == 0 ||
1253
- strcmp (extra_fmts [i ][0 ], "%f" ) == 0 ||
1254
- strcmp (extra_fmts [i ][0 ], "%F" ) == 0 ) {
1255
-
1256
- asfreq_info af_info ;
1257
- int qtr_freq , year , quarter , year_len ;
1258
-
1259
- if (get_freq_group (freq ) == FR_QTR ) {
1260
- qtr_freq = freq ;
1261
- } else { qtr_freq = FR_QTR ; }
1262
- get_asfreq_info (FR_DAY , qtr_freq , & af_info );
1263
-
1264
- if (DtoQ_yq (daily_ord , & af_info , & year , & quarter ) == INT_ERR_CODE )
1265
- { return NULL ; }
1266
-
1267
- if (strcmp (extra_fmts [i ][0 ], "%q" ) == 0 ) {
1268
- if ((extra_str = PyArray_malloc (2 * sizeof (char ))) == NULL ) {
1269
- free (tmp_str );
1270
- return (char * )PyErr_NoMemory ();
1271
- }
1272
- sprintf (extra_str , "%i" , quarter );
1273
- } else {
1274
- if ((qtr_freq % 1000 ) > 12 ) { year -= 1 ; }
1275
-
1276
- if (strcmp (extra_fmts [i ][0 ], "%f" ) == 0 ) {
1277
- year_len = 2 ;
1278
- year = year % 100 ;
1279
- } else { year_len = 4 ; }
1280
-
1281
- if ((extra_str = PyArray_malloc ((year_len + 1 ) * sizeof (char ))) == NULL ) {
1282
- free (tmp_str );
1283
- return (char * )PyErr_NoMemory ();
1284
- }
1285
-
1286
- if (year_len == 2 && year < 10 ) {
1287
- sprintf (extra_str , "0%i" , year );
1288
- } else { sprintf (extra_str , "%i" , year ); }
1289
- }
1290
-
1291
- } else {
1292
- PyErr_SetString (PyExc_RuntimeError ,"Unrecognized format string" );
1293
- return NULL ;
1294
- }
1207
+ return 0 ;
1208
+ }
1295
1209
1296
- result = str_replace (result , extra_fmts [i ][1 ], extra_str );
1297
- free (tmp_str );
1298
- free (extra_str );
1299
- if (result == NULL ) { return NULL ; }
1300
- }
1301
- }
1302
1210
1303
- return result ;
1304
- }
1305
1211
1306
- char * period_to_string (npy_int64 value , int freq )
1307
- {
1308
- int freq_group = get_freq_group (freq );
1309
- PyObject * string_arg ;
1310
- char * retval ;
1311
-
1312
- string_arg = NULL ;
1313
- if (freq_group == FR_UND ) {
1314
- int digits = log10 (value ) + 1 ;
1315
- if ((retval = PyArray_malloc (digits * sizeof (char ))) == NULL ) {
1316
- return (char * )PyErr_NoMemory ();
1317
- }
1318
- sprintf (retval , "%ld" , (long int ) value );
1319
- return retval ;
1320
- }
1321
- else if (freq_group == FR_ANN ) { string_arg = Py_BuildValue ("(s)" , "%Y" ); }
1322
- else if (freq_group == FR_QTR ) { string_arg = Py_BuildValue ("(s)" , "%FQ%q" ); }
1323
- else if (freq_group == FR_MTH ) { string_arg = Py_BuildValue ("(s)" , "%b-%Y" ); }
1324
- else if (freq_group == FR_DAY ||
1325
- freq_group == FR_BUS ||
1326
- freq_group == FR_WK ) { string_arg = Py_BuildValue ("(s)" , "%d-%b-%Y" ); }
1327
- else if (freq_group == FR_HR ) { string_arg = Py_BuildValue ("(s)" , "%d-%b-%Y %H:00" ); }
1328
- else if (freq_group == FR_MIN ) { string_arg = Py_BuildValue ("(s)" , "%d-%b-%Y %H:%M" ); }
1329
- else if (freq_group == FR_SEC ) { string_arg = Py_BuildValue ("(s)" , "%d-%b-%Y %H:%M:%S" ); }
1330
-
1331
- if (string_arg == NULL ) { return (char * )NULL ; }
1332
-
1333
- retval = skts_strftime (value , freq , string_arg );
1334
- Py_DECREF (string_arg );
1335
-
1336
- return retval ;
1337
- }
1338
1212
1339
- char * period_to_string2 (npy_int64 value , int freq , char * fmt )
1340
- {
1341
- PyObject * string_arg ;
1342
- char * retval ;
1343
- string_arg = Py_BuildValue ("(s)" , fmt );
1344
- if (string_arg == NULL ) { return (char * )NULL ; }
1345
- retval = skts_strftime (value , freq , string_arg );
1346
- Py_DECREF (string_arg );
1347
- return retval ;
1348
- }
1349
1213
1350
1214
static int _quarter_year (npy_int64 ordinal , int freq , int * year , int * quarter ) {
1351
1215
asfreq_info af_info ;
0 commit comments