I am encountering an issue where my time calculations are producing different results in two environments:
Local Environment: MacBook Air, time zone set to Ecuador (UTC-5).
PythonAnywhere: UK-based server (UTC+0).
I use a function datetime_continental() to ensure that the time is calculated based on Ecuador’s time zone (UTC-5), regardless of the server location. Here is the function:
import pytz
from datetime import datetime
def datetime_continental():
ecuador_tz = pytz.timezone('America/Guayaquil')
return datetime.now(ecuador_tz)
The goal is to compare a constant KPI_AWAITING_TIME (set to 1 minute) with the result of subtracting turno.hora from datetime_continental.now(). The logic is implemented in a Jinja2 template as follows:
{% if datetime_continental.now() - turno.hora > KPI_AWAITING_TIME %}
{{ "GO TO RECEPTION" }}
{% else %}
{{ datetime_continental.now() - turno.hora }}
{% endif %}
This comparison works perfectly on my local machine. Here’s a screenshot of the result on my local system, which correctly shows 0:00:29.170766 without adding 5 hours to the time:
However, when running the same code on PythonAnywhere, the result unexpectedly adds 5 hours to the time. For example, the output on PythonAnywhere shows 5:00:33.440425, which is a 5-hour difference from the expected value:
Questions:
-
Does the function datetime_continental() handle the time zone conversion correctly, ensuring the Ecuadorian time zone (UTC-5) is applied consistently across environments?
-
Why does PythonAnywhere show an additional 5 hours in the result, even though the same logic works correctly locally?
-
Could this issue be related to PythonAnywhere’s server configuration (possibly using UTC+0 by default), and how can I resolve this to make sure Ecuadorian time is used on both platforms?
Thank you for your assistance in resolving this!