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
Percy
Percy2y ago
Project ID: 93d1a87d-d4a4-4741-b068-fb19cbd10629
Percy
Percy2y ago
No search results found.
⚠️ experimental feature
Brody
Brody2y ago
in production 5 hours does not get added to the time, correct?
mattey
mattey2y ago
yep that is correct
Brody
Brody2y ago
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
mattey
mattey2y ago
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.
Brody
Brody2y ago
are the new deploys confirmed to be building with a dockerfile?
mattey
mattey2y ago
not a docker file, sorry , should i dockerize the project and deploy
Brody
Brody2y ago
did you read the linked thread?
mattey
mattey2y ago
yep, he was using docker, which i am not at the moment, and use the python locale module, which i tried too
Brody
Brody2y ago
...railway uses docker
mattey
mattey2y ago
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
Brody
Brody2y ago
you do not need docker on your computer
mattey
mattey2y ago
i'm listening
Brody
Brody2y ago
please try to deploy with the dockerfile in the linked thread
mattey
mattey2y ago
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 perfectly
Brody
Brody2y ago
give me a screenshot of the error and your requirements.txt and i will look into this further in the morning
mattey
mattey2y ago
sure, its a bit long though... following is my requirements.txt
mattey
mattey2y ago
Brody
Brody2y ago
Upload the file please
mattey
mattey2y ago
sorry..
mattey
mattey2y ago
mattey
mattey2y ago
the docker file?
Brody
Brody2y ago
requirements.txt No Send requirements.txt please
mattey
mattey2y ago
sorry for sending runtime,.txt :p
Brody
Brody2y ago
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"]
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"]
bumping the image version seems to do the trick please update your Dockerfile with that and redeploy
mattey
mattey2y ago
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.
Brody
Brody2y ago
show me deploy logs please
mattey
mattey2y ago
Brody
Brody2y ago
what command do you use to start the project when running locally?
mattey
mattey2y ago
py manage.py runserver
Brody
Brody2y ago
CMD ["python3", "manage.py", "runserver"]
CMD ["python3", "manage.py", "runserver"]
mattey
mattey2y ago
however i tried, https://docs.railway.app/deploy/dockerfiles after the deployment
Railway Docs
Dockerfiles | Railway Docs
Documentation for Railway
Brody
Brody2y ago
please update your dockerfile with that
mattey
mattey2y ago
sure
mattey
mattey2y ago
mattey
mattey2y ago
m sorry this turned into docker troubleshooting
Brody
Brody2y ago
did you update the dockerfile and redeploy?
mattey
mattey2y ago
yep
Brody
Brody2y ago
we gonna hit a record for longest thread soon
mattey
mattey2y ago
hehe, m thinking this is something to do with django and postgres the time_zone thing
Brody
Brody2y ago
what port are you listing on
mattey
mattey2y ago
8000
Brody
Brody2y ago
unlikely show me your service variables
mattey
mattey2y ago
u mean this one right
mattey
mattey2y ago
Brody
Brody2y ago
add PORT = 8000
mattey
mattey2y ago
yep port is 8000, in local development
Brody
Brody2y ago
🤦‍♂️ (sorry) add PORT = 8000 to the service variables
mattey
mattey2y ago
sure my bad
Brody
Brody2y ago
all good
milo
milo2y ago
try not to be rude pls
Brody
Brody2y ago
my bad, i mean well 🙂
milo
milo2y ago
all good
mattey
mattey2y ago
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?
milo
milo2y ago
0.0.0.0 serve on 0.0.0.0 instead of 127.0.0.1
mattey
mattey2y ago
Hi, thanks, its back online 😊 how ever, the time is still on UTC
Brody
Brody2y ago
and youre sure its being built with the dockerfile?
mattey
mattey2y ago
wait, m checking
milo
milo2y ago
im confused. why do u want to change timezone?
mattey
mattey2y ago
the build logs says it is using an image no i want to convert time zone from UTC to a local timezone
milo
milo2y ago
right well nixpacks doesnt support this so dockerfile is the way to go
mattey
mattey2y ago
since Railway app's servers default time zone is UTC
Brody
Brody2y ago
does the builder say dockerfile under the details tab?
mattey
mattey2y ago
ohh, sure @Brody is helping out v much to accomplish this
milo
milo2y ago
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
milo
milo2y ago
u can track that issue
mattey
mattey2y ago
Brody
Brody2y ago
do you have any idea why there isnt any timezone data in the final container?
milo
milo2y ago
uhhh send the dockerfile [pls
mattey
mattey2y ago
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"]
milo
milo2y ago
there is nothying about timezone in that
Brody
Brody2y ago
as far as i understand it, theyre just trying to add +5 hours to utc time
mattey
mattey2y ago
yepp that is correct. i have used python packages to accomplish this, yet it did not work
Brody
Brody2y ago
because i am guessing you want to display the time somewhere and you want the time to display in your timezone?
mattey
mattey2y ago
so m approaching for the simplest solution available 🙏 🙏 exactly
milo
milo2y ago
try usinjg env variables# they get carried to the container
Brody
Brody2y ago
at this point i think you might be better off doing a proper timezone conversion pytz
mattey
mattey2y ago
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
Brody
Brody2y ago
https://stackoverflow.com/a/13287083 i think theyre pytz example might work
milo
milo2y ago
once more, that wont work waitg is manage.py being run at build or run time
Brody
Brody2y ago
the args in cmd are used during the deploy stage and are not run during build time
milo
milo2y ago
hmm i guess that could work but try setting via env variaBLES oops
Brody
Brody2y ago
the code is running and the web server is responding, its just not adding +5 hours, at this point it must be code issue
mattey
mattey2y ago
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
Brody
Brody2y ago
what is your timezone?
mattey
mattey2y ago
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_zone
Brody
Brody2y ago
replace
local_time= utc_time.astimezone(datetime.timezone(datetime.timedelta(hours=5)))
local_time= utc_time.astimezone(datetime.timezone(datetime.timedelta(hours=5)))
with
local_time= utc_time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone("Indian/Maldives"))
local_time= utc_time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone("Indian/Maldives"))
mattey
mattey2y ago
hi, think i managed to solve it
Brody
Brody2y ago
do tell 🙂
mattey
mattey2y ago
Django Project
Django
The web framework for perfectionists with deadlines.
mattey
mattey2y ago
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.
Brody
Brody2y ago
So you're all set?
mattey
mattey2y ago
thanks a lot for the help
Brody
Brody2y ago
No problem, sorry I couldn't solve it. But I'm glad you got it in the end
mattey
mattey2y ago
couldn't have done it without your help 🙏 🙏 🙏
Brody
Brody2y ago
The answer is always in the docs 😂
mattey
mattey2y ago
yep
Brody
Brody2y ago
Happy coding!
mattey
mattey2y ago
Happy coding 🙂
Want results from more Discord servers?
Add your server