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
//! # `Date` Statement
//!
//! Sets the current system date.
//!
//! ## Syntax
//!
//! ```vb
//! Date = dateexpression
//! ```
//!
//! ## Parts
//!
//! - **dateexpression**: Required. Any expression that can represent a date,
//! including date literals (`#1/1/2025#`), date serial values, or string
//! expressions that parse as dates.
//!
//! ## Return Value
//!
//! None. This is a statement, not a function.
//!
//! ## Remarks
//!
//! The `Date` statement sets the system date to the value of `dateexpression`.
//! In VB6 on Windows this modifies the real system clock and requires
//! administrator privileges. In this runtime the behavior depends on the
//! [`allow_system_clock`](crate::state::clock) flag:
//!
//! - **Default (system clock allowed)**: The date is written to the real
//! system clock via platform APIs. Subsequent `Date` and `Date$` calls
//! return the real system date.
//! - **Internal-only mode**: The date is stored in an offset-based mock
//! clock that advances in real time from the set point. The real system
//! clock is never modified.
//!
//! - **Date Storage**: Internally, dates are stored as `Double` values
//! representing the number of days since December 30, 1899.
//! - **Date Range**: January 1, 100 through December 31, 9999.
//! - **Permissions**: On real VB6, changing the system date requires
//! administrator privileges.
//! - **Date Function**: Use the `Date` function (without assignment) to
//! retrieve the current system date.
//! - **Date$ Function**: Use `Date$` to retrieve the current date as a string.
//! - **Error Handling**: Invalid date values generate a runtime error (error 13,
//! type mismatch).
//!
//! ## Examples
//!
//! ### Set Date to Specific Value
//!
//! ```vb
//! Date = #1/1/2025#
//! ```
//!
//! ### Set Date Using Variable
//!
//! ```vb
//! Dim newDate As Date
//! newDate = #6/15/2025#
//! Date = newDate
//! ```
//!
//! ### Set Date Using DateSerial
//!
//! ```vb
//! Date = DateSerial(2025, 12, 25)
//! ```
//!
//! ### Set Date Using String
//!
//! ```vb
//! Date = "January 1, 2025"
//! ```
//!
//! ### Set Date Using DateAdd
//!
//! ```vb
//! Date = DateAdd("d", 30, Date)
//! ```
//!
//! ### Set Date from User Input
//!
//! ```vb
//! Dim userDate As String
//! userDate = InputBox("Enter new date (MM/DD/YYYY):")
//! If IsDate(userDate) Then
//! Date = userDate
//! Else
//! MsgBox "Invalid date format"
//! End If
//! ```
//!
//! ### Set Date with Error Handling
//!
//! ```vb
//! On Error Resume Next
//! Date = #1/1/2025#
//! If Err.Number <> 0 Then
//! MsgBox "Failed to set date: " & Err.Description
//! End If
//! On Error GoTo 0
//! ```
//!
//! ### Reset Date to Current System Date
//!
//! ```vb
//! ' Clear any override by setting to today's real date
//! Date = Date
//! ```
//!
//! ## Common Errors
//!
//! - **Error 13**: Type mismatch - occurs when the expression cannot be
//! converted to a date.
//! - **Error 5**: Invalid procedure call or argument - occurs with invalid
//! date values (e.g., February 30).
//!
//! ## Best Practices
//!
//! - Validate dates before assignment using `IsDate()`.
//! - Use error handling when setting dates from user input.
//! - Prefer `DateSerial` for programmatic date construction.
//! - Be aware that changing the system date affects all applications.
//!
//! ## See Also
//!
//! - `Date` function (retrieve current system date)
//! - `Date$` function (retrieve current date as string)
//! - `Time` statement (set system time)
//! - `DateSerial` function (create date from components)
//! - `DateValue` function (convert string to date)
//! - `Now` function (get current date and time)
//!
//! ## References
//!
//! - [Date Statement - Microsoft Docs](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/date-statement)
use crateVBResult;
use crateVBVariant;
/// Implementation of the `Date` statement.
///
/// VB6 behavior:
/// - sets the system date to the value of the given expression
/// - in this runtime, when the system clock is allowed, writes to the real
/// clock; otherwise stores in the offset-based mock clock
/// - raises error 13 (type mismatch) if the value cannot be converted to a date