forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plvdate.c
858 lines (698 loc) · 19.9 KB
/
plvdate.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
/*
This code implements one part of functonality of
free available library PL/Vision. Please look www.quest.com
This library isn't optimalized for big numbers, for working
with n days (n > 10000), can be slow (on my P4 31ms).
Original author: Steven Feuerstein, 1996 - 2002
PostgreSQL implementation author: Pavel Stehule, 2006
This module is under BSD Licence
History:
1.0. first public version 13. March 2006
*/
#define PLVDATE_VERSION "PostgreSQL PLVdate, version 1.0, March 2006"
#include "postgres.h"
#include "utils/date.h"
#include "utils/builtins.h"
#include "utils/nabstime.h"
#include <sys/time.h>
#include <stdlib.h>
#include "orafunc.h"
#include "builtins.h"
/*
* External (defined in PgSQL datetime.c (timestamp utils))
*/
#if PG_VERSION_NUM >= 90400
#define STRING_PTR_FIELD_TYPE const char *const
#else
#define STRING_PTR_FIELD_TYPE char *
#endif
extern PGDLLIMPORT STRING_PTR_FIELD_TYPE days[];
extern int ora_seq_search(const char *name, STRING_PTR_FIELD_TYPE array[], int max);
PG_FUNCTION_INFO_V1(plvdate_add_bizdays);
PG_FUNCTION_INFO_V1(plvdate_nearest_bizday);
PG_FUNCTION_INFO_V1(plvdate_next_bizday);
PG_FUNCTION_INFO_V1(plvdate_bizdays_between);
PG_FUNCTION_INFO_V1(plvdate_prev_bizday);
PG_FUNCTION_INFO_V1(plvdate_isbizday);
PG_FUNCTION_INFO_V1(plvdate_set_nonbizday_dow);
PG_FUNCTION_INFO_V1(plvdate_unset_nonbizday_dow);
PG_FUNCTION_INFO_V1(plvdate_set_nonbizday_day);
PG_FUNCTION_INFO_V1(plvdate_unset_nonbizday_day);
PG_FUNCTION_INFO_V1(plvdate_use_easter);
PG_FUNCTION_INFO_V1(plvdate_using_easter);
PG_FUNCTION_INFO_V1(plvdate_include_start);
PG_FUNCTION_INFO_V1(plvdate_including_start);
PG_FUNCTION_INFO_V1(plvdate_default_holidays);
PG_FUNCTION_INFO_V1(plvdate_version);
PG_FUNCTION_INFO_V1(plvdate_days_inmonth);
PG_FUNCTION_INFO_V1(plvdate_isleapyear);
#define CHECK_SEQ_SEARCH(_l, _s) \
do { \
if ((_l) < 0) { \
ereport(ERROR, \
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), \
errmsg("invalid value for %s", (_s)))); \
} \
} while (0)
#define SUNDAY (1 << 0)
#define SATURDAY (1 << 6)
static unsigned char nonbizdays = SUNDAY | SATURDAY;
static bool use_easter = true;
static bool include_start = true;
#define MAX_holidays 30
#define MAX_EXCEPTIONS 50
typedef struct {
char day;
char month;
} holiday_desc;
typedef struct {
unsigned char nonbizdays;
bool use_easter;
holiday_desc *holidays;
int holidays_c;
} cultural_info;
static holiday_desc holidays[MAX_holidays]; /* sorted array */
static DateADT exceptions[MAX_EXCEPTIONS]; /* sorted array */
static int holidays_c = 0;
static int exceptions_c = 0;
static holiday_desc czech_holidays[] = {
{1,1}, // Novy rok
{1,5}, // Svatek prace
{8,5}, // Den osvobozeni
{5,7}, // Den slovanskych verozvestu
{6,7}, // Den upaleni mistra Jana Husa
{28,9}, // Den ceske statnosti
{28,10}, // Den vzniku samostatneho ceskoslovenskeho statu
{17,11}, // Den boje za svobodu a demokracii
{24,12}, // Stedry den
{25,12}, // 1. svatek vanocni
{26,12} // 2. svatek vanocni
};
static holiday_desc germany_holidays[] = {
{1,1},{1,5},{25,5},{4,6},{5,6},
{15,8},{3,10},{25,12},{26,12}
};
static holiday_desc poland_holidays[] = {
{1,1},{1,5},{3,5},{15,6},{15,8},
{1,11},{11,11},{25,12},{26,12}
};
static holiday_desc austria_holidays[] = {
{1,1},{6,1},{1,5},{25,5},{4,6},
{5,6},{15,6},{15,8},{26,10},{1,11},
{8,12},{25,12},{26,12}
};
static holiday_desc slovakia_holidays[] = {
{1,1},{6,1},{1,5},{8,5},{5,7},
{29,8},{1,9},{15,9},{1,11},{17,11},
{24,12},{25,12},{26,12}
};
static holiday_desc russian_holidays[] = {
{1,1},{2,1},{3,1},{4,1},{5,1},
{7,1},{23,2},{8,3},{1,5},{9,5},
{12,6}, {4,11}
};
static holiday_desc england_holidays[] = {
{1,1},{2,1},{1,5},{29,5},{28,8},
{25,12},{26,12}
};
static holiday_desc usa_holidays[] = {
{1,1},{16,1},{20,2},{29,5},{4,7},
{4,9},{9,10},{11,11},{23,11},{25,12}
};
cultural_info defaults_ci[] = {
{SUNDAY | SATURDAY, true, czech_holidays, 11},
{SUNDAY | SATURDAY, true, germany_holidays, 9},
{SUNDAY | SATURDAY, true, poland_holidays, 9},
{SUNDAY | SATURDAY, true, austria_holidays, 13},
{SUNDAY | SATURDAY, true, slovakia_holidays, 13},
{SUNDAY | SATURDAY, false, russian_holidays, 12},
{SUNDAY | SATURDAY, true, england_holidays, 7},
{SUNDAY | SATURDAY, false, usa_holidays, 10}
};
STRING_PTR_FIELD_TYPE states[] = {
"Czech", "Germany", "Poland",
"Austria", "Slovakia", "Russia",
"Gb", "Usa",
NULL,
};
static int
dateadt_comp(const void* a, const void* b)
{
DateADT *_a = (DateADT*)a;
DateADT *_b = (DateADT*)b;
return *_a - *_b;
}
static int
holiday_desc_comp(const void* a, const void* b)
{
int result;
if (0 == (result = ((holiday_desc*)a)->month - ((holiday_desc*)b)->month))
result = ((holiday_desc*)a)->day - ((holiday_desc*)b)->day;
return result;
}
static void
easter_sunday(int year, int* dd, int* mm)
{
int b, d, e, q;
if (year < 1900 || year > 2099)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("date is out of range"),
errdetail("Easter is defined only for years between 1900 and 2099")));
b = 255 - 11 * (year % 19);
d = ((b - 21) % 30) + 21;
if (d > 38) d -= 1;
e = (year + year/4 + d + 1) % 7;
q = d + 7 - e;
if (q < 32)
{
*dd = q; *mm = 3;
}
else
{
*dd = q - 31; *mm = 4;
}
}
static Datum
ora_add_bizdays(DateADT day, int days)
{
int d, dx;
int y, m, auxd;
holiday_desc hd;
d = j2day(day+POSTGRES_EPOCH_JDATE);
dx = days > 0? 1 : -1;
while (days != 0)
{
d = (d+dx) % 7;
d = (d < 0) ? 6:d;
day += dx;
if ((1 << d) & nonbizdays)
continue;
if (NULL != bsearch(&day, exceptions, exceptions_c,
sizeof(DateADT), dateadt_comp))
continue;
j2date(day + POSTGRES_EPOCH_JDATE, &y, &m, &auxd);
hd.day = (char) auxd;
hd.month = (char) m;
if (use_easter && (m == 3 || m == 4))
{
easter_sunday(y, &auxd, &m);
if (m == hd.month && (auxd == hd.day || d+1 == hd.day))
continue;
}
if (NULL != bsearch(&hd, holidays, holidays_c,
sizeof(holiday_desc), holiday_desc_comp))
continue;
days -= dx;
}
return day;
}
static int
ora_diff_bizdays(DateADT day1, DateADT day2)
{
int d, days;
int y, m, auxd;
holiday_desc hd;
int cycle_c = 0;
bool start_is_bizday = false;
DateADT aux_day;
if (day1 > day2)
{
aux_day = day1;
day1 = day2; day2 = aux_day;
}
d = j2day(day1+POSTGRES_EPOCH_JDATE);
days = 0;
while (day1 <= day2)
{
++ cycle_c;
d = (d+1) % 7;
d = (d < 0) ? 6:d;
day1 += 1;
if ((1 << d) & nonbizdays)
continue;
if (NULL != bsearch(&day1, exceptions, exceptions_c,
sizeof(DateADT), dateadt_comp))
continue;
j2date(day1 + POSTGRES_EPOCH_JDATE, &y, &m, &auxd);
hd.day = (char) auxd;
hd.month = (char) m;
if (use_easter && (m == 3 || m == 4))
{
easter_sunday(y, &auxd, &m);
if (m == hd.month && (auxd == hd.day || d+1 == hd.day))
continue;
}
if (NULL != bsearch(&hd, holidays, holidays_c,
sizeof(holiday_desc), holiday_desc_comp))
continue;
days += 1;
if (cycle_c == 1)
start_is_bizday = true;
}
if (include_start && start_is_bizday && days >= 1)
days -= 1;
return days;
}
/****************************************************************
* PLVdate.add_bizdays
*
* Syntax:
* FUNCTION add_bizdays(IN dt DATE, IN days int) RETURNS DATE;
*
* Purpouse:
* Get the date created by adding <n> business days to a date
*
****************************************************************/
Datum
plvdate_add_bizdays (PG_FUNCTION_ARGS)
{
DateADT day = PG_GETARG_DATEADT(0);
int days = PG_GETARG_INT32(1);
PG_RETURN_DATEADT(ora_add_bizdays(day,days));
}
/****************************************************************
* PLVdate.nearest_bizday
*
* Syntax:
* FUNCTION nearest_bizday(IN dt DATE) RETURNS DATE;
*
* Purpouse:
* Get the nearest business date to a given date, user defined
*
****************************************************************/
Datum
plvdate_nearest_bizday (PG_FUNCTION_ARGS)
{
DateADT dt = PG_GETARG_DATEADT(0);
DateADT d1, d2, res;
d1 = ora_add_bizdays(dt, -1);
d2 = ora_add_bizdays(dt, 1);
if ((dt - d1) > (d2 - dt))
res = d2;
else
res = d1;
PG_RETURN_DATEADT(res);
}
/****************************************************************
* PLVdate.next_bizday
*
* Syntax:
* FUNCTION next_bizday(IN dt DATE) RETURNS DATE;
*
* Purpouse:
* Get the next business date from a given date, user defined
*
****************************************************************/
Datum
plvdate_next_bizday (PG_FUNCTION_ARGS)
{
DateADT day = PG_GETARG_DATEADT(0);
PG_RETURN_DATEADT(ora_add_bizdays(day,1));
}
/****************************************************************
* PLVdate.bizdays_between
*
* Syntax:
* FUNCTION bizdays_between(IN dt1 DATE, IN dt2 DATE)
* RETURNS int;
*
* Purpouse:
* Get the number of business days between two dates
*
****************************************************************/
Datum
plvdate_bizdays_between (PG_FUNCTION_ARGS)
{
DateADT day1 = PG_GETARG_DATEADT(0);
DateADT day2 = PG_GETARG_DATEADT(1);
PG_RETURN_INT32(ora_diff_bizdays(day1,day2));
}
/****************************************************************
* PLVdate.prev_bizday
*
* Syntax:
* FUNCTION prev_bizday(IN dt DATE) RETURNS date;
*
* Purpouse:
* Get the previous business date from a given date, user
* defined
*
****************************************************************/
Datum
plvdate_prev_bizday (PG_FUNCTION_ARGS)
{
DateADT day = PG_GETARG_DATEADT(0);
PG_RETURN_DATEADT(ora_add_bizdays(day,-1));
}
/****************************************************************
* PLVdate.isbizday
*
* Syntax:
* FUNCTION isbizday(IN dt DATE) RETURNS bool;
*
* Purpouse:
* Call this function to determine if a date is a business day
*
****************************************************************/
Datum
plvdate_isbizday (PG_FUNCTION_ARGS)
{
DateADT day = PG_GETARG_DATEADT(0);
int y, m, d;
holiday_desc hd;
if (0 != ((1 << j2day(day+POSTGRES_EPOCH_JDATE)) & nonbizdays))
return false;
if (NULL != bsearch(&day, exceptions, exceptions_c,
sizeof(DateADT), dateadt_comp))
return false;
j2date(day + POSTGRES_EPOCH_JDATE, &y, &m, &d);
hd.month = m; hd.day = d;
if (use_easter && (m == 3 || m == 4))
{
easter_sunday(y, &d, &m);
if (m == hd.month && (d == hd.day || d+1 == hd.day))
return false;
}
PG_RETURN_BOOL (NULL == bsearch(&hd, holidays, holidays_c,
sizeof(holiday_desc), holiday_desc_comp));
}
/****************************************************************
* PLVdate.set_nonbizday
*
* Syntax:
* FUNCTION set_nonbizday(IN dow VARCHAR) RETURNS void;
*
* Purpouse:
* Set day of week as non bussines day
*
****************************************************************/
Datum
plvdate_set_nonbizday_dow (PG_FUNCTION_ARGS)
{
unsigned char check;
text *day_txt = PG_GETARG_TEXT_PP(0);
int d = ora_seq_search(VARDATA_ANY(day_txt), days, VARSIZE_ANY_EXHDR(day_txt));
CHECK_SEQ_SEARCH(d, "DAY/Day/day");
check = nonbizdays | (1 << d);
if (check == 0x7f)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("nonbizday registeration error"),
errdetail("Constraint violation."),
errhint("One day in week have to be bizday.")));
nonbizdays = nonbizdays | (1 << d);
PG_RETURN_VOID();
}
/****************************************************************
* PLVdate.unset_nonbizday
*
* Syntax:
* FUNCTION unset_nonbizday(IN dow VARCHAR) RETURNS void;
*
* Purpouse:
* Unset day of week as non bussines day
*
****************************************************************/
Datum
plvdate_unset_nonbizday_dow (PG_FUNCTION_ARGS)
{
text *day_txt = PG_GETARG_TEXT_PP(0);
int d = ora_seq_search(VARDATA_ANY(day_txt), days, VARSIZE_ANY_EXHDR(day_txt));
CHECK_SEQ_SEARCH(d, "DAY/Day/day");
nonbizdays = (nonbizdays | (1 << d)) ^ (1 << d);
PG_RETURN_VOID();
}
/****************************************************************
* PLVdate.set_nonbizday
*
* Syntax:
* FUNCTION set_nonbizday(IN day DATE) RETURNS void;
* FUNCTION set_nonbizday(IN day DATE, IN repeat := false BOOL) RETURNS void;
*
* Purpouse:
* Set day as non bussines day, second arg specify year's
* periodicity
*
****************************************************************/
Datum
plvdate_set_nonbizday_day (PG_FUNCTION_ARGS)
{
DateADT arg1 = PG_GETARG_DATEADT(0);
bool arg2 = PG_GETARG_BOOL(1);
int y, m, d;
holiday_desc hd;
if (arg2)
{
if (holidays_c == MAX_holidays)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("nonbizday registeration error"),
errdetail("Too much registered nonbizdays."),
errhint("Increase MAX_holidays in 'plvdate.c'.")));
j2date(arg1 + POSTGRES_EPOCH_JDATE, &y, &m, &d);
hd.month = m; hd.day = d;
if (NULL != bsearch(&hd, holidays, holidays_c, sizeof(holiday_desc), holiday_desc_comp))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("nonbizday registeration error"),
errdetail("Date is registered.")));
holidays[holidays_c].month = m;
holidays[holidays_c].day = d;
holidays_c += 1;
qsort(holidays, holidays_c, sizeof(holiday_desc), holiday_desc_comp);
}
else
{
if (exceptions_c == MAX_EXCEPTIONS)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("nonbizday registeration error"),
errdetail("Too much registered nonrepeated nonbizdays."),
errhint("Increase MAX_EXCEPTIONS in 'plvdate.c'.")));
if (NULL != bsearch(&arg1, exceptions, exceptions_c, sizeof(DateADT), dateadt_comp))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("nonbizday registeration error"),
errdetail("Date is registered.")));
exceptions[exceptions_c++] = arg1;
qsort(exceptions, exceptions_c, sizeof(DateADT), dateadt_comp);
}
PG_RETURN_VOID();
}
/****************************************************************
* PLVdate.unset_nonbizday
*
* Syntax:
* FUNCTION unset_nonbizday(IN day DATE) RETURNS void;
* FUNCTION unset_nonbizday(IN day DATE, IN repeat := false BOOL) RETURNS void;
*
* Purpouse:
* Unset day as non bussines day, second arg specify year's
* periodicity
*
****************************************************************/
Datum
plvdate_unset_nonbizday_day (PG_FUNCTION_ARGS)
{
DateADT arg1 = PG_GETARG_DATEADT(0);
bool arg2 = PG_GETARG_BOOL(1);
int y, m, d;
bool found = false;
int i;
if (arg2)
{
j2date(arg1 + POSTGRES_EPOCH_JDATE, &y, &m, &d);
for (i = 0; i < holidays_c; i++)
{
if (!found && holidays[i].month == m && holidays[i].day == d)
found = true;
else if (found)
{
holidays[i-1].month = holidays[i].month;
holidays[i-1].day = holidays[i].day;
}
}
if (found)
holidays_c -= 1;
}
else
{
for (i = 0; i < exceptions_c; i++)
if (!found && exceptions[i] == arg1)
found = true;
else if (found)
exceptions[i-1] = exceptions[i];
if (found)
exceptions_c -= 1;
}
if (!found)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("nonbizday unregisteration error"),
errdetail("Nonbizday not found.")));
PG_RETURN_VOID();
}
/****************************************************************
* PLVdate.use_easter
*
* Syntax:
* FUNCTION unuse_easter() RETURNS void;
* FUNCTION use_easter() RETURNS void;
* FUNCTION use_easter(IN bool) RETURNS void
*
* Purpouse:
* Have to use easter as nonbizday?
*
****************************************************************/
Datum
plvdate_use_easter (PG_FUNCTION_ARGS)
{
use_easter = PG_GETARG_BOOL(0);
PG_RETURN_VOID();
}
/****************************************************************
* PLVdate.using_easter
*
* Syntax:
* FUNCTION using_easter() RETURNS bool
*
* Purpouse:
* Use it easter as nonbizday?
*
****************************************************************/
Datum
plvdate_using_easter (PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(use_easter);
}
/****************************************************************
* PLVdate.include_start
*
* Syntax:
* FUNCTION include_start() RETURNS void;
* FUNCTION noinclude_start() RETURNS void;
* FUNCTION include_start(IN bool) RETURNS void
*
* Purpouse:
* Have to include current day in bizdays_between calculation?
*
****************************************************************/
Datum
plvdate_include_start (PG_FUNCTION_ARGS)
{
include_start = PG_GETARG_BOOL(0);
PG_RETURN_VOID();
}
/****************************************************************
* PLVdate.including_start
*
* Syntax:
* FUNCTION including_start() RETURNS bool
*
* Purpouse:
* include current day in bizdays_between calculation?
*
****************************************************************/
Datum
plvdate_including_start (PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(include_start);
}
/*
* Load some national configurations
*
*/
Datum
plvdate_default_holidays (PG_FUNCTION_ARGS)
{
text *country = PG_GETARG_TEXT_PP(0);
int c = ora_seq_search(VARDATA_ANY(country), states, VARSIZE_ANY_EXHDR(country));
CHECK_SEQ_SEARCH(c, "STATE/State/state");
nonbizdays = defaults_ci[c].nonbizdays;
use_easter = defaults_ci[c].use_easter;
exceptions_c = 0;
holidays_c = defaults_ci[c].holidays_c;
memcpy(holidays, defaults_ci[c].holidays, holidays_c*sizeof(holiday_desc));
PG_RETURN_VOID();
}
/*
* helper maintaince functions
*/
Datum
plvdate_version (PG_FUNCTION_ARGS)
{
PG_RETURN_CSTRING(PLVDATE_VERSION);
}
/****************************************************************
* PLVdate.days_inmonth
*
* Syntax:
* FUNCTION days_inmonth(date) RETURNS integer
*
* Purpouse:
* Returns month's length
*
****************************************************************/
Datum
plvdate_days_inmonth(PG_FUNCTION_ARGS)
{
DateADT day = PG_GETARG_DATEADT(0);
int result;
int y, m, d;
j2date(day + POSTGRES_EPOCH_JDATE, &y, &m, &d);
result = date2j(y, m+1, 1) - date2j(y, m, 1);
PG_RETURN_INT32(result);
}
/****************************************************************
* PLVdate.isleapyear
*
* Syntax:
* FUNCTION isleapyear() RETURNS bool
*
* Purpouse:
* Returns true, if year is leap
*
****************************************************************/
Datum
plvdate_isleapyear(PG_FUNCTION_ARGS)
{
DateADT day = PG_GETARG_DATEADT(0);
int y, m, d;
bool result;
j2date(day + POSTGRES_EPOCH_JDATE, &y, &m, &d);
result = ((( y % 4) == 0) && ((y % 100) != 0)) || ((y / 400) == 0);
PG_RETURN_BOOL(result);
}
/****************************************************************
* PLVdate.set_nonbizdays
*
* Syntax:
* FUNCTION set_nonbizdays(IN dow bool[7]) RETURNS void;
*
* Purpouse:
* Set pattern bussines/nonbussines days in week
*
****************************************************************/
/****************************************************************
* PLVdate.set_nonbizday
*
* Syntax:
* FUNCTION set_nonbizdays(IN days DATE[]) RETURNS void;
* FUNCTION set_nonbizdays(IN days DATE[], IN repeat := false BOOL) RETURNS void;
*
* Purpouse:
* Set days as non bussines day, second arg specify year's
* periodicity
*
****************************************************************/
/****************************************************************
* PLVdate.display
*
* Syntax:
* FUNCTION display() RETURNS void;
*
* Purpouse:
* Show current calendar
*
****************************************************************/