|
81 | 81 | ``` |
82 | 82 | """ |
83 | 83 |
|
| 84 | +import decimal |
84 | 85 | from datetime import datetime, timedelta, timezone |
85 | | -from typing import Any, Optional, Union, overload |
| 86 | +from typing import Any, Optional, Union, cast, overload |
86 | 87 |
|
87 | 88 | from dateutil import parser |
88 | 89 | from typing_extensions import Never |
@@ -360,8 +361,8 @@ def ab_datetime_now() -> AirbyteDateTime: |
360 | 361 |
|
361 | 362 |
|
362 | 363 | def ab_datetime_parse( |
363 | | - dt_str: str | int, |
364 | | - formats: list[str | None] | None = None, |
| 364 | + dt_str: str | int | float, |
| 365 | + formats: list[str | None] | list[str] | None = None, |
365 | 366 | disallow_other_formats: bool = False, |
366 | 367 | ) -> AirbyteDateTime: |
367 | 368 | """Parses a datetime string or timestamp into an AirbyteDateTime with timezone awareness. |
@@ -404,6 +405,27 @@ def ab_datetime_parse( |
404 | 405 | # Remove None values from formats list, and coalesce to None if empty |
405 | 406 | formats = [f for f in formats or [] if f] or None |
406 | 407 |
|
| 408 | + if isinstance(dt_str, str): |
| 409 | + if dt_str.startswith("-"): |
| 410 | + raise ValueError("Timestamp cannot be negative: " + dt_str) |
| 411 | + |
| 412 | + if dt_str[1:].replace(".", "").isdigit(): |
| 413 | + # Handle floats and ints as strings |
| 414 | + if "." in dt_str: |
| 415 | + dt_str = float(dt_str) |
| 416 | + else: |
| 417 | + dt_str = int(dt_str) |
| 418 | + |
| 419 | + if isinstance(dt_str, float): |
| 420 | + # Handle float values as Unix timestamps (UTC) |
| 421 | + if dt_str < 0: |
| 422 | + raise ValueError("Timestamp cannot be negative") |
| 423 | + if len(str(abs(int(dt_str)))) > 10: |
| 424 | + raise ValueError("Timestamp value too large") |
| 425 | + |
| 426 | + instant = Instant.from_timestamp(dt_str) |
| 427 | + return AirbyteDateTime.from_datetime(instant.py_datetime()) |
| 428 | + |
407 | 429 | # Handle numeric values as Unix timestamps (UTC) |
408 | 430 | if isinstance(dt_str, int) or ( |
409 | 431 | isinstance(dt_str, str) |
|
0 commit comments