forked from pierregm/scikits.timeseries-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 5
/
c_dates.c
3091 lines (2609 loc) · 102 KB
/
c_dates.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
#include "c_dates.h"
#include <datetime.h>
#include <time.h>
//#include <sys/type.h>
#include <unistd.h>
#define DEBUGPRINTF(fmt, args...) fprintf(stderr, "+++ " fmt, ## args);
int get_freq_group(int freq) { return (freq/1000)*1000; }
long get_periods_per_day(int freq){
switch(freq)
{
case FR_HR:
return 24;
case FR_MIN:
return 24*60;
case FR_SEC:
return 24*60*60;
default:
return 24*60*60 - 1;
};
}
static asfreq_info NULL_AF_INFO;
/*********************************************************
** Python callbacks. These functions must be called by **
** the module __init__ script **
*********************************************************/
static PyObject *DateFromString = NULL;
PyObject *
set_callback_DateFromString(PyObject *dummy, PyObject *args) {
return set_callback(args, &DateFromString);
}
static PyObject *DateTimeFromString = NULL;
PyObject *
set_callback_DateTimeFromString(PyObject *dummy, PyObject *args) {
return set_callback(args, &DateTimeFromString);
}
//DERIVED FROM mx.DateTime
/*
Functions in the following section are borrowed from mx.DateTime version
2.0.6, and hence this code is subject to the terms of the egenix public
license version 1.0.0
*/
#define Py_AssertWithArg(x,errortype,errorstr,a1) {if (!(x)) {PyErr_Format(errortype,errorstr,a1);goto onError;}}
#define Py_Error(errortype,errorstr) {PyErr_SetString(errortype,errorstr);goto onError;}
/* Error Exception objects */
static PyObject *DateCalc_Error;
static PyObject *DateCalc_RangeError;
#define GREGORIAN_CALENDAR 0
#define JULIAN_CALENDAR 1
#define SECONDS_PER_DAY ((double) 86400.0)
/* 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 }
};
struct date_info {
long absdate;
double abstime;
double second;
int minute;
int hour;
int day;
int month;
int quarter;
int year;
int day_of_week;
int day_of_year;
int calendar;
};
/* Return 1/0 iff year points to a leap year in calendar. */
static
int dInfoCalc_Leapyear(register long year, int calendar) {
if (calendar == GREGORIAN_CALENDAR) {
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
} else {
return (year % 4 == 0);
}
}
static
int dInfoCalc_ISOWeek(struct date_info *dinfo) {
int week;
/* Estimate */
week = (dinfo->day_of_year-1) - dinfo->day_of_week + 3;
if (week >= 0) week = week / 7 + 1;
/* Verify */
if (week < 0) {
/* The day lies in last week of the previous year */
if ((week > -2) ||
(week == -2 && dInfoCalc_Leapyear(dinfo->year-1, dinfo->calendar)))
week = 53;
else
week = 52;
} else if (week == 53) {
/* Check if the week belongs to year or year+1 */
if (31-dinfo->day + dinfo->day_of_week < 3) {
week = 1;
}
}
return week;
}
/* Return the day of the week for the given absolute date. */
static
int dInfoCalc_DayOfWeek(register long 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;
}
/* 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(register long 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(DateCalc_Error, "unknown calendar");
onError:
return -1;
}
/* 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;
long yearoffset,absdate;
/* Range check */
Py_AssertWithArg(year > -(INT_MAX / 366) && year < (INT_MAX / 366),
DateCalc_RangeError,
"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,
DateCalc_RangeError,
"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],
DateCalc_RangeError,
"day out of range: %i",
day);
yearoffset = dInfoCalc_YearOffset(year,calendar);
if (PyErr_Occurred()) 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,
DateCalc_RangeError,
"hour out of range (0-23): %i",
hour);
Py_AssertWithArg(minute >= 0 && minute <= 59,
DateCalc_RangeError,
"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)),
DateCalc_RangeError,
"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 -1;
}
static int monthToQuarter(int month) { return ((month-1)/3)+1; }
/* 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,
long absdate,
int calendar)
{
register long year;
long yearoffset;
int leap,dayoffset;
int *monthoffset;
/* Approximate year */
if (calendar == GREGORIAN_CALENDAR) {
year = (long)(((double)absdate) / 365.2425);
} else if (calendar == JULIAN_CALENDAR) {
year = (long)(((double)absdate) / 365.25);
} else {
Py_Error(DateCalc_Error, "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 (PyErr_Occurred())
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 -1;
}
/* Sets the time part of the DateTime object. */
static
int dInfoCalc_SetFromAbsTime(struct date_info *dinfo,
double abstime)
{
int inttime;
int hour,minute;
double second;
inttime = (int)abstime;
hour = inttime / 3600;
minute = (inttime % 3600) / 60;
second = abstime - (double)(hour*3600 + minute*60);
dinfo->hour = hour;
dinfo->minute = minute;
dinfo->second = second;
dinfo->abstime = abstime;
return 0;
}
/* 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_SetFromAbsDateTime(struct date_info *dinfo,
long absdate,
double abstime,
int calendar)
{
/* Bounds check */
Py_AssertWithArg(abstime >= 0.0 && abstime <= SECONDS_PER_DAY,
DateCalc_Error,
"abstime out of range (0.0 - 86400.0): %f",
abstime);
/* Calculate the date */
if (dInfoCalc_SetFromAbsDate(dinfo, absdate, calendar)) goto onError;
/* Calculate the time */
if (dInfoCalc_SetFromAbsTime(dinfo, abstime)) goto onError;
return 0;
onError:
return -1;
}
/*
====================================================
== End of section borrowed from mx.DateTime ==
====================================================
*/
///////////////////////////////////////////////////////////////////////
static long 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;
};
// helpers for frequency conversion routines //
static long DtoB_weekday(long fromDate) {
return (((fromDate) / 7) * 5) + (fromDate)%7;
};
static long DtoB_WeekendToMonday(long 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 long DtoB_WeekendToFriday(long absdate, int day_of_week) {
if (day_of_week > 4) {
//change to friday before weekend
absdate -= (day_of_week - 4);
};
return DtoB_weekday(absdate);
};
///////////////////////////////////////////////
// frequency specifc conversion routines
// each function must take an integer fromDate and a char relation ('S' or 'E' for 'START' or 'END')
//************ FROM DAILY ***************
static long asfreq_DtoA(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, fromDate, GREGORIAN_CALENDAR)) return INT_ERR_CODE;
if (dinfo.month > af_info->to_a_year_end) {
return (long)(dinfo.year + 1);
}
else {
return (long)(dinfo.year);
}
}
static long DtoQ_yq(long fromDate, asfreq_info *af_info,
int *year, int *quarter) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, fromDate, 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 long asfreq_DtoQ(long fromDate, char relation, asfreq_info *af_info) {
int year, quarter;
if (DtoQ_yq(fromDate, af_info, &year, &quarter) == INT_ERR_CODE){
return INT_ERR_CODE;
};
return (long)((year - 1) * 4 + quarter);
}
static long asfreq_DtoM(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, fromDate, GREGORIAN_CALENDAR)) {
return INT_ERR_CODE;
};
return (long)((dinfo.year - 1) * 12 + dinfo.month);
}
static long asfreq_DtoW(long fromDate, char relation, asfreq_info *af_info) {
return (fromDate - (1 + af_info->to_week_end))/7 + 1;
}
static long asfreq_DtoB(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, fromDate, 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);
}
}
static long asfreq_DtoB_forConvert(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, fromDate, GREGORIAN_CALENDAR)) {
return INT_ERR_CODE;
};
if (dinfo.day_of_week > 4) {
return -1;
} else {
return DtoB_weekday(fromDate);
};
};
// needed for getDateInfo function
static long asfreq_DtoD(long fromDate, char relation, asfreq_info *af_info) {
return fromDate;
};
static long asfreq_DtoHIGHFREQ(long fromDate, char relation, long periodsPerDay) {
// DEBUGPRINTF("asfreq_DtoH: fromDate: %i\n", fromDate);
if (relation == 'S') {
return (fromDate - HIGHFREQ_ORIG)*(periodsPerDay) + 1;
} else {
return (fromDate - HIGHFREQ_ORIG + 1)*(periodsPerDay);
};
}
static long asfreq_DtoH(long fromDate, char relation, asfreq_info *af_info)
{ return asfreq_DtoHIGHFREQ(fromDate, relation, 24); }
static long asfreq_DtoT(long fromDate, char relation, asfreq_info *af_info)
{ return asfreq_DtoHIGHFREQ(fromDate, relation, 24*60); }
static long asfreq_DtoS(long fromDate, char relation, asfreq_info *af_info)
{ return asfreq_DtoHIGHFREQ(fromDate, relation, 24*60*60); }
//************ FROM SECONDLY ***************
static long asfreq_StoD(long fromDate, char relation, asfreq_info *af_info) {
if (fromDate <= 0){
return fromDate/(60*60*24) + HIGHFREQ_ORIG - 1;
} else {
return (fromDate - 1)/(60*60*24) + HIGHFREQ_ORIG;
};
};
static long asfreq_StoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_StoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_StoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_StoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_StoM(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoM(asfreq_StoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_StoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_StoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_StoB(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoB(asfreq_StoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_StoB_forConvert(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoB_forConvert(asfreq_StoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_StoT(long fromDate, char relation, asfreq_info *af_info) {
if (fromDate <= 0){
return fromDate/60;
} else {
return (fromDate - 1)/60 + 1;
};
};
static long asfreq_StoH(long fromDate, char relation, asfreq_info *af_info) {
if (fromDate <= 0){
return fromDate/(60*60);
} else {
return (fromDate - 1)/(60*60) + 1;
};
};
//************ FROM MINUTELY ***************
static long asfreq_TtoD(long fromDate, char relation, asfreq_info *af_info){
if (fromDate <= 0) {
return fromDate/(60*24) + HIGHFREQ_ORIG - 1;
} else {
return (fromDate - 1)/(60*24) + HIGHFREQ_ORIG;
};
};
static long asfreq_TtoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_TtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_TtoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_TtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_TtoM(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoM(asfreq_TtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_TtoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_TtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_TtoB(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoB(asfreq_TtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_TtoB_forConvert(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoB_forConvert(asfreq_TtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_TtoH(long fromDate, char relation, asfreq_info *af_info) {
if (fromDate <= 0) {
return fromDate/60;
} else {
return (fromDate - 1)/60 + 1;
};
};
static long asfreq_TtoS(long fromDate, char relation, asfreq_info *af_info) {
if (relation == 'S') {
return fromDate*60 - 59;
} else {
return fromDate*60;
}
};
//************ FROM HOURLY ***************
static long asfreq_HtoD(long fromDate, char relation, asfreq_info *af_info) {
if (fromDate <= 0){
return fromDate/24 + HIGHFREQ_ORIG - 1;
} else{
return (fromDate - 1)/24 + HIGHFREQ_ORIG;
};
};
static long asfreq_HtoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_HtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_HtoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_HtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_HtoM(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoM(asfreq_HtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_HtoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_HtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_HtoB(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoB(asfreq_HtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_HtoB_forConvert(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoB_forConvert(asfreq_HtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
// calculation works out the same as TtoS, so we just call that function for HtoT
static long asfreq_HtoT(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_TtoS(fromDate, relation, &NULL_AF_INFO);
};
static long asfreq_HtoS(long fromDate, char relation, asfreq_info *af_info) {
if (relation == 'S') {
return fromDate*60*60 - 60*60 + 1;
} else {
return fromDate*60*60;
};
};
//************ FROM BUSINESS ***************
static long asfreq_BtoD(long fromDate, char relation, asfreq_info *af_info) {
return ((fromDate-1)/5)*7 + (fromDate-1)%5 + 1;
};
static long asfreq_BtoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_BtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_BtoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_BtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_BtoM(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoM(asfreq_BtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_BtoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_BtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_BtoH(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoH(asfreq_BtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_BtoT(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoT(asfreq_BtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_BtoS(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoS(asfreq_BtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
//************ FROM WEEKLY ***************
static long asfreq_WtoD(long fromDate, char relation, asfreq_info *af_info) {
if (relation == 'S') {
return fromDate * 7 - 6 + af_info->from_week_end;
} else {
return fromDate * 7 + af_info->from_week_end;
};
};
static long asfreq_WtoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_WtoD(fromDate, 'E', af_info), relation, af_info);
};
static long asfreq_WtoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_WtoD(fromDate, 'E', af_info), relation, af_info);
};
static long asfreq_WtoM(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoM(asfreq_WtoD(fromDate, 'E', af_info), relation, &NULL_AF_INFO);
};
static long asfreq_WtoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_WtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_WtoB(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, asfreq_WtoD(fromDate, relation, af_info), 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 long asfreq_WtoH(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoH(asfreq_WtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long asfreq_WtoT(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoT(asfreq_WtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long asfreq_WtoS(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoS(asfreq_WtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
//************ FROM MONTHLY ***************
static void MtoD_ym(long fromDate, long *y, long *m) {
*y = (fromDate - 1) / 12 + 1;
*m = fromDate - 12 * (*y) - 1;
};
static long asfreq_MtoD(long fromDate, char relation, asfreq_info *af_info) {
long y, m, absdate;
if (relation == 'S') {
MtoD_ym(fromDate, &y, &m);
if ((absdate = absdate_from_ymd(y, m, 1)) == INT_ERR_CODE) return INT_ERR_CODE;
return absdate;
} else {
MtoD_ym(fromDate+1, &y, &m);
if ((absdate = absdate_from_ymd(y, m, 1)) == INT_ERR_CODE) return INT_ERR_CODE;
return absdate-1;
};
};
static long asfreq_MtoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_MtoD(fromDate, 'E', &NULL_AF_INFO), relation, af_info);
};
static long asfreq_MtoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_MtoD(fromDate, 'E', &NULL_AF_INFO), relation, af_info);
};
static long asfreq_MtoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_MtoD(fromDate, relation, &NULL_AF_INFO), relation, af_info);
};
static long asfreq_MtoB(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, asfreq_MtoD(fromDate, relation, &NULL_AF_INFO), 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 long asfreq_MtoH(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoH(asfreq_MtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_MtoT(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoT(asfreq_MtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
static long asfreq_MtoS(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoS(asfreq_MtoD(fromDate, relation, &NULL_AF_INFO), relation, &NULL_AF_INFO);
};
//************ FROM QUARTERLY ***************
static void QtoD_ym(long fromDate, long *y, long *m, asfreq_info *af_info) {
*y = (fromDate - 1) / 4 + 1;
*m = (fromDate + 4) * 3 - 12 * (*y) - 2;
if (af_info->from_q_year_end != 12) {
*m += af_info->from_q_year_end;
if (*m > 12) { *m -= 12; }
else { *y -= 1; }
}
}
static long asfreq_QtoD(long fromDate, char relation, asfreq_info *af_info) {
long y, m, absdate;
if (relation == 'S') {
QtoD_ym(fromDate, &y, &m, af_info);
if ((absdate = absdate_from_ymd(y, m, 1)) == INT_ERR_CODE) return INT_ERR_CODE;
return absdate;
} else {
QtoD_ym(fromDate+1, &y, &m, af_info);
if ((absdate = absdate_from_ymd(y, m, 1)) == INT_ERR_CODE) return INT_ERR_CODE;
return absdate - 1;
};
};
static long asfreq_QtoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_QtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_QtoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_QtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_QtoM(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoM(asfreq_QtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long asfreq_QtoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_QtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_QtoB(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, asfreq_QtoD(fromDate, relation, af_info), 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 long asfreq_QtoH(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoH(asfreq_QtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long asfreq_QtoT(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoT(asfreq_QtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long asfreq_QtoS(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoS(asfreq_QtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
//************ FROM ANNUAL ***************
static long asfreq_AtoD(long fromDate, char relation, asfreq_info *af_info) {
long absdate, year, final_adj;
int month = (af_info->from_a_year_end) % 12;
if (month == 0) { month = 1;}
else { month += 1; }
if (relation == 'S') {
if (af_info->from_a_year_end == 12) {year = fromDate;}
else {year = fromDate - 1;}
final_adj = 0;
} else {
if (af_info->from_a_year_end == 12) {year = fromDate+1;}
else {year = fromDate;}
final_adj = -1;
}
absdate = absdate_from_ymd(year, month, 1);
if (absdate == INT_ERR_CODE) return INT_ERR_CODE;
return absdate + final_adj;
}
static long asfreq_AtoA(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoA(asfreq_AtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_AtoQ(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoQ(asfreq_AtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_AtoM(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoM(asfreq_AtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_AtoW(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoW(asfreq_AtoD(fromDate, relation, af_info), relation, af_info);
};
static long asfreq_AtoB(long fromDate, char relation, asfreq_info *af_info) {
struct date_info dinfo;
if (dInfoCalc_SetFromAbsDate(&dinfo, asfreq_AtoD(fromDate, relation, af_info), 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 long asfreq_AtoH(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoH(asfreq_AtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long asfreq_AtoT(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoT(asfreq_AtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long asfreq_AtoS(long fromDate, char relation, asfreq_info *af_info) {
return asfreq_DtoS(asfreq_AtoD(fromDate, relation, af_info), relation, &NULL_AF_INFO);
};
static long nofunc(long fromDate, char relation, asfreq_info *af_info) { return -1; }
// end of frequency specific conversion routines
// return a pointer to appropriate conversion function
long (*get_asfreq_func(int fromFreq, int toFreq, int forConvert))(long, char, asfreq_info*) {
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: return &asfreq_AtoD;
case FR_HR: return &asfreq_AtoH;
case FR_MIN: return &asfreq_AtoT;
case FR_SEC: return &asfreq_AtoS;
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: return &asfreq_QtoD;
case FR_HR: return &asfreq_QtoH;
case FR_MIN: return &asfreq_QtoT;
case FR_SEC: return &asfreq_QtoS;
default: return &nofunc;
}
case FR_MTH:
switch(toGroup)
{
case FR_ANN: return &asfreq_MtoA;
case FR_QTR: return &asfreq_MtoQ;
case FR_WK: return &asfreq_MtoW;
case FR_BUS: return &asfreq_MtoB;
case FR_DAY: return &asfreq_MtoD;
case FR_HR: return &asfreq_MtoH;
case FR_MIN: return &asfreq_MtoT;
case FR_SEC: return &asfreq_MtoS;
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: return &asfreq_WtoD;
case FR_HR: return &asfreq_WtoH;
case FR_MIN: return &asfreq_WtoT;
case FR_SEC: return &asfreq_WtoS;
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_DAY: return &asfreq_BtoD;
case FR_HR: return &asfreq_BtoH;
case FR_MIN: return &asfreq_BtoT;
case FR_SEC: return &asfreq_BtoS;
default: return &nofunc;
}
case FR_DAY:
switch(toGroup)
{
case FR_ANN: return &asfreq_DtoA;
case FR_QTR: return &asfreq_DtoQ;
case FR_MTH: return &asfreq_DtoM;
case FR_WK: return &asfreq_DtoW;
case FR_BUS:
if (forConvert) { return &asfreq_DtoB_forConvert; }
else { return &asfreq_DtoB; }