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
//! # `DatePart` Function
//!
//! Returns a `Variant` (`Integer`) containing the specified part of a given date.
//!
//! ## Syntax
//!
//! ```vb
//! DatePart(interval, date[, firstdayofweek[, firstweekofyear]])
//! ```
//!
//! ## Parameters
//!
//! - **interval**: Required. `String` expression that is the interval of time you want to return.
//!   See the Interval Settings section for valid values.
//! - **date**: Required. `Variant` (`Date`) value that you want to evaluate.
//! - **firstdayofweek**: Optional. Constant that specifies the first day of the week.
//!   If not specified, Sunday is assumed. See `FirstDayOfWeek` Constants.
//! - **firstweekofyear**: Optional. Constant that specifies the first week of the year.
//!   If not specified, the first week is assumed to be the week containing January 1.
//!   See `FirstWeekOfYear` Constants.
//!
//! ## Interval Settings
//!
//! The `interval` parameter can have the following values:
//!
//! | Setting | Description | Return Range |
//! |---------|-------------|--------------|
//! | "yyyy" | Year | 100-9999 |
//! | "q" | Quarter | 1-4 |
//! | "m" | Month | 1-12 |
//! | "y" | Day of year | 1-366 |
//! | "d" | Day | 1-31 |
//! | "w" | Weekday | 1-7 (Sunday=1) |
//! | "ww" | Week of year | 1-53 |
//! | "h" | Hour | 0-23 |
//! | "n" | Minute | 0-59 |
//! | "s" | Second | 0-59 |
//!
//! ## `FirstDayOfWeek` Constants
//!
//! | Constant | Value | Description |
//! |----------|-------|-------------|
//! | vbUseSystem | 0 | Use system setting |
//! | vbSunday | 1 | Sunday (default) |
//! | vbMonday | 2 | Monday |
//! | vbTuesday | 3 | Tuesday |
//! | vbWednesday | 4 | Wednesday |
//! | vbThursday | 5 | Thursday |
//! | vbFriday | 6 | Friday |
//! | vbSaturday | 7 | Saturday |
//!
//! ## `FirstWeekOfYear` Constants
//!
//! | Constant | Value | Description |
//! |----------|-------|-------------|
//! | vbUseSystem | 0 | Use system setting |
//! | vbFirstJan1 | 1 | Start with week containing January 1 (default) |
//! | vbFirstFourDays | 2 | Start with week having at least 4 days in new year |
//! | vbFirstFullWeek | 3 | Start with first full week of the year |
//!
//! ## Return Value
//!
//! Returns an `Integer` representing the specified part of the date. Returns `Null` if the date is `Null`.
//!
//! ## Remarks
//!
//! The `DatePart` function is used to extract a specific component from a date value.
//! It's particularly useful for date-based calculations, filtering, and grouping operations.
//!
//! **Important Characteristics:**
//!
//! - More flexible than `Year()`, `Month()`, or `Day()` functions.
//! - Can extract quarter, week, and day of year.
//! - Weekday numbering depends on `firstdayofweek` parameter.
//! - Week numbering depends on `firstweekofyear` parameter.
//! - Hours use 24-hour format (0-23).
//! - Sunday is 1 by default for weekday ("w").
//! - Compatible with SQL Server's `DATEPART` function
//!
//! ## Equivalent Simple Functions
//!
//! Some intervals have equivalent dedicated functions:
//! - `DatePart("yyyy", date)` = `Year(date)`
//! - `DatePart("m", date)` = `Month(date)`
//! - `DatePart("d", date)` = `Day(date)`
//! - `DatePart("w", date)` = `Weekday(date)`
//! - `DatePart("h", date)` = `Hour(date)`
//! - `DatePart("n", date)` = `Minute(date)`
//! - `DatePart("s", date)` = `Second(date)`
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```vb
//! Dim testDate As Date
//! testDate = #3/15/2025 14:30:45#
//!
//! ' Extract various parts
//! MsgBox "Year: " & DatePart("yyyy", testDate)      ' 2025
//! MsgBox "Quarter: " & DatePart("q", testDate)      ' 1
//! MsgBox "Month: " & DatePart("m", testDate)        ' 3
//! MsgBox "Day: " & DatePart("d", testDate)          ' 15
//! MsgBox "Day of Year: " & DatePart("y", testDate)  ' 74
//! MsgBox "Weekday: " & DatePart("w", testDate)      ' Varies by day
//! MsgBox "Week: " & DatePart("ww", testDate)        ' Week number
//! MsgBox "Hour: " & DatePart("h", testDate)         ' 14
//! MsgBox "Minute: " & DatePart("n", testDate)       ' 30
//! MsgBox "Second: " & DatePart("s", testDate)       ' 45
//! ```
//!
//! ### Quarter Calculation
//!
//! ```vb
//! Function GetQuarter(dateValue As Date) As Integer
//!     GetQuarter = DatePart("q", dateValue)
//! End Function
//!
//! ' Usage
//! Dim currentQuarter As Integer
//! currentQuarter = GetQuarter(Date)
//! MsgBox "We are in Q" & currentQuarter
//! ```
//!
//! ### Week Number
//!
//! ```vb
//! Function GetWeekNumber(dateValue As Date) As Integer
//!     ' ISO week number (Monday start, 4-day rule)
//!     GetWeekNumber = DatePart("ww", dateValue, vbMonday, vbFirstFourDays)
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ### Fiscal Quarter Determination
//!
//! ```vb
//! Function GetFiscalQuarter(dateValue As Date, fiscalYearStart As Integer) As Integer
//!     ' fiscalYearStart is the month number (e.g., 4 for April)
//!     Dim currentMonth As Integer
//!     Dim adjustedMonth As Integer
//!     
//!     currentMonth = DatePart("m", dateValue)
//!     adjustedMonth = currentMonth - fiscalYearStart + 1
//!     
//!     If adjustedMonth <= 0 Then
//!         adjustedMonth = adjustedMonth + 12
//!     End If
//!     
//!     GetFiscalQuarter = Int((adjustedMonth - 1) / 3) + 1
//! End Function
//! ```
//!
//! ### Group By Time Period
//!
//! ```vb
//! Function GroupByPeriod(dateValue As Date, period As String) As String
//!     Select Case LCase(period)
//!         Case "year"
//!             GroupByPeriod = CStr(DatePart("yyyy", dateValue))
//!         Case "quarter"
//!             GroupByPeriod = DatePart("yyyy", dateValue) & "-Q" & DatePart("q", dateValue)
//!         Case "month"
//!             GroupByPeriod = DatePart("yyyy", dateValue) & "-" & Format(DatePart("m", dateValue), "00")
//!         Case "week"
//!             GroupByPeriod = DatePart("yyyy", dateValue) & "-W" & Format(DatePart("ww", dateValue), "00")
//!         Case Else
//!             GroupByPeriod = Format(dateValue, "yyyy-mm-dd")
//!     End Select
//! End Function
//! ```
//!
//! ### Day Name from Weekday
//!
//! ```vb
//! Function GetDayName(dateValue As Date) As String
//!     Select Case DatePart("w", dateValue)
//!         Case 1: GetDayName = "Sunday"
//!         Case 2: GetDayName = "Monday"
//!         Case 3: GetDayName = "Tuesday"
//!         Case 4: GetDayName = "Wednesday"
//!         Case 5: GetDayName = "Thursday"
//!         Case 6: GetDayName = "Friday"
//!         Case 7: GetDayName = "Saturday"
//!     End Select
//! End Function
//! ```
//!
//! ### Time of Day Category
//!
//! ```vb
//! Function GetTimeOfDay(dateValue As Date) As String
//!     Dim hour As Integer
//!     hour = DatePart("h", dateValue)
//!     
//!     Select Case hour
//!         Case 0 To 5
//!             GetTimeOfDay = "Night"
//!         Case 6 To 11
//!             GetTimeOfDay = "Morning"
//!         Case 12 To 17
//!             GetTimeOfDay = "Afternoon"
//!         Case 18 To 23
//!             GetTimeOfDay = "Evening"
//!     End Select
//! End Function
//! ```
//!
//! ### Business Hour Check
//!
//! ```vb
//! Function IsBusinessHours(checkTime As Date) As Boolean
//!     Dim hour As Integer
//!     Dim weekday As Integer
//!     
//!     hour = DatePart("h", checkTime)
//!     weekday = DatePart("w", checkTime)
//!     
//!     ' Monday-Friday, 9 AM - 5 PM
//!     If weekday >= 2 And weekday <= 6 Then  ' Mon-Fri
//!         If hour >= 9 And hour < 17 Then
//!             IsBusinessHours = True
//!         End If
//!     End If
//! End Function
//! ```
//!
//! ### Month Name Lookup
//!
//! ```vb
//! Function GetMonthName(dateValue As Date) As String
//!     Dim monthNames As Variant
//!     Dim monthNum As Integer
//!     
//!     monthNames = Array("January", "February", "March", "April", "May", "June", _
//!                       "July", "August", "September", "October", "November", "December")
//!     
//!     monthNum = DatePart("m", dateValue)
//!     GetMonthName = monthNames(monthNum - 1)
//! End Function
//! ```
//!
//! ### Quarter End Date
//!
//! ```vb
//! Function GetQuarterEnd(dateValue As Date) As Date
//!     Dim quarter As Integer
//!     Dim year As Integer
//!     Dim endMonth As Integer
//!     
//!     quarter = DatePart("q", dateValue)
//!     year = DatePart("yyyy", dateValue)
//!     endMonth = quarter * 3
//!     
//!     GetQuarterEnd = DateSerial(year, endMonth + 1, 0)  ' Last day of quarter
//! End Function
//! ```
//!
//! ### Data Binning by Hour
//!
//! ```vb
//! Function GetHourBucket(timestamp As Date) As String
//!     Dim hour As Integer
//!     hour = DatePart("h", timestamp)
//!     GetHourBucket = Format(hour, "00") & ":00"
//! End Function
//!
//! ' Use for grouping log entries
//! Sub AnalyzeLogs()
//!     Dim entry As Date
//!     Dim bucket As String
//!     
//!     For Each entry In logEntries
//!         bucket = GetHourBucket(entry)
//!         hourCounts(bucket) = hourCounts(bucket) + 1
//!     Next
//! End Sub
//! ```
//!
//! ## Advanced Usage
//!
//! ### ISO 8601 Week Number
//!
//! ```vb
//! Function GetISOWeekNumber(dateValue As Date) As Integer
//!     ' ISO 8601 week number: Monday start, 4-day rule
//!     GetISOWeekNumber = DatePart("ww", dateValue, vbMonday, vbFirstFourDays)
//! End Function
//!
//! Function GetISOYear(dateValue As Date) As Integer
//!     ' Year for ISO week (may differ from calendar year)
//!     Dim weekNum As Integer
//!     Dim month As Integer
//!     
//!     weekNum = GetISOWeekNumber(dateValue)
//!     month = DatePart("m", dateValue)
//!     
//!     If month = 1 And weekNum > 51 Then
//!         GetISOYear = DatePart("yyyy", dateValue) - 1
//!     ElseIf month = 12 And weekNum = 1 Then
//!         GetISOYear = DatePart("yyyy", dateValue) + 1
//!     Else
//!         GetISOYear = DatePart("yyyy", dateValue)
//!     End If
//! End Function
//! ```
//!
//! ### Dynamic Date Grouping
//!
//! ```vb
//! Function GetDateKey(dateValue As Date, granularity As String) As String
//!     Dim year As Integer
//!     Dim month As Integer
//!     Dim day As Integer
//!     Dim week As Integer
//!     Dim quarter As Integer
//!     
//!     year = DatePart("yyyy", dateValue)
//!     
//!     Select Case LCase(granularity)
//!         Case "year"
//!             GetDateKey = CStr(year)
//!         
//!         Case "quarter"
//!             quarter = DatePart("q", dateValue)
//!             GetDateKey = year & "Q" & quarter
//!         
//!         Case "month"
//!             month = DatePart("m", dateValue)
//!             GetDateKey = year & Format(month, "00")
//!         
//!         Case "week"
//!             week = DatePart("ww", dateValue, vbMonday)
//!             GetDateKey = year & "W" & Format(week, "00")
//!         
//!         Case "day"
//!             month = DatePart("m", dateValue)
//!             day = DatePart("d", dateValue)
//!             GetDateKey = year & Format(month, "00") & Format(day, "00")
//!         
//!         Case Else
//!             GetDateKey = Format(dateValue, "yyyymmdd")
//!     End Select
//! End Function
//! ```
//!
//! ### Custom Calendar System
//!
//! ```vb
//! Type CustomCalendar
//!     Year As Integer
//!     Period As Integer
//!     Week As Integer
//!     Day As Integer
//! End Type
//!
//! Function ConvertToCustomCalendar(dateValue As Date) As CustomCalendar
//!     Dim cal As CustomCalendar
//!     Dim yearStart As Date
//!     Dim dayOfYear As Integer
//!     
//!     cal.Year = DatePart("yyyy", dateValue)
//!     
//!     ' 13 periods of 4 weeks each
//!     yearStart = DateSerial(cal.Year, 1, 1)
//!     dayOfYear = DatePart("y", dateValue)
//!     
//!     cal.Week = Int((dayOfYear - 1) / 7) + 1
//!     cal.Period = Int((cal.Week - 1) / 4) + 1
//!     cal.Day = DatePart("w", dateValue, vbMonday)
//!     
//!     ConvertToCustomCalendar = cal
//! End Function
//! ```
//!
//! ### Time Series Aggregation
//!
//! ```vb
//! Function AggregateByInterval(dates() As Date, values() As Double, _
//!                             interval As String) As Collection
//!     Dim results As New Collection
//!     Dim i As Long
//!     Dim key As String
//!     Dim total As Double
//!     Dim count As Long
//!     
//!     For i = LBound(dates) To UBound(dates)
//!         key = GetDateKey(dates(i), interval)
//!         
//!         On Error Resume Next
//!         total = results(key)
//!         If Err.Number <> 0 Then
//!             results.Add values(i), key
//!         Else
//!             results.Remove key
//!             results.Add total + values(i), key
//!         End If
//!         On Error GoTo 0
//!     Next i
//!     
//!     Set AggregateByInterval = results
//! End Function
//! ```
//!
//! ### Shift Schedule Detector
//!
//! ```vb
//! Function GetShift(timestamp As Date) As String
//!     Dim hour As Integer
//!     Dim weekday As Integer
//!     
//!     hour = DatePart("h", timestamp)
//!     weekday = DatePart("w", timestamp)
//!     
//!     ' Weekend check
//!     If weekday = 1 Or weekday = 7 Then
//!         GetShift = "Weekend"
//!         Exit Function
//!     End If
//!     
//!     ' Shift determination
//!     Select Case hour
//!         Case 6 To 13
//!             GetShift = "Morning Shift"
//!         Case 14 To 21
//!             GetShift = "Afternoon Shift"
//!         Case Else
//!             GetShift = "Night Shift"
//!     End Select
//! End Function
//! ```
//!
//! ### Calendar Week Display
//!
//! ```vb
//! Function FormatCalendarWeek(dateValue As Date, Optional useISO As Boolean = False) As String
//!     Dim year As Integer
//!     Dim week As Integer
//!     
//!     If useISO Then
//!         year = GetISOYear(dateValue)
//!         week = GetISOWeekNumber(dateValue)
//!     Else
//!         year = DatePart("yyyy", dateValue)
//!         week = DatePart("ww", dateValue)
//!     End If
//!     
//!     FormatCalendarWeek = year & "-W" & Format(week, "00")
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! Function SafeDatePart(interval As String, dateValue As Variant) As Variant
//!     On Error GoTo ErrorHandler
//!     
//!     ' Validate date
//!     If Not IsDate(dateValue) Then
//!         SafeDatePart = Null
//!         Exit Function
//!     End If
//!     
//!     ' Validate interval
//!     Select Case LCase(interval)
//!         Case "yyyy", "q", "m", "y", "d", "w", "ww", "h", "n", "s"
//!             SafeDatePart = DatePart(interval, CDate(dateValue))
//!         Case Else
//!             SafeDatePart = Null
//!     End Select
//!     
//!     Exit Function
//!     
//! ErrorHandler:
//!     SafeDatePart = Null
//! End Function
//! ```
//!
//! ### Common Errors
//!
//! - **Error 5** (Invalid procedure call): Invalid interval string
//! - **Error 13** (Type mismatch): Non-date value passed as date parameter
//!
//! ## Performance Considerations
//!
//! - `DatePart` is efficient for single extractions
//! - For multiple parts from same date, consider using dedicated functions:
//!   ```vb
//!   ' Less efficient
//!   y = DatePart("yyyy", d)
//!   m = DatePart("m", d)
//!   d = DatePart("d", d)
//!   
//!   ' More efficient
//!   y = Year(d)
//!   m = Month(d)
//!   d = Day(d)
//!   ```
//! - Week calculations are more expensive than other intervals
//! - Cache results when processing large datasets
//!
//! ## Best Practices
//!
//! ### Use Named Constants
//!
//! ```vb
//! ' Define interval constants
//! Const INTERVAL_YEAR As String = "yyyy"
//! Const INTERVAL_QUARTER As String = "q"
//! Const INTERVAL_MONTH As String = "m"
//! Const INTERVAL_WEEK As String = "ww"
//!
//! ' Use in code
//! quarter = DatePart(INTERVAL_QUARTER, Date)
//! ```
//!
//! ### Prefer Specific Functions for Simple Cases
//!
//! ```vb
//! ' Good - Use specific function
//! y = Year(someDate)
//!
//! ' Less clear - Using DatePart
//! y = DatePart("yyyy", someDate)
//! ```
//!
//! ### Be Aware of Weekday Numbering
//!
//! ```vb
//! ' Default: Sunday = 1
//! day = DatePart("w", Date)
//!
//! ' Explicit: Monday = 1
//! day = DatePart("w", Date, vbMonday)
//! ```
//!
//! ## Comparison with Other Functions
//!
//! ### `DatePart` vs Dedicated Functions
//!
//! ```vb
//! ' DatePart - Flexible, supports all intervals
//! quarter = DatePart("q", Date)
//! dayOfYear = DatePart("y", Date)
//!
//! ' Dedicated - Simpler, more readable for common cases
//! year = Year(Date)
//! month = Month(Date)
//! day = Day(Date)
//! weekday = Weekday(Date)
//! ```
//!
//! ## Limitations
//!
//! - No millisecond support
//! - Week numbering can be confusing with different standards (ISO vs US)
//! - Quarter calculation doesn't support fiscal quarters directly
//! - No built-in locale-aware day/month names
//! - `FirstWeekOfYear` affects week numbering interpretation
//!
//! ## Related Functions
//!
//! - `Year`: Returns the year part of a date
//! - `Month`: Returns the month part of a date
//! - `Day`: Returns the day part of a date
//! - `Weekday`: Returns the day of the week
//! - `Hour`: Returns the hour part of a time
//! - `Minute`: Returns the minute part of a time
//! - `Second`: Returns the second part of a time
//! - `DateAdd`: Adds a time interval to a date
//! - `DateDiff`: Returns the difference between two dates
//! - `DateSerial`: Creates a date from year, month, and day values
//! - `Format`: Formats a date as a string (alternative for custom formatting)

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

    #[test]
    fn datepart_basic() {
        let source = r#"
year = DatePart("yyyy", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_quarter() {
        let source = r#"
quarter = DatePart("q", currentDate)
"#;
        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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_month() {
        let source = r#"
month = DatePart("m", #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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_day() {
        let source = r#"
day = DatePart("d", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_day_of_year() {
        let source = r#"
dayOfYear = DatePart("y", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_weekday() {
        let source = r#"
weekday = DatePart("w", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_week() {
        let source = r#"
week = DatePart("ww", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_hour() {
        let source = r#"
hour = DatePart("h", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_minute() {
        let source = r#"
minute = DatePart("n", timestamp)
"#;
        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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_second() {
        let source = r#"
second = DatePart("s", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_with_firstdayofweek() {
        let source = r#"
weekday = DatePart("w", Date, vbMonday)
"#;
        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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_with_all_params() {
        let source = r#"
week = DatePart("ww", Date, vbMonday, vbFirstFourDays)
"#;
        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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_in_function() {
        let source = r#"
Function GetQuarter(d As Date) As Integer
    GetQuarter = DatePart("q", d)
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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_in_select_case() {
        let source = r#"
Select Case DatePart("q", Date)
    Case 1
        MsgBox "Q1"
    Case 2
        MsgBox "Q2"
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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_in_if() {
        let source = r#"
If DatePart("h", Now) >= 17 Then
    MsgBox "After hours"
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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_concatenation() {
        let source = r#"
key = DatePart("yyyy", Date) & "-" & DatePart("m", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_in_calculation() {
        let source = r#"
endMonth = DatePart("q", Date) * 3
"#;
        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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_in_loop() {
        let source = r#"
For i = 1 To count
    months(i) = DatePart("m", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_comparison() {
        let source = r#"
If DatePart("w", Date) = vbSaturday Then
    MsgBox "Weekend"
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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_multiple_calls() {
        let source = r#"
y = DatePart("yyyy", Date)
m = DatePart("m", Date)
d = DatePart("d", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_in_msgbox() {
        let source = r#"
MsgBox "Quarter: " & DatePart("q", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_with_format() {
        let source = r#"
formatted = Format(DatePart("m", 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_nested_in_dateserial() {
        let source = r#"
quarterEnd = DateSerial(DatePart("yyyy", Date), DatePart("q", Date) * 3 + 1, 0)
"#;
        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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_with_variable_interval() {
        let source = r#"
Dim interval As String
interval = "q"
result = DatePart(interval, 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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn datepart_range_check() {
        let source = r#"
If DatePart("h", Now) >= 9 And DatePart("h", Now) < 17 Then
    MsgBox "Business hours"
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/datepart",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}