41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from datetime import datetime
|
|
from typing import Union
|
|
|
|
import pytz
|
|
|
|
|
|
def to_utc(date: datetime, timezone: Union[str, None] = None) -> datetime:
|
|
"""*DEPRECATED AND WILL BE REMOVED IN FUTURE RELEASES*
|
|
|
|
Move timezone unaware datetime object to UTC timezone and return it.
|
|
|
|
### 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(pytz.utc).replace(tzinfo=None)
|
|
)
|
|
|
|
|
|
def from_utc(date: datetime, timezone: Union[str, None] = None) -> datetime:
|
|
"""*DEPRECATED AND WILL BE REMOVED IN FUTURE RELEASES*
|
|
|
|
Move timezone unaware datetime object to the timezone specified and return it.
|
|
|
|
### 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.utc.localize(date).astimezone(pytz.timezone(timezone)).replace(tzinfo=None)
|
|
)
|