vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
//! # Month Function
//!
//! Returns a Variant (Integer) specifying a whole number between 1 and 12, inclusive,
//! representing the month of the year.
//!
//! ## Syntax
//!
//! ```vb
//! Month(date)
//! ```
//!
//! ## Parameters
//!
//! - **date** (Required) - Any Variant, numeric expression, string expression, or any combination
//!   that can represent a date. If date contains Null, Null is returned.
//!
//! ## Return Value
//!
//! Returns a **Variant (Integer)** from 1 to 12 representing the month of the year:
//! - 1 = January
//! - 2 = February
//! - 3 = March
//! - 4 = April
//! - 5 = May
//! - 6 = June
//! - 7 = July
//! - 8 = August
//! - 9 = September
//! - 10 = October
//! - 11 = November
//! - 12 = December
//!
//! ## Remarks
//!
//! The Month function extracts the month component from a date value. It is commonly used
//! for date calculations, filtering data by month, generating reports, and fiscal year processing.
//!
//! ### Key Characteristics:
//! - Returns an Integer from 1 (January) to 12 (December)
//! - Works with Date literals, Date variables, and string expressions that can be converted to dates
//! - If the date parameter contains Null, the function returns Null
//! - The time portion of the date value is ignored; only the date portion is used
//! - Can be used with Now, Date, or any valid date expression
//! - Type mismatch error (Error 13) occurs if the argument cannot be interpreted as a date
//! - Independent of the day and year components of the date
//!
//! ### Common Use Cases:
//! - Extract month from date for display or calculation
//! - Filter records by month
//! - Group data by month for reporting
//! - Calculate fiscal periods (quarters, fiscal years)
//! - Determine season based on month
//! - Validate date ranges
//! - Calculate months between dates
//! - Generate month-based file names or identifiers
//!
//! ## Typical Uses
//!
//! 1. **Extract Month from Date** - Get the month number from any date value
//! 2. **Filter by Month** - Select records from a specific month
//! 3. **Month-based Reports** - Generate monthly summaries and reports
//! 4. **Fiscal Year Calculations** - Determine fiscal quarters and periods
//! 5. **Season Determination** - Map month to season (Winter, Spring, Summer, Fall)
//! 6. **Date Validation** - Verify dates fall within expected month ranges
//! 7. **Monthly Aggregation** - Group transactions or data by month
//! 8. **File Organization** - Create month-based folder or file naming schemes
//!
//! ## Basic Examples
//!
//! ```vb
//! ' Example 1: Get current month
//! Dim currentMonth As Integer
//! currentMonth = Month(Now)
//! ' If today is November 23, 2025, returns 11
//! ```
//!
//! ```vb
//! ' Example 2: Extract month from date literal
//! Dim birthMonth As Integer
//! birthMonth = Month(#3/15/1990#)
//! ' Returns 3 (March)
//! ```
//!
//! ```vb
//! ' Example 3: Filter records by month
//! Dim orderDate As Date
//! Dim orderMonth As Integer
//!
//! orderDate = rs("OrderDate")
//! orderMonth = Month(orderDate)
//!
//! If orderMonth = 12 Then
//!     Debug.Print "December order"
//! End If
//! ```
//!
//! ```vb
//! ' Example 4: Determine fiscal quarter
//! Dim transactionDate As Date
//! Dim fiscalQuarter As Integer
//!
//! transactionDate = #5/15/2025#
//!
//! Select Case Month(transactionDate)
//!     Case 1, 2, 3
//!         fiscalQuarter = 1
//!     Case 4, 5, 6
//!         fiscalQuarter = 2
//!     Case 7, 8, 9
//!         fiscalQuarter = 3
//!     Case 10, 11, 12
//!         fiscalQuarter = 4
//! End Select
//!
//! Debug.Print "Q" & fiscalQuarter
//! ```
//!
//! ## Common Patterns
//!
//! ```vb
//! ' Pattern 1: Safe month extraction with Null handling
//! Function SafeMonth(dateValue As Variant) As Variant
//!     If IsNull(dateValue) Then
//!         SafeMonth = Null
//!     ElseIf Not IsDate(dateValue) Then
//!         SafeMonth = Null
//!     Else
//!         SafeMonth = Month(dateValue)
//!     End If
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 2: Get month name from number
//! Function GetMonthName(dateValue As Date) As String
//!     Dim monthNames(1 To 12) As String
//!     monthNames(1) = "January"
//!     monthNames(2) = "February"
//!     monthNames(3) = "March"
//!     monthNames(4) = "April"
//!     monthNames(5) = "May"
//!     monthNames(6) = "June"
//!     monthNames(7) = "July"
//!     monthNames(8) = "August"
//!     monthNames(9) = "September"
//!     monthNames(10) = "October"
//!     monthNames(11) = "November"
//!     monthNames(12) = "December"
//!     
//!     GetMonthName = monthNames(Month(dateValue))
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 3: Check if date is in current month
//! Function IsCurrentMonth(dateValue As Date) As Boolean
//!     IsCurrentMonth = (Month(dateValue) = Month(Date))
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 4: Get fiscal quarter (Oct-Dec = Q1)
//! Function GetFiscalQuarter(dateValue As Date, fiscalStartMonth As Integer) As Integer
//!     Dim monthNum As Integer
//!     Dim adjustedMonth As Integer
//!     
//!     monthNum = Month(dateValue)
//!     adjustedMonth = monthNum - fiscalStartMonth + 1
//!     
//!     If adjustedMonth <= 0 Then
//!         adjustedMonth = adjustedMonth + 12
//!     End If
//!     
//!     GetFiscalQuarter = ((adjustedMonth - 1) \ 3) + 1
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 5: Calculate months between two dates
//! Function MonthsBetween(startDate As Date, endDate As Date) As Integer
//!     Dim years As Integer
//!     Dim months As Integer
//!     
//!     years = Year(endDate) - Year(startDate)
//!     months = Month(endDate) - Month(startDate)
//!     
//!     MonthsBetween = (years * 12) + months
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 6: Get season from month
//! Function GetSeason(dateValue As Date) As String
//!     Select Case Month(dateValue)
//!         Case 12, 1, 2
//!             GetSeason = "Winter"
//!         Case 3, 4, 5
//!             GetSeason = "Spring"
//!         Case 6, 7, 8
//!             GetSeason = "Summer"
//!         Case 9, 10, 11
//!             GetSeason = "Fall"
//!     End Select
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 7: Format month with leading zero
//! Function FormatMonth(dateValue As Date) As String
//!     FormatMonth = Format(Month(dateValue), "00")
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 8: Check if same month and year
//! Function IsSameMonthYear(date1 As Date, date2 As Date) As Boolean
//!     IsSameMonthYear = (Month(date1) = Month(date2)) And _
//!                       (Year(date1) = Year(date2))
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 9: Get first day of month
//! Function GetFirstDayOfMonth(dateValue As Date) As Date
//!     GetFirstDayOfMonth = DateSerial(Year(dateValue), Month(dateValue), 1)
//! End Function
//! ```
//!
//! ```vb
//! ' Pattern 10: Get last day of month
//! Function GetLastDayOfMonth(dateValue As Date) As Date
//!     Dim nextMonth As Date
//!     nextMonth = DateSerial(Year(dateValue), Month(dateValue) + 1, 1)
//!     GetLastDayOfMonth = DateAdd("d", -1, nextMonth)
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Monthly Sales Report Generator
//!
//! ```vb
//! ' Class: MonthlySalesReport
//! ' Generates comprehensive monthly sales analysis
//!
//! Option Explicit
//!
//! Private m_year As Integer
//! Private m_month As Integer
//! Private m_sales() As Double
//! Private m_recordCount As Long
//!
//! Public Sub Initialize(targetYear As Integer, targetMonth As Integer)
//!     m_year = targetYear
//!     m_month = targetMonth
//!     m_recordCount = 0
//!     ReDim m_sales(0)
//! End Sub
//!
//! Public Sub ProcessRecordset(rs As ADODB.Recordset)
//!     Dim saleDate As Date
//!     Dim saleAmount As Double
//!     
//!     rs.MoveFirst
//!     Do While Not rs.EOF
//!         saleDate = rs("SaleDate")
//!         
//!         If Year(saleDate) = m_year And Month(saleDate) = m_month Then
//!             saleAmount = rs("Amount")
//!             
//!             ReDim Preserve m_sales(m_recordCount)
//!             m_sales(m_recordCount) = saleAmount
//!             m_recordCount = m_recordCount + 1
//!         End If
//!         
//!         rs.MoveNext
//!     Loop
//! End Sub
//!
//! Public Function GetTotalSales() As Double
//!     Dim i As Long
//!     Dim total As Double
//!     
//!     total = 0
//!     For i = 0 To m_recordCount - 1
//!         total = total + m_sales(i)
//!     Next i
//!     
//!     GetTotalSales = total
//! End Function
//!
//! Public Function GetAverageSale() As Double
//!     If m_recordCount = 0 Then
//!         GetAverageSale = 0
//!     Else
//!         GetAverageSale = GetTotalSales() / m_recordCount
//!     End If
//! End Function
//!
//! Public Function GenerateReport() As String
//!     Dim report As String
//!     Dim monthName As String
//!     
//!     monthName = Format(DateSerial(m_year, m_month, 1), "mmmm")
//!     
//!     report = "Sales Report - " & monthName & " " & m_year & vbCrLf
//!     report = report & String(50, "-") & vbCrLf
//!     report = report & "Total Sales: " & Format(GetTotalSales(), "$#,##0.00") & vbCrLf
//!     report = report & "Number of Transactions: " & m_recordCount & vbCrLf
//!     report = report & "Average Sale: " & Format(GetAverageSale(), "$#,##0.00") & vbCrLf
//!     
//!     GenerateReport = report
//! End Function
//! ```
//!
//! ### Example 2: Fiscal Calendar Manager
//!
//! ```vb
//! ' Class: FiscalCalendar
//! ' Manages fiscal year calculations with custom start month
//!
//! Option Explicit
//!
//! Private m_fiscalStartMonth As Integer  ' 1-12
//!
//! Public Sub Initialize(fiscalStartMonth As Integer)
//!     If fiscalStartMonth < 1 Or fiscalStartMonth > 12 Then
//!         Err.Raise 5, "FiscalCalendar", "Invalid fiscal start month"
//!     End If
//!     m_fiscalStartMonth = fiscalStartMonth
//! End Sub
//!
//! Public Function GetFiscalYear(calendarDate As Date) As Integer
//!     Dim calendarYear As Integer
//!     Dim calendarMonth As Integer
//!     
//!     calendarYear = Year(calendarDate)
//!     calendarMonth = Month(calendarDate)
//!     
//!     If calendarMonth >= m_fiscalStartMonth Then
//!         GetFiscalYear = calendarYear
//!     Else
//!         GetFiscalYear = calendarYear - 1
//!     End If
//! End Function
//!
//! Public Function GetFiscalQuarter(calendarDate As Date) As Integer
//!     Dim monthNum As Integer
//!     Dim monthsFromStart As Integer
//!     
//!     monthNum = Month(calendarDate)
//!     monthsFromStart = monthNum - m_fiscalStartMonth
//!     
//!     If monthsFromStart < 0 Then
//!         monthsFromStart = monthsFromStart + 12
//!     End If
//!     
//!     GetFiscalQuarter = (monthsFromStart \ 3) + 1
//! End Function
//!
//! Public Function GetFiscalPeriod(calendarDate As Date) As Integer
//!     ' Returns 1-12 for each month of fiscal year
//!     Dim monthNum As Integer
//!     Dim period As Integer
//!     
//!     monthNum = Month(calendarDate)
//!     period = monthNum - m_fiscalStartMonth + 1
//!     
//!     If period <= 0 Then
//!         period = period + 12
//!     End If
//!     
//!     GetFiscalPeriod = period
//! End Function
//!
//! Public Function GetQuarterStartDate(calendarDate As Date) As Date
//!     Dim fiscalYear As Integer
//!     Dim quarter As Integer
//!     Dim quarterStartMonth As Integer
//!     
//!     fiscalYear = GetFiscalYear(calendarDate)
//!     quarter = GetFiscalQuarter(calendarDate)
//!     
//!     quarterStartMonth = m_fiscalStartMonth + ((quarter - 1) * 3)
//!     If quarterStartMonth > 12 Then
//!         quarterStartMonth = quarterStartMonth - 12
//!         fiscalYear = fiscalYear + 1
//!     End If
//!     
//!     GetQuarterStartDate = DateSerial(fiscalYear, quarterStartMonth, 1)
//! End Function
//!
//! Public Function FormatFiscalPeriod(calendarDate As Date) As String
//!     Dim fy As Integer
//!     Dim period As Integer
//!     
//!     fy = GetFiscalYear(calendarDate)
//!     period = GetFiscalPeriod(calendarDate)
//!     
//!     FormatFiscalPeriod = "FY" & fy & "-P" & Format(period, "00")
//! End Function
//! ```
//!
//! ### Example 3: Date Range Validator
//!
//! ```vb
//! ' Module: DateRangeValidator
//! ' Validates dates against month-based constraints
//!
//! Option Explicit
//!
//! Public Function IsInMonthRange(testDate As Date, _
//!                                startMonth As Integer, _
//!                                endMonth As Integer) As Boolean
//!     Dim testMonth As Integer
//!     testMonth = Month(testDate)
//!     
//!     If startMonth <= endMonth Then
//!         ' Same year range (e.g., March to September)
//!         IsInMonthRange = (testMonth >= startMonth) And (testMonth <= endMonth)
//!     Else
//!         ' Wraps year boundary (e.g., November to February)
//!         IsInMonthRange = (testMonth >= startMonth) Or (testMonth <= endMonth)
//!     End If
//! End Function
//!
//! Public Function IsValidBusinessCycle(startDate As Date, endDate As Date) As Boolean
//!     Dim monthsDiff As Integer
//!     
//!     monthsDiff = ((Year(endDate) - Year(startDate)) * 12) + _
//!                  (Month(endDate) - Month(startDate))
//!     
//!     ' Business cycle should be at least 3 months, max 18 months
//!     IsValidBusinessCycle = (monthsDiff >= 3) And (monthsDiff <= 18)
//! End Function
//!
//! Public Function GetMonthsOverlap(start1 As Date, end1 As Date, _
//!                                  start2 As Date, end2 As Date) As Integer
//!     Dim overlapStart As Date
//!     Dim overlapEnd As Date
//!     Dim monthsOverlap As Integer
//!     
//!     ' Determine overlap period
//!     If start1 > start2 Then
//!         overlapStart = start1
//!     Else
//!         overlapStart = start2
//!     End If
//!     
//!     If end1 < end2 Then
//!         overlapEnd = end1
//!     Else
//!         overlapEnd = end2
//!     End If
//!     
//!     If overlapStart > overlapEnd Then
//!         GetMonthsOverlap = 0
//!     Else
//!         monthsOverlap = ((Year(overlapEnd) - Year(overlapStart)) * 12) + _
//!                        (Month(overlapEnd) - Month(overlapStart)) + 1
//!         GetMonthsOverlap = monthsOverlap
//!     End If
//! End Function
//!
//! Public Function IsQuarterEnd(testDate As Date) As Boolean
//!     Dim monthNum As Integer
//!     Dim lastDay As Date
//!     
//!     monthNum = Month(testDate)
//!     
//!     ' Check if month is quarter-end month
//!     If monthNum Mod 3 <> 0 Then
//!         IsQuarterEnd = False
//!         Exit Function
//!     End If
//!     
//!     ' Check if it's the last day of the month
//!     lastDay = DateSerial(Year(testDate), monthNum + 1, 1) - 1
//!     IsQuarterEnd = (Day(testDate) = Day(lastDay))
//! End Function
//!
//! Public Function GetNextQuarterStart(fromDate As Date) As Date
//!     Dim currentMonth As Integer
//!     Dim nextQuarterMonth As Integer
//!     Dim targetYear As Integer
//!     
//!     currentMonth = Month(fromDate)
//!     targetYear = Year(fromDate)
//!     
//!     ' Calculate next quarter start month
//!     nextQuarterMonth = ((currentMonth - 1) \ 3 + 1) * 3 + 1
//!     
//!     If nextQuarterMonth > 12 Then
//!         nextQuarterMonth = nextQuarterMonth - 12
//!         targetYear = targetYear + 1
//!     End If
//!     
//!     GetNextQuarterStart = DateSerial(targetYear, nextQuarterMonth, 1)
//! End Function
//! ```
//!
//! ### Example 4: Subscription Manager
//!
//! ```vb
//! ' Class: SubscriptionManager
//! ' Tracks monthly recurring subscriptions
//!
//! Option Explicit
//!
//! Private Type Subscription
//!     CustomerID As String
//!     StartDate As Date
//!     EndDate As Date
//!     MonthlyFee As Double
//!     Active As Boolean
//! End Type
//!
//! Private m_subscriptions() As Subscription
//! Private m_count As Long
//!
//! Public Sub AddSubscription(customerID As String, startDate As Date, _
//!                            endDate As Date, monthlyFee As Double)
//!     ReDim Preserve m_subscriptions(m_count)
//!     
//!     m_subscriptions(m_count).CustomerID = customerID
//!     m_subscriptions(m_count).StartDate = startDate
//!     m_subscriptions(m_count).EndDate = endDate
//!     m_subscriptions(m_count).MonthlyFee = monthlyFee
//!     m_subscriptions(m_count).Active = True
//!     
//!     m_count = m_count + 1
//! End Sub
//!
//! Public Function GetMonthlyRevenue(targetYear As Integer, _
//!                                   targetMonth As Integer) As Double
//!     Dim i As Long
//!     Dim revenue As Double
//!     Dim checkDate As Date
//!     
//!     checkDate = DateSerial(targetYear, targetMonth, 15) ' Mid-month
//!     revenue = 0
//!     
//!     For i = 0 To m_count - 1
//!         If m_subscriptions(i).Active Then
//!             If IsSubscriptionActive(m_subscriptions(i), checkDate) Then
//!                 revenue = revenue + m_subscriptions(i).MonthlyFee
//!             End If
//!         End If
//!     Next i
//!     
//!     GetMonthlyRevenue = revenue
//! End Function
//!
//! Private Function IsSubscriptionActive(sub As Subscription, _
//!                                       checkDate As Date) As Boolean
//!     IsSubscriptionActive = (checkDate >= sub.StartDate) And _
//!                           (checkDate <= sub.EndDate)
//! End Function
//!
//! Public Function GetActiveSubscriptions(targetYear As Integer, _
//!                                        targetMonth As Integer) As Long
//!     Dim i As Long
//!     Dim count As Long
//!     Dim checkDate As Date
//!     
//!     checkDate = DateSerial(targetYear, targetMonth, 15)
//!     count = 0
//!     
//!     For i = 0 To m_count - 1
//!         If m_subscriptions(i).Active Then
//!             If IsSubscriptionActive(m_subscriptions(i), checkDate) Then
//!                 count = count + 1
//!             End If
//!         End If
//!     Next i
//!     
//!     GetActiveSubscriptions = count
//! End Function
//!
//! Public Function CalculateLifetimeMonths(customerID As String) As Integer
//!     Dim i As Long
//!     Dim totalMonths As Integer
//!     Dim monthsActive As Integer
//!     
//!     totalMonths = 0
//!     
//!     For i = 0 To m_count - 1
//!         If m_subscriptions(i).CustomerID = customerID Then
//!             monthsActive = ((Year(m_subscriptions(i).EndDate) - _
//!                            Year(m_subscriptions(i).StartDate)) * 12) + _
//!                           (Month(m_subscriptions(i).EndDate) - _
//!                            Month(m_subscriptions(i).StartDate)) + 1
//!             totalMonths = totalMonths + monthsActive
//!         End If
//!     Next i
//!     
//!     CalculateLifetimeMonths = totalMonths
//! End Function
//!
//! Public Sub GenerateAnnualReport(targetYear As Integer)
//!     Dim monthNum As Integer
//!     Dim revenue As Double
//!     
//!     Debug.Print "Annual Subscription Report - " & targetYear
//!     Debug.Print String(60, "-")
//!     
//!     For monthNum = 1 To 12
//!         revenue = GetMonthlyRevenue(targetYear, monthNum)
//!         Debug.Print Format(DateSerial(targetYear, monthNum, 1), "mmmm") & ": " & _
//!                    Format(revenue, "$#,##0.00") & " (" & _
//!                    GetActiveSubscriptions(targetYear, monthNum) & " subs)"
//!     Next monthNum
//! End Sub
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! On Error Resume Next
//! monthValue = Month(dateInput)
//! If Err.Number = 13 Then
//!     MsgBox "Invalid date format"
//! ElseIf Err.Number <> 0 Then
//!     MsgBox "Error extracting month: " & Err.Description
//! End If
//! On Error GoTo 0
//! ```
//!
//! ## Performance Considerations
//!
//! - `Month()` is a fast operation - extracting a component from an internal date value
//! - For repeated calls with the same date, consider caching the result
//! - When filtering large recordsets by month, use database queries when possible
//! - `Month()` can be called millions of times without significant performance impact
//! - Combining with `Year()` and `Day()` is efficient for complete date decomposition
//!
//! ## Best Practices
//!
//! 1. **Validate input** - Use `IsDate()` to check if the value can be converted to a date before calling `Month()`
//! 2. **Handle Null** - Check for Null values when working with Variant date parameters
//! 3. **Use with Format** - Combine `Month()` with `Format()` for display purposes (leading zeros, month names)
//! 4. **Combine with Year** - When filtering or comparing, consider both `Month()` and `Year()` for accuracy
//! 5. **Fiscal year awareness** - Remember that fiscal years may not align with calendar months
//! 6. **Use constants** - Define constants for month numbers to improve code readability
//! 7. **Document assumptions** - Clearly state whether code expects calendar or fiscal months
//! 8. **Consider `MonthName()`** - Use `MonthName()` function for getting month names instead of arrays
//! 9. **Date arithmetic** - Use `DateSerial()` with `Month()` for date calculations
//! 10. **Test edge cases** - Test with leap years, year boundaries, and Null values
//!
//! ## Comparison with Other Date Functions
//!
//! | Function | Returns | Range | Use Case |
//! |----------|---------|-------|----------|
//! | **Month** | Month number | 1-12 | Extract month component |
//! | **Day** | Day of month | 1-31 | Extract day component |
//! | **Year** | Year | e.g., 2025 | Extract year component |
//! | **Weekday** | Day of week | 1-7 | Determine day of week |
//! | **`DatePart`** | Any date component | Varies | General date part extraction |
//! | **`MonthName`** | Month name | String | Get month name (not number) |
//!
//! ## Platform Notes
//!
//! - Available in VBA (Excel, Access, Word, etc.)
//! - Available in VB6
//! - Available in `VBScript`
//! - Returns Integer type (not Long)
//! - Consistent across all VB6/VBA platforms
//! - Uses system's regional settings for date interpretation (when parsing strings)
//!
//! ## Limitations
//!
//! - Cannot extract month from time-only values (use Date data type)
//! - Returns Null if input is Null (not an error)
//! - Type mismatch error if input cannot be interpreted as a date
//! - Always returns 1-12, no support for zero-based indexing
//! - Does not provide month name (use `MonthName()` or `Format()` for that)
//! - Time component is ignored (only date portion matters)
//!
//! ## Related Functions
//!
//! - **Year** - Returns the year component of a date (1-9999)
//! - **Day** - Returns the day component of a date (1-31)
//! - **Weekday** - Returns the day of the week (1-7)
//! - **Hour** - Returns the hour component of a time (0-23)
//! - **Minute** - Returns the minute component of a time (0-59)
//! - **Second** - Returns the second component of a time (0-59)
//! - **`DatePart`** - Returns a specified part of a date (flexible)
//! - **`DateSerial`** - Creates a date from year, month, and day components
//! - **`MonthName`** - Returns the name of the month as a string
//! - **Format** - Formats a date with custom patterns including month
//!
//! ## VB6 Parser Notes
//!
//! Month is parsed as a regular function call (`CallExpression`). This module exists primarily
//! for documentation purposes to provide comprehensive reference material for VB6 developers
//! working with date calculations and month extraction operations.

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn month_basic() {
        let source = r"
Dim m As Integer
m = Month(Now)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_variable_assignment() {
        let source = r"
Dim currentMonth As Integer
currentMonth = Month(Date)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_date_literal() {
        let source = r"
Dim m As Integer
m = Month(#3/15/2025#)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_if_statement() {
        let source = r#"
If Month(orderDate) = 12 Then
    MsgBox "December order"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_function_return() {
        let source = r"
Function GetCurrentMonth() As Integer
    GetCurrentMonth = Month(Date)
End Function
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_select_case() {
        let source = r"
Select Case Month(transactionDate)
    Case 1, 2, 3
        quarter = 1
    Case 4, 5, 6
        quarter = 2
    Case 7, 8, 9
        quarter = 3
    Case 10, 11, 12
        quarter = 4
End Select
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_debug_print() {
        let source = r"
Debug.Print Month(Now)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_with_statement() {
        let source = r"
With employeeRecord
    .HireMonth = Month(.HireDate)
End With
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_elseif() {
        let source = r"
If x > 0 Then
    y = 1
ElseIf Month(startDate) > 6 Then
    y = 2
End If
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_parentheses() {
        let source = r"
Dim m As Integer
m = (Month(Date))
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_iif() {
        let source = r#"
Dim result As String
result = IIf(Month(Date) = 12, "December", "Other")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_class_usage() {
        let source = r"
Private m_month As Integer

Public Sub SetMonth()
    m_month = Month(Now)
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_function_argument() {
        let source = r"
Call ProcessMonth(Month(Date))
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_property_assignment() {
        let source = r"
Set obj = New DateInfo
obj.CurrentMonth = Month(Date)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_array_assignment() {
        let source = r"
Dim months(10) As Integer
Dim i As Integer
months(i) = Month(Date)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_for_loop() {
        let source = r"
Dim i As Integer
For i = 0 To 10
    monthValues(i) = Month(dates(i))
Next i
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_while_wend() {
        let source = r#"
While Month(currentDate) <= 6
    currentDate = DateAdd("m", 1, currentDate)
Wend
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_do_while() {
        let source = r#"
Do While Month(endDate) < 12
    endDate = DateAdd("m", 1, endDate)
Loop
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_do_until() {
        let source = r#"
Do Until Month(targetDate) = 1
    targetDate = DateAdd("m", 1, targetDate)
Loop
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_msgbox() {
        let source = r#"
MsgBox "Current month: " & Month(Now)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_concatenation() {
        let source = r#"
Dim dateStr As String
dateStr = Year(Date) & "/" & Month(Date) & "/" & Day(Date)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_comparison() {
        let source = r#"
If Month(date1) = Month(date2) Then
    MsgBox "Same month"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_format() {
        let source = r#"
Dim formatted As String
formatted = Format(Month(Date), "00")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_arithmetic() {
        let source = r"
Dim quarter As Integer
quarter = ((Month(Date) - 1) \ 3) + 1
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_label_caption() {
        let source = r#"
lblMonth.Caption = "Month: " & CStr(Month(Date))
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_calculation() {
        let source = r"
Dim fiscalPeriod As Integer
fiscalPeriod = Month(Date) - 9
If fiscalPeriod <= 0 Then fiscalPeriod = fiscalPeriod + 12
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn month_mod_operator() {
        let source = r#"
If Month(Date) Mod 3 = 0 Then
    MsgBox "Quarter end month"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/datetime/month");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}