This repository has been archived on 2024-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramBot/modules/utils.py

43 lines
1.5 KiB
Python
Raw Normal View History

2023-09-25 00:47:09 +03:00
from datetime import datetime
from typing import Union
from pytz import UTC
from pytz import timezone as pytz_timezone
def to_utc(date: datetime, timezone: Union[str, None] = None) -> datetime:
2024-05-30 18:47:23 +03:00
"""*DEPRECATED AND WILL BE REMOVED IN FUTURE RELEASES*
Move timezone unaware datetime object to UTC timezone and return it.
2023-09-25 00:47:09 +03:00
### Args:
* date (`datetime`): Datetime to be converted.
* timezone (`Union[str, None] = None`): Timezone name (must be pytz-compatible). Defaults to `None` (UTC).
### Returns:
* `datetime`: Timezone unaware datetime in UTC with timezone's offset applied to it.
"""
timezone = "UTC" if timezone is None else timezone
return pytz_timezone(timezone).localize(date).astimezone(UTC).replace(tzinfo=None)
def from_utc(date: datetime, timezone: Union[str, None] = None) -> datetime:
2024-05-30 18:47:23 +03:00
"""*DEPRECATED AND WILL BE REMOVED IN FUTURE RELEASES*
Move timezone unaware datetime object to the timezone specified and return it.
2023-09-25 00:47:09 +03:00
### Args:
* date (`datetime`): Datetime to be converted.
* timezone (`Union[str, None] = None`): Timezone name (must be pytz-compatible). Defaults to `None` (UTC).
### Returns:
* `datetime`: Timezone unaware datetime in timezone provided with offset from UTC applied to it.
"""
timezone = "UTC" if timezone is None else timezone
return (
pytz_timezone("UTC")
.localize(date)
.astimezone(pytz_timezone(timezone))
.replace(tzinfo=None)
)