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
//! VB6 `TimeValue` Function
//!
//! The `TimeValue` function returns a Variant (Date) containing the time value represented by a string.
//!
//! ## Syntax
//! ```vb6
//! TimeValue(time)
//! ```
//!
//! ## Parameters
//! - `time`: Required. String expression representing a time (e.g., "14:30:00", "2:30 PM"). Can also be a Variant containing a string or a numeric value representing a time.
//!
//! ## Returns
//! Returns a `Variant` of subtype `Date` containing the time value. The date portion is set to zero (December 30, 1899).
//!
//! ## Remarks
//! The `TimeValue` function parses a string and returns the corresponding time value:
//!
//! - **String input**: Accepts time strings in various formats (e.g., "14:30", "2:30 PM", "23:59:59")
//! - **Date portion**: Always returns zero date (12/30/1899)
//! - **Locale aware**: Accepts time formats based on system locale
//! - **Numeric input**: Accepts numeric values representing fractional days (e.g., 0.5 = 12:00 PM)
//! - **Null handling**: Returns Null if input is Null
//! - **Error handling**: Error 13 (Type mismatch) if string cannot be parsed as a time
//! - **No milliseconds**: Only parses up to seconds
//! - **Companion to `DateValue`**: Use `DateValue` for date-only parsing
//! - **Type returned**: Returns Variant (Date), not a string
//!
//! ### Accepted Time Formats
//! - 24-hour: "14:30", "23:59:59"
//! - 12-hour: "2:30 PM", "11:59:59 PM"
//! - With/without seconds: "8:00", "8:00:00"
//! - With/without AM/PM: "7:15", "7:15 AM"
//! - Numeric: 0.75 (6:00 PM)
//!
//! ### Invalid Inputs
//! - "25:00" (invalid hour)
//! - "13:60" (invalid minute)
//! - "not a time" (not parseable)
//!
//! ## Typical Uses
//! 1. **Parse User Input**: Convert time strings to time values
//! 2. **Time Comparison**: Compare parsed times to other time values
//! 3. **Scheduling**: Store and use times from configuration or user entry
//! 4. **Time Calculations**: Perform arithmetic with parsed times
//! 5. **Validation**: Check if input is a valid time
//! 6. **Database Import**: Parse time strings from data sources
//! 7. **Time Filtering**: Filter records by parsed time
//! 8. **Time Formatting**: Convert string to time for display or calculation
//!
//! ## Basic Examples
//!
//! ### Example 1: Parse Time String
//! ```vb6
//! Sub ParseTime()
//! Dim t As Date
//! t = TimeValue("14:30:00")
//! MsgBox "Parsed time: " & Format$(t, "hh:mm AM/PM")
//! End Sub
//! ```
//!
//! ### Example 2: Parse 12-hour Time
//! ```vb6
//! Function ParseNoon() As Date
//! ParseNoon = TimeValue("12:00 PM")
//! End Function
//! ```
//!
//! ### Example 3: Validate Time Input
//! ```vb6
//! Function IsValidTime(inputStr As String) As Boolean
//! On Error Resume Next
//! Dim t As Date
//! t = TimeValue(inputStr)
//! IsValidTime = (Err.Number = 0)
//! On Error GoTo 0
//! End Function
//! ```
//!
//! ### Example 4: Compare Parsed Time
//! ```vb6
//! Function IsAfterNoon(timeStr As String) As Boolean
//! IsAfterNoon = (TimeValue(timeStr) > TimeValue("12:00 PM"))
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: Parse and Add Minutes
//! ```vb6
//! Function AddMinutes(timeStr As String, minutes As Integer) As Date
//! AddMinutes = TimeValue(timeStr) + TimeSerial(0, minutes, 0)
//! End Function
//! ```
//!
//! ### Pattern 2: Parse and Compare to Now
//! ```vb6
//! Function IsPast(timeStr As String) As Boolean
//! IsPast = (TimeValue(timeStr) < Time)
//! End Function
//! ```
//!
//! ### Pattern 3: Parse and Format
//! ```vb6
//! Function FormatParsedTime(timeStr As String) As String
//! FormatParsedTime = Format$(TimeValue(timeStr), "hh:mm:ss")
//! End Function
//! ```
//!
//! ### Pattern 4: Parse with Error Handling
//! ```vb6
//! Function TryParseTime(timeStr As String, ByRef result As Date) As Boolean
//! On Error Resume Next
//! result = TimeValue(timeStr)
//! TryParseTime = (Err.Number = 0)
//! On Error GoTo 0
//! End Function
//! ```
//!
//! ### Pattern 5: Parse Numeric Time
//! ```vb6
//! Function ParseNumericTime(numericValue As Double) As Date
//! ParseNumericTime = TimeValue(numericValue)
//! End Function
//! ```
//!
//! ### Pattern 6: Parse and Set Property
//! ```vb6
//! Sub SetStartTime(obj As Object, timeStr As String)
//! obj.StartTime = TimeValue(timeStr)
//! End Sub
//! ```
//!
//! ### Pattern 7: Parse Array of Times
//! ```vb6
//! Function ParseTimeArray(timeStrings() As String) As Variant
//! Dim i As Integer
//! Dim times() As Date
//! ReDim times(LBound(timeStrings) To UBound(timeStrings))
//! For i = LBound(timeStrings) To UBound(timeStrings)
//! times(i) = TimeValue(timeStrings(i))
//! Next i
//! ParseTimeArray = times
//! End Function
//! ```
//!
//! ### Pattern 8: Filter Valid Times
//! ```vb6
//! Function FilterValidTimes(timeStrings() As String) As Collection
//! Dim validTimes As New Collection
//! Dim i As Integer
//! For i = LBound(timeStrings) To UBound(timeStrings)
//! If IsValidTime(timeStrings(i)) Then
//! validTimes.Add TimeValue(timeStrings(i))
//! End If
//! Next i
//! Set FilterValidTimes = validTimes
//! End Function
//! ```
//!
//! ### Pattern 9: Parse and Use in Schedule
//! ```vb6
//! Sub AddScheduleEntry(schedule As Collection, timeStr As String, description As String)
//! Dim entry As New Collection
//! entry.Add TimeValue(timeStr), "Time"
//! entry.Add description, "Description"
//! schedule.Add entry
//! End Sub
//! ```
//!
//! ### Pattern 10: Parse and Compare Range
//! ```vb6
//! Function IsWithinRange(timeStr As String, startStr As String, endStr As String) As Boolean
//! Dim t As Date
//! t = TimeValue(timeStr)
//! IsWithinRange = (t >= TimeValue(startStr) And t <= TimeValue(endStr))
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Time Parser Class
//! ```vb6
//! ' Class: TimeParser
//! ' Parses and validates time strings
//! Option Explicit
//!
//! Public Function Parse(timeStr As String) As Date
//! Parse = TimeValue(timeStr)
//! End Function
//!
//! Public Function TryParse(timeStr As String, ByRef result As Date) As Boolean
//! On Error Resume Next
//! result = TimeValue(timeStr)
//! TryParse = (Err.Number = 0)
//! On Error GoTo 0
//! End Function
//!
//! Public Function IsValid(timeStr As String) As Boolean
//! On Error Resume Next
//! Dim t As Date
//! t = TimeValue(timeStr)
//! IsValid = (Err.Number = 0)
//! On Error GoTo 0
//! End Function
//!
//! Public Function Format(timeStr As String, formatStr As String) As String
//! Format = Format$(TimeValue(timeStr), formatStr)
//! End Function
//! ```
//!
//! ### Example 2: Schedule Importer Module
//! ```vb6
//! ' Module: ScheduleImporter
//! ' Imports and parses schedule times from data
//! Option Explicit
//!
//! Public Function ImportSchedule(times() As String, descriptions() As String) As Collection
//! Dim schedule As New Collection
//! Dim i As Integer
//! For i = LBound(times) To UBound(times)
//! Dim entry As New Collection
//! entry.Add TimeValue(times(i)), "Time"
//! entry.Add descriptions(i), "Description"
//! schedule.Add entry
//! Next i
//! Set ImportSchedule = schedule
//! End Function
//!
//! Public Function GetEarliestTime(times() As String) As Date
//! Dim minTime As Date
//! Dim i As Integer
//! minTime = TimeValue(times(LBound(times)))
//! For i = LBound(times) + 1 To UBound(times)
//! If TimeValue(times(i)) < minTime Then minTime = TimeValue(times(i))
//! Next i
//! GetEarliestTime = minTime
//! End Function
//!
//! Public Function GetLatestTime(times() As String) As Date
//! Dim maxTime As Date
//! Dim i As Integer
//! maxTime = TimeValue(times(LBound(times)))
//! For i = LBound(times) + 1 To UBound(times)
//! If TimeValue(times(i)) > maxTime Then maxTime = TimeValue(times(i))
//! Next i
//! GetLatestTime = maxTime
//! End Function
//! ```
//!
//! ### Example 3: Time Validator Class
//! ```vb6
//! ' Class: TimeValidator
//! ' Validates and normalizes time strings
//! Option Explicit
//!
//! Public Function IsValid(timeStr As String) As Boolean
//! On Error Resume Next
//! Dim t As Date
//! t = TimeValue(timeStr)
//! IsValid = (Err.Number = 0)
//! On Error GoTo 0
//! End Function
//!
//! Public Function Normalize(timeStr As String) As String
//! On Error Resume Next
//! Dim t As Date
//! t = TimeValue(timeStr)
//! If Err.Number = 0 Then
//! Normalize = Format$(t, "hh:mm:ss")
//! Else
//! Normalize = ""
//! End If
//! On Error GoTo 0
//! End Function
//! ```
//!
//! ### Example 4: Time Range Analyzer Module
//! ```vb6
//! ' Module: TimeRangeAnalyzer
//! ' Analyzes and compares time ranges
//! Option Explicit
//!
//! Public Function GetTimeRange(times() As String) As String
//! Dim minTime As Date, maxTime As Date
//! Dim i As Integer
//! minTime = TimeValue(times(LBound(times)))
//! maxTime = minTime
//! For i = LBound(times) + 1 To UBound(times)
//! If TimeValue(times(i)) < minTime Then minTime = TimeValue(times(i))
//! If TimeValue(times(i)) > maxTime Then maxTime = TimeValue(times(i))
//! Next i
//! GetTimeRange = Format$(minTime, "hh:mm") & " - " & Format$(maxTime, "hh:mm")
//! End Function
//!
//! Public Function IsWithinRange(timeStr As String, startStr As String, endStr As String) As Boolean
//! Dim t As Date
//! t = TimeValue(timeStr)
//! IsWithinRange = (t >= TimeValue(startStr) And t <= TimeValue(endStr))
//! End Function
//! ```
//!
//! ## Error Handling
//! The `TimeValue` function can raise the following errors:
//!
//! - **Error 13 (Type mismatch)**: If the string cannot be parsed as a time
//! - **Returns Null**: If the input is Null (not an error)
//!
//! ## Performance Notes
//! - Fast operation - simple parsing
//! - Constant time O(1) complexity for valid strings
//! - Locale-dependent parsing speed
//! - Safe to call repeatedly
//!
//! ## Best Practices
//! 1. **Validate input** before calling if user-provided
//! 2. **Handle Null** explicitly when working with nullable fields
//! 3. **Use Format$** for display after parsing
//! 4. **Test with different locales** for internationalization
//! 5. **Use `TryParse` pattern** for robust error handling
//! 6. **Document expected formats** for maintainability
//! 7. **Store as Date type** for calculations
//! 8. **Use with `TimeSerial`** for arithmetic
//! 9. **Test edge cases** (midnight, noon, invalid times)
//! 10. **Combine with `DateValue`** for full date/time parsing
//!
//! ## Comparison Table
//!
//! | Function | Purpose | Input | Returns |
//! |----------|---------|-------|---------|
//! | `TimeValue` | Parse time from string | time string | Date (time only) |
//! | `DateValue` | Parse date from string | date string | Date (date only) |
//! | `TimeSerial` | Create time from components | hour, minute, second | Date (time only) |
//! | `DateSerial` | Create date from components | year, month, day | Date (date only) |
//! | `CDate` | Convert to date | expression | Date |
//!
//! ## Platform Notes
//! - Available in VB6, VBA, and `VBScript`
//! - Consistent behavior across platforms
//! - Locale-aware parsing
//! - Date portion always zero (12/30/1899)
//! - Compatible with all date/time functions
//!
//! ## Limitations
//! - Only parses up to seconds (no milliseconds)
//! - Locale-dependent parsing (may fail on some formats)
//! - Cannot parse date and time together (use `CDate` for full datetime)
//! - No timezone support
//! - No daylight saving time handling
//! - Returns only time portion (date is zero)
//! - Error on invalid input (not Null)
use crate;
use crate;
/// Implementation of the `TimeValue` function.
///
/// VB6 behavior:
/// - `Null` is returned unchanged
/// - a string must be recognized as a time (a bare time, or a date with an
/// attached time) — the date portion is discarded; otherwise error 13
/// (type mismatch) is raised
/// - other values (including `Empty` and numerics) are treated as date
/// serials via `CDate` semantics, keeping only the fractional time
/// - the date portion of the result is always midnight of 12/30/1899
/// (serial 0)