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
//! # `Date` Function
//!
//! Returns the current system date as a `Variant` of subtype `Date`.
//!
//! ## Syntax
//!
//! ```vb
//! Date
//! ```
//!
//! ## Parameters
//!
//! None. The `Date` function takes no parameters.
//!
//! ## Return Value
//!
//! Returns a `Variant` of subtype `Date` (`VarType = 7`) containing the current system date.
//! The time portion is set to midnight (00:00:00).
//!
//! ## Remarks
//!
//! The `Date` function returns the current date from the system clock. Unlike `Now`,
//! which returns both date and time, `Date` returns only the date portion with the
//! time set to midnight.
//!
//! **Important Characteristics:**
//!
//! - Returns only the date portion (time is midnight)
//! - Uses system date from computer's clock
//! - Can also be used as a statement to set the system date: `Date = #1/1/2025#`
//! - Date values are stored internally as Double (days since Dec 30, 1899)
//! - `VarType` of result is 7 (vbDate)
//! - Locale-aware for display formatting
//! - Date range: January 1, 100 to December 31, 9999
//!
//! ## Date Storage Format
//!
//! Internally, dates are stored as Double precision floating-point numbers:
//! - Integer part: Number of days since December 30, 1899
//! - Fractional part: Time of day (0.0 = midnight, 0.5 = noon, etc.)
//! - Date function always returns fractional part as 0.0
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```vb
//! ' Get current date
//! Dim today As Date
//! today = Date
//! MsgBox "Today is: " & today
//!
//! ' Display in message box
//! MsgBox "Current date: " & Date
//!
//! ' Store in Variant
//! Dim currentDate As Variant
//! currentDate = Date
//! ```
//!
//! ### Date Calculations
//!
//! ```vb
//! ' Calculate days until end of year
//! Dim daysLeft As Long
//! daysLeft = DateSerial(Year(Date), 12, 31) - Date
//! MsgBox daysLeft & " days left in the year"
//!
//! ' Add 30 days to current date
//! Dim futureDate As Date
//! futureDate = Date + 30
//! MsgBox "30 days from now: " & futureDate
//!
//! ' Subtract dates to get difference
//! Dim startDate As Date
//! Dim daysPassed As Long
//! startDate = #1/1/2025#
//! daysPassed = Date - startDate
//! ```
//!
//! ### Date Comparison
//!
//! ```vb
//! ' Check if date is in the past
//! Dim deadline As Date
//! deadline = #12/31/2025#
//!
//! If Date > deadline Then
//! MsgBox "Deadline has passed!"
//! Else
//! MsgBox "Still time remaining"
//! End If
//!
//! ' Compare with specific date
//! If Date = #1/1/2025# Then
//! MsgBox "Happy New Year!"
//! End If
//! ```
//!
//! ## Common Patterns
//!
//! ### Date Stamping Records
//!
//! ```vb
//! ' Add timestamp to database record
//! rs.AddNew
//! rs("CustomerName") = txtName.Text
//! rs("OrderDate") = Date
//! rs("OrderTime") = Time
//! rs.Update
//!
//! ' File naming with date
//! Dim fileName As String
//! fileName = "Report_" & Format(Date, "yyyymmdd") & ".txt"
//! ```
//!
//! ### Date Validation
//!
//! ```vb
//! Function IsDateInRange(checkDate As Date, startDate As Date, endDate As Date) As Boolean
//! IsDateInRange = (checkDate >= startDate And checkDate <= endDate)
//! End Function
//!
//! ' Check if today is within range
//! If IsDateInRange(Date, #1/1/2025#, #12/31/2025#) Then
//! MsgBox "Date is within 2025"
//! End If
//! ```
//!
//! ### Age Calculation
//!
//! ```vb
//! Function CalculateAge(birthDate As Date) As Integer
//! Dim age As Integer
//! age = Year(Date) - Year(birthDate)
//!
//! ' Adjust if birthday hasn't occurred yet this year
//! If Month(Date) < Month(birthDate) Or _
//! (Month(Date) = Month(birthDate) And Day(Date) < Day(birthDate)) Then
//! age = age - 1
//! End If
//!
//! CalculateAge = age
//! End Function
//! ```
//!
//! ### Business Days Calculation
//!
//! ```vb
//! Function IsWeekday(checkDate As Date) As Boolean
//! Dim dayOfWeek As Integer
//! dayOfWeek = Weekday(checkDate)
//! IsWeekday = (dayOfWeek > 1 And dayOfWeek < 7) ' Not Sunday(1) or Saturday(7)
//! End Function
//!
//! Function AddBusinessDays(startDate As Date, days As Integer) As Date
//! Dim currentDate As Date
//! Dim daysAdded As Integer
//!
//! currentDate = startDate
//! daysAdded = 0
//!
//! Do While daysAdded < days
//! currentDate = currentDate + 1
//! If IsWeekday(currentDate) Then
//! daysAdded = daysAdded + 1
//! End If
//! Loop
//!
//! AddBusinessDays = currentDate
//! End Function
//! ```
//!
//! ### Date Range Reporting
//!
//! ```vb
//! Sub GenerateMonthlyReport()
//! Dim firstDay As Date
//! Dim lastDay As Date
//!
//! ' Get first day of current month
//! firstDay = DateSerial(Year(Date), Month(Date), 1)
//!
//! ' Get last day of current month
//! lastDay = DateSerial(Year(Date), Month(Date) + 1, 0)
//!
//! ' Generate report for date range
//! MsgBox "Report period: " & firstDay & " to " & lastDay
//! End Sub
//! ```
//!
//! ### Date-Based File Organization
//!
//! ```vb
//! Function GetArchiveFolder() As String
//! Dim folderPath As String
//! folderPath = "C:\Archive\" & Year(Date) & "\" & Format(Date, "mm")
//!
//! ' Create folder if it doesn't exist
//! If Dir(folderPath, vbDirectory) = "" Then
//! MkDir folderPath
//! End If
//!
//! GetArchiveFolder = folderPath
//! End Function
//! ```
//!
//! ### Expiration Checking
//!
//! ```vb
//! Function IsExpired(expirationDate As Date) As Boolean
//! IsExpired = (Date > expirationDate)
//! End Function
//!
//! Function DaysUntilExpiration(expirationDate As Date) As Long
//! If IsExpired(expirationDate) Then
//! DaysUntilExpiration = 0
//! Else
//! DaysUntilExpiration = expirationDate - Date
//! End If
//! End Function
//! ```
//!
//! ### Logging with Timestamps
//!
//! ```vb
//! Sub LogMessage(message As String)
//! Dim logFile As Integer
//! Dim logFileName As String
//!
//! ' Create daily log file
//! logFileName = "Log_" & Format(Date, "yyyy-mm-dd") & ".txt"
//!
//! logFile = FreeFile
//! Open logFileName For Append As logFile
//! Print #logFile, Date & " " & Time & ": " & message
//! Close logFile
//! End Sub
//! ```
//!
//! ## Advanced Usage
//!
//! ### Date Cache for Performance
//!
//! ```vb
//! ' Module-level variables
//! Private m_cachedDate As Date
//! Private m_cacheValid As Boolean
//!
//! Function GetTodaysDate() As Date
//! ' Cache date to avoid repeated system calls in tight loops
//! Static lastCheck As Date
//!
//! If Not m_cacheValid Or lastCheck <> Date Then
//! m_cachedDate = Date
//! lastCheck = m_cachedDate
//! m_cacheValid = True
//! End If
//!
//! GetTodaysDate = m_cachedDate
//! End Function
//! ```
//!
//! ### Fiscal Year Calculations
//!
//! ```vb
//! Function GetFiscalYear(Optional checkDate As Variant) As Integer
//! Dim workDate As Date
//!
//! ' Use current date if not specified
//! If IsMissing(checkDate) Then
//! workDate = Date
//! Else
//! workDate = checkDate
//! End If
//!
//! ' Fiscal year starts April 1
//! If Month(workDate) < 4 Then
//! GetFiscalYear = Year(workDate) - 1
//! Else
//! GetFiscalYear = Year(workDate)
//! End If
//! End Function
//!
//! Function GetFiscalQuarter(Optional checkDate As Variant) As Integer
//! Dim workDate As Date
//! Dim fiscalMonth As Integer
//!
//! If IsMissing(checkDate) Then
//! workDate = Date
//! Else
//! workDate = checkDate
//! End If
//!
//! ' Calculate month in fiscal year (April = 1)
//! fiscalMonth = Month(workDate) - 3
//! If fiscalMonth <= 0 Then fiscalMonth = fiscalMonth + 12
//!
//! ' Determine quarter
//! GetFiscalQuarter = Int((fiscalMonth - 1) / 3) + 1
//! End Function
//! ```
//!
//! ### Date Sequence Generator
//!
//! ```vb
//! Function GenerateDateRange(startDate As Date, endDate As Date) As Variant
//! Dim dates() As Date
//! Dim currentDate As Date
//! Dim index As Long
//! Dim dayCount As Long
//!
//! dayCount = endDate - startDate + 1
//! ReDim dates(0 To dayCount - 1)
//!
//! currentDate = startDate
//! For index = 0 To dayCount - 1
//! dates(index) = currentDate
//! currentDate = currentDate + 1
//! Next index
//!
//! GenerateDateRange = dates
//! End Function
//! ```
//!
//! ### Date-Based Conditional Logic
//!
//! ```vb
//! Function GetDiscountRate() As Double
//! Dim dayOfMonth As Integer
//! dayOfMonth = Day(Date)
//!
//! Select Case dayOfMonth
//! Case 1 To 10
//! GetDiscountRate = 0.1 ' 10% early month discount
//! Case 11 To 20
//! GetDiscountRate = 0.05 ' 5% mid-month discount
//! Case Else
//! GetDiscountRate = 0 ' No discount
//! End Select
//! End Function
//! ```
//!
//! ## Date vs Now vs Time
//!
//! ```vb
//! ' Date - Returns only date portion (time = midnight)
//! Dim d As Date
//! d = Date ' Example: 1/15/2025 12:00:00 AM
//!
//! ' Now - Returns date and time
//! Dim n As Date
//! n = Now ' Example: 1/15/2025 2:30:45 PM
//!
//! ' Time - Returns only time portion (date = Dec 30, 1899)
//! Dim t As Date
//! t = Time ' Example: 12/30/1899 2:30:45 PM
//!
//! ' Extract components
//! Dim today As Date
//! today = Date ' Gets just the date
//! Dim dateTime As Date
//! dateTime = today + Time ' Combines current date and time
//! ```
//!
//! ## Setting the System Date
//!
//! ```vb
//! ' Date can also be used as a statement (requires admin rights)
//! Date = #1/1/2025# ' Sets system date
//!
//! ' More common: just read the date
//! Dim currentDate As Date
//! currentDate = Date ' Read-only operation
//! ```
//!
//! ## Performance Considerations
//!
//! - `Date` is a system call that reads the system clock
//! - In tight loops, consider caching if the date won't change during execution
//! - Faster than `Now` when you only need the date portion
//! - Date comparisons are fast (numeric comparison of Double values)
//! - Format conversions can be slower than raw date operations
//!
//! ## Best Practices
//!
//! ### Cache in Long-Running Loops
//!
//! ```vb
//! ' Inefficient - calls Date repeatedly
//! For i = 1 To 10000
//! If records(i).ExpiryDate < Date Then
//! ' Process expired record
//! End If
//! Next i
//!
//! ' Better - cache the date
//! Dim today As Date
//! today = Date
//! For i = 1 To 10000
//! If records(i).ExpiryDate < today Then
//! ' Process expired record
//! End If
//! Next i
//! ```
//!
//! ### Use Appropriate Date Functions
//!
//! ```vb
//! ' Good - Use Date for date-only operations
//! Dim orderDate As Date
//! orderDate = Date
//!
//! ' Good - Use Now for timestamps
//! Dim timestamp As Date
//! timestamp = Now
//!
//! ' Avoid - Don't extract date from Now if you just need date
//! Dim today As Date
//! today = Int(Now) ' Works but less clear than Date
//! ```
//!
//! ### Store Dates as Date Type
//!
//! ```vb
//! ' Good - Proper type usage
//! Dim birthDate As Date
//! birthDate = Date
//!
//! ' Avoid - String storage loses type safety and comparison capability
//! Dim birthDateStr As String
//! birthDateStr = CStr(Date) ' Comparison becomes string-based
//! ```
//!
//! ## Locale Considerations
//!
//! - Display format depends on system locale settings
//! - Internal storage is always the same (Double)
//! - Use `Format` function for explicit formatting
//! - Date literals use US format (#MM/DD/YYYY#) regardless of locale
//!
//! ```vb
//! ' Display varies by locale
//! MsgBox Date ' US: 1/15/2025, UK: 15/01/2025
//!
//! ' Explicit formatting
//! MsgBox Format(Date, "yyyy-mm-dd") ' Always: 2025-01-15
//! ```
//!
//! ## Limitations
//!
//! - Cannot directly extract error information from invalid dates
//! - System date setting requires administrative privileges
//! - Date range limited to January 1, 100 through December 31, 9999
//! - Accuracy depends on system clock
//! - No built-in timezone support
//! - Daylight saving time handled by system, not VB6
//!
//! ## Related Functions
//!
//! - `Now`: Returns current date and time
//! - `Time`: Returns current time (date portion is Dec 30, 1899)
//! - `DateSerial`: Creates a date from year, month, and day values
//! - `DateValue`: Converts a string to a date
//! - `Year`, `Month`, `Day`: Extract components from a date
//! - `Weekday`: Returns day of week (1=Sunday, 7=Saturday)
//! - `DateAdd`: Adds a time interval to a date
//! - `DateDiff`: Returns the difference between two dates
//! - `DatePart`: Returns a specific part of a date
//! - `Format`: Formats a date as a string
//! - `IsDate`: Tests if a value can be converted to a date
//! - `CDate`: Converts an expression to a Date
use crate::;
/// Implementation of the `Date` function.
///
/// VB6 behavior:
/// - returns the current system date as a `Date` variant with the time
/// portion set to midnight
/// - never raises an error