2021-09-22 16:49:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2021-09-22 16:49:10 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""This module contains helper functions related to datetime and timestamp conversations.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2021-09-22 16:49:10 +02:00
|
|
|
Previously, the contents of this module were available through the (no longer existing)
|
2021-10-10 15:10:21 +02:00
|
|
|
module ``telegram._utils.helpers``.
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
Warning:
|
|
|
|
Contents of this module are intended to be used internally by the library and *not* by the
|
|
|
|
user. Changes to this module are not considered breaking changes and may not be documented in
|
|
|
|
the changelog.
|
|
|
|
"""
|
2024-02-07 20:45:57 +01:00
|
|
|
import datetime as dtm
|
2021-09-22 16:49:10 +02:00
|
|
|
import time
|
2023-04-18 16:16:23 +02:00
|
|
|
from typing import TYPE_CHECKING, Optional, Union
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Bot
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-10-31 10:12:18 +01:00
|
|
|
# pytz is only available if it was installed as dependency of APScheduler, so we make a little
|
|
|
|
# workaround here
|
2021-09-22 16:49:10 +02:00
|
|
|
DTM_UTC = dtm.timezone.utc
|
|
|
|
try:
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
UTC = pytz.utc
|
|
|
|
except ImportError:
|
|
|
|
UTC = DTM_UTC # type: ignore[assignment]
|
|
|
|
|
|
|
|
|
|
|
|
def _localize(datetime: dtm.datetime, tzinfo: dtm.tzinfo) -> dtm.datetime:
|
|
|
|
"""Localize the datetime, where UTC is handled depending on whether pytz is available or not"""
|
|
|
|
if tzinfo is DTM_UTC:
|
|
|
|
return datetime.replace(tzinfo=DTM_UTC)
|
|
|
|
return tzinfo.localize(datetime) # type: ignore[attr-defined]
|
|
|
|
|
|
|
|
|
|
|
|
def to_float_timestamp(
|
2023-09-09 23:12:30 +02:00
|
|
|
time_object: Union[float, dtm.timedelta, dtm.datetime, dtm.time],
|
2023-05-18 07:57:59 +02:00
|
|
|
reference_timestamp: Optional[float] = None,
|
|
|
|
tzinfo: Optional[dtm.tzinfo] = None,
|
2021-09-22 16:49:10 +02:00
|
|
|
) -> float:
|
|
|
|
"""
|
|
|
|
Converts a given time object to a float POSIX timestamp.
|
|
|
|
Used to convert different time specifications to a common format. The time object
|
|
|
|
can be relative (i.e. indicate a time increment, or a time of day) or absolute.
|
2022-04-24 12:38:09 +02:00
|
|
|
Objects from the :class:`datetime` module that are timezone-naive will be assumed
|
2021-09-22 16:49:10 +02:00
|
|
|
to be in UTC, if ``bot`` is not passed or ``bot.defaults`` is :obj:`None`.
|
|
|
|
|
|
|
|
Args:
|
2023-09-09 23:12:30 +02:00
|
|
|
time_object (:obj:`float` | :obj:`datetime.timedelta` | \
|
2021-09-22 16:49:10 +02:00
|
|
|
:obj:`datetime.datetime` | :obj:`datetime.time`):
|
|
|
|
Time value to convert. The semantics of this parameter will depend on its type:
|
|
|
|
|
2023-09-09 23:12:30 +02:00
|
|
|
* :obj:`float` will be interpreted as "seconds from :paramref:`reference_t`"
|
2021-09-22 16:49:10 +02:00
|
|
|
* :obj:`datetime.timedelta` will be interpreted as
|
2022-04-24 12:38:09 +02:00
|
|
|
"time increment from :paramref:`reference_timestamp`"
|
2021-09-22 16:49:10 +02:00
|
|
|
* :obj:`datetime.datetime` will be interpreted as an absolute date/time value
|
|
|
|
* :obj:`datetime.time` will be interpreted as a specific time of day
|
|
|
|
|
|
|
|
reference_timestamp (:obj:`float`, optional): POSIX timestamp that indicates the absolute
|
2022-04-24 12:38:09 +02:00
|
|
|
time from which relative calculations are to be performed (e.g. when
|
|
|
|
:paramref:`time_object` is given as an :obj:`int`, indicating "seconds from
|
|
|
|
:paramref:`reference_time`"). Defaults to now (the time at which this function is
|
|
|
|
called).
|
|
|
|
|
|
|
|
If :paramref:`time_object` is given as an absolute representation of date & time (i.e.
|
|
|
|
a :obj:`datetime.datetime` object), :paramref:`reference_timestamp` is not relevant
|
|
|
|
and so its value should be :obj:`None`. If this is not the case, a :exc:`ValueError`
|
|
|
|
will be raised.
|
2022-10-31 10:12:18 +01:00
|
|
|
tzinfo (:class:`datetime.tzinfo`, optional): If :paramref:`time_object` is a naive object
|
2022-04-24 12:38:09 +02:00
|
|
|
from the :mod:`datetime` module, it will be interpreted as this timezone. Defaults to
|
2022-10-31 10:12:18 +01:00
|
|
|
``pytz.utc``, if available, and :attr:`datetime.timezone.utc` otherwise.
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
Note:
|
|
|
|
Only to be used by ``telegram.ext``.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`float` | :obj:`None`:
|
2022-04-24 12:38:09 +02:00
|
|
|
The return value depends on the type of argument :paramref:`time_object`.
|
|
|
|
If :paramref:`time_object` is given as a time increment (i.e. as a :obj:`int`,
|
|
|
|
:obj:`float` or :obj:`datetime.timedelta`), then the return value will be
|
|
|
|
:paramref:`reference_timestamp` + :paramref:`time_object`.
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
Else if it is given as an absolute date/time value (i.e. a :obj:`datetime.datetime`
|
|
|
|
object), the equivalent value as a POSIX timestamp will be returned.
|
|
|
|
|
|
|
|
Finally, if it is a time of the day without date (i.e. a :obj:`datetime.time`
|
|
|
|
object), the return value is the nearest future occurrence of that time of day.
|
|
|
|
|
|
|
|
Raises:
|
2022-04-24 12:38:09 +02:00
|
|
|
TypeError: If :paramref:`time_object` s type is not one of those described above.
|
|
|
|
ValueError: If :paramref:`time_object` is a :obj:`datetime.datetime` and
|
|
|
|
:paramref:`reference_timestamp` is not :obj:`None`.
|
2021-09-22 16:49:10 +02:00
|
|
|
"""
|
|
|
|
if reference_timestamp is None:
|
|
|
|
reference_timestamp = time.time()
|
|
|
|
elif isinstance(time_object, dtm.datetime):
|
2022-05-05 17:40:22 +02:00
|
|
|
raise ValueError("t is an (absolute) datetime while reference_timestamp is not None")
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
if isinstance(time_object, dtm.timedelta):
|
|
|
|
return reference_timestamp + time_object.total_seconds()
|
|
|
|
if isinstance(time_object, (int, float)):
|
|
|
|
return reference_timestamp + time_object
|
|
|
|
|
|
|
|
if tzinfo is None:
|
|
|
|
tzinfo = UTC
|
|
|
|
|
|
|
|
if isinstance(time_object, dtm.time):
|
|
|
|
reference_dt = dtm.datetime.fromtimestamp(
|
|
|
|
reference_timestamp, tz=time_object.tzinfo or tzinfo
|
|
|
|
)
|
|
|
|
reference_date = reference_dt.date()
|
|
|
|
reference_time = reference_dt.timetz()
|
|
|
|
|
|
|
|
aware_datetime = dtm.datetime.combine(reference_date, time_object)
|
|
|
|
if aware_datetime.tzinfo is None:
|
|
|
|
aware_datetime = _localize(aware_datetime, tzinfo)
|
|
|
|
|
|
|
|
# if the time of day has passed today, use tomorrow
|
|
|
|
if reference_time > aware_datetime.timetz():
|
|
|
|
aware_datetime += dtm.timedelta(days=1)
|
|
|
|
return _datetime_to_float_timestamp(aware_datetime)
|
|
|
|
if isinstance(time_object, dtm.datetime):
|
|
|
|
if time_object.tzinfo is None:
|
|
|
|
time_object = _localize(time_object, tzinfo)
|
|
|
|
return _datetime_to_float_timestamp(time_object)
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
raise TypeError(f"Unable to convert {type(time_object).__name__} object to timestamp")
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def to_timestamp(
|
2023-09-09 23:12:30 +02:00
|
|
|
dt_obj: Union[float, dtm.timedelta, dtm.datetime, dtm.time, None],
|
2023-05-18 07:57:59 +02:00
|
|
|
reference_timestamp: Optional[float] = None,
|
|
|
|
tzinfo: Optional[dtm.tzinfo] = None,
|
2021-09-22 16:49:10 +02:00
|
|
|
) -> Optional[int]:
|
|
|
|
"""
|
|
|
|
Wrapper over :func:`to_float_timestamp` which returns an integer (the float value truncated
|
|
|
|
down to the nearest integer).
|
|
|
|
|
|
|
|
See the documentation for :func:`to_float_timestamp` for more details.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
int(to_float_timestamp(dt_obj, reference_timestamp, tzinfo))
|
|
|
|
if dt_obj is not None
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-04-18 16:16:23 +02:00
|
|
|
def from_timestamp(
|
|
|
|
unixtime: Optional[int],
|
|
|
|
tzinfo: Optional[dtm.tzinfo] = None,
|
|
|
|
) -> Optional[dtm.datetime]:
|
2021-09-22 16:49:10 +02:00
|
|
|
"""
|
|
|
|
Converts an (integer) unix timestamp to a timezone aware datetime object.
|
|
|
|
:obj:`None` s are left alone (i.e. ``from_timestamp(None)`` is :obj:`None`).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
unixtime (:obj:`int`): Integer POSIX timestamp.
|
|
|
|
tzinfo (:obj:`datetime.tzinfo`, optional): The timezone to which the timestamp is to be
|
2023-04-18 16:16:23 +02:00
|
|
|
converted to. Defaults to :obj:`None`, in which case the returned datetime object will
|
|
|
|
be timezone aware and in UTC.
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
Returns:
|
2022-04-24 12:38:09 +02:00
|
|
|
Timezone aware equivalent :obj:`datetime.datetime` value if :paramref:`unixtime` is not
|
2021-09-22 16:49:10 +02:00
|
|
|
:obj:`None`; else :obj:`None`.
|
|
|
|
"""
|
|
|
|
if unixtime is None:
|
|
|
|
return None
|
|
|
|
|
2023-04-18 16:16:23 +02:00
|
|
|
return dtm.datetime.fromtimestamp(unixtime, tz=UTC if tzinfo is None else tzinfo)
|
|
|
|
|
|
|
|
|
|
|
|
def extract_tzinfo_from_defaults(bot: "Bot") -> Union[dtm.tzinfo, None]:
|
|
|
|
"""
|
|
|
|
Extracts the timezone info from the default values of the bot.
|
|
|
|
If the bot has no default values, :obj:`None` is returned.
|
|
|
|
"""
|
|
|
|
# We don't use `ininstance(bot, ExtBot)` here so that this works
|
|
|
|
# in `python-telegram-bot-raw` as well
|
|
|
|
if hasattr(bot, "defaults") and bot.defaults:
|
|
|
|
return bot.defaults.tzinfo
|
|
|
|
return None
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _datetime_to_float_timestamp(dt_obj: dtm.datetime) -> float:
|
|
|
|
"""
|
|
|
|
Converts a datetime object to a float timestamp (with sub-second precision).
|
|
|
|
If the datetime object is timezone-naive, it is assumed to be in UTC.
|
|
|
|
"""
|
|
|
|
if dt_obj.tzinfo is None:
|
|
|
|
dt_obj = dt_obj.replace(tzinfo=dtm.timezone.utc)
|
|
|
|
return dt_obj.timestamp()
|