-
Notifications
You must be signed in to change notification settings - Fork 4
/
foxdates.prg
1212 lines (1151 loc) · 40.8 KB
/
foxdates.prg
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
*==============================================================================
* Program: FOXDATES.PRG
* Author: Rick Borup
* Date Written: 11/23/2019
* Compiler: Visual FoxPro 09.00.0000.7423 for Windows
* Abstract: A set of functions for working with dates and datetimes.
*==============================================================================
#DEFINE C_Version 2021.0
#DEFINE dc_nSecondsInADay 86400
DEFINE CLASS clsFoxDates AS Custom
*--------------------------------------------------------------------
* clsFoxDates :: Init
*------------------------------------
* Function: Class constructor
* Pass: Nothing
* Return: Logical
* Comments:
*--------------------------------------------------------------------
FUNCTION Init() as Logical
ENDFUNC && Init
*--------------------------------------------------------------------
* clsFoxDates :: Destroy
*------------------------------------
* Function: Class destructor
* Pass: Nothing
* Return: Nothing
* Comments:
*--------------------------------------------------------------------
PROCEDURE Destroy() as VOID
ENDPROC && Destroy
*--------------------------------------------------------------------
* clsFoxDates :: GetFirstOfMonth
*------------------------------------
* Function: Calculate the first day of the month
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
*--------------------------------------------------------------------
FUNCTION GetFirstOfMonth( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldDate, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = IIF( lcParmType = 'D', tdDate, TTOD( tdDate))
ENDIF
ldReturnDate = ldDate - DAY( ldDate) + 1
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate))
ENDFUNC && GetFirstOfMonth
*--------------------------------------------------------------------
* clsFoxDates :: GetLastOfMonth
*------------------------------------
* Function: Calculate the last day of the month
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
*--------------------------------------------------------------------
FUNCTION GetLastOfMonth( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldDate, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = IIF( lcParmType = 'D', tdDate, TTOD( tdDate))
ENDIF
ldReturnDate = GOMONTH( ldDate - DAY( ldDate) + 1, 1) - 1
* Another alternative...
* ldReturnDate = GOMONTH( tdDate, 1) - DAY( GOMONTH( tdDate, 1))
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate) + ( dc_nSecondsInADay -1))
ENDFUNC && GetLastOfMonth
*--------------------------------------------------------------------
* clsFoxDates :: GetDaysInMonth
*------------------------------------
* Function: Get the number of days in the specified month and year
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Integer - the number of days in the month
* Comments:
*--------------------------------------------------------------------
FUNCTION GetDaysInMonth( tdDate as Variant) as Integer
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldDate, ldFirstOfMonth, lnDaysInMonth
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = IIF( lcParmType = 'D', tdDate, TTOD( tdDate))
ENDIF
ldFirstOfMonth = DATE( YEAR( ldDate), MONTH( ldDate), 1) && first of the month
lnDaysInMonth = GOMONTH( ldFirstOfMonth, 1) - ldFirstOfMonth && days in that month
RETURN lnDaysInMonth
ENDFUNC && GetDaysInMonth
*--------------------------------------------------------------------
* clsFoxDates :: GetLastEOM
*------------------------------------
* Function: Calculate the last day of the previous month
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
*--------------------------------------------------------------------
FUNCTION GetLastEOM( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldDate, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = tdDate
ENDIF
ldReturnDate = this.GetLastOfMonth( GOMONTH( ldDate, -1)) && GOMONTH() always returns a 'date' datatype
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate) + ( dc_nSecondsInADay - 1))
ENDFUNC && GetLastEOM
*--------------------------------------------------------------------
* clsFoxDates :: GetBOQ
*------------------------------------
* Function: Calculate the beginning of the quarter date
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
* Calendar quarters are Jan-Mar, Apr-Jun, Jul-Sep, and Oct-Dec
*--------------------------------------------------------------------
FUNCTION GetBOQ( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = tdDate
ENDIF
LOCAL lnMonth
lnMonth = MONTH( ldDate)
lnMonth = ICASE( BETWEEN( lnMonth, 1, 3), 1, ;
BETWEEN( lnMonth, 4, 6), 4, ;
BETWEEN( lnMonth, 7, 9), 7, ;
10)
ldReturnDate = DATE( YEAR( ldDate), lnMonth, 1)
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate))
ENDFUNC && GetBOQ
*--------------------------------------------------------------------
* clsFoxDates :: GetLastBOQ
*------------------------------------
* Function: Calculate the beginning of the previous quarter
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
* Calendar quarters are Jan-Mar, Apr-Jun, Jul-Sep, and Oct-Dec
*--------------------------------------------------------------------
FUNCTION GetLastBOQ( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = tdDate
ENDIF
LOCAL lnMonth
lnMonth = MONTH( ldDate)
lnMonth = ICASE( BETWEEN( lnMonth, 1, 3), 1, ;
BETWEEN( lnMonth, 4, 6), 4, ;
BETWEEN( lnMonth, 7, 9), 7, ;
10)
ldReturnDate = GOMONTH( DATE( YEAR( ldDate), lnMonth, 1), -3)
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate))
ENDFUNC && GetLastBOQ
*--------------------------------------------------------------------
* clsFoxDates :: GetEOQ
*------------------------------------
* Function: Calculate the end of quarter date
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
* Calendar quarters are Jan-Mar, Apr-Jun, Jul-Sep, and Oct-Dec
*--------------------------------------------------------------------
FUNCTION GetEOQ( tdDate as Date) as Date
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = tdDate
ENDIF
IF ( MONTH( ldDate) = 3 and DAY( ldDate) = 31) OR ;
( MONTH( ldDate) = 6 and DAY( ldDate) = 30) OR ;
( MONTH( ldDate) = 9 and DAY( ldDate) = 30) OR ;
( MONTH( ldDate) = 12 and DAY( ldDate) = 31)
ldReturnDate = ldDate
ELSE
LOCAL lnYear, lnMonth, lnDay
lnYear = YEAR( ldDate)
DO CASE
CASE INLIST( MONTH( ldDate), 1, 2, 3)
lnMonth = 3
lnDay = 31
CASE INLIST( MONTH( ldDate), 4, 5, 6)
lnMonth = 6
lnDay = 30
CASE INLIST( MONTH( ldDate), 7, 8, 9)
lnMonth = 9
lnDay = 30
OTHERWISE
lnMonth = 12
lnDay = 31
ENDCASE
ldReturnDate = DATE( lnYear, lnMonth, lnDay)
ENDIF
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate) + ( dc_nSecondsInADay - 1))
ENDFUNC && GetEOQ
*--------------------------------------------------------------------
* clsFoxDates :: GetLastEOQ
*------------------------------------
* Function: Calculate the most recent end of quarter date
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
* Calendar quarters are Jan-Mar, Apr-Jun, Jul-Sep, and Oct-Dec
*--------------------------------------------------------------------
FUNCTION GetLastEOQ( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = tdDate
ENDIF
LOCAL lnYear, lnMonth, lnDay
lnYear = YEAR( ldDate)
DO CASE
CASE INLIST( MONTH( ldDate), 1, 2, 3)
lnMonth = 12
lnDay = 31
lnYear = lnYear - 1
CASE INLIST( MONTH( ldDate), 4, 5, 6)
lnMonth = 3
lnDay = 31
CASE INLIST( MONTH( ldDate), 7, 8, 9)
lnMonth = 6
lnDay = 30
OTHERWISE
lnMonth = 9
lnDay = 30
ENDCASE
ldReturnDate = DATE( lnYear, lnMonth, lnDay)
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate) + ( dc_nSecondsInADay - 1))
ENDFUNC && GetLastEOQ
*--------------------------------------------------------------------
* clsFoxDates :: GetLastEOY
*------------------------------------
* Function: Calculate the most recent end-of-year date
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
*--------------------------------------------------------------------
FUNCTION GetLastEOY( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldDate, ldReturnDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = DATE()
ELSE
ldDate = tdDate
ENDIF
LOCAL lnYear, lnMonth, lnDay
lnYear = YEAR( ldDate) - 1
lnMonth = 12
lnDay = 31
ldReturnDate = DATE( lnYear, lnMonth, lnDay)
RETURN IIF( lcParmType = 'D', ldReturnDate, DTOT( ldReturnDate) + ( dc_nSecondsInADay - 1))
ENDFUNC && GetLastEOY
*--------------------------------------------------------------------
* clsFoxDates :: GetLastMonday
*------------------------------------
* Function: Calculate last Monday's date
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
*--------------------------------------------------------------------
FUNCTION GetLastMonday( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = IIF( lcParmType = 'D', DATE(), DTOT( DATE()))
ELSE
ldDate = tdDate
ENDIF
LOCAL lnDOW, ldMonday
lnDOW = DOW( ldDate)
ldMonday = ldDate
IF lcParmType = 'D'
DO CASE
CASE lnDOW = 1 && Sunday
ldMonday = ldDate - 6
CASE lnDOW = 2 && Monday
ldMonday = ldDate - 7
OTHERWISE && Tuesday thru Saturday
ldMonday = ldDate - (lnDOW - 2)
ENDCASE
ELSE
DO CASE
CASE lnDOW = 1 && Sunday
ldMonday = ldDate - ( dc_nSecondsInADay * 6)
CASE lnDOW = 2 && Monday
ldMonday = ldDate - ( dc_nSecondsInADay * 7)
OTHERWISE && Tuesday thru Saturday
ldMonday = ldDate - ( dc_nSecondsInADay * (lnDOW - 2))
ENDCASE
ENDIF && lcParmType = 'D'
RETURN ldMonday
ENDFUNC && GetLastMonday
*--------------------------------------------------------------------
* clsFoxDates :: GetNextMonday
*------------------------------------
* Function: Calculates next Monday's date
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Date or datetime
* Comments: Return value is same datatype as parameter
*--------------------------------------------------------------------
FUNCTION GetNextMonday( tdDate as Variant) as Variant
IF INLIST( VARTYPE( tdDate), 'D', 'T')
ELSE
tdDate = DATE()
ENDIF
LOCAL lcParmType, ldDate
lcParmType = VARTYPE( tdDate)
IF EMPTY( tdDate)
ldDate = IIF( lcParmType = 'D', DATE(), DTOT( DATE()))
ELSE
ldDate = tdDate
ENDIF
LOCAL lnDOW, ldMonday
lnDOW = DOW( ldDate)
ldMonday = ldDate
IF lcParmType = 'D'
DO CASE
CASE lnDOW = 1 && Sunday
ldMonday = ldDate + 1
CASE lnDOW = 2 && Monday
ldMonday = ldDate + 7
OTHERWISE && Tuesday thru Saturday
ldMonday = ldDate + (7 - lnDOW) + 2
ENDCASE
ELSE
DO CASE
CASE lnDOW = 1 && Sunday
ldMonday = ldDate + ( dc_nSecondsInADay * 1)
CASE lnDOW = 2 && Monday
ldMonday = ldDate + ( dc_nSecondsInADay * 7)
OTHERWISE && Tuesday thru Saturday
ldMonday = ldDate + ( dc_nSecondsInADay * ((7 - lnDOW) + 2))
ENDCASE
ENDIF && lcParmType = 'D'
RETURN ldMonday
ENDFUNC && GetNextMonday
*--------------------------------------------------------------------
* clsFoxDates :: GetDateFromString
*------------------------------------
* Function: Convert a string in the format of MM/DD/YYYY to a date
* data type. Allow for variations such as M/D/YY, MM/D/YY,
* M/DD/YY, MM-DD-YYY, MM.DD.YYYY, etc.
* Pass: tcDate - a date in month/day/year string format
* Return: Date
* Comments: Returns the empty date if the calculation fails to
* produce a valid date (e.g., bad parameter string)
*--------------------------------------------------------------------
FUNCTION GetDateFromString( tcDate as String ) as Date
IF VARTYPE( tcDate) = "C"
ELSE
RETURN {}
ENDIF
LOCAL lcDate
lcDate = ALLTRIM( tcDate) && Trim leading or trailing spaces.
lcDate = STRTRAN( lcDate, " ", "") && Take out any embedded spaces.
lcDate = STRTRAN( lcDate, "-", "/") && Replace any hyphens with slashes.
lcDate = STRTRAN( lcDate, ".", "/") && Replace any periods with slashes.
LOCAL lnSlash1, lnSlash2, lnYear, lnMonth, lnDay
* Extract the month.
lnSlash1 = AT( "/", lcDate)
IF lnSlash1 > 4
RETURN {} && something's wrong with the date string.
ENDIF
lnMonth = VAL( LEFT( lcDate, lnSlash1 - 1))
* Extract the day.
lnSlash2 = AT( "/", lcDate, 2)
IF lnSlash2 > 6
RETURN {} && something's wrong with the date string.
ENDIF
lnDay = VAL( SUBSTR( lcDate, lnSlash1 + 1, lnSlash2 - lnSlash1 + 1))
* Extract the year. If year is only two characters long then we can assume
* that the century was left off and needs to be added. The century is added
* based on value of SET CENTURY TO nCentury ROLLOVER nYear.
* SET( "CENTURY", 1) = nCentury
* SET( "CENTURY", 2) = nYear
LOCAL lcYear
lcYear = SUBSTR( lcDate, lnSlash2 + 1) && Returns characters until the end of the string.
IF LEN( lcYear) = 2
IF VAL( lcYear) >= SET( "CENTURY", 2) && last century, i.e., 1900's
lnYear = ( SET( "CENTURY", 1) * 100) + VAL( lcYear)
ELSE && next century, i.e., 2000's
lnYear = ( ( SET( "CENTURY", 1) + 1) * 100) + VAL( lcYear)
ENDIF
ELSE
lnYear = VAL( lcYear)
ENDIF
* Return the date in date format. Trap any errors, which could
* occur if year is < 100, or day or month is an invalid number.
LOCAL ldReturnDate
TRY
ldReturnDate = DATE( lnYear, lnMonth, lnDay)
CATCH
ldReturnDate = {}
ENDTRY
RETURN ldReturnDate
ENDFUNC && GetDateFromString()
*--------------------------------------------------------------------
* clsFoxDates :: IsLeapYear
*------------------------------------
* Function: Determine if a year is a leap year
* Pass: txParm - a string, numeric value, date, or datetime representing a year
* Return: Logical
* Comments: Returns True if the year is a leap year, otherwise
* returns False.
*--------------------------------------------------------------------
FUNCTION IsLeapYear( txParm as Variant) as Logical
LOCAL lcYear, lcSetDate, lcCentury, llLeapYear
lcSetDate = SET( "date")
lcCentury = SET( "century")
SET DATE AMERICAN && so CTOD will work as expected.
SET CENTURY ON && ditto.
DO CASE
CASE TYPE( "txParm") = "C"
lcYear = ALLTRIM( txParm)
CASE TYPE( "txParm") = "N"
lcYear = ALLTRIM( STR( txParm))
CASE INLIST( TYPE( 'txParm'), 'D', 'T')
IF !EMPTY( YEAR( txParm)) && If we got a valid date
lcYear = ALLTRIM( STR( YEAR( txParm))) && then capture its year.
ELSE && Otherwise if we got an invalid date then force
lcYear = "X" && '!empty( CTOD( "02/29/" + lcYear))' to be .F.
ENDIF
ENDCASE
IF TYPE("lcYear") = "C" && .T. if parameter was type C, N, or D
llLeapYear = !EMPTY( DATE( VAL( lcYear), 02, 29)) && to satisfy strictdate
ELSE && if parameter was not type C, N, or D
llLeapYear = .F.
ENDIF
SET DATE &lcSetDate
SET CENTURY &lcCentury
RETURN llLeapYear
ENDFUNC && IsLeapYear
*--------------------------------------------------------------------
* clsFoxDates :: GetDateDayOrdinal
*------------------------------------
* Function: Returns the day of a date as an ordinal string, as in
* "first" or "tenth".
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* Return: Character
* Comments:
*--------------------------------------------------------------------
FUNCTION GetDateDayOrdinal( tdDate as Date) as String
IF INLIST( VARTYPE( tdDate), 'D', 'T') AND NOT EMPTY( tdDate)
ELSE
tdDate = DATE()
ENDIF
LOCAL lnDay, lcDay
lnDay = DAY( tdDate)
DO CASE
CASE lnDay = 1
lcDay = "first"
CASE lnDay = 2
lcDay = "second"
CASE lnDay = 3
lcDay = "third"
CASE lnDay = 4
lcDay = "fourth"
CASE lnDay = 5
lcDay = "fifth"
CASE lnDay = 6
lcDay = "sixth"
CASE lnDay = 7
lcDay = "seventh"
CASE lnDay = 8
lcDay = "eighth"
CASE lnDay = 9
lcDay = "ninth"
CASE lnDay = 10
lcDay = "tenth"
CASE lnDay = 11
lcDay = "eleventh"
CASE lnDay = 12
lcDay = "twelfth"
CASE lnDay = 13
lcDay = "thirteenth"
CASE lnDay = 14
lcDay = "fourteenth"
CASE lnDay = 15
lcDay = "fifteenth"
CASE lnDay = 16
lcDay = "sixteenth"
CASE lnDay = 17
lcDay = "seventeenth"
CASE lnDay = 18
lcDay = "eighteenth"
CASE lnDay = 19
lcDay = "nineteenth"
CASE lnDay = 20
lcDay = "twentieth"
CASE lnDay = 21
lcDay = "twenty-first"
CASE lnDay = 22
lcDay = "twenty-second"
CASE lnDay = 23
lcDay = "twenty-third"
CASE lnDay = 24
lcDay = "twenty-fourth"
CASE lnDay = 25
lcDay = "twenty-fifth"
CASE lnDay = 26
lcDay = "twenty-sixth"
CASE lnDay = 27
lcDay = "twenty-seventh"
CASE lnDay = 28
lcDay = "twenty-eighth"
CASE lnDay =29
lcDay = "twenty-ninth"
CASE lnDay = 30
lcDay = "thirtieth"
CASE lnDay = 31
lcDay = "thirty-first"
OTHERWISE && Shouldn't ever happen, but just in case.
lcDay = ALLTRIM( STR( lnDay))
ENDCASE
RETURN lcDay
ENDFUNC && GetDateDayOrdinal
*--------------------------------------------------------------------
* clsFoxDates :: GetFormattedDateString
*------------------------------------
* Function: Format a date as a string for display.
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* tnFormat - 1=format like May 15, 2012 (default)
* 2=format like Tuesday, May 15, 2012
* Return: String
* Comments:
*--------------------------------------------------------------------
FUNCTION GetFormattedDateString( tdDate as Date, tnFormat as Integer ) as String
IF INLIST( VARTYPE( tdDate), 'D', 'T') AND NOT EMPTY( tdDate)
ELSE
tdDate = DATE()
ENDIF
LOCAL lnFormat, lcString
lnFormat = IIF( VARTYPE( tnFormat) = "N" AND BETWEEN( INT( tnFormat), 1, 2), INT( tnFormat), 1)
lcString = CMONTH( tdDate) + SPACE(1) + TRANSFORM( DAY( tdDate)) + ", " + TRANSFORM( YEAR( tdDate))
DO CASE
CASE lnFormat = 1
RETURN lcString
CASE lnFormat = 2
RETURN CDOW( tdDate) + ", " + lcString
OTHERWISE
RETURN ""
ENDCASE
ENDFUNC && GetFormattedDateString
*--------------------------------------------------------------------
* clsFoxDates :: GetNthBusinessDay
*------------------------------------
* Function: Calculate the date of the nth business of the month
* Pass: tnMonth - the desired month
* tnYear - the desired year
* tnBusinessDay - the desired business day of the month
* (for example, the 1st business day or
* the 3rd business day)
* Return: Date
* Comments: This function knows how to adjust for weekends and the
* six major holidays in the United States, but it does
* not attempt to adjust for the minor holidays.
*
* This function uses the IsHoliday() method in this class.
*--------------------------------------------------------------------
FUNCTION GetNthBusinessDay( tnMonth as Integer, ;
tnYear as Integer, ;
tnBusinessDay as Integer) as Date
IF VARTYPE( tnMonth) <> "N" OR VARTYPE( tnYear) <> "N" OR VARTYPE( tnBusinessDay) <> "N"
RETURN {}
ENDIF
IF NOT BETWEEN( tnMonth, 1, 12)
RETURN {}
ENDIF
IF NOT BETWEEN( tnYear, 1752, 9999)
RETURN {}
ENDIF
LOCAL ldDate, lnDaysInMonth
* Get number of days in the month.
ldDate = DATE( tnYear, tnMonth, 1) && first of the month
lnDaysInMonth = this.GetDaysInMonth( ldDate) && days in that month
* The ordinal target business day in a given month must be less than
* or equal to the actual number of days in that month.
IF BETWEEN( tnBusinessDay, 1, lnDaysInMonth)
ELSE
RETURN {}
ENDIF
LOCAL lnDay, lnBusinessDay
lnDay = 1
lnBusinessDay = 0
DO WHILE lnDay < lnDaysInMonth
* Count business days as Monday through Friday except holidays.
ldDate = DATE( tnYear, tnMonth, lnDay)
IF BETWEEN( DOW( ldDate), 2, 6) AND NOT this.IsHoliday( ldDate)
* If it's a weekday and not a holiday, then it's a business day.
lnBusinessDay = lnBusinessDay + 1
ENDIF
IF lnBusinessDay = tnBusinessDay
EXIT
ENDIF
lnDay = lnDay + 1
ENDDO
RETURN DATE( YEAR( ldDate), MONTH( ldDate), lnDay)
ENDFUNC && GetNthBusinessDay
*--------------------------------------------------------------------
* clsFoxDates :: IsHoliday
*------------------------------------
* Function: Determines is a given date is a holiday.
* Pass: tdDate - a date or datetime (optional, defaults to the current date)
* tcCountry - optional, defaults to USA
* Return: Logical - .T. if holiday, otherwise .F.
* Comments: Future enhancement - add other country codes.
*--------------------------------------------------------------------
FUNCTION IsHoliday( tdDate as Date, tcCountry as String) as Logical
IF INLIST( VARTYPE( tdDate), 'D', 'T') AND NOT EMPTY( tdDate)
ELSE
tdDate = DATE()
ENDIF
LOCAL lcCountry
IF VARTYPE( tcCountry) = "C"
lcCountry = UPPER( ALLTRIM( tcCountry))
ELSE
lcCountry = "USA"
ENDIF
DO CASE
CASE lcCountry = "USA"
RETURN this.IsHolidayUSA( tdDate)
CASE lcCountry = "CANADA"
RETURN this.IsHolidayCanada( tdDate)
OTHERWISE
RETURN .F.
ENDCASE
ENDFUNC
*--------------------------------------------------------------------
* clsFoxDates :: IsHolidayUSA
*------------------------------------
* Function: Determines if the given date is a holiday in the United States
* Pass: tdDate - the date
* Return: Logical - True if it's a holiday, otherwise False
* Comments: This function recognizes the following U.S. holidays:
* New Year's Day, Memorial Day, Independence Day,
* Labor Day, Veteran's Day, Thanksgiving Day, and Christmas Day
*--------------------------------------------------------------------
PROTECTED FUNCTION IsHolidayUSA( tdDate as Date) as Logical
LOCAL lnMonth, lnDay, lnDOW, llHoliday
lnMonth = MONTH( tdDate)
lnDay = DAY( tdDate)
lnDOW = DOW( tdDate)
llHoliday = .F.
DO CASE
* New Year's Day,
CASE lnMonth = 1 AND lnDay = 1
llHoliday = .T.
* Memorial Day (last Monday in May)
CASE lnMonth = 5 AND lnDOW = 2 AND lnDay > 24
llHoliday = .T.
* Independence Day (4th of July)
CASE lnMonth = 7 AND lnDay = 4
llHoliday = .T.
* Labor Day (first Monday in September)
CASE lnMonth = 9 AND lnDOW = 2 AND lnDay < 8
llHoliday = .T.
* Veternan's Day (Remembrance Day)
CASE lnMonth = 11 AND lnDay = 11
llHoliday = .T.
* Thanksgiving Day (fourth Thursday in November)
CASE lnMonth = 11 AND lnDOW = 5 AND BETWEEN( lnDay, 22, 28)
llHoliday = .T.
* Christmas Day
CASE lnMonth = 12 AND lnDay = 25
llHoliday = .T.
OTHERWISE
llHoliday = .F.
ENDCASE
RETURN llHoliday
ENDFUNC && IsHolidayUSA
*--------------------------------------------------------------------
* clsFoxDates :: IsHolidayCanada
*------------------------------------
* Function: Determines if the given date is a holiday in Canada
* Pass: tdDate - the date
* Return: Logical - True if it's a holiday, otherwise False
* Comments: This function recognizes the following Canadian holidays:
* New Year's Day, Victoria Day, Canada Day, Labour Day,
* Thanksgiving Day, Remembrance Day, Christmas Day, and Boxing Day.
*
* Source: https://www.thecanadianencyclopedia.ca/en/article/national-holidays
*--------------------------------------------------------------------
PROTECTED FUNCTION IsHolidayCanada( tdDate as Date) as Logical
LOCAL lnMonth, lnDay, lnDOW, llHoliday
lnMonth = MONTH( tdDate)
lnDay = DAY( tdDate)
lnDOW = DOW( tdDate)
llHoliday = .F.
DO CASE
* New Year's Day (January 1st)
CASE lnMonth = 1 AND lnDay = 1
llHoliday = .T.
* Victoria Day (the Monday before May 25)
CASE lnMonth = 5 AND lnDOW = 2 AND BETWEEN( lnDay, 18, 24)
llHoliday = .T.
* Canada Day (July 1st)
CASE lnMonth = 7 AND lnDay = 1
llHoliday = .T.
* Labour Day (first Monday in September)
CASE lnMonth = 9 AND lnDOW = 2 AND lnDay < 8
llHoliday = .T.
* Thanksgiving Day (second Monday in October)
CASE lnMonth = 10 AND lnDOW = 2 AND BETWEEN( lnDay, 8, 14)
llHoliday = .T.
* Remembrance Day
CASE lnMonth = 11 AND lnDay = 11
llHoliday = .T.
* Christmas Day
CASE lnMonth = 12 AND lnDay = 25
llHoliday = .T.
* Boxing Day
CASE lnMonth = 12 AND lnDay = 26
llHoliday = .T.
OTHERWISE
llHoliday = .F.
ENDCASE
RETURN llHoliday
ENDFUNC && IsHolidayCanada
*--------------------------------------------------------------------
* clsFoxDates :: GetTimeString
*------------------------------------
* Function: Format a time as a string in the format hh:mm
* Pass: tnHours - the hours value (00 - 23)
* tnMinutes - the minutes value (00 - 59)
* Return: String
* Comments:
*--------------------------------------------------------------------
FUNCTION GetTimeString( tnHours as Integer, tnMinutes as Integer) as String
IF VARTYPE( tnHours) = "N" AND BETWEEN( tnHours, 0, 23) AND ;
VARTYPE( tnMinutes) = "N" AND BETWEEN( tnMinutes, 0, 59)
ELSE
RETURN ""
ENDIF
LOCAL lcHours, lcMinutes
lcHours = PADL( ALLTRIM( STR( tnHours)), 2, "0")
lcMinutes = PADL( ALLTRIM( STR( tnMinutes)), 2, "0")
RETURN lcHours + ":" + lcMinutes
ENDFUNC && GetTimeString
*--------------------------------------------------------------------
* clsFoxDates :: GetDisplayTime
*------------------------------------
* Function: Format a time string for display
* Pass: tcTimeString - the time string in the format of hh:mm
* tlSuppressZero - True to suppress leading zero, or
* False to display leading zero (default)
* tlSuppressAMPM - True to suppress AM or PM
* False to append AM or PM (default)
* Return: String
* Comments: Returns hh:mm AM or hh:mm PM. Subtracts 12 from hh
* for PM times. Returns the empty string if tcTimeString
* is invalid or can't be parsed.
*--------------------------------------------------------------------
FUNCTION GetDisplayTime( tcTimeString as String, ;
tlSuppressZero as Logical, ;
tlSuppressAMPM as Logical) as String
IF this.IsValidTimeString( tcTimeString)
ELSE
RETURN ""
ENDIF
IF PCOUNT() < 2 OR VARTYPE( tlSuppressZero) <> "L"
tlSuppressZero = .F.
ENDIF
IF PCOUNT() < 3 OR VARTYPE( tlSuppressAMPM) <> "L"
tlSuppressAMPM = .F.
ENDIF
LOCAL lnHours, lcDisplayTime
lnHours = VAL( LEFT( tcTimeString, 2))
DO CASE
CASE lnHours < 12 && morning
lcDisplayTime = tcTimeString + IIF( tlSuppressAMPM, "", " AM")
CASE lnHours = 12 && 12 noon
lcDisplayTime = tcTimeString + IIF( tlSuppressAMPM, "", " PM")
OTHERWISE && afternoon
lcDisplayTime = PADL( ALLTRIM( STR( lnHours - 12)), 2, "0") + ;
SUBSTR( tcTimeString, 3, 3) + IIF( tlSuppressAMPM, "", " PM")
ENDCASE
IF tlSuppressZero
IF LEFT( lcDisplayTime, 1) = "0"
lcDisplayTime = SUBSTR( lcDisplayTime, 2)
ENDIF
ENDIF
RETURN lcDisplayTime
ENDFUNC && GetDisplayTime
*--------------------------------------------------------------------
* clsFoxDates :: GetSecondsFromTimeString
*------------------------------------
* Function: Convert a time string (hh:mm) into seconds since midnight.
* Pass: tcTimeString - the time string
* Return: Integer
* Comments: Returns zero if parameter is invalid type or can't be parsed
*--------------------------------------------------------------------
FUNCTION GetSecondsFromTimeString( tcTimeString as String) as Integer
IF this.IsValidTimeString( tcTimeString)
ELSE
RETURN 0
ENDIF
LOCAL lnColonPosition
lnColonPosition = AT( ":", tcTimeString)
IF lnColonPosition = 0 && no colon
RETURN 0
ENDIF
LOCAL lnHours, lnMinutes
lnHours = VAL( LEFT( tcTimeString, lnColonPosition - 1))
lnMinutes = VAL( SUBSTR( tcTimeString, lnColonPosition + 1))
RETURN ( lnHours * 3600) + ( lnMinutes * 60)
ENDFUNC && GetSecondsFromTimeString
*--------------------------------------------------------------------
* clsFoxDates :: GetTimeStringFromSeconds
*------------------------------------
* Function: Construct a time string hh:mm from a number of seconds
* past midnight.
* Pass: tnSeconds - the number of seconds past midnight
* Return: Character
* Comments: If tnSeconds is greater than one day the result is
* adjusted to be relative to the closest previous midnight.
*--------------------------------------------------------------------
FUNCTION GetTimeStringFromSeconds( tnSeconds as Integer) as String
IF VARTYPE( tnSeconds) = "N"
ELSE
RETURN ""
ENDIF
LOCAL lnSeconds, lnHours, lnMinutes, lcEndTime
IF tnSeconds >= 86400 && There are 86,400 seconds in 24 hours.
lnSeconds = MOD( tnSeconds, 86400)
ELSE
lnSeconds = tnSeconds
ENDIF
lnHours = ( lnSeconds - MOD( lnSeconds, 3600)) / 3600
lnSeconds = MOD( lnSeconds, 3600) && minutes remaining after subtracting hours
lnMinutes = ( lnSeconds - MOD( lnSeconds, 60)) / 60
* Ignore any remaining seconds.
lcEndTime = this.GetTimeString( lnHours, lnMinutes)
RETURN lcEndTime
ENDFUNC && GetTimeStringFromSeconds
*--------------------------------------------------------------------
* clsFoxDates :: GetEndTime
*------------------------------------
* Function: Calculate an ending time from a starting time and a duration.
* Pass: tcStartTime - the starting time, as hh:mm
* tnDuration - the duration, in minutes
* Return: Character
* Comments: Returns the empty string if tcStartTime is invalid.
*--------------------------------------------------------------------
FUNCTION GetEndTime( tcStartTime as String, tnDuration as Integer) as String
IF this.IsValidTimeString( tcStartTime)
ELSE
RETURN ""
ENDIF
LOCAL lcStartTime, lnDuration
lcStartTime = LEFT( ALLTRIM( tcStartTime), 5)
lnDuration = IIF( VARTYPE( tnDuration) = "N", tnDuration, 0)
IF ( ISDIGIT( LEFT( lcStartTime, 1)) AND ;
ISDIGIT( SUBSTR( lcStartTime, 2, 1)) AND ;
SUBSTR( lcStartTime, 3, 1) = ":" AND ;
ISDIGIT( SUBSTR( lcStartTime, 4, 1)) AND ;
ISDIGIT( SUBSTR( lcStartTime, 5, 1)))
ELSE
RETURN ""
ENDIF
LOCAL lnStartSeconds, lnEndSeconds, lcEndTime
lnStartSeconds = this.GetSecondsFromTimeString( tcStartTime)
lnEndSeconds = lnStartSeconds + ( lnDuration * 60)
lcEndTime = this.GetTimeStringFromSeconds( lnEndSeconds)
RETURN lcEndTime
ENDFUNC && GetEndTime
*--------------------------------------------------------------------
* clsFoxDates :: GetDuration
*------------------------------------
* Function: Get the duration of a time interval, in minutes
* Pass: tcStartTime - starting time as hh:mm
* tcEndTime - ending time as hh:mm
* Return: Numeric
* Comments: Assumes both times are on the same date. If end time
* is earlier than start time, duration is returned as
* a negative number. Returns 0 if a parameter is invalid.
*--------------------------------------------------------------------
FUNCTION GetDuration( tcStartTime as String, tcEndTime as String) as Integer
IF this.IsValidTimeString( tcStartTime) AND ;
this.IsValidTimeString( tcEndTime)
ELSE
RETURN 0
ENDIF
LOCAL lnStartTime, lnEndTime, lnDuration
lnStartTime = this.GetSecondsFromTimeString( tcStartTime)
lnEndTime = this.GetSecondsFromTimeString( tcEndTime)
RETURN ( lnEndTime - lnStartTime) / 60
ENDFUNC && GetDuration
*--------------------------------------------------------------------
* clsFoxDates :: GetRFC2822
*------------------------------------
* Function: Get the RFC 2822 time string from a date or datetime.
* Pass: txDateTime - the date or datetime
* txOffset - the time zone offset as a number (e.g., -5)
* or as a string (e.g., "-0500")
* Return: Character