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
//! # `DateValue` Function
//!
//! Returns a `Variant` (`Date`) containing the date represented by a string expression.
//!
//! ## Syntax
//!
//! ```vb
//! DateValue(date)
//! ```
//!
//! ## Parameters
//!
//! - **date**: Required. `String` expression representing a date from January 1, 100 through
//! December 31, 9999. Can also be any expression that can represent a date, a time, or
//! both a date and time, in that range.
//!
//! ## Return Value
//!
//! Returns a `Variant` of subtype `Date`. If the string includes valid time information, it's
//! not returned as part of the date (time is set to midnight). Returns `Null` if the string
//! cannot be converted to a valid date.
//!
//! ## Remarks
//!
//! The `DateValue` function is used to convert string representations of dates into actual
//! `Date` values. It recognizes dates according to the system locale settings.
//!
//! **Important Characteristics:**
//!
//! - Interprets strings according to system locale settings
//! - Recognizes various date formats (MM/DD/YYYY, Month DD, YYYY, etc.)
//! - Strips time information if present (returns date portion only)
//! - Two-digit years: 0-29 → 2000-2029, 30-99 → 1930-1999
//! - Month names can be spelled out or abbreviated
//! - Accepts dates in various formats depending on locale
//! - Returns midnight (00:00:00) for time portion
//! - Case-insensitive for month names
//!
//! ## Recognized Date Formats
//!
//! `DateValue` recognizes many formats (locale-dependent):
//!
//! ```vb
//! ' Numeric formats
//! DateValue("1/15/2025") ' MM/DD/YYYY (US)
//! DateValue("15/1/2025") ' DD/MM/YYYY (UK)
//! DateValue("2025-01-15") ' ISO format
//! DateValue("1-15-2025") ' With dashes
//!
//! ' Text formats
//! DateValue("January 15, 2025") ' Full month name
//! DateValue("Jan 15, 2025") ' Abbreviated month
//! DateValue("15 January 2025") ' Different order
//! DateValue("15-Jan-2025") ' Mixed format
//!
//! ' Short formats
//! DateValue("1/15/25") ' Two-digit year
//! DateValue("Jan 15") ' Assumes current year
//! ```
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```vb
//! ' Convert string to date
//! Dim birthday As Date
//! birthday = DateValue("5/15/1990")
//! MsgBox birthday
//!
//! ' With month name
//! Dim holiday As Date
//! holiday = DateValue("December 25, 2025")
//!
//! ' ISO format
//! Dim isoDate As Date
//! isoDate = DateValue("2025-01-15")
//! ```
//!
//! ### Parse User Input
//!
//! ```vb
//! Function ParseDate(userInput As String) As Variant
//! On Error Resume Next
//! ParseDate = DateValue(userInput)
//!
//! If Err.Number <> 0 Then
//! ParseDate = Null
//! End If
//! End Function
//!
//! ' Usage
//! Dim inputDate As Variant
//! inputDate = ParseDate(txtDate.Text)
//! If IsNull(inputDate) Then
//! MsgBox "Invalid date format"
//! End If
//! ```
//!
//! ### Strip Time from `DateTime`
//!
//! ```vb
//! Function GetDateOnly(dateTime As Variant) As Date
//! ' Convert to string and back to strip time
//! GetDateOnly = DateValue(CStr(dateTime))
//! End Function
//!
//! ' Alternative using Int
//! Function GetDateOnly2(dateTime As Date) As Date
//! GetDateOnly2 = Int(dateTime)
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ### Validate Date String
//!
//! ```vb
//! Function IsValidDateString(dateStr As String) As Boolean
//! On Error Resume Next
//! Dim testDate As Date
//! testDate = DateValue(dateStr)
//! IsValidDateString = (Err.Number = 0)
//! End Function
//!
//! ' Usage
//! If IsValidDateString(txtDate.Text) Then
//! MsgBox "Valid date"
//! Else
//! MsgBox "Invalid date"
//! End If
//! ```
//!
//! ### Parse Various Formats
//!
//! ```vb
//! Function TryParseDateFormats(dateStr As String) As Variant
//! Dim formats As Variant
//! Dim i As Integer
//!
//! formats = Array("MM/DD/YYYY", "DD/MM/YYYY", "YYYY-MM-DD", _
//! "Month DD, YYYY", "DD Month YYYY")
//!
//! On Error Resume Next
//! TryParseDateFormats = DateValue(dateStr)
//!
//! If Err.Number = 0 Then Exit Function
//!
//! ' Try with current year if not specified
//! TryParseDateFormats = DateValue(dateStr & " " & Year(Date))
//! End Function
//! ```
//!
//! ### Import Data with Date Parsing
//!
//! ```vb
//! Sub ImportDataWithDates(filePath As String)
//! Dim line As String
//! Dim fields() As String
//! Dim recordDate As Date
//!
//! Open filePath For Input As #1
//!
//! Do Until EOF(1)
//! Line Input #1, line
//! fields = Split(line, ",")
//!
//! On Error Resume Next
//! recordDate = DateValue(fields(0))
//!
//! If Err.Number = 0 Then
//! ' Process valid date record
//! ProcessRecord recordDate, fields
//! Else
//! ' Log invalid date
//! LogError "Invalid date: " & fields(0)
//! End If
//!
//! On Error GoTo 0
//! Loop
//!
//! Close #1
//! End Sub
//! ```
//!
//! ### Date Range Validator
//!
//! ```vb
//! Function ValidateDateRange(startStr As String, endStr As String) As Boolean
//! Dim startDate As Date
//! Dim endDate As Date
//!
//! On Error Resume Next
//! startDate = DateValue(startStr)
//! If Err.Number <> 0 Then Exit Function
//!
//! endDate = DateValue(endStr)
//! If Err.Number <> 0 Then Exit Function
//!
//! ValidateDateRange = (startDate <= endDate)
//! End Function
//! ```
//!
//! ### Convert Text File Dates
//!
//! ```vb
//! Function ConvertTextDate(textDate As String) As Date
//! ' Convert various text formats to standard date
//! ConvertTextDate = DateValue(textDate)
//! End Function
//!
//! Sub ProcessLogFile()
//! Dim logDate As Date
//! Dim dateStr As String
//!
//! dateStr = "Jan 15, 2025"
//! logDate = ConvertTextDate(dateStr)
//!
//! MsgBox Format(logDate, "yyyy-mm-dd")
//! End Sub
//! ```
//!
//! ### Flexible Date Parser
//!
//! ```vb
//! Function SmartDateParse(input As String) As Variant
//! Dim result As Variant
//!
//! On Error Resume Next
//!
//! ' Try as-is
//! result = DateValue(input)
//! If Err.Number = 0 Then
//! SmartDateParse = result
//! Exit Function
//! End If
//! Err.Clear
//!
//! ' Try adding current year
//! result = DateValue(input & ", " & Year(Date))
//! If Err.Number = 0 Then
//! SmartDateParse = result
//! Exit Function
//! End If
//!
//! ' Return Null if unparseable
//! SmartDateParse = Null
//! End Function
//! ```
//!
//! ### Database Date Import
//!
//! ```vb
//! Sub ImportDatabaseDates(rs As ADODB.Recordset)
//! Dim dateField As String
//! Dim parsedDate As Date
//!
//! Do Until rs.EOF
//! dateField = rs("DateField").Value
//!
//! On Error Resume Next
//! parsedDate = DateValue(dateField)
//!
//! If Err.Number = 0 Then
//! ' Update with parsed date
//! rs("DateField") = parsedDate
//! rs.Update
//! End If
//!
//! rs.MoveNext
//! Loop
//! End Sub
//! ```
//!
//! ### Form Date Validation
//!
//! ```vb
//! Function ValidateFormDate(ByRef txt As TextBox, fieldName As String) As Boolean
//! Dim testDate As Date
//!
//! On Error Resume Next
//! testDate = DateValue(txt.Text)
//!
//! If Err.Number <> 0 Then
//! MsgBox "Invalid " & fieldName & " format", vbExclamation
//! txt.SetFocus
//! ValidateFormDate = False
//! Else
//! ValidateFormDate = True
//! End If
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Multi-Locale Date Parser
//!
//! ```vb
//! Function ParseInternationalDate(dateStr As String, locale As String) As Variant
//! ' This is simplified - VB6 doesn't have full locale switching
//! ' Would need Windows API for true locale switching
//!
//! On Error Resume Next
//!
//! Select Case UCase(locale)
//! Case "US"
//! ' Try MM/DD/YYYY first
//! ParseInternationalDate = DateValue(dateStr)
//!
//! Case "UK", "EU"
//! ' Parse with assumption of DD/MM/YYYY
//! ' Would need custom parsing logic
//! ParseInternationalDate = DateValue(dateStr)
//!
//! Case "ISO"
//! ' YYYY-MM-DD format
//! ParseInternationalDate = DateValue(dateStr)
//!
//! Case Else
//! ParseInternationalDate = DateValue(dateStr)
//! End Select
//!
//! If Err.Number <> 0 Then
//! ParseInternationalDate = Null
//! End If
//! End Function
//! ```
//!
//! ### Batch Date Conversion
//!
//! ```vb
//! Function ConvertDateArray(dateStrings() As String) As Variant
//! Dim dates() As Date
//! Dim i As Long
//! Dim validCount As Long
//!
//! ReDim dates(LBound(dateStrings) To UBound(dateStrings))
//! validCount = 0
//!
//! For i = LBound(dateStrings) To UBound(dateStrings)
//! On Error Resume Next
//! dates(i) = DateValue(dateStrings(i))
//!
//! If Err.Number = 0 Then
//! validCount = validCount + 1
//! End If
//! Err.Clear
//! Next i
//!
//! ConvertDateArray = dates
//! End Function
//! ```
//!
//! ### Date String Normalizer
//!
//! ```vb
//! Function NormalizeDateString(input As String, outputFormat As String) As String
//! Dim parsedDate As Date
//!
//! On Error Resume Next
//! parsedDate = DateValue(input)
//!
//! If Err.Number = 0 Then
//! NormalizeDateString = Format(parsedDate, outputFormat)
//! Else
//! NormalizeDateString = ""
//! End If
//! End Function
//!
//! ' Usage: Convert various formats to ISO
//! Dim normalized As String
//! normalized = NormalizeDateString("Jan 15, 2025", "yyyy-mm-dd") ' Returns "2025-01-15"
//! ```
//!
//! ### Excel Date Converter
//!
//! ```vb
//! Function ExcelDateToVBDate(excelDateStr As String) As Variant
//! ' Excel stores dates as numbers, but when exported may be text
//! Dim dateVal As Variant
//!
//! On Error Resume Next
//!
//! ' Try as text date
//! dateVal = DateValue(excelDateStr)
//!
//! If Err.Number <> 0 Then
//! Err.Clear
//! ' Try as Excel serial number
//! If IsNumeric(excelDateStr) Then
//! dateVal = CDate(CDbl(excelDateStr))
//! Else
//! dateVal = Null
//! End If
//! End If
//!
//! ExcelDateToVBDate = dateVal
//! End Function
//! ```
//!
//! ### Calendar Date Picker Helper
//!
//! ```vb
//! Function ParseCalendarInput(input As String) As Variant
//! ' Handle various calendar input formats
//! Dim result As Date
//!
//! On Error Resume Next
//!
//! ' Remove extra whitespace
//! input = Trim(input)
//!
//! ' Try direct conversion
//! result = DateValue(input)
//! If Err.Number = 0 Then
//! ParseCalendarInput = result
//! Exit Function
//! End If
//!
//! ' Try common substitutions
//! If LCase(input) = "today" Then
//! ParseCalendarInput = Date
//! ElseIf LCase(input) = "yesterday" Then
//! ParseCalendarInput = Date - 1
//! ElseIf LCase(input) = "tomorrow" Then
//! ParseCalendarInput = Date + 1
//! Else
//! ParseCalendarInput = Null
//! End If
//! End Function
//! ```
//!
//! ### Report Date Filter
//!
//! ```vb
//! Function BuildDateFilter(fromStr As String, toStr As String) As String
//! Dim fromDate As Date
//! Dim toDate As Date
//!
//! On Error Resume Next
//! fromDate = DateValue(fromStr)
//! toDate = DateValue(toStr)
//!
//! If Err.Number = 0 Then
//! BuildDateFilter = "DateField >= #" & fromDate & "# AND DateField <= #" & toDate & "#"
//! Else
//! BuildDateFilter = ""
//! End If
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! Function SafeDateValue(dateStr As String) As Variant
//! On Error GoTo ErrorHandler
//!
//! ' Validate input
//! If Len(Trim(dateStr)) = 0 Then
//! SafeDateValue = Null
//! Exit Function
//! End If
//!
//! SafeDateValue = DateValue(dateStr)
//! Exit Function
//!
//! ErrorHandler:
//! SafeDateValue = Null
//! End Function
//!
//! Function SafeDateValueWithDefault(dateStr As String, defaultDate As Date) As Date
//! On Error Resume Next
//! SafeDateValueWithDefault = DateValue(dateStr)
//!
//! If Err.Number <> 0 Then
//! SafeDateValueWithDefault = defaultDate
//! End If
//! End Function
//! ```
//!
//! ### Common Errors
//!
//! - **Error 13** (Type mismatch): `String` cannot be recognized as a date
//! - **Error 5** (Invalid procedure call): `Date` is outside valid range
//!
//! ## Performance Considerations
//!
//! - `DateValue` involves string parsing, slower than `DateSerial`
//! - Use `DateSerial` when constructing dates from numeric components
//! - Cache parsed dates when processing large datasets
//! - Locale detection adds overhead
//! - For known formats, custom parsing may be faster
//!
//! ## Best Practices
//!
//! ### Always Validate User Input
//!
//! ```vb
//! ' Good - Validate before use
//! On Error Resume Next
//! userDate = DateValue(txtInput.Text)
//! If Err.Number <> 0 Then
//! MsgBox "Please enter a valid date"
//! Exit Sub
//! End If
//!
//! ' Avoid - Assuming input is valid
//! userDate = DateValue(txtInput.Text) ' May crash
//! ```
//!
//! ### Use `IsDate` for Pre-validation
//!
//! ```vb
//! If IsDate(txtInput.Text) Then
//! processDate = DateValue(txtInput.Text)
//! Else
//! MsgBox "Invalid date"
//! End If
//! ```
//!
//! ### Prefer `DateSerial` for Programmatic Dates
//!
//! ```vb
//! ' Good - Fast and unambiguous
//! dt = DateSerial(2025, 12, 25)
//!
//! ' Less ideal - String parsing overhead
//! dt = DateValue("12/25/2025")
//! ```
//!
//! ### Be Aware of Locale Issues
//!
//! ```vb
//! ' US locale: MM/DD/YYYY
//! dt = DateValue("3/5/2025") ' March 5 in US
//!
//! ' UK locale: DD/MM/YYYY
//! dt = DateValue("3/5/2025") ' May 3 in UK
//!
//! ' Use unambiguous formats when possible
//! dt = DateValue("2025-03-05") ' ISO format, clearer
//! dt = DateValue("March 5, 2025") ' Text format, clearer
//! ```
//!
//! ## Comparison with Other Functions
//!
//! ### `DateValue` vs `DateSerial`
//!
//! ```vb
//! ' DateValue - From string representation
//! dt = DateValue("12/25/2025")
//!
//! ' DateSerial - From numeric components (faster, more reliable)
//! dt = DateSerial(2025, 12, 25)
//! ```
//!
//! ### `DateValue` vs `CDate`
//!
//! ```vb
//! ' DateValue - Returns date portion only (strips time)
//! dt = DateValue("12/25/2025 3:30 PM") ' Returns 12/25/2025 00:00:00
//!
//! ' CDate - Preserves time information
//! dt = CDate("12/25/2025 3:30 PM") ' Returns 12/25/2025 15:30:00
//! ```
//!
//! ### `DateValue` vs `IsDate`
//!
//! ```vb
//! ' IsDate - Tests if string can be converted (returns Boolean)
//! If IsDate("12/25/2025") Then...
//!
//! ' DateValue - Actually converts (returns Date or error)
//! dt = DateValue("12/25/2025")
//! ```
//!
//! ## Limitations
//!
//! - Locale-dependent interpretation can cause unexpected results
//! - Cannot directly parse custom date formats
//! - Limited control over parsing rules
//! - Strips time information (use `CDate` to preserve time)
//! - Two-digit year interpretation fixed (0-29=2000-2029, 30-99=1930-1999)
//! - Error handling required for user input
//!
//! ## Related Functions
//!
//! - `CDate`: Converts expression to `Date` (preserves time)
//! - `DateSerial`: Creates date from year, month, day (numeric)
//! - `IsDate`: Tests if expression can be converted to date
//! - `Format`: Formats date as `String` (opposite direction)
//! - `TimeValue`: Returns time portion from `String`
//! - `Year`, `Month`, `Day`: Extract date components
//! - `Date`: Returns current system date
//! - `CVDate`: Converts expression to `Date` (legacy function)
use crate;
use crate;
/// Implementation of the `DateValue` function.
///
/// VB6 behavior:
/// - `Null` is returned unchanged
/// - a string must be recognized as a date (date, or date + time); otherwise
/// error 13 (type mismatch) is raised — numeric strings are not accepted
/// - other values (including `Empty` and numerics) are treated as date
/// serials via `CDate` semantics
/// - the time portion is stripped, so the result is always midnight
/// - results outside the supported year range raise error 6 (overflow)
/// Strip the time portion and validate the resulting date serial is within
/// the supported year range.
/// `jiff` rejects day spans beyond this magnitude, so guard before converting.
const MAX_SAFE_DAYS: f64 = 7_304_484.0;