-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTUTIL.RPGLE
More file actions
1300 lines (1046 loc) · 31 KB
/
Copy pathDTUTIL.RPGLE
File metadata and controls
1300 lines (1046 loc) · 31 KB
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
**FREE
// ================================================================
// DTUTIL -- Date Utility Service Program for RPG IV
// ================================================================
//
// A collection of date procedures that every RPG shop writes
// from scratch at least once, usually badly, always at 4pm
// on the day before go-live. This aims to be the last time.
//
// Covers: business days, ISO weeks, day-count conventions,
// quarter arithmetic, holiday awareness, date validation,
// and the general calendar misery that comes from living
// on a planet with a 365.2422-day orbital period.
//
// Written in fully free-form ILE RPG IV (V7R3+).
// Distributed via MPB (Mainframe Package Bureau).
//
// (c) 2026 Zane Hambly. Apache 2.0.
// ================================================================
CTL-OPT NOMAIN;
// ---- Exported Procedures ----
// ================================================================
// dt_isleap -- Is this year a leap year?
// ================================================================
// The Gregorian calendar: where every rule has an exception
// and every exception has an exception to the exception.
DCL-PROC dt_isleap EXPORT;
DCL-PI dt_isleap IND;
year INT(10) CONST;
END-PI;
IF %REM(year : 400) = 0;
RETURN *ON;
ENDIF;
IF %REM(year : 100) = 0;
RETURN *OFF;
ENDIF;
IF %REM(year : 4) = 0;
RETURN *ON;
ENDIF;
RETURN *OFF;
END-PROC;
// ================================================================
// dt_dim -- Days in month (1-12). Returns 0 for invalid.
// ================================================================
// February: the month that can't commit to a length.
DCL-PROC dt_dim EXPORT;
DCL-PI dt_dim INT(10);
year INT(10) CONST;
month INT(10) CONST;
END-PI;
DCL-S days INT(10) DIM(12) INZ;
days(1) = 31;
days(2) = 28;
days(3) = 31;
days(4) = 30;
days(5) = 31;
days(6) = 30;
days(7) = 31;
days(8) = 31;
days(9) = 30;
days(10) = 31;
days(11) = 30;
days(12) = 31;
IF month < 1 OR month > 12;
RETURN 0;
ENDIF;
IF month = 2 AND dt_isleap(year);
RETURN 29;
ENDIF;
RETURN days(month);
END-PROC;
// ================================================================
// dt_doy -- Day of year (1-366). The Julian day number
// that JCL programmers have been using since 1964.
// ================================================================
DCL-PROC dt_doy EXPORT;
DCL-PI dt_doy INT(10);
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S m INT(10);
DCL-S d INT(10);
DCL-S i INT(10);
DCL-S doy INT(10);
y = %SUBDT(inDate : *YEAR);
m = %SUBDT(inDate : *MONTH);
d = %SUBDT(inDate : *DAY);
doy = 0;
FOR i = 1 TO m - 1;
doy = doy + dt_dim(y : i);
ENDFOR;
doy = doy + d;
RETURN doy;
END-PROC;
// ================================================================
// dt_dow -- Day of week (1=Monday .. 7=Sunday, ISO 8601).
// ================================================================
// Zeller's congruence, adapted for people who think weeks
// start on Monday like civilised nations.
DCL-PROC dt_dow EXPORT;
DCL-PI dt_dow INT(10);
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S m INT(10);
DCL-S d INT(10);
DCL-S q INT(10);
DCL-S k INT(10);
DCL-S j INT(10);
DCL-S h INT(10);
DCL-S w INT(10);
y = %SUBDT(inDate : *YEAR);
m = %SUBDT(inDate : *MONTH);
d = %SUBDT(inDate : *DAY);
// Zeller treats Jan/Feb as months 13/14 of prior year
IF m < 3;
m = m + 12;
y = y - 1;
ENDIF;
q = d;
k = %REM(y : 100);
j = %DIV(y : 100);
// Zeller's formula gives 0=Saturday
h = %REM(q + %DIV(13 * (m + 1) : 5) + k
+ %DIV(k : 4) + %DIV(j : 4) - 2 * j : 7);
// Adjust negatives — RPG %REM can return negative
IF h < 0;
h = h + 7;
ENDIF;
// Convert: Zeller 0=Sat → ISO 1=Mon..7=Sun
// Zeller: 0=Sat 1=Sun 2=Mon 3=Tue 4=Wed 5=Thu 6=Fri
// ISO: 6=Sat 7=Sun 1=Mon 2=Tue 3=Wed 4=Thu 5=Fri
SELECT;
WHEN h = 0;
w = 6; // Saturday
WHEN h = 1;
w = 7; // Sunday
OTHER;
w = h - 1;
ENDSL;
RETURN w;
END-PROC;
// ================================================================
// dt_isoweek -- ISO 8601 week number (1-53).
// ================================================================
// Week 1 is the week containing the first Thursday of January.
// This means 29 Dec can be week 1 of next year, and 1 Jan
// can be week 53 of last year. Calendar pedantry at its finest.
DCL-PROC dt_isoweek EXPORT;
DCL-PI dt_isoweek INT(10);
inDate DATE CONST;
END-PI;
DCL-S doy INT(10);
DCL-S dow INT(10);
DCL-S wk INT(10);
DCL-S y INT(10);
DCL-S jan1dow INT(10);
DCL-S jan1 DATE;
DCL-S dec31 DATE;
y = %SUBDT(inDate : *YEAR);
doy = dt_doy(inDate);
dow = dt_dow(inDate);
// ISO week: wk = (doy - dow + 10) / 7
wk = %DIV(doy - dow + 10 : 7);
IF wk < 1;
// Belongs to last week of previous year
// Dec 31 of previous year tells us how many weeks
dec31 = %DATE(y - 1 * 10000 + 1231 : *ISO0);
RETURN dt_isoweek(dec31);
ENDIF;
IF wk = 53;
// Check if this actually belongs to week 1 of next year
jan1 = %DATE((y + 1) * 10000 + 101 : *ISO0);
jan1dow = dt_dow(jan1);
// Week 53 only valid if year starts on Thursday,
// or leap year starts on Wednesday
IF jan1dow <> 5;
IF NOT (jan1dow = 4 AND dt_isleap(y));
wk = 1;
ENDIF;
ENDIF;
ENDIF;
RETURN wk;
END-PROC;
// ================================================================
// dt_isowkyr -- ISO week-year (may differ from calendar year).
// ================================================================
// The year in which your ISO week lives. Sometimes disagrees
// with the calendar year by ±1, which is excellent for
// generating confused tickets from QA.
DCL-PROC dt_isowkyr EXPORT;
DCL-PI dt_isowkyr INT(10);
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S wk INT(10);
DCL-S m INT(10);
y = %SUBDT(inDate : *YEAR);
m = %SUBDT(inDate : *MONTH);
wk = dt_isoweek(inDate);
// If week >= 52 but month is January → previous year
IF wk >= 52 AND m = 1;
RETURN y - 1;
ENDIF;
// If week = 1 but month is December → next year
IF wk = 1 AND m = 12;
RETURN y + 1;
ENDIF;
RETURN y;
END-PROC;
// ================================================================
// dt_qtr -- Quarter (1-4) from a date.
// ================================================================
// Q1 = Jan-Mar, Q2 = Apr-Jun, Q3 = Jul-Sep, Q4 = Oct-Dec.
// Unless you're a government fiscal year, in which case
// all bets are off and you should adjust accordingly.
DCL-PROC dt_qtr EXPORT;
DCL-PI dt_qtr INT(10);
inDate DATE CONST;
END-PI;
DCL-S m INT(10);
m = %SUBDT(inDate : *MONTH);
RETURN %DIV(m - 1 : 3) + 1;
END-PROC;
// ================================================================
// dt_qtrst -- First day of the quarter containing inDate.
// ================================================================
// The day the board starts asking "how are we tracking?"
DCL-PROC dt_qtrst EXPORT;
DCL-PI dt_qtrst DATE;
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S q INT(10);
DCL-S m INT(10);
y = %SUBDT(inDate : *YEAR);
q = dt_qtr(inDate);
m = (q - 1) * 3 + 1;
RETURN %DATE(y * 10000 + m * 100 + 1 : *ISO0);
END-PROC;
// ================================================================
// dt_qtred -- Last day of the quarter containing inDate.
// ================================================================
// The day the board finds out the answer was "not well."
DCL-PROC dt_qtred EXPORT;
DCL-PI dt_qtred DATE;
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S q INT(10);
DCL-S m INT(10);
DCL-S d INT(10);
y = %SUBDT(inDate : *YEAR);
q = dt_qtr(inDate);
m = q * 3;
d = dt_dim(y : m);
RETURN %DATE(y * 10000 + m * 100 + d : *ISO0);
END-PROC;
// ================================================================
// dt_eom -- Last day of the month containing inDate.
// ================================================================
// The "end of month" question: sounds simple until
// you try to explain it to an auditor.
DCL-PROC dt_eom EXPORT;
DCL-PI dt_eom DATE;
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S m INT(10);
DCL-S d INT(10);
y = %SUBDT(inDate : *YEAR);
m = %SUBDT(inDate : *MONTH);
d = dt_dim(y : m);
RETURN %DATE(y * 10000 + m * 100 + d : *ISO0);
END-PROC;
// ================================================================
// dt_bom -- First day of the month containing inDate.
// ================================================================
// When the direct debits hit and the account weeps.
DCL-PROC dt_bom EXPORT;
DCL-PI dt_bom DATE;
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S m INT(10);
y = %SUBDT(inDate : *YEAR);
m = %SUBDT(inDate : *MONTH);
RETURN %DATE(y * 10000 + m * 100 + 1 : *ISO0);
END-PROC;
// ================================================================
// dt_iswkd -- Is this a weekday? (Monday-Friday)
// ================================================================
// The question every payroll system asks 260 times a year.
DCL-PROC dt_iswkd EXPORT;
DCL-PI dt_iswkd IND;
inDate DATE CONST;
END-PI;
DCL-S dow INT(10);
dow = dt_dow(inDate);
RETURN (dow >= 1 AND dow <= 5);
END-PROC;
// ================================================================
// dt_iswke -- Is this a weekend? (Saturday-Sunday)
// ================================================================
// The inverse of dt_iswkd. Exists because typing
// NOT dt_iswkd() apparently constitutes "too much effort."
DCL-PROC dt_iswke EXPORT;
DCL-PI dt_iswke IND;
inDate DATE CONST;
END-PI;
RETURN NOT dt_iswkd(inDate);
END-PROC;
// ================================================================
// dt_nxwkd -- Next weekday on or after inDate.
// ================================================================
// If it's Friday 5pm you get Monday. Sorry about that.
DCL-PROC dt_nxwkd EXPORT;
DCL-PI dt_nxwkd DATE;
inDate DATE CONST;
END-PI;
DCL-S d DATE;
DCL-S i INT(10);
d = inDate;
FOR i = 1 TO 7;
IF dt_iswkd(d);
RETURN d;
ENDIF;
d = d + %DAYS(1);
ENDFOR;
// Shouldn't reach here unless calendars are broken
RETURN d;
END-PROC;
// ================================================================
// dt_pvwkd -- Previous weekday on or before inDate.
// ================================================================
// For when you needed that report yesterday. Literally.
DCL-PROC dt_pvwkd EXPORT;
DCL-PI dt_pvwkd DATE;
inDate DATE CONST;
END-PI;
DCL-S d DATE;
DCL-S i INT(10);
d = inDate;
FOR i = 1 TO 7;
IF dt_iswkd(d);
RETURN d;
ENDIF;
d = d - %DAYS(1);
ENDFOR;
RETURN d;
END-PROC;
// ================================================================
// dt_bdays -- Business days between two dates (exclusive end).
// ================================================================
// Counts weekdays only. Does NOT account for holidays —
// call dt_hdays for that, or subtract them yourself.
// The two dates need not be in order; result is always >= 0.
DCL-PROC dt_bdays EXPORT;
DCL-PI dt_bdays INT(10);
date1 DATE CONST;
date2 DATE CONST;
END-PI;
DCL-S d1 DATE;
DCL-S d2 DATE;
DCL-S cnt INT(10);
DCL-S total INT(10);
DCL-S weeks INT(10);
DCL-S extra INT(10);
DCL-S d1dow INT(10);
DCL-S d2dow INT(10);
DCL-S i INT(10);
// Normalise order
IF date1 <= date2;
d1 = date1;
d2 = date2;
ELSE;
d1 = date2;
d2 = date1;
ENDIF;
total = %DIFF(d2 : d1 : *DAYS);
IF total = 0;
RETURN 0;
ENDIF;
// Full weeks contribute 5 business days each
weeks = %DIV(total : 7);
extra = %REM(total : 7);
cnt = weeks * 5;
// Count remaining days manually (bounded: max 6 iterations)
d1 = d1 + %DAYS(weeks * 7);
FOR i = 1 TO extra;
IF dt_iswkd(d1);
cnt = cnt + 1;
ENDIF;
d1 = d1 + %DAYS(1);
ENDFOR;
RETURN cnt;
END-PROC;
// ================================================================
// dt_addbd -- Add N business days to a date.
// ================================================================
// If N=0 returns inDate. If N>0 skips weekends forward.
// If N<0 skips weekends backward.
DCL-PROC dt_addbd EXPORT;
DCL-PI dt_addbd DATE;
inDate DATE CONST;
nDays INT(10) CONST;
END-PI;
DCL-S d DATE;
DCL-S rem INT(10);
DCL-S dir INT(10);
DCL-S i INT(10);
d = inDate;
IF nDays = 0;
RETURN d;
ENDIF;
IF nDays > 0;
dir = 1;
rem = nDays;
ELSE;
dir = -1;
rem = -nDays;
ENDIF;
// Bounded loop: worst case 2 weekend days per 5 business days
// so max iterations = rem * 2 + 10 (generous upper bound)
FOR i = 1 TO rem * 3;
d = d + %DAYS(dir);
IF dt_iswkd(d);
rem = rem - 1;
IF rem = 0;
RETURN d;
ENDIF;
ENDIF;
ENDFOR;
RETURN d;
END-PROC;
// ================================================================
// dt_valid -- Validate a date triplet (y, m, d).
// ================================================================
// Returns *ON if valid, *OFF otherwise. RPG's TEST opcode
// handles format validation, but this is for when you've
// already parsed the components and want to check them.
DCL-PROC dt_valid EXPORT;
DCL-PI dt_valid IND;
year INT(10) CONST;
month INT(10) CONST;
day INT(10) CONST;
END-PI;
DCL-S maxd INT(10);
IF year < 1 OR year > 9999;
RETURN *OFF;
ENDIF;
IF month < 1 OR month > 12;
RETURN *OFF;
ENDIF;
maxd = dt_dim(year : month);
IF day < 1 OR day > maxd;
RETURN *OFF;
ENDIF;
RETURN *ON;
END-PROC;
// ================================================================
// dt_easter -- Date of Easter Sunday for given year.
// ================================================================
// Computus: the Anonymous Gregorian algorithm. Published
// by the Papal Bull in 1582 and still causing arguments
// between Catholics and Orthodox Christians 440 years later.
//
// Valid for Gregorian calendar (1583-9999).
DCL-PROC dt_easter EXPORT;
DCL-PI dt_easter DATE;
year INT(10) CONST;
END-PI;
DCL-S a INT(10);
DCL-S b INT(10);
DCL-S c INT(10);
DCL-S d INT(10);
DCL-S e INT(10);
DCL-S f INT(10);
DCL-S g INT(10);
DCL-S h INT(10);
DCL-S i INT(10);
DCL-S k INT(10);
DCL-S l INT(10);
DCL-S m INT(10);
DCL-S p INT(10);
DCL-S em INT(10);
DCL-S ed INT(10);
a = %REM(year : 19);
b = %DIV(year : 100);
c = %REM(year : 100);
d = %DIV(b : 4);
e = %REM(b : 4);
f = %DIV(b + 8 : 25);
g = %DIV(b - f + 1 : 3);
h = %REM(19 * a + b - d - g + 15 : 30);
i = %DIV(c : 4);
k = %REM(c : 4);
l = %REM(32 + 2*e + 2*i - h - k : 7);
m = %DIV(a + 11*h + 22*l : 451);
p = h + l - 7*m + 114;
em = %DIV(p : 31);
ed = %REM(p : 31) + 1;
RETURN %DATE(year * 10000 + em * 100 + ed : *ISO0);
END-PROC;
// ================================================================
// dt_gfri -- Good Friday for given year.
// ================================================================
// Two days before Easter. The day the AS/400 gets a rest
// and the operators get a long weekend.
DCL-PROC dt_gfri EXPORT;
DCL-PI dt_gfri DATE;
year INT(10) CONST;
END-PI;
RETURN dt_easter(year) - %DAYS(2);
END-PROC;
// ================================================================
// dt_emon -- Easter Monday for given year.
// ================================================================
// The day after Easter. When batch jobs pile up because
// nobody scheduled the Monday catch-up run.
DCL-PROC dt_emon EXPORT;
DCL-PI dt_emon DATE;
year INT(10) CONST;
END-PI;
RETURN dt_easter(year) + %DAYS(1);
END-PROC;
// ================================================================
// dt_mtk -- Matariki public holiday date for given year.
// ================================================================
// Te Kāhui o Matariki Public Holiday Act 2022, Schedule 1.
// Returns the legislated Matariki Friday for 2022-2052.
// Returns D'0001-01-01' for years outside the table,
// because the stars haven't been consulted that far out yet.
//
// These dates follow the Maramataka (Māori lunar calendar)
// and the heliacal rising of the Matariki star cluster
// (Pleiades/M45). Not algorithmically derivable — each date
// was set by the Te Kāhui o Matariki advisory committee
// and enacted by Parliament. All fall on a Friday.
DCL-PROC dt_mtk EXPORT;
DCL-PI dt_mtk DATE;
year INT(10) CONST;
END-PI;
// 31 legislated dates, 2022-2052.
// Stored as MMDD for compactness — all are June or July.
DCL-S mmdd INT(10);
SELECT;
WHEN year = 2022; mmdd = 0624;
WHEN year = 2023; mmdd = 0714;
WHEN year = 2024; mmdd = 0628;
WHEN year = 2025; mmdd = 0620;
WHEN year = 2026; mmdd = 0710;
WHEN year = 2027; mmdd = 0625;
WHEN year = 2028; mmdd = 0714;
WHEN year = 2029; mmdd = 0706;
WHEN year = 2030; mmdd = 0621;
WHEN year = 2031; mmdd = 0711;
WHEN year = 2032; mmdd = 0702;
WHEN year = 2033; mmdd = 0624;
WHEN year = 2034; mmdd = 0707;
WHEN year = 2035; mmdd = 0629;
WHEN year = 2036; mmdd = 0718;
WHEN year = 2037; mmdd = 0710;
WHEN year = 2038; mmdd = 0625;
WHEN year = 2039; mmdd = 0715;
WHEN year = 2040; mmdd = 0706;
WHEN year = 2041; mmdd = 0719;
WHEN year = 2042; mmdd = 0711;
WHEN year = 2043; mmdd = 0703;
WHEN year = 2044; mmdd = 0624;
WHEN year = 2045; mmdd = 0707;
WHEN year = 2046; mmdd = 0629;
WHEN year = 2047; mmdd = 0719;
WHEN year = 2048; mmdd = 0703;
WHEN year = 2049; mmdd = 0625;
WHEN year = 2050; mmdd = 0715;
WHEN year = 2051; mmdd = 0630;
WHEN year = 2052; mmdd = 0621;
OTHER;
RETURN D'0001-01-01'; // outside legislated range
ENDSL;
RETURN %DATE(year * 10000 + mmdd : *ISO0);
END-PROC;
// ================================================================
// dt_nzhol -- Is this a New Zealand public holiday?
// ================================================================
// The Holidays Act 2003: 11 public holidays per year.
// Mondayisation rules apply: if a holiday falls on Saturday
// it moves to Monday, if on Sunday it moves to Monday.
// (Unless it's ANZAC Day or Waitangi Day — those only
// mondayise if they fall on Saturday or Sunday AND
// the employee would otherwise work that day. We take
// the simple approach: always mondayise.)
//
// Returns *ON if inDate is a NZ public holiday.
//
// Does NOT include regional anniversary days — those vary
// by province and are a whole separate argument.
DCL-PROC dt_nzhol EXPORT;
DCL-PI dt_nzhol IND;
inDate DATE CONST;
END-PI;
DCL-S y INT(10);
DCL-S m INT(10);
DCL-S d INT(10);
DCL-S dow INT(10);
DCL-S h DATE;
y = %SUBDT(inDate : *YEAR);
m = %SUBDT(inDate : *MONTH);
d = %SUBDT(inDate : *DAY);
dow = dt_dow(inDate);
// ---- Fixed holidays (with mondayisation) ----
// New Year's Day (1 Jan)
h = %DATE(y * 10000 + 101 : *ISO0);
IF dt_dow(h) = 6;
h = h + %DAYS(2); // Sat → Mon
ELSEIF dt_dow(h) = 7;
h = h + %DAYS(1); // Sun → Mon
ENDIF;
IF inDate = h;
RETURN *ON;
ENDIF;
// Day after New Year's (2 Jan)
h = %DATE(y * 10000 + 102 : *ISO0);
IF dt_dow(h) = 6;
h = h + %DAYS(2); // Sat → Mon
ELSEIF dt_dow(h) = 7;
h = h + %DAYS(2); // Sun → Tue (Mon is NY Day obs)
ENDIF;
IF inDate = h;
RETURN *ON;
ENDIF;
// Waitangi Day (6 Feb)
h = %DATE(y * 10000 + 206 : *ISO0);
IF dt_dow(h) = 6;
h = h + %DAYS(2);
ELSEIF dt_dow(h) = 7;
h = h + %DAYS(1);
ENDIF;
IF inDate = h;
RETURN *ON;
ENDIF;
// ANZAC Day (25 Apr)
h = %DATE(y * 10000 + 425 : *ISO0);
IF dt_dow(h) = 6;
h = h + %DAYS(2);
ELSEIF dt_dow(h) = 7;
h = h + %DAYS(1);
ENDIF;
IF inDate = h;
RETURN *ON;
ENDIF;
// Matariki — Te Kāhui o Matariki Public Holiday Act 2022
// Schedule 1: dates through 2052. Based on the Maramataka
// (Maori lunar calendar), not computable from first principles
// unless you fancy reimplementing Polynesian star navigation.
// Verified against legislation.govt.nz, 2026-03-26.
h = dt_mtk(y);
IF h > D'0001-01-03' AND inDate = h;
RETURN *ON;
ENDIF;
// King's Birthday — first Monday of June
h = %DATE(y * 10000 + 601 : *ISO0);
IF dt_dow(h) <> 1;
h = h + %DAYS(%REM(8 - dt_dow(h) : 7));
ENDIF;
IF inDate = h;
RETURN *ON;
ENDIF;
// Labour Day — fourth Monday of October
h = %DATE(y * 10000 + 1001 : *ISO0);
IF dt_dow(h) <> 1;
h = h + %DAYS(%REM(8 - dt_dow(h) : 7));
ENDIF;
h = h + %DAYS(21); // 4th Monday = 1st + 3 weeks
IF inDate = h;
RETURN *ON;
ENDIF;
// Christmas Day (25 Dec)
h = %DATE(y * 10000 + 1225 : *ISO0);
IF dt_dow(h) = 6;
h = h + %DAYS(2);
ELSEIF dt_dow(h) = 7;
h = h + %DAYS(1);
ENDIF;
IF inDate = h;
RETURN *ON;
ENDIF;
// Boxing Day (26 Dec)
h = %DATE(y * 10000 + 1226 : *ISO0);
IF dt_dow(h) = 6;
h = h + %DAYS(2);
ELSEIF dt_dow(h) = 7;
h = h + %DAYS(2); // Sun → Tue (Mon is Xmas obs)
ENDIF;
IF inDate = h;
RETURN *ON;
ENDIF;
// ---- Moveable holidays ----
// Good Friday
IF inDate = dt_gfri(y);
RETURN *ON;
ENDIF;
// Easter Monday
IF inDate = dt_emon(y);
RETURN *ON;
ENDIF;
RETURN *OFF;
END-PROC;
// ================================================================
// dt_nzbdy -- NZ business days between two dates.
// ================================================================
// Like dt_bdays but also excludes NZ public holidays.
DCL-PROC dt_nzbdy EXPORT;
DCL-PI dt_nzbdy INT(10);
date1 DATE CONST;
date2 DATE CONST;
END-PI;
DCL-S d1 DATE;
DCL-S d2 DATE;
DCL-S cnt INT(10);
DCL-S total INT(10);
DCL-S d DATE;
DCL-S i INT(10);
IF date1 <= date2;
d1 = date1;
d2 = date2;
ELSE;
d1 = date2;
d2 = date1;
ENDIF;
total = %DIFF(d2 : d1 : *DAYS);
cnt = 0;
d = d1;
FOR i = 1 TO total;
IF dt_iswkd(d) AND NOT dt_nzhol(d);
cnt = cnt + 1;
ENDIF;
d = d + %DAYS(1);
ENDFOR;
RETURN cnt;
END-PROC;
// ================================================================
// dt_a360 -- ACT/360 day count fraction.
// ================================================================
// Money market convention. Actual days / 360.
// Because bankers decided the year has 360 days
// and somehow nobody stopped them.
DCL-PROC dt_a360 EXPORT;
DCL-PI dt_a360 PACKED(15 : 10);
date1 DATE CONST;
date2 DATE CONST;
END-PI;
DCL-S days PACKED(15 : 10);
days = %DIFF(date2 : date1 : *DAYS);
RETURN days / 360;
END-PROC;
// ================================================================
// dt_a365 -- ACT/365 day count fraction.
// ================================================================
// ISDA convention. Actual days / 365.
// Slightly more honest than ACT/360 but still wrong.
DCL-PROC dt_a365 EXPORT;
DCL-PI dt_a365 PACKED(15 : 10);
date1 DATE CONST;
date2 DATE CONST;
END-PI;
DCL-S days PACKED(15 : 10);
days = %DIFF(date2 : date1 : *DAYS);
RETURN days / 365;
END-PROC;
// ================================================================
// dt_30360 -- 30/360 day count fraction (US/NASD method).
// ================================================================
// Pretend every month has 30 days. Bonds love this.
// The Excel YEARFRAC basis=0 convention.
DCL-PROC dt_30360 EXPORT;
DCL-PI dt_30360 PACKED(15 : 10);
date1 DATE CONST;
date2 DATE CONST;
END-PI;
DCL-S y1 INT(10);
DCL-S m1 INT(10);
DCL-S d1 INT(10);
DCL-S y2 INT(10);
DCL-S m2 INT(10);
DCL-S d2 INT(10);
DCL-S num PACKED(15 : 10);
y1 = %SUBDT(date1 : *YEAR);
m1 = %SUBDT(date1 : *MONTH);
d1 = %SUBDT(date1 : *DAY);
y2 = %SUBDT(date2 : *YEAR);
m2 = %SUBDT(date2 : *MONTH);
d2 = %SUBDT(date2 : *DAY);
// US/NASD 30/360 adjustment rules
IF d1 = 31;
d1 = 30;
ENDIF;
IF d2 = 31 AND d1 >= 30;
d2 = 30;
ENDIF;
num = 360 * (y2 - y1) + 30 * (m2 - m1) + (d2 - d1);
RETURN num / 360;
END-PROC;
// ================================================================