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
//! VB6 `Timer` Function
//!
//! The `Timer` function returns a Single representing the number of seconds that have elapsed since midnight.
//!
//! ## Syntax
//! ```vb6
//! Timer()
//! ```
//! or
//! ```vb6
//! Timer
//! ```
//!
//! ## Parameters
//! None. The `Timer` function takes no arguments.
//!
//! ## Returns
//! Returns a `Single` representing the number of seconds elapsed since midnight (00:00:00). The value ranges from 0 to 86,400 (the number of seconds in 24 hours).
//!
//! ## Remarks
//! The `Timer` function provides high-precision time measurements:
//!
//! - **No arguments**: Called without parentheses or with empty parentheses
//! - **Seconds since midnight**: Returns elapsed seconds from 00:00:00
//! - **High precision**: Resolution approximately 10-55 milliseconds depending on platform
//! - **Single type**: Returns floating-point value with fractional seconds
//! - **Midnight rollover**: Value resets to 0 at midnight (00:00:00)
//! - **Performance timing**: Ideal for measuring elapsed time for operations
//! - **System dependent**: Precision varies by platform and Windows version
//! - **Time vs Timer**: `Time` returns Date type for clock time, `Timer` returns Single for measurements
//! - **Midnight handling**: When crossing midnight, current - start will be negative
//! - **Maximum value**: Approximately 86,400 (24 hours × 3600 seconds/hour)
//!
//! ### Timer vs Related Functions
//! - `Timer` - Returns seconds since midnight as Single (high precision)
//! - `Time` - Returns current time as Date (1 second precision)
//! - `Now` - Returns current date and time as Date
//! - `GetTickCount` - Windows API function for milliseconds since system start
//!
//! ### Precision Notes
//! - Windows 95/98/ME: ~55 milliseconds
//! - Windows NT/2000/XP and later: ~10-15 milliseconds
//! - Not suitable for microsecond precision measurements
//! - Use `QueryPerformanceCounter` API for higher precision
//!
//! ### Midnight Rollover Handling
//! ```vb6
//! Function SafeElapsedTime(startTime As Single) As Single
//!     Dim elapsed As Single
//!     elapsed = Timer - startTime
//!     
//!     ' Handle midnight rollover
//!     If elapsed < 0 Then
//!         elapsed = elapsed + 86400   ' Add 24 hours worth of seconds
//!     End If
//!     
//!     SafeElapsedTime = elapsed
//! End Function
//! ```
//!
//! ## Typical Uses
//! 1. **Performance Measurement**: Time how long operations take
//! 2. **Timeout Implementation**: Check if time limit exceeded
//! 3. **Animation Timing**: Control animation frame timing
//! 4. **Delay Implementation**: Create precise delays
//! 5. **Benchmark Testing**: Compare performance of different approaches
//! 6. **Rate Limiting**: Control operation frequency
//! 7. **Elapsed Time Display**: Show how long process has been running
//! 8. **Time-based Triggers**: Execute code after specific duration
//!
//! ## Basic Examples
//!
//! ### Example 1: Measure Operation Time
//! ```vb6
//! Sub MeasureOperation()
//!     Dim startTime As Single
//!     Dim elapsed As Single
//!     
//!     startTime = Timer
//!     
//!     ' Perform operation
//!     Call LongRunningOperation
//!     
//!     elapsed = Timer - startTime
//!     MsgBox "Operation took " & Format$(elapsed, "0.000") & " seconds"
//! End Sub
//! ```
//!
//! ### Example 2: Simple Timeout
//! ```vb6
//! Sub WaitWithTimeout(seconds As Single)
//!     Dim startTime As Single
//!     startTime = Timer
//!     
//!     Do While Timer - startTime < seconds
//!         DoEvents  ' Allow other processing
//!     Loop
//! End Sub
//! ```
//!
//! ### Example 3: Check If Timeout Exceeded
//! ```vb6
//! Function IsTimeout(startTime As Single, timeoutSeconds As Single) As Boolean
//!     IsTimeout = (Timer - startTime >= timeoutSeconds)
//! End Function
//! ```
//!
//! ### Example 4: Benchmark Comparison
//! ```vb6
//! Sub CompareMethods()
//!     Dim time1 As Single, time2 As Single
//!     Dim elapsed1 As Single, elapsed2 As Single
//!     
//!     ' Test Method 1
//!     time1 = Timer
//!     Call Method1
//!     elapsed1 = Timer - time1
//!     
//!     ' Test Method 2
//!     time2 = Timer
//!     Call Method2
//!     elapsed2 = Timer - time2
//!     
//!     Debug.Print "Method1: " & elapsed1 & "s, Method2: " & elapsed2 & "s"
//! End Sub
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: Basic Elapsed Time
//! ```vb6
//! Function GetElapsedTime(startTime As Single) As Single
//!     GetElapsedTime = Timer - startTime
//! End Function
//! ```
//!
//! ### Pattern 2: Elapsed Time with Midnight Handling
//! ```vb6
//! Function GetElapsedTimeSafe(startTime As Single) As Single
//!     Dim elapsed As Single
//!     elapsed = Timer - startTime
//!     If elapsed < 0 Then elapsed = elapsed + 86400
//!     GetElapsedTimeSafe = elapsed
//! End Function
//! ```
//!
//! ### Pattern 3: Wait with `DoEvents`
//! ```vb6
//! Sub WaitSeconds(seconds As Single)
//!     Dim endTime As Single
//!     endTime = Timer + seconds
//!     
//!     Do While Timer < endTime
//!         DoEvents
//!     Loop
//! End Sub
//! ```
//!
//! ### Pattern 4: Timeout Loop
//! ```vb6
//! Function WaitForCondition(timeoutSeconds As Single) As Boolean
//!     Dim startTime As Single
//!     startTime = Timer
//!     
//!     Do While Not ConditionMet()
//!         If Timer - startTime > timeoutSeconds Then
//!             WaitForCondition = False
//!             Exit Function
//!         End If
//!         DoEvents
//!     Loop
//!     
//!     WaitForCondition = True
//! End Function
//! ```
//!
//! ### Pattern 5: Frame Rate Control
//! ```vb6
//! Sub ControlFrameRate(targetFPS As Single)
//!     Static lastFrameTime As Single
//!     Dim targetDelay As Single
//!     Dim elapsed As Single
//!     
//!     targetDelay = 1 / targetFPS
//!     elapsed = Timer - lastFrameTime
//!     
//!     If elapsed < targetDelay Then
//!         ' Wait for remaining time
//!         Do While Timer - lastFrameTime < targetDelay
//!             DoEvents
//!         Loop
//!     End If
//!     
//!     lastFrameTime = Timer
//! End Sub
//! ```
//!
//! ### Pattern 6: Rate Limiter
//! ```vb6
//! Function CanExecute(minimumInterval As Single) As Boolean
//!     Static lastExecuteTime As Single
//!     
//!     If Timer - lastExecuteTime >= minimumInterval Then
//!         lastExecuteTime = Timer
//!         CanExecute = True
//!     Else
//!         CanExecute = False
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 7: Performance Counter
//! ```vb6
//! Sub StartTimer()
//!     Static timerStart As Single
//!     timerStart = Timer
//! End Sub
//!
//! Function GetTimerElapsed() As Single
//!     Static timerStart As Single
//!     GetTimerElapsed = Timer - timerStart
//! End Function
//! ```
//!
//! ### Pattern 8: Format Elapsed Time
//! ```vb6
//! Function FormatElapsed(seconds As Single) As String
//!     Dim hrs As Long, mins As Long, secs As Long
//!     
//!     hrs = Int(seconds / 3600)
//!     mins = Int((seconds - hrs * 3600) / 60)
//!     secs = Int(seconds - hrs * 3600 - mins * 60)
//!     
//!     FormatElapsed = Format$(hrs, "00") & ":" & _
//!                    Format$(mins, "00") & ":" & _
//!                    Format$(secs, "00")
//! End Function
//! ```
//!
//! ### Pattern 9: Delay with Accuracy Check
//! ```vb6
//! Sub AccurateDelay(milliseconds As Long)
//!     Dim targetTime As Single
//!     targetTime = Timer + (milliseconds / 1000)
//!     
//!     Do While Timer < targetTime
//!         ' Busy wait for precision
//!     Loop
//! End Sub
//! ```
//!
//! ### Pattern 10: Periodic Execution
//! ```vb6
//! Sub ExecutePeriodically(intervalSeconds As Single)
//!     Static lastRun As Single
//!     
//!     If Timer - lastRun >= intervalSeconds Or lastRun = 0 Then
//!         ' Execute periodic task
//!         PerformTask
//!         lastRun = Timer
//!     End If
//! End Sub
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Performance Profiler Class
//! ```vb6
//! ' Class: PerformanceProfiler
//! ' Profiles code execution times with statistics
//! Option Explicit
//!
//! Private m_StartTime As Single
//! Private m_Measurements As Collection
//! Private m_IsRunning As Boolean
//!
//! Private Sub Class_Initialize()
//!     Set m_Measurements = New Collection
//! End Sub
//!
//! Public Sub Start()
//!     m_StartTime = Timer
//!     m_IsRunning = True
//! End Sub
//!
//! Public Function Stop() As Single
//!     Dim elapsed As Single
//!     
//!     If Not m_IsRunning Then
//!         Err.Raise 5, , "Profiler not started"
//!     End If
//!     
//!     elapsed = Timer - m_StartTime
//!     If elapsed < 0 Then elapsed = elapsed + 86400  ' Handle midnight
//!     
//!     m_Measurements.Add elapsed
//!     m_IsRunning = False
//!     Stop = elapsed
//! End Function
//!
//! Public Function GetAverageTime() As Single
//!     Dim total As Single
//!     Dim measurement As Variant
//!     
//!     If m_Measurements.Count = 0 Then
//!         GetAverageTime = 0
//!         Exit Function
//!     End If
//!     
//!     total = 0
//!     For Each measurement In m_Measurements
//!         total = total + measurement
//!     Next measurement
//!     
//!     GetAverageTime = total / m_Measurements.Count
//! End Function
//!
//! Public Function GetMinTime() As Single
//!     Dim minTime As Single
//!     Dim measurement As Variant
//!     
//!     If m_Measurements.Count = 0 Then
//!         GetMinTime = 0
//!         Exit Function
//!     End If
//!     
//!     minTime = 999999
//!     For Each measurement In m_Measurements
//!         If measurement < minTime Then minTime = measurement
//!     Next measurement
//!     
//!     GetMinTime = minTime
//! End Function
//!
//! Public Function GetMaxTime() As Single
//!     Dim maxTime As Single
//!     Dim measurement As Variant
//!     
//!     If m_Measurements.Count = 0 Then
//!         GetMaxTime = 0
//!         Exit Function
//!     End If
//!     
//!     maxTime = 0
//!     For Each measurement In m_Measurements
//!         If measurement > maxTime Then maxTime = measurement
//!     Next measurement
//!     
//!     GetMaxTime = maxTime
//! End Function
//!
//! Public Sub Reset()
//!     Set m_Measurements = New Collection
//!     m_IsRunning = False
//! End Sub
//!
//! Public Property Get MeasurementCount() As Long
//!     MeasurementCount = m_Measurements.Count
//! End Property
//! ```
//!
//! ### Example 2: Timeout Manager Module
//! ```vb6
//! ' Module: TimeoutManager
//! ' Manages multiple concurrent timeouts
//! Option Explicit
//!
//! Private Type TimeoutEntry
//!     Name As String
//!     StartTime As Single
//!     TimeoutSeconds As Single
//!     Active As Boolean
//! End Type
//!
//! Private m_Timeouts() As TimeoutEntry
//! Private m_TimeoutCount As Long
//!
//! Public Sub StartTimeout(name As String, timeoutSeconds As Single)
//!     Dim i As Long
//!     
//!     ' Check if already exists
//!     For i = 0 To m_TimeoutCount - 1
//!         If m_Timeouts(i).Name = name Then
//!             m_Timeouts(i).StartTime = Timer
//!             m_Timeouts(i).TimeoutSeconds = timeoutSeconds
//!             m_Timeouts(i).Active = True
//!             Exit Sub
//!         End If
//!     Next i
//!     
//!     ' Add new timeout
//!     ReDim Preserve m_Timeouts(m_TimeoutCount)
//!     m_Timeouts(m_TimeoutCount).Name = name
//!     m_Timeouts(m_TimeoutCount).StartTime = Timer
//!     m_Timeouts(m_TimeoutCount).TimeoutSeconds = timeoutSeconds
//!     m_Timeouts(m_TimeoutCount).Active = True
//!     m_TimeoutCount = m_TimeoutCount + 1
//! End Sub
//!
//! Public Function IsTimedOut(name As String) As Boolean
//!     Dim i As Long
//!     Dim elapsed As Single
//!     
//!     For i = 0 To m_TimeoutCount - 1
//!         If m_Timeouts(i).Name = name And m_Timeouts(i).Active Then
//!             elapsed = Timer - m_Timeouts(i).StartTime
//!             If elapsed < 0 Then elapsed = elapsed + 86400
//!             
//!             IsTimedOut = (elapsed >= m_Timeouts(i).TimeoutSeconds)
//!             Exit Function
//!         End If
//!     Next i
//!     
//!     IsTimedOut = False
//! End Function
//!
//! Public Sub CancelTimeout(name As String)
//!     Dim i As Long
//!     
//!     For i = 0 To m_TimeoutCount - 1
//!         If m_Timeouts(i).Name = name Then
//!             m_Timeouts(i).Active = False
//!             Exit Sub
//!         End If
//!     Next i
//! End Sub
//!
//! Public Function GetTimeRemaining(name As String) As Single
//!     Dim i As Long
//!     Dim elapsed As Single
//!     
//!     For i = 0 To m_TimeoutCount - 1
//!         If m_Timeouts(i).Name = name And m_Timeouts(i).Active Then
//!             elapsed = Timer - m_Timeouts(i).StartTime
//!             If elapsed < 0 Then elapsed = elapsed + 86400
//!             
//!             GetTimeRemaining = m_Timeouts(i).TimeoutSeconds - elapsed
//!             If GetTimeRemaining < 0 Then GetTimeRemaining = 0
//!             Exit Function
//!         End If
//!     Next i
//!     
//!     GetTimeRemaining = 0
//! End Function
//! ```
//!
//! ### Example 3: Animation Timer Class
//! ```vb6
//! ' Class: AnimationTimer
//! ' Controls animation frame timing and FPS
//! Option Explicit
//!
//! Private m_TargetFPS As Single
//! Private m_LastFrameTime As Single
//! Private m_FrameCount As Long
//! Private m_FPSCalculationTime As Single
//! Private m_CurrentFPS As Single
//!
//! Public Sub Initialize(targetFPS As Single)
//!     m_TargetFPS = targetFPS
//!     m_LastFrameTime = Timer
//!     m_FPSCalculationTime = Timer
//!     m_FrameCount = 0
//!     m_CurrentFPS = 0
//! End Sub
//!
//! Public Function ShouldRenderFrame() As Boolean
//!     Dim currentTime As Single
//!     Dim targetDelay As Single
//!     Dim elapsed As Single
//!     
//!     currentTime = Timer
//!     targetDelay = 1 / m_TargetFPS
//!     
//!     elapsed = currentTime - m_LastFrameTime
//!     If elapsed < 0 Then elapsed = elapsed + 86400
//!     
//!     If elapsed >= targetDelay Then
//!         m_LastFrameTime = currentTime
//!         m_FrameCount = m_FrameCount + 1
//!         
//!         ' Update FPS calculation every second
//!         If currentTime - m_FPSCalculationTime >= 1 Then
//!             m_CurrentFPS = m_FrameCount / (currentTime - m_FPSCalculationTime)
//!             m_FrameCount = 0
//!             m_FPSCalculationTime = currentTime
//!         End If
//!         
//!         ShouldRenderFrame = True
//!     Else
//!         ShouldRenderFrame = False
//!     End If
//! End Function
//!
//! Public Property Get CurrentFPS() As Single
//!     CurrentFPS = m_CurrentFPS
//! End Property
//!
//! Public Property Get TargetFPS() As Single
//!     TargetFPS = m_TargetFPS
//! End Property
//!
//! Public Property Let TargetFPS(value As Single)
//!     If value > 0 Then m_TargetFPS = value
//! End Property
//! ```
//!
//! ### Example 4: Benchmark Suite Module
//! ```vb6
//! ' Module: BenchmarkSuite
//! ' Runs and compares multiple benchmark tests
//! Option Explicit
//!
//! Private Type BenchmarkResult
//!     Name As String
//!     Iterations As Long
//!     TotalTime As Single
//!     AverageTime As Single
//!     MinTime As Single
//!     MaxTime As Single
//! End Type
//!
//! Public Function RunBenchmark(testName As String, iterations As Long) As BenchmarkResult
//!     Dim result As BenchmarkResult
//!     Dim i As Long
//!     Dim startTime As Single
//!     Dim elapsed As Single
//!     Dim minTime As Single
//!     Dim maxTime As Single
//!     Dim totalTime As Single
//!     
//!     result.Name = testName
//!     result.Iterations = iterations
//!     minTime = 999999
//!     maxTime = 0
//!     totalTime = 0
//!     
//!     For i = 1 To iterations
//!         startTime = Timer
//!         
//!         ' Execute test
//!         Call ExecuteBenchmarkTest(testName)
//!         
//!         elapsed = Timer - startTime
//!         If elapsed < 0 Then elapsed = elapsed + 86400
//!         
//!         totalTime = totalTime + elapsed
//!         If elapsed < minTime Then minTime = elapsed
//!         If elapsed > maxTime Then maxTime = elapsed
//!     Next i
//!     
//!     result.TotalTime = totalTime
//!     result.AverageTime = totalTime / iterations
//!     result.MinTime = minTime
//!     result.MaxTime = maxTime
//!     
//!     RunBenchmark = result
//! End Function
//!
//! Public Function FormatBenchmarkResult(result As BenchmarkResult) As String
//!     Dim output As String
//!     
//!     output = "Benchmark: " & result.Name & vbCrLf
//!     output = output & "Iterations: " & result.Iterations & vbCrLf
//!     output = output & "Total Time: " & Format$(result.TotalTime, "0.0000") & "s" & vbCrLf
//!     output = output & "Average: " & Format$(result.AverageTime * 1000, "0.000") & "ms" & vbCrLf
//!     output = output & "Min: " & Format$(result.MinTime * 1000, "0.000") & "ms" & vbCrLf
//!     output = output & "Max: " & Format$(result.MaxTime * 1000, "0.000") & "ms" & vbCrLf
//!     
//!     FormatBenchmarkResult = output
//! End Function
//!
//! Private Sub ExecuteBenchmarkTest(testName As String)
//!     ' Placeholder - implement actual test logic
//! End Sub
//! ```
//!
//! ## Error Handling
//! The `Timer` function typically does not raise errors:
//!
//! - **No parameters**: Cannot have parameter errors
//! - **Always succeeds**: Returns current timer value
//! - **Midnight rollover**: Not an error, but requires handling in elapsed time calculations
//! - **Negative elapsed time**: Indicates midnight rollover, add 86400 to correct
//!
//! ## Performance Notes
//! - Very fast operation - direct system call
//! - Minimal CPU overhead
//! - Precision varies by Windows version (10-55ms)
//! - Not suitable for microsecond-level timing
//! - For busy-wait loops, consider CPU usage impact
//! - Use `DoEvents` in wait loops to prevent UI freeze
//!
//! ## Best Practices
//! 1. **Handle midnight rollover** when measuring elapsed time
//! 2. **Use `DoEvents`** in timing loops to prevent application freeze
//! 3. **Store as Single** type for consistency and precision
//! 4. **Avoid long-running measurements** crossing midnight
//! 5. **Use for relative timing** not absolute time-of-day
//! 6. **Consider `GetTickCount` API** for measurements exceeding 24 hours
//! 7. **Test across midnight** boundary for production code
//! 8. **Use Time function** for actual clock time display
//! 9. **Format appropriately** when displaying to users (ms, seconds, minutes)
//! 10. **Profile performance** in release builds, not debug mode
//!
//! ## Comparison Table
//!
//! | Function | Returns | Precision | Range | Purpose |
//! |----------|---------|-----------|-------|---------|
//! | `Timer` | Single | 10-55ms | 0-86400 | Performance timing |
//! | `Time` | Date | 1 second | Full time | Clock time |
//! | `Now` | Date | 1 second | Full date/time | Current date/time |
//! | `GetTickCount` | Long | 10-16ms | 0-49.7 days | Uptime timing |
//!
//! ## Platform Notes
//! - Available in VB6, VBA, and `VBScript`
//! - Precision varies by Windows version
//! - Windows 95/98/ME: ~55ms resolution
//! - Windows NT/2000/XP+: ~10-15ms resolution
//! - Returns fractional seconds (not milliseconds)
//! - Resets at midnight (00:00:00)
//! - Based on system timer tick
//!
//! ## Limitations
//! - Resets to 0 at midnight (requires handling)
//! - Not suitable for measurements exceeding 24 hours
//! - Limited precision (10-55ms, not microseconds)
//! - No timezone awareness
//! - Cannot measure absolute time, only relative
//! - Precision varies between systems
//! - Not monotonic (can jump backwards at midnight)
//! - No built-in high-resolution timer (use `QueryPerformanceCounter` API)

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

    #[test]
    fn timer_basic() {
        let source = r"
Sub Test()
    elapsed = Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_with_parentheses() {
        let source = r"
Sub Test()
    elapsed = Timer()
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_variable_assignment() {
        let source = r"
Sub Test()
    Dim startTime As Single
    startTime = Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_elapsed_time() {
        let source = r"
Sub Test()
    elapsed = Timer - startTime
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_function_return() {
        let source = r"
Function GetStartTime() As Single
    GetStartTime = Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_debug_print() {
        let source = r"
Sub Test()
    Debug.Print Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_msgbox() {
        let source = r#"
Sub Test()
    MsgBox "Elapsed: " & (Timer - startTime)
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_if_statement() {
        let source = r"
Sub Test()
    If Timer - startTime > 10 Then
        Timeout
    End If
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_comparison() {
        let source = r"
Sub Test()
    If Timer < endTime Then
        Continue
    End If
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_do_while() {
        let source = r"
Sub Test()
    Do While Timer - startTime < 5
        DoEvents
    Loop
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_do_until() {
        let source = r"
Sub Test()
    Do Until Timer - startTime >= timeout
        Process
    Loop
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_while_wend() {
        let source = r"
Sub Test()
    While Timer < targetTime
        Wait
    Wend
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_function_argument() {
        let source = r"
Sub Test()
    Call LogTime(Timer)
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_property_assignment() {
        let source = r"
Sub Test()
    obj.StartTime = Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_with_statement() {
        let source = r"
Sub Test()
    With profiler
        .Start = Timer
    End With
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_array_assignment() {
        let source = r"
Sub Test()
    timestamps(i) = Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_print_statement() {
        let source = r"
Sub Test()
    Print #1, Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_format() {
        let source = r#"
Sub Test()
    formatted = Format$(Timer - startTime, "0.000")
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_addition() {
        let source = r"
Sub Test()
    targetTime = Timer + 10
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_elseif() {
        let source = r"
Sub Test()
    If x = 1 Then
        y = 1
    ElseIf Timer - startTime > 5 Then
        y = 2
    End If
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_select_case() {
        let source = r"
Sub Test()
    Select Case Int(Timer - startTime)
        Case 0 To 5
            Fast
        Case 6 To 10
            Medium
        Case Else
            Slow
    End Select
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_iif() {
        let source = r#"
Sub Test()
    status = IIf(Timer - startTime > timeout, "Timeout", "OK")
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_midnight_handling() {
        let source = r"
Sub Test()
    elapsed = Timer - startTime
    If elapsed < 0 Then elapsed = elapsed + 86400
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_class_usage() {
        let source = r"
Sub Test()
    Set profiler = New PerformanceProfiler
    profiler.Start Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_csng() {
        let source = r"
Sub Test()
    timerValue = CSng(Timer)
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_parentheses_expression() {
        let source = r"
Sub Test()
    result = (Timer - startTime) * 1000
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_concatenation() {
        let source = r#"
Sub Test()
    logEntry = "Time: " & Timer
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn timer_multiple_usage() {
        let source = r"
Sub Test()
    start1 = Timer
    DoSomething
    end1 = Timer
    elapsed = end1 - start1
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/timer");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}