Skip to content

temporal

Classes:

TemporalVisitorMixin

Bases: VisitorMixinBase

Methods:

visit

visit(node: LiteralDateTime) -> None
Source code in src/irx/builders/llvmliteir/visitors/temporal.py
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
@VisitorCore.visit.dispatch
def visit(self, node: astx.LiteralDateTime) -> None:
    """
    title: Visit LiteralDateTime nodes.
    parameters:
      node:
        type: astx.LiteralDateTime
    """
    hour_minute_count = 2
    hour_minute_second_count = 3
    max_hour = 23
    max_minute_second = 59

    value = node.value.strip()
    if "T" in value:
        date_part, time_part = value.split("T", 1)
    elif " " in value:
        date_part, time_part = value.split(" ", 1)
    else:
        raise ValueError(
            f"LiteralDateTime: invalid format '{node.value}'. "
            "Expected 'YYYY-MM-DDTHH:MM[:SS]' (or space instead of 'T')."
        )

    if "." in time_part:
        raise ValueError(
            "LiteralDateTime: fractional seconds not supported in "
            f"'{node.value}'. Use LiteralTimestamp instead."
        )
    if time_part.endswith("Z") or "+" in time_part or "-" in time_part[2:]:
        raise ValueError(
            "LiteralDateTime: timezone offsets not supported in "
            f"'{node.value}'. Use LiteralTimestamp for timezones."
        )

    try:
        y_str, m_str, d_str = date_part.split("-")
        year = int(y_str)
        month = int(m_str)
        day = int(d_str)
    except Exception as exc:
        raise ValueError(
            f"LiteralDateTime: invalid date part in '{node.value}'. "
            "Expected 'YYYY-MM-DD'."
        ) from exc

    int32_min, int32_max = -(2**31), 2**31 - 1
    if not (int32_min <= year <= int32_max):
        raise ValueError(
            f"LiteralDateTime: year out of 32-bit range in '{node.value}'."
        )

    try:
        parts = time_part.split(":")
        if len(parts) not in (hour_minute_count, hour_minute_second_count):
            raise ValueError("time must be HH:MM or HH:MM:SS")
        hour = int(parts[0])
        minute = int(parts[1])
        second = (
            int(parts[2]) if len(parts) == hour_minute_second_count else 0
        )
    except Exception as exc:
        raise ValueError(
            f"LiteralDateTime: invalid time part in '{node.value}'. "
            "Expected 'HH:MM' or 'HH:MM:SS'."
        ) from exc

    if not (0 <= hour <= max_hour):
        raise ValueError(
            f"LiteralDateTime: hour out of range in '{node.value}'."
        )
    if not (0 <= minute <= max_minute_second):
        raise ValueError(
            f"LiteralDateTime: minute out of range in '{node.value}'."
        )
    if not (0 <= second <= max_minute_second):
        raise ValueError(
            f"LiteralDateTime: second out of range in '{node.value}'."
        )

    try:
        datetime(year, month, day)
        time_value(hour, minute, second)
    except ValueError as exc:
        raise ValueError(
            "LiteralDateTime: invalid calendar date/time in "
            f"'{node.value}'."
        ) from exc

    i32 = self._llvm.INT32_TYPE
    const_dt = ir.Constant(
        self._llvm.DATETIME_TYPE,
        [
            ir.Constant(i32, year),
            ir.Constant(i32, month),
            ir.Constant(i32, day),
            ir.Constant(i32, hour),
            ir.Constant(i32, minute),
            ir.Constant(i32, second),
        ],
    )
    self.result_stack.append(const_dt)