I am facing some issues trying to change time zone of a datetime object without pytz module. So basically I need exactly this code without using pytz:
def change_timezone(date: str, tz: str):
tz = pytz.timezone(tz)
date = datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
return date = date.replace(tzinfo=pytz.utc).astimezone(tz=tz)
I am running this script in an external platform which doesn't allow me to use external Python modules, that is why I can't use pytz.
The input to this function is a string containing the date in UTC timezone (for example: date = "2021-11-30T23:00:00Z"
) and another string specifying the timezone to which the date should be converted (for example: tz = "Europe/Madrid"
).
Is there any way around this without using pytz?
Thanks!
UPDATE
When using zoneinfo module:
from zoneinfo import ZoneInfo
SPAIN = ZoneInfo("America/Los_Angeles")
I get the following error:
Traceback (most recent call last):
File "C:\Users\sydea\AppData\Local\Programs\Python\Python39-32\lib\zoneinfo\_common.py", line 12, in load_tzdata
return importlib.resources.open_binary(package_name, resource_name)
File "C:\Users\sydea\AppData\Local\Programs\Python\Python39-32\lib\importlib\resources.py", line 88, in open_binary
package = _get_package(package)
File "C:\Users\sydea\AppData\Local\Programs\Python\Python39-32\lib\importlib\resources.py", line 49, in _get_package
module = _resolve(package)
File "C:\Users\sydea\AppData\Local\Programs\Python\Python39-32\lib\importlib\resources.py", line 40, in _resolve
return import_module(name)
File "C:\Users\sydea\AppData\Local\Programs\Python\Python39-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tzdata'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\sydea\Desktop\Cargadores solares\Proyecto\OCPP-1.6J-CS-I\pruebas.py", line 8, in <module>
SPAIN = ZoneInfo("America/Los_Angeles")
File "C:\Users\sydea\AppData\Local\Programs\Python\Python39-32\lib\zoneinfo\_common.py", line 24, in load_tzdata
raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key America/Los_Angeles'
UPDATE I don't know if this is helpful, but running this code:
import sysconfig
print(sysconfig.get_config_var("TZPATH"))
shows this path:
/usr/share/zoneinfo:/usr/lib/zoneinfo:/usr/share/lib/zoneinfo:/etc/zoneinfo