2

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
iandresolares
  • 43
  • 1
  • 7
  • 2
    What's the Python version you can use on that host? 3.9+ has a builtin [`zoneinfo`](https://docs.python.org/3/library/zoneinfo.html) module. – deceze Dec 23 '21 at 09:48
  • 1
    [a way to parse](https://stackoverflow.com/a/62769371/10197418) ISO8601 with Z for UTC to aware datetime, [a way to convert](https://stackoverflow.com/a/63628816/10197418) using `zoneinfo` (basically just use astimezone as you have it already). – FObersteiner Dec 23 '21 at 09:51
  • Also, what exactly does "external" mean? You can usually just include the entire pytz folder with your .py file, you don't need tools like `pip` to "install" it. As long as you can upload .py files, you can add any modules you want with very few exceptions. – deceze Dec 23 '21 at 09:52
  • Hello @deceze, I'm using python 3.9.5. When using zoneinfo module like this: from zoneinfo import ZoneInfo SPAIN = ZoneInfo("Europe/Madrid") I get the following error: https://ibb.co/wNx0WbY. I can't upload any files, I just have one editor available with a python script, nothing else. – iandresolares Dec 23 '21 at 10:05
  • @iandresolares concerning the error, unfortunately on Windows machines, you'll need the `tzdata` package to use zoneinfo to keep time zone rule definitions up-to-date. – FObersteiner Dec 23 '21 at 10:11
  • 1
    Sooo… what *can* you do on that weird editor? https://docs.python.org/3/library/zoneinfo.html#configuring-the-data-sources – deceze Dec 23 '21 at 10:14
  • hmmm, then this is not an option, any other suggestion? Is there any way to create this timezone object by myself? I could make a class with the most common timezones my system will face or something like that. – iandresolares Dec 23 '21 at 10:15
  • @deceze, usually when trying to access anything from the system I get a permission dennied error, but let me check – iandresolares Dec 23 '21 at 10:16
  • See https://docs.python.org/3/library/datetime.html#datetime.tzinfo for defining timezones manually. – deceze Dec 23 '21 at 10:17
  • @deceze please check the update in the question, is that what you asked me to do? – iandresolares Dec 23 '21 at 10:23
  • "*external platform*" - can you clarify what that exactly is? The output of `sysconfig` suggests you're on a Unix system, but then `tzdata` is not required... – FObersteiner Dec 23 '21 at 10:30
  • @MrFuppes it is an IoT platform (https://tago.io/) which allow you to run python code. You are right, it isn't windows, it is actually linux – iandresolares Dec 23 '21 at 10:33
  • I'm not familiar with that but I have a feeling that this question here actually comes down how the Python interpreter behaves on their system, what it is allowed to access etc. In any case, if they offer to run Python code, then the installed Python version should support up-to-date time zone definitions, i.e. a working Python zoneinfo library! – FObersteiner Dec 23 '21 at 10:39
  • @MrFuppes I agree! All they thought is that I can not use external python modules, I will ask them about zoneinfo library since it is not external. – iandresolares Dec 23 '21 at 10:49
  • I found a way!! Using tz from dateutils!! – iandresolares Dec 23 '21 at 11:27
  • dateutil is external as well… does it happen to be pre-installed on that platform…? – deceze Dec 23 '21 at 12:21
  • @deceze, it does yeah! I guess it was pre-installed for some reason. – iandresolares Dec 28 '21 at 08:40

1 Answers1

-1

Using tz from dateutil:

  from dateutil import tz
  import datetime

  start_date = datetime.datetime.strptime(str_datetime, "%Y-%m-%dT%H:%M:%SZ")
  from_zone = tz.gettz('UTC')
  to_zone = tz.gettz('Europe/Madrid')

  start_date = start_date.replace(tzinfo = from_zone)

  local = start_date.astimezone(to_zone)
iandresolares
  • 43
  • 1
  • 7