|
| 1 | +import logging |
1 | 2 | import platform
|
2 | 3 | import re
|
3 | 4 |
|
|
19 | 20 |
|
20 | 21 | localized = True
|
21 | 22 |
|
| 23 | +log = logging.getLogger(__name__) |
| 24 | + |
22 | 25 |
|
23 | 26 | def datetime_to_timestamp(dt: Union[dtdate, datetime]) -> int:
|
24 |
| - if isinstance(dt, datetime) and getattr(dt, "tzinfo", None) is not None: |
25 |
| - dt = dt.astimezone(tzutc()) |
| 27 | + if dt == datetime.min: |
| 28 | + return timegm(dt.timetuple()) |
| 29 | + if isinstance(dt, datetime): |
| 30 | + try: |
| 31 | + dt = dt.astimezone(tzutc()) |
| 32 | + except OSError: |
| 33 | + pass |
26 | 34 | return timegm(dt.timetuple())
|
27 | 35 |
|
28 | 36 |
|
29 | 37 | def convert_timestamp_to_datetime(timestamp: Union[int, float], tzinfo: TzInfo) -> datetime:
|
30 |
| - import datetime as dt |
31 |
| - |
32 | 38 | if timestamp >= 0:
|
33 |
| - return dt.datetime.fromtimestamp(timestamp, tzinfo) |
| 39 | + try: |
| 40 | + return datetime.fromtimestamp(timestamp, tzinfo) |
| 41 | + except OSError: |
| 42 | + log.exception("OSError occurred while converting timestamp '%s' to datetime", timestamp) |
| 43 | + raise |
34 | 44 | else:
|
35 |
| - return dt.datetime(1970, 1, 1, tzinfo=tzinfo) + dt.timedelta(seconds=int(timestamp)) |
| 45 | + return datetime(1970, 1, 1, tzinfo=tzinfo) + timedelta(seconds=int(timestamp)) |
36 | 46 |
|
37 | 47 |
|
38 | 48 | def timestamp_to_datetime(timestamp: Union[int, float], tzinfo: Optional[TzInfo]) -> datetime:
|
@@ -1849,10 +1859,15 @@ def _get_reference_date_time(
|
1849 | 1859 | If both are absolute, return the most recent one.
|
1850 | 1860 | If both are None, return now.
|
1851 | 1861 | """
|
| 1862 | + |
1852 | 1863 | min_ = datetime_to_timestamp(datetime.min)
|
1853 | 1864 | now = datetime.now(tzinfo)
|
1854 | 1865 | if start_date is None and end_date is None:
|
1855 | 1866 | return now
|
| 1867 | + if start_date is None and end_date is not None: |
| 1868 | + start_date = now |
| 1869 | + elif start_date is not None and end_date is None: |
| 1870 | + end_date = now |
1856 | 1871 |
|
1857 | 1872 | start_int = cls._parse_date_time(start_date, now) if start_date is not None else min_
|
1858 | 1873 | end_int = cls._parse_date_time(end_date, now) if end_date is not None else min_
|
@@ -1965,7 +1980,14 @@ def unix_time(
|
1965 | 1980 |
|
1966 | 1981 | :example: 1061306726.6
|
1967 | 1982 | """
|
1968 |
| - now = self._get_reference_date_time(start_datetime, end_datetime, tzinfo=None) |
| 1983 | + if start_datetime is not None and end_datetime is None: |
| 1984 | + if start_datetime == "now": |
| 1985 | + end_datetime = "+30d" |
| 1986 | + else: |
| 1987 | + end_datetime = datetime.now(tz=tzutc()) |
| 1988 | + elif start_datetime is None and end_datetime is not None: |
| 1989 | + start_datetime = datetime(1970, 1, 1, tzinfo=tzutc()) |
| 1990 | + now = self._get_reference_date_time(start_datetime, end_datetime, tzinfo=tzutc()) |
1969 | 1991 | start_datetime = self._parse_start_datetime(now, start_datetime)
|
1970 | 1992 | end_datetime = self._parse_end_datetime(now, end_datetime)
|
1971 | 1993 | return float(self._rand_seconds(start_datetime, end_datetime))
|
@@ -2220,7 +2242,7 @@ def date_time_between_dates(
|
2220 | 2242 | if tzinfo is None:
|
2221 | 2243 | pick = convert_timestamp_to_datetime(timestamp, tzlocal())
|
2222 | 2244 | try:
|
2223 |
| - pick = pick.astimezone(tzutc()).replace(tzinfo=None) |
| 2245 | + pick = pick.replace(tzinfo=None) |
2224 | 2246 | except OSError:
|
2225 | 2247 | pass
|
2226 | 2248 | else:
|
|
0 commit comments