Forums

Setting User Time Zone

Probably everyone in the world but me already knew this, but you can put your user time zone into .bashrc like this:

#export time zone
export TZ="/usr/share/zoneinfo/US/Eastern"

If you look in /usr/share/zoneinfo you can see all the zones that there are.

Then when you do a listing, you don't get UTC times unless you want them. Here the dates and times are all US Eastern Time.

~/Projects/$ date
Wed May 22 07:18:44 EDT 2013

~/Projects/$ ls -l
total 4
-rw-r--r-- 1 greg  registered_users    0 May 22 06:42 hello.py
drwxrwxr-x 2 65432 registered_users 4096 May  9 03:33 tangerine

I probably knew this back in the dim reaches of memory, but these days most OS installs just ask you for the time zone, so I'd forgotten you can do it manually.

-greg

Neat! Thanks for sharing that.

Great tip. One minor point - I've always seen the recommendation that the TZ variable be set to just the name of the zone, you shouldn't need the full path. This probably doesn't matter much unless you share your .bashrc with a system which locates the timezone files somewhere different, but it's probably a good habit.

So you can just do:

export TZ="US/Eastern"

Wikipedia also has a mirror of the zone.tab file which lists the available zones. The name you want to place in TZ is in the third column of the table. There is some duplication so you can use convenient names - for example, US/Eastern is actually an alias for the canonical America/New_York (where America refers to the whole Americas, so there's also an America/Lima for example). No reason not to use the aliases if you find them more convenient, however - anything in that list should be standard, as far as I'm aware.

This is helpful, thank you for sharing. Does anyone know if there's a way to have PythonAnywhere remember this setting, so one wouldn't have to enter it in every time they start a new console?

Edit: Answered my own question... one can just add this line to the .bashrc file and it, will remember it which is what you were saying. (I had just been entering it in the Bash console.)

Yup, that's the right way to do it :-)

Didn't know it yet. Thanks :)

Folks, I tried to configure the server, using the 'export tz command'. Now the server date is working. When I run the date command and is fine. But when I fetch the date using 't = datetime.datetime.now()' its returns to me a different time from the server. Any suggestions? I have no idea what to do :/

Python's datetime is not timezone aware. now() will always return the system time which, on PythonAnywhere, is UTC. If you want timezone info on a datetime, you need to use timezone aware datetimes by providing tzinfo. pytz can help with that.

I need help in localizing my time to Indian Time Zone.

My application is hosted on Pythonanywhere server but my application is being used in India by Indian clients. I need to set the date and time they see in Indian Time Zone.

Can you please help? What should I do?

Regards Imtiyaz Bengali MyDentalPlan

Check the docs for your web framework. There's probably a setting to configure the timezone.

How can i set it for a actual project not only in bash

What do you mean actual project? Do you mean for a webapp? Or from when you run a file from the file editor?

Thank you, Greg! You saved my day!

Is it true that it doesn't work with the time offset? in the summer? when we all put the clock an hour off.

hm. probably depends on which timezone you choose?

How can I get my local timestamp in access log file? Though it successfully works in bash console but I want local timezone in accesslog.

The access log uses UTC and it cannot be changed individually.

Is it possible to set the time automatically with user login? I'm running a trip organizer web application and users will move around changing timezones . How the system can detect it? Thanks in advance

If your users are changing timezones, you would probably need them to select the timezone that they want to use.

Thanks so much for your response

Glad we could help!

Im lost folks, i tried bash files and eveything, but no work for me, im sure im new and am just doing something poorly. i updated the bash file like this =

export time zone

export TZ="US/Phoenix" and the wsgi like = import os import time os.environ["TZ"] = "US/Phoenix" time.tzset() any lead? thanks in adavance!

That would correctly set the timezone to what you specify. What is not working?

The time zone has not updated and now I'm getting a error message = https://twitter.com/TheMyRickCorp/status/1581383982175830016/photo/1

and i checked the error log and it says, "2022-10-15 20:03:42,142: Error running WSGI application"

What timezone is not updated? The timezone that you are talking about may not be taken from the timezone set with time.tzset() or there may be some other code in your web app that is changing it to something else at some point.

Read the rest of the traceback in the error log and use that to debug what is causing the error.