-
Couldn't load subscription status.
- Fork 6
Replace pendulum with whenever #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed support for Python 3.13, it's no longer required to have Rust installed on the system |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| Fix typing for Python 3.9 and remove support for Python 3.13 | ||
| Fix typing for Python 3.9 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Timestamp: Direct access to `obj` and `add_delta` have been deprecated and will be removed in a future version. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Refactor Timestamp to use `whenever` instead of `pendulum` and extend Timestamp with add(), subtract(), and to_datetime(). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| import warnings | ||
| from datetime import datetime, timezone | ||
| from typing import Literal | ||
|
|
||
| import pendulum | ||
| from pendulum.datetime import DateTime | ||
| from whenever import Date, Instant, LocalDateTime, Time, ZonedDateTime | ||
|
|
||
| from .exceptions import TimestampFormatError | ||
|
|
||
| UTC = timezone.utc # Required for older versions of Python | ||
|
|
||
| REGEX_MAPPING = { | ||
| "seconds": r"(\d+)(s|sec|second|seconds)", | ||
|
|
@@ -12,80 +18,175 @@ | |
| } | ||
|
|
||
|
|
||
| class TimestampFormatError(ValueError): ... | ||
|
|
||
|
|
||
| class Timestamp: | ||
| def __init__(self, value: str | DateTime | Timestamp | None = None): | ||
| if value and isinstance(value, DateTime): | ||
| self.obj = value | ||
| _obj: ZonedDateTime | ||
|
|
||
| def __init__(self, value: str | ZonedDateTime | Timestamp | None = None): | ||
| if value and isinstance(value, ZonedDateTime): | ||
| self._obj = value | ||
| elif value and isinstance(value, self.__class__): | ||
| self.obj = value.obj | ||
| self._obj = value._obj | ||
| elif isinstance(value, str): | ||
| self.obj = self._parse_string(value) | ||
| self._obj = self._parse_string(value) | ||
| else: | ||
| self.obj = DateTime.now(tz="UTC") | ||
| self._obj = ZonedDateTime.now("UTC").round(unit="microsecond") | ||
|
|
||
| @property | ||
| def obj(self) -> ZonedDateTime: | ||
| warnings.warn( | ||
| "Direct access to obj property is deprecated. Use to_string(), to_timestamp(), or to_datetime() instead.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| ) | ||
| return self._obj | ||
|
|
||
| @classmethod | ||
| def _parse_string(cls, value: str) -> DateTime: | ||
| def _parse_string(cls, value: str) -> ZonedDateTime: | ||
| try: | ||
| zoned_date = ZonedDateTime.parse_common_iso(value) | ||
| return zoned_date | ||
| except ValueError: | ||
| pass | ||
|
|
||
| try: | ||
| parsed_date = pendulum.parse(value) | ||
| if isinstance(parsed_date, DateTime): | ||
| return parsed_date | ||
| except (pendulum.parsing.exceptions.ParserError, ValueError): | ||
| instant_date = Instant.parse_common_iso(value) | ||
| return instant_date.to_tz("UTC") | ||
| except ValueError: | ||
| pass | ||
|
|
||
| params = {} | ||
| try: | ||
| local_date_time = LocalDateTime.parse_common_iso(value) | ||
| return local_date_time.assume_utc().to_tz("UTC") | ||
| except ValueError: | ||
| pass | ||
|
|
||
| try: | ||
| date = Date.parse_common_iso(value) | ||
| local_date = date.at(Time(12, 00)) | ||
| return local_date.assume_tz("UTC", disambiguate="compatible") | ||
| except ValueError: | ||
| pass | ||
|
|
||
| params: dict[str, float] = {} | ||
| for key, regex in REGEX_MAPPING.items(): | ||
| match = re.search(regex, value) | ||
| if match: | ||
| params[key] = int(match.group(1)) | ||
| params[key] = float(match.group(1)) | ||
|
|
||
| if not params: | ||
| raise TimestampFormatError(f"Invalid time format for {value}") | ||
| if params: | ||
| return ZonedDateTime.now("UTC").subtract(**params) # type: ignore[call-overload] | ||
|
|
||
| return DateTime.now(tz="UTC").subtract(**params) | ||
| raise TimestampFormatError(f"Invalid time format for {value}") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know it was like this before but I think the error definition should live in exceptions.py like the other core SDK exceptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, I moved it |
||
|
|
||
| def __repr__(self) -> str: | ||
| return f"Timestamp: {self.to_string()}" | ||
|
|
||
| def to_string(self, with_z: bool = True) -> str: | ||
| iso8601_string = self.obj.to_iso8601_string() | ||
| if not with_z and iso8601_string[-1] == "Z": | ||
| iso8601_string = iso8601_string[:-1] + "+00:00" | ||
| return iso8601_string | ||
| if with_z: | ||
| return self._obj.instant().format_common_iso() | ||
| return self.to_datetime().isoformat() | ||
|
|
||
| def to_timestamp(self) -> int: | ||
| return self.obj.int_timestamp | ||
| return self._obj.timestamp() | ||
|
|
||
| def to_datetime(self) -> datetime: | ||
| return self._obj.py_datetime() | ||
|
|
||
| def get_obj(self) -> ZonedDateTime: | ||
| return self._obj | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| if not isinstance(other, Timestamp): | ||
| return NotImplemented | ||
| return self.obj == other.obj | ||
| return self._obj == other._obj | ||
|
|
||
| def __lt__(self, other: object) -> bool: | ||
| if not isinstance(other, Timestamp): | ||
| return NotImplemented | ||
| return self.obj < other.obj | ||
| return self._obj < other._obj | ||
|
|
||
| def __gt__(self, other: object) -> bool: | ||
| if not isinstance(other, Timestamp): | ||
| return NotImplemented | ||
| return self.obj > other.obj | ||
| return self._obj > other._obj | ||
|
|
||
| def __le__(self, other: object) -> bool: | ||
| if not isinstance(other, Timestamp): | ||
| return NotImplemented | ||
| return self.obj <= other.obj | ||
| return self._obj <= other._obj | ||
|
|
||
| def __ge__(self, other: object) -> bool: | ||
| if not isinstance(other, Timestamp): | ||
| return NotImplemented | ||
| return self.obj >= other.obj | ||
| return self._obj >= other._obj | ||
|
|
||
| def __hash__(self) -> int: | ||
| return hash(self.to_string()) | ||
|
|
||
| def add_delta(self, hours: int = 0, minutes: int = 0, seconds: int = 0, microseconds: int = 0) -> Timestamp: | ||
| time = self.obj.add(hours=hours, minutes=minutes, seconds=seconds, microseconds=microseconds) | ||
| return Timestamp(time) | ||
| warnings.warn( | ||
| "add_delta() is deprecated. Use add() instead.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| ) | ||
| return self.add(hours=hours, minutes=minutes, seconds=seconds, microseconds=microseconds) | ||
|
|
||
| def add( | ||
| self, | ||
| years: int = 0, | ||
| months: int = 0, | ||
| weeks: int = 0, | ||
| days: int = 0, | ||
| hours: float = 0, | ||
| minutes: float = 0, | ||
| seconds: float = 0, | ||
| milliseconds: float = 0, | ||
| microseconds: float = 0, | ||
| nanoseconds: int = 0, | ||
| disambiguate: Literal["compatible"] = "compatible", | ||
| ) -> Timestamp: | ||
| return Timestamp( | ||
| self._obj.add( | ||
| years=years, | ||
| months=months, | ||
| weeks=weeks, | ||
| days=days, | ||
| hours=hours, | ||
| minutes=minutes, | ||
| seconds=seconds, | ||
| milliseconds=milliseconds, | ||
| microseconds=microseconds, | ||
| nanoseconds=nanoseconds, | ||
| disambiguate=disambiguate, | ||
| ) | ||
| ) | ||
|
|
||
| def subtract( | ||
| self, | ||
| years: int = 0, | ||
| months: int = 0, | ||
| weeks: int = 0, | ||
| days: int = 0, | ||
| hours: float = 0, | ||
| minutes: float = 0, | ||
| seconds: float = 0, | ||
| milliseconds: float = 0, | ||
| microseconds: float = 0, | ||
| nanoseconds: int = 0, | ||
| disambiguate: Literal["compatible"] = "compatible", | ||
| ) -> Timestamp: | ||
| return Timestamp( | ||
| self._obj.subtract( | ||
| years=years, | ||
| months=months, | ||
| weeks=weeks, | ||
| days=days, | ||
| hours=hours, | ||
| minutes=minutes, | ||
| seconds=seconds, | ||
| milliseconds=milliseconds, | ||
| microseconds=microseconds, | ||
| nanoseconds=nanoseconds, | ||
| disambiguate=disambiguate, | ||
| ) | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest creating a follow up issue for when this should be removed and tie it into a future milestone so we know when to remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do