fixing time zone from utc to localtime
my id is 93d1a87d-d4a4-4741-b068-fb19cbd10629
since railway app's servers set their time zone to UTC, I quarried db to serve the datetime and add +5 to the UTC time zone, however the changes do not get integrated in production server, in development it works as expected
following is my code snippet:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
utc_time= self.object.trip_date
local_time= utc_time.astimezone(datetime.timezone(datetime.timedelta(hours=5)))
context['time'] = local_time.strftime("%H:%M")
return context
any idea what is missing, thanks.101 Replies
Project ID:
93d1a87d-d4a4-4741-b068-fb19cbd10629
No search results found.
⚠️ experimental feature
in production 5 hours does not get added to the time, correct?
yep
that is correct
theres been two other help threads related to issues with timezone conversions, this one pertains to python:
https://discordapp.com/channels/713503345364697088/1075437876143198299
read through that thread and hopefully the proposed solution can help you too, even though theyre using the locale package it still uses timezone data
thanks, will give a read.
hi, (after many attempts and iterations of the code) it is still not updating the timezone, which is stuck in UTC , m thinking it is related to postgres, is there a way i can update production servers postgres's time zone, or is it not recommended.
are the new deploys confirmed to be building with a dockerfile?
not a docker file,
sorry , should i dockerize the project and deploy
did you read the linked thread?
yep, he was using docker, which i am not at the moment, and use the python locale module, which i tried too
...railway uses docker
we can deploy projects without docker right
in the docs it states how to deploy with docker, at the moment the project is very simple, with only 1 service
you do not need docker on your computer
i'm listening
please try to deploy with the dockerfile in the linked thread
ok, will update on what happens, thanks
hi, iv tried to deploying with the dockerfile
FROM python:3.8-alpine3.17
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py"]
some how this error occurs:
installation of the package "backports.zoneinfo" has failed.
in my local (windows) machine, it works perfectlygive me a screenshot of the error and your requirements.txt and i will look into this further in the morning
sure, its a bit long though...
following is my requirements.txt
Upload the file please
sorry..
the docker file?
requirements.txt
No
Send requirements.txt please
sorry for sending runtime,.txt :p
bumping the image version seems to do the trick
please update your Dockerfile with that and redeploy
k, is trying
hi, deployment went successfully, after that i did a
railway run
however when i visit the link it says, Application Error, This application failed to respond.show me deploy logs please
what command do you use to start the project when running locally?
py manage.py runserver
please update your dockerfile with that
sure
m sorry this turned into docker troubleshooting
did you update the dockerfile and redeploy?
yep
we gonna hit a record for longest thread soon
hehe, m thinking this is something to do with django and postgres
the time_zone thing
what port are you listing on
8000
unlikely
show me your service variables
u mean this one right
add PORT = 8000
yep port is 8000, in local development
🤦♂️ (sorry)
add PORT = 8000 to the service variables
sure
my bad
all good
try not to be rude pls
my bad, i mean well 🙂
all good
the url is still the same, m thinking when i did railway run:
Running at 127.0.0.1:4411, this might be the port?
0.0.0.0
serve on 0.0.0.0 instead of 127.0.0.1
Hi, thanks, its back online 😊
how ever, the time is still on UTC
and youre sure its being built with the dockerfile?
wait, m checking
im confused. why do u want to change timezone?
the build logs says it is using an image
no i want to convert time zone from UTC to a local timezone
right
well nixpacks doesnt support this so dockerfile is the way to go
since Railway app's servers default time zone is UTC
does the builder say dockerfile under the details tab?
ohh, sure @Brody is helping out v much to accomplish this
GitHub
Support changing timezone · Issue #794 · railwayapp/nixpacks
Feature request Support changing the timezone on the running machine via an environment variable Motivation This would allow users to change the timezone of their project Contribution No response
u can track that issue
do you have any idea why there isnt any timezone data in the final container?
uhhh
send the dockerfile [pls
no
FROM python:3.11.2-alpine3.17
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
there is nothying about timezone in that
as far as i understand it, theyre just trying to add +5 hours to utc time
yepp
that is correct.
i have used python packages to accomplish this, yet it did not work
because i am guessing you want to display the time somewhere and you want the time to display in your timezone?
so m approaching for the simplest solution available
🙏 🙏
exactly
try usinjg env variables#
they get carried to the container
at this point i think you might be better off doing a proper timezone conversion pytz
i did use pytz, in the begging how ever when it did not work, i approached to use the simplest solution
will give it a try again
https://stackoverflow.com/a/13287083
i think theyre pytz example might work
once more, that wont work
waitg
is manage.py being run at build or run time
the args in cmd are used during the deploy stage and are not run during build time
hmm i guess that could work
but try setting via env variaBLES
oops
the code is running and the web server is responding, its just not adding +5 hours, at this point it must be code issue
wait here is my code, that works on development:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
utc_time= self.object.trip_date
local_time= utc_time.astimezone(datetime.timezone(datetime.timedelta(hours=5)))
context['time'] = local_time.strftime("%H:%M")
return context
what is your timezone?
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['time'] = timezone.localtime(self.object.trip_date, pytz.timezone("Indian/Maldives")).strftime("%H:%M %p")
return context
same code using pytz
with my time_zonereplace with
hi, think i managed to solve it
do tell 🙂
The PostgreSQL backend stores datetimes as timestamp with time zone. In practice, this means it converts datetimes from the connection’s time zone to UTC on storage, and from UTC to the connection’s time zone on retrieval.
As a consequence, if you’re using PostgreSQL, you can switch between USE_TZ = False and USE_TZ = True freely. The database connection’s time zone will be set to TIME_ZONE or UTC respectively, so that Django obtains correct datetimes in all cases. You don’t need to perform any data conversions.
So you're all set?
thanks a lot for the help
No problem, sorry I couldn't solve it. But I'm glad you got it in the end
couldn't have done it without your help 🙏 🙏 🙏
The answer is always in the docs 😂
yep
Happy coding!
Happy coding 🙂