forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
period_helper.c
1518 lines (1250 loc) · 45.7 KB
/
period_helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2016, PyData Development Team
All rights reserved.
Distributed under the terms of the BSD Simplified License.
The full license is in the LICENSE file, distributed with this software.
Borrowed and derived code from scikits.timeseries that we will expose via
Cython to pandas. This primarily concerns interval representation and
frequency conversion routines.
See end of file for stuff pandas uses (search for 'pandas').
*/
#include "period_helper.h"
/* ------------------------------------------------------------------
* Code derived from scikits.timeseries
* ------------------------------------------------------------------*/
static int mod_compat(int x, int m) {
int result = x % m;
if (result < 0) return result + m;
return result;
}
static int floordiv(int x, int divisor) {
if (x < 0) {
if (mod_compat(x, divisor)) {
return x / divisor - 1;
} else {
return x / divisor;
}
} else {
return x / divisor;
}
}
/* Table with day offsets for each month (0-based, without and with leap) */
static int month_offset[2][13] = {
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
/* Table of number of days in a month (0-based, without and with leap) */
static int days_in_month[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}};
/* Return 1/0 iff year points to a leap year in calendar. */
static int dInfoCalc_Leapyear(npy_int64 year, int calendar) {
if (calendar == GREGORIAN_CALENDAR) {
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
} else {
return (year % 4 == 0);
}
}
/* Return the day of the week for the given absolute date. */
static int dInfoCalc_DayOfWeek(npy_int64 absdate) {
int day_of_week;
if (absdate >= 1) {
day_of_week = (absdate - 1) % 7;
} else {
day_of_week = 6 - ((-absdate) % 7);
}
return day_of_week;
}
static int monthToQuarter(int month) { return ((month - 1) / 3) + 1; }
/* Return the year offset, that is the absolute date of the day
31.12.(year-1) in the given calendar.
Note:
For the Julian calendar we shift the absdate (which is measured
using the Gregorian Epoch) value by two days because the Epoch
(0001-01-01) in the Julian calendar lies 2 days before the Epoch in
the Gregorian calendar. */
static int dInfoCalc_YearOffset(npy_int64 year, int calendar) {
year--;
if (calendar == GREGORIAN_CALENDAR) {
if (year >= 0 || -1 / 4 == -1)
return year * 365 + year / 4 - year / 100 + year / 400;
else
return year * 365 + (year - 3) / 4 - (year - 99) / 100 +
(year - 399) / 400;
} else if (calendar == JULIAN_CALENDAR) {
if (year >= 0 || -1 / 4 == -1)
return year * 365 + year / 4 - 2;
else
return year * 365 + (year - 3) / 4 - 2;
}
Py_Error(PyExc_ValueError, "unknown calendar");
onError:
return INT_ERR_CODE;
}
/* Set the instance's value using the given date and time. calendar may be set
* to the flags: GREGORIAN_CALENDAR, JULIAN_CALENDAR to indicate the calendar
* to be used. */
static int dInfoCalc_SetFromDateAndTime(struct date_info *dinfo, int year,
int month, int day, int hour,
int minute, double second,
int calendar) {
/* Calculate the absolute date */
{
int leap;
npy_int64 absdate;
int yearoffset;
/* Range check */
Py_AssertWithArg(year > -(INT_MAX / 366) && year < (INT_MAX / 366),
PyExc_ValueError, "year out of range: %i", year);
/* Is it a leap year ? */
leap = dInfoCalc_Leapyear(year, calendar);
/* Negative month values indicate months relative to the years end */
if (month < 0) month += 13;
Py_AssertWithArg(month >= 1 && month <= 12, PyExc_ValueError,
"month out of range (1-12): %i", month);
/* Negative values indicate days relative to the months end */
if (day < 0) day += days_in_month[leap][month - 1] + 1;
Py_AssertWithArg(day >= 1 && day <= days_in_month[leap][month - 1],
PyExc_ValueError, "day out of range: %i", day);
yearoffset = dInfoCalc_YearOffset(year, calendar);
if (yearoffset == INT_ERR_CODE) goto onError;
absdate = day + month_offset[leap][month - 1] + yearoffset;
dinfo->absdate = absdate;
dinfo->year = year;
dinfo->month = month;
dinfo->quarter = ((month - 1) / 3) + 1;
dinfo->day = day;
dinfo->day_of_week = dInfoCalc_DayOfWeek(absdate);
dinfo->day_of_year = (short)(absdate - yearoffset);
dinfo->calendar = calendar;
}
/* Calculate the absolute time */
{
Py_AssertWithArg(hour >= 0 && hour <= 23, PyExc_ValueError,
"hour out of range (0-23): %i", hour);
Py_AssertWithArg(minute >= 0 && minute <= 59, PyExc_ValueError,
"minute out of range (0-59): %i", minute);
Py_AssertWithArg(
second >= (double)0.0 &&
(second < (double)60.0 ||
(hour == 23 && minute == 59 && second < (double)61.0)),
PyExc_ValueError,
"second out of range (0.0 - <60.0; <61.0 for 23:59): %f", second);
dinfo->abstime = (double)(hour * 3600 + minute * 60) + second;
dinfo->hour = hour;
dinfo->minute = minute;
dinfo->second = second;
}
return 0;
onError:
return INT_ERR_CODE;
}
/* Sets the date part of the date_info struct using the indicated
calendar.
XXX This could also be done using some integer arithmetics rather
than with this iterative approach... */
static int dInfoCalc_SetFromAbsDate(register struct date_info *dinfo,
npy_int64 absdate, int calendar) {
register npy_int64 year;
npy_int64 yearoffset;
int leap, dayoffset;
int *monthoffset;
/* Approximate year */
if (calendar == GREGORIAN_CALENDAR) {
year = (npy_int64)(((double)absdate) / 365.2425);
} else if (calendar == JULIAN_CALENDAR) {
year = (npy_int64)(((double)absdate) / 365.25);
} else {
Py_Error(PyExc_ValueError, "unknown calendar");
}
if (absdate > 0) year++;
/* Apply corrections to reach the correct year */
while (1) {
/* Calculate the year offset */
yearoffset = dInfoCalc_YearOffset(year, calendar);
if (yearoffset == INT_ERR_CODE) goto onError;
/* Backward correction: absdate must be greater than the
yearoffset */
if (yearoffset >= absdate) {
year--;
continue;
}
dayoffset = absdate - yearoffset;
leap = dInfoCalc_Leapyear(year, calendar);
/* Forward correction: non leap years only have 365 days */
if (dayoffset > 365 && !leap) {
year++;
continue;
}
break;
}
dinfo->year = year;
dinfo->calendar = calendar;
/* Now iterate to find the month */
monthoffset = month_offset[leap];
{
register int month;
for (month = 1; month < 13; month++) {
if (monthoffset[month] >= dayoffset) break;
}
dinfo->month = month;
dinfo->quarter = monthToQuarter(month);
dinfo->day = dayoffset - month_offset[leap][month - 1];
}
dinfo->day_of_week = dInfoCalc_DayOfWeek(absdate);
dinfo->day_of_year = dayoffset;
dinfo->absdate = absdate;
return 0;
onError:
return INT_ERR_CODE;
}
///////////////////////////////////////////////
// frequency specifc conversion routines
// each function must take an integer fromDate and
// a char relation ('S' or 'E' for 'START' or 'END')
///////////////////////////////////////////////////////////////////////
// helpers for frequency conversion routines //
static int daytime_conversion_factors[][2] = {
{FR_DAY, 1}, {FR_HR, 24}, {FR_MIN, 60}, {FR_SEC, 60},
{FR_MS, 1000}, {FR_US, 1000}, {FR_NS, 1000}, {0, 0}};
static npy_int64 **daytime_conversion_factor_matrix = NULL;
PANDAS_INLINE int max_value(int a, int b) { return a > b ? a : b; }
PANDAS_INLINE int min_value(int a, int b) { return a < b ? a : b; }
PANDAS_INLINE int get_freq_group(int freq) { return (freq / 1000) * 1000; }
PANDAS_INLINE int get_freq_group_index(int freq) { return freq / 1000; }
static int calc_conversion_factors_matrix_size(void) {
int matrix_size = 0;
int index;
for (index = 0;; index++) {
int period_value =
get_freq_group_index(daytime_conversion_factors[index][0]);
if (period_value == 0) {
break;
}
matrix_size = max_value(matrix_size, period_value);
}
return matrix_size + 1;
}
static void alloc_conversion_factors_matrix(int matrix_size) {
int row_index;
int column_index;
daytime_conversion_factor_matrix =
malloc(matrix_size * sizeof(**daytime_conversion_factor_matrix));
for (row_index = 0; row_index < matrix_size; row_index++) {
daytime_conversion_factor_matrix[row_index] =
malloc(matrix_size * sizeof(**daytime_conversion_factor_matrix));
for (column_index = 0; column_index < matrix_size; column_index++) {
daytime_conversion_factor_matrix[row_index][column_index] = 0;
}
}
}
static npy_int64 calculate_conversion_factor(int start_value, int end_value) {
npy_int64 conversion_factor = 0;
int index;
for (index = 0;; index++) {
int freq_group = daytime_conversion_factors[index][0];
if (freq_group == 0) {
conversion_factor = 0;
break;
}
if (freq_group == start_value) {
conversion_factor = 1;
} else {
conversion_factor *= daytime_conversion_factors[index][1];
}
if (freq_group == end_value) {
break;
}
}
return conversion_factor;
}
static void populate_conversion_factors_matrix(void) {
int row_index_index;
int row_value, row_index;
int column_index_index;
int column_value, column_index;
for (row_index_index = 0;; row_index_index++) {
row_value = daytime_conversion_factors[row_index_index][0];
if (row_value == 0) {
break;
}
row_index = get_freq_group_index(row_value);
for (column_index_index = row_index_index;; column_index_index++) {
column_value = daytime_conversion_factors[column_index_index][0];
if (column_value == 0) {
break;
}
column_index = get_freq_group_index(column_value);
daytime_conversion_factor_matrix[row_index][column_index] =
calculate_conversion_factor(row_value, column_value);
}
}
}
void initialize_daytime_conversion_factor_matrix() {
if (daytime_conversion_factor_matrix == NULL) {
int matrix_size = calc_conversion_factors_matrix_size();
alloc_conversion_factors_matrix(matrix_size);
populate_conversion_factors_matrix();
}
}
PANDAS_INLINE npy_int64 get_daytime_conversion_factor(int from_index,
int to_index) {
return daytime_conversion_factor_matrix[min_value(from_index, to_index)]
[max_value(from_index, to_index)];
}
PANDAS_INLINE npy_int64 upsample_daytime(npy_int64 ordinal,
asfreq_info *af_info, int atEnd) {
if (atEnd) {
return (ordinal + 1) * af_info->intraday_conversion_factor - 1;
} else {
return ordinal * af_info->intraday_conversion_factor;
}
}
PANDAS_INLINE npy_int64 downsample_daytime(npy_int64 ordinal,
asfreq_info *af_info, int atEnd) {
return ordinal / (af_info->intraday_conversion_factor);
}
PANDAS_INLINE npy_int64 transform_via_day(npy_int64 ordinal, char relation,
asfreq_info *af_info,
freq_conv_func first_func,
freq_conv_func second_func) {
// printf("transform_via_day(%ld, %ld, %d)\n", ordinal,
// af_info->intraday_conversion_factor,
// af_info->intraday_conversion_upsample);
npy_int64 result;
result = (*first_func)(ordinal, relation, af_info);
result = (*second_func)(result, relation, af_info);
return result;
}
static npy_int64 DtoB_weekday(npy_int64 absdate) {
return (((absdate) / 7) * 5) + (absdate) % 7 - BDAY_OFFSET;
}
static npy_int64 DtoB_WeekendToMonday(npy_int64 absdate, int day_of_week) {
if (day_of_week > 4) {
// change to Monday after weekend
absdate += (7 - day_of_week);
}
return DtoB_weekday(absdate);
}
static npy_int64 DtoB_WeekendToFriday(npy_int64 absdate, int day_of_week) {
if (day_of_week > 4) {
// change to friday before weekend
absdate -= (day_of_week - 4);
}
return DtoB_weekday(absdate);
}
static npy_int64 absdate_from_ymd(int y, int m, int d) {
struct date_info tempDate;
if (dInfoCalc_SetFromDateAndTime(&tempDate, y, m, d, 0, 0, 0,
GREGORIAN_CALENDAR)) {
return INT_ERR_CODE;
}
return tempDate.absdate;
}
//************ FROM DAILY ***************
static npy_int64 asfreq_DTtoA(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
struct date_info dinfo;
ordinal = downsample_daytime(ordinal, af_info, 0);
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
if (dinfo.month > af_info->to_a_year_end) {
return (npy_int64)(dinfo.year + 1 - BASE_YEAR);
} else {
return (npy_int64)(dinfo.year - BASE_YEAR);
}
}
static npy_int64 DtoQ_yq(npy_int64 ordinal, asfreq_info *af_info, int *year,
int *quarter) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
if (af_info->to_q_year_end != 12) {
dinfo.month -= af_info->to_q_year_end;
if (dinfo.month <= 0) {
dinfo.month += 12;
} else {
dinfo.year += 1;
}
dinfo.quarter = monthToQuarter(dinfo.month);
}
*year = dinfo.year;
*quarter = dinfo.quarter;
return 0;
}
static npy_int64 asfreq_DTtoQ(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
int year, quarter;
ordinal = downsample_daytime(ordinal, af_info, 0);
if (DtoQ_yq(ordinal, af_info, &year, &quarter) == INT_ERR_CODE) {
return INT_ERR_CODE;
}
return (npy_int64)((year - BASE_YEAR) * 4 + quarter - 1);
}
static npy_int64 asfreq_DTtoM(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
struct date_info dinfo;
ordinal = downsample_daytime(ordinal, af_info, 0);
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
return (npy_int64)((dinfo.year - BASE_YEAR) * 12 + dinfo.month - 1);
}
static npy_int64 asfreq_DTtoW(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
ordinal = downsample_daytime(ordinal, af_info, 0);
return (ordinal + ORD_OFFSET - (1 + af_info->to_week_end)) / 7 + 1 -
WEEK_OFFSET;
}
static npy_int64 asfreq_DTtoB(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
struct date_info dinfo;
ordinal = downsample_daytime(ordinal, af_info, 0);
if (dInfoCalc_SetFromAbsDate(&dinfo, ordinal + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
if (relation == 'S') {
return DtoB_WeekendToFriday(dinfo.absdate, dinfo.day_of_week);
} else {
return DtoB_WeekendToMonday(dinfo.absdate, dinfo.day_of_week);
}
}
// all intra day calculations are now done within one function
static npy_int64 asfreq_DownsampleWithinDay(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return downsample_daytime(ordinal, af_info, relation == 'E');
}
static npy_int64 asfreq_UpsampleWithinDay(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return upsample_daytime(ordinal, af_info, relation == 'E');
}
//************ FROM BUSINESS ***************
static npy_int64 asfreq_BtoDT(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
ordinal += BDAY_OFFSET;
ordinal =
(((ordinal - 1) / 5) * 7 + mod_compat(ordinal - 1, 5) + 1 - ORD_OFFSET);
return upsample_daytime(ordinal, af_info, relation != 'S');
}
static npy_int64 asfreq_BtoA(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_BtoDT,
asfreq_DTtoA);
}
static npy_int64 asfreq_BtoQ(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_BtoDT,
asfreq_DTtoQ);
}
static npy_int64 asfreq_BtoM(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_BtoDT,
asfreq_DTtoM);
}
static npy_int64 asfreq_BtoW(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_BtoDT,
asfreq_DTtoW);
}
//************ FROM WEEKLY ***************
static npy_int64 asfreq_WtoDT(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
ordinal += WEEK_OFFSET;
if (relation != 'S') {
ordinal += 1;
}
ordinal = ordinal * 7 - 6 + af_info->from_week_end - ORD_OFFSET;
if (relation != 'S') {
ordinal -= 1;
}
return upsample_daytime(ordinal, af_info, relation != 'S');
}
static npy_int64 asfreq_WtoA(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_WtoDT,
asfreq_DTtoA);
}
static npy_int64 asfreq_WtoQ(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_WtoDT,
asfreq_DTtoQ);
}
static npy_int64 asfreq_WtoM(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_WtoDT,
asfreq_DTtoM);
}
static npy_int64 asfreq_WtoW(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_WtoDT,
asfreq_DTtoW);
}
static npy_int64 asfreq_WtoB(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(
&dinfo, asfreq_WtoDT(ordinal, relation, af_info) + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
if (relation == 'S') {
return DtoB_WeekendToMonday(dinfo.absdate, dinfo.day_of_week);
} else {
return DtoB_WeekendToFriday(dinfo.absdate, dinfo.day_of_week);
}
}
//************ FROM MONTHLY ***************
static void MtoD_ym(npy_int64 ordinal, int *y, int *m) {
*y = floordiv(ordinal, 12) + BASE_YEAR;
*m = mod_compat(ordinal, 12) + 1;
}
static npy_int64 asfreq_MtoDT(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
npy_int64 absdate;
int y, m;
if (relation == 'E') {
ordinal += 1;
}
MtoD_ym(ordinal, &y, &m);
if ((absdate = absdate_from_ymd(y, m, 1)) == INT_ERR_CODE)
return INT_ERR_CODE;
ordinal = absdate - ORD_OFFSET;
if (relation == 'E') {
ordinal -= 1;
}
return upsample_daytime(ordinal, af_info, relation != 'S');
}
static npy_int64 asfreq_MtoA(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_MtoDT,
asfreq_DTtoA);
}
static npy_int64 asfreq_MtoQ(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_MtoDT,
asfreq_DTtoQ);
}
static npy_int64 asfreq_MtoW(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_MtoDT,
asfreq_DTtoW);
}
static npy_int64 asfreq_MtoB(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(
&dinfo, asfreq_MtoDT(ordinal, relation, af_info) + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
if (relation == 'S') {
return DtoB_WeekendToMonday(dinfo.absdate, dinfo.day_of_week);
} else {
return DtoB_WeekendToFriday(dinfo.absdate, dinfo.day_of_week);
}
}
//************ FROM QUARTERLY ***************
static void QtoD_ym(npy_int64 ordinal, int *y, int *m, asfreq_info *af_info) {
*y = floordiv(ordinal, 4) + BASE_YEAR;
*m = mod_compat(ordinal, 4) * 3 + 1;
if (af_info->from_q_year_end != 12) {
*m += af_info->from_q_year_end;
if (*m > 12) {
*m -= 12;
} else {
*y -= 1;
}
}
}
static npy_int64 asfreq_QtoDT(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
npy_int64 absdate;
int y, m;
if (relation == 'E') {
ordinal += 1;
}
QtoD_ym(ordinal, &y, &m, af_info);
if ((absdate = absdate_from_ymd(y, m, 1)) == INT_ERR_CODE)
return INT_ERR_CODE;
if (relation == 'E') {
absdate -= 1;
}
return upsample_daytime(absdate - ORD_OFFSET, af_info, relation != 'S');
}
static npy_int64 asfreq_QtoQ(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_QtoDT,
asfreq_DTtoQ);
}
static npy_int64 asfreq_QtoA(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_QtoDT,
asfreq_DTtoA);
}
static npy_int64 asfreq_QtoM(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_QtoDT,
asfreq_DTtoM);
}
static npy_int64 asfreq_QtoW(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_QtoDT,
asfreq_DTtoW);
}
static npy_int64 asfreq_QtoB(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(
&dinfo, asfreq_QtoDT(ordinal, relation, af_info) + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
if (relation == 'S') {
return DtoB_WeekendToMonday(dinfo.absdate, dinfo.day_of_week);
} else {
return DtoB_WeekendToFriday(dinfo.absdate, dinfo.day_of_week);
}
}
//************ FROM ANNUAL ***************
static npy_int64 asfreq_AtoDT(npy_int64 year, char relation,
asfreq_info *af_info) {
npy_int64 absdate;
int month = (af_info->from_a_year_end) % 12;
// start from 1970
year += BASE_YEAR;
month += 1;
if (af_info->from_a_year_end != 12) {
year -= 1;
}
if (relation == 'E') {
year += 1;
}
absdate = absdate_from_ymd(year, month, 1);
if (absdate == INT_ERR_CODE) {
return INT_ERR_CODE;
}
if (relation == 'E') {
absdate -= 1;
}
return upsample_daytime(absdate - ORD_OFFSET, af_info, relation != 'S');
}
static npy_int64 asfreq_AtoA(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_AtoDT,
asfreq_DTtoA);
}
static npy_int64 asfreq_AtoQ(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_AtoDT,
asfreq_DTtoQ);
}
static npy_int64 asfreq_AtoM(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_AtoDT,
asfreq_DTtoM);
}
static npy_int64 asfreq_AtoW(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return transform_via_day(ordinal, relation, af_info, asfreq_AtoDT,
asfreq_DTtoW);
}
static npy_int64 asfreq_AtoB(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(
&dinfo, asfreq_AtoDT(ordinal, relation, af_info) + ORD_OFFSET,
GREGORIAN_CALENDAR))
return INT_ERR_CODE;
if (relation == 'S') {
return DtoB_WeekendToMonday(dinfo.absdate, dinfo.day_of_week);
} else {
return DtoB_WeekendToFriday(dinfo.absdate, dinfo.day_of_week);
}
}
static npy_int64 nofunc(npy_int64 ordinal, char relation,
asfreq_info *af_info) {
return INT_ERR_CODE;
}
static npy_int64 no_op(npy_int64 ordinal, char relation, asfreq_info *af_info) {
return ordinal;
}
// end of frequency specific conversion routines
static int calc_a_year_end(int freq, int group) {
int result = (freq - group) % 12;
if (result == 0) {
return 12;
} else {
return result;
}
}
static int calc_week_end(int freq, int group) { return freq - group; }
void get_asfreq_info(int fromFreq, int toFreq, asfreq_info *af_info) {
int fromGroup = get_freq_group(fromFreq);
int toGroup = get_freq_group(toFreq);
af_info->intraday_conversion_factor = get_daytime_conversion_factor(
get_freq_group_index(max_value(fromGroup, FR_DAY)),
get_freq_group_index(max_value(toGroup, FR_DAY)));
// printf("get_asfreq_info(%d, %d) %ld, %d\n", fromFreq, toFreq,
// af_info->intraday_conversion_factor,
// af_info->intraday_conversion_upsample);
switch (fromGroup) {
case FR_WK:
af_info->from_week_end = calc_week_end(fromFreq, fromGroup);
break;
case FR_ANN:
af_info->from_a_year_end = calc_a_year_end(fromFreq, fromGroup);
break;
case FR_QTR:
af_info->from_q_year_end = calc_a_year_end(fromFreq, fromGroup);
break;
}
switch (toGroup) {
case FR_WK:
af_info->to_week_end = calc_week_end(toFreq, toGroup);
break;
case FR_ANN:
af_info->to_a_year_end = calc_a_year_end(toFreq, toGroup);
break;
case FR_QTR:
af_info->to_q_year_end = calc_a_year_end(toFreq, toGroup);
break;
}
}
freq_conv_func get_asfreq_func(int fromFreq, int toFreq) {
int fromGroup = get_freq_group(fromFreq);
int toGroup = get_freq_group(toFreq);
if (fromGroup == FR_UND) {
fromGroup = FR_DAY;
}
switch (fromGroup) {
case FR_ANN:
switch (toGroup) {
case FR_ANN:
return &asfreq_AtoA;
case FR_QTR:
return &asfreq_AtoQ;
case FR_MTH:
return &asfreq_AtoM;
case FR_WK:
return &asfreq_AtoW;
case FR_BUS:
return &asfreq_AtoB;
case FR_DAY:
case FR_HR:
case FR_MIN:
case FR_SEC:
case FR_MS:
case FR_US:
case FR_NS:
return &asfreq_AtoDT;
default:
return &nofunc;
}
case FR_QTR:
switch (toGroup) {
case FR_ANN:
return &asfreq_QtoA;
case FR_QTR:
return &asfreq_QtoQ;
case FR_MTH:
return &asfreq_QtoM;
case FR_WK:
return &asfreq_QtoW;
case FR_BUS:
return &asfreq_QtoB;
case FR_DAY:
case FR_HR:
case FR_MIN:
case FR_SEC:
case FR_MS:
case FR_US:
case FR_NS:
return &asfreq_QtoDT;
default:
return &nofunc;
}
case FR_MTH:
switch (toGroup) {
case FR_ANN:
return &asfreq_MtoA;
case FR_QTR:
return &asfreq_MtoQ;
case FR_MTH:
return &no_op;
case FR_WK:
return &asfreq_MtoW;
case FR_BUS:
return &asfreq_MtoB;
case FR_DAY:
case FR_HR:
case FR_MIN:
case FR_SEC:
case FR_MS:
case FR_US:
case FR_NS:
return &asfreq_MtoDT;
default:
return &nofunc;
}
case FR_WK:
switch (toGroup) {
case FR_ANN:
return &asfreq_WtoA;
case FR_QTR:
return &asfreq_WtoQ;
case FR_MTH:
return &asfreq_WtoM;
case FR_WK:
return &asfreq_WtoW;
case FR_BUS:
return &asfreq_WtoB;
case FR_DAY:
case FR_HR:
case FR_MIN:
case FR_SEC:
case FR_MS:
case FR_US:
case FR_NS:
return &asfreq_WtoDT;
default:
return &nofunc;
}
case FR_BUS:
switch (toGroup) {
case FR_ANN:
return &asfreq_BtoA;
case FR_QTR:
return &asfreq_BtoQ;
case FR_MTH:
return &asfreq_BtoM;
case FR_WK:
return &asfreq_BtoW;
case FR_BUS:
return &no_op;
case FR_DAY:
case FR_HR:
case FR_MIN:
case FR_SEC:
case FR_MS:
case FR_US:
case FR_NS:
return &asfreq_BtoDT;