mmkw43
Posts: 653
Joined: Tue Dec 24, 2013 6:18 pm

Setting a timezone

Fri Nov 13, 2020 7:10 pm

Is there any way to set a timezone from within a python script? Naively I tried a subprocess call to raspi-config thinking it might display but the script crashed.

Is there any way to do this? BIG THANKS.

I'm wanting to do it in a preliminary tkinter with button window that pops up before the actual program starts. "Set time zone first then click button to continue".

User avatar
DougieLawson
Posts: 40486
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Setting a timezone

Fri Nov 13, 2020 7:18 pm

You shouldn't EVER need to change the system timezone after you've set it at first boot up. Leave it set to the zone where your machine is located or set it to UTC and leave it there. That's done with sudo dkpg-reconfigure tzdata. The tzdata stuff even works for daylight saving if you're in a territory that uses DST.

To deal with timezones in python use the python3 pytz package (which needs tzdata to be installed).

https://pypi.org/project/pytz/
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

mmkw43
Posts: 653
Joined: Tue Dec 24, 2013 6:18 pm

Re: Setting a timezone

Fri Nov 13, 2020 7:46 pm

Many thanks Doug. Yeah, I may have users in different time zones. I'll study that info. -- thanks again.

billintad
Posts: 52
Joined: Tue Jul 28, 2015 10:21 pm

Re: Setting a timezone

Fri Nov 13, 2020 9:49 pm

Python version 3.9 has improved timezone processing.

Unfortunately this version of Python is not the one loaded when you use 'python3'. I am not sure when RpiOS will offer python3.9 but it can be downloaded and compiled. I followed these instructions :

https://raspberrypi.stackexchange.com/q ... 3-7-to-3-8

but used 3.9 instead of 3.8.0. It takes a while but works.

User avatar
DougieLawson
Posts: 40486
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Setting a timezone

Fri Nov 13, 2020 9:52 pm

billintad wrote:
Fri Nov 13, 2020 9:49 pm
Python version 3.9 has improved timezone processing.

Unfortunately this version of Python is not the one loaded when you use 'python3'. I am not sure when RpiOS will offer python3.9 but it can be downloaded and compiled. I followed these instructions :

https://raspberrypi.stackexchange.com/q ... 3-7-to-3-8

but used 3.9 instead of 3.8.0. It takes a while but works.
It can't be any worse than it is now, because it's stupidly broken.

The standard RaspiOS (and DebIan) version of python won't change before Buster is replaced by Bullseye in the Autumn of 2021.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

mmkw43
Posts: 653
Joined: Tue Dec 24, 2013 6:18 pm

Re: Setting a timezone

Fri Nov 13, 2020 10:09 pm

Just one simple thing I need to understand before I proceed as this is new territory.

When I'm using Python with Raspian and I manipulate the time zone setting through my code, does it overwrite (permanently) how I set up time with raspi-config? May sound like a dumb question but stranger things have happened,

I just also saw timedatectl. Saw a "set time zone" command with that. Haven't tried anything yet though, will jump into it tomorrow.

Thanks for the help.

billintad
Posts: 52
Joined: Tue Jul 28, 2015 10:21 pm

Re: Setting a timezone

Sat Nov 14, 2020 9:20 am

DougieLawson wrote:
Fri Nov 13, 2020 9:52 pm

...

The standard RaspiOS (and DebIan) version of python won't change before Buster is replaced by Bullseye in the Autumn of 2021.
Installing new versions of Python is described in this recent thread :

viewtopic.php?f=32&t=291158

Dealing with the continual upgrades to Python is something to be sorted out. Waiting for autumn 2021 before an upgrade will mean many useful improvements to Python will be missing from RaspiOS, for example 3.10 will bring an improvement in speed of 10%, with the aim to improve processing speed by 50% over the next few releases. Any discussion of this probably needs a separate thread, but the issue does need to be addressed.

User avatar
DougieLawson
Posts: 40486
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Setting a timezone

Sat Nov 14, 2020 9:55 am

Don't blame me, go and moan at the less than agile folks who maintain DebIan.

The conservative approach to new versions and releases with DebIan is one reason to switch to Ubuntu (which has a more modern approach but more frequent package upgrades).
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

fruitoftheloom
Posts: 25164
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: Setting a timezone

Sat Nov 14, 2020 10:52 am

billintad wrote:
Sat Nov 14, 2020 9:20 am
DougieLawson wrote:
Fri Nov 13, 2020 9:52 pm

...

The standard RaspiOS (and DebIan) version of python won't change before Buster is replaced by Bullseye in the Autumn of 2021.
Installing new versions of Python is described in this recent thread :

viewtopic.php?f=32&t=291158

Dealing with the continual upgrades to Python is something to be sorted out. Waiting for autumn 2021 before an upgrade will mean many useful improvements to Python will be missing from RaspiOS, for example 3.10 will bring an improvement in speed of 10%, with the aim to improve processing speed by 50% over the next few releases. Any discussion of this probably needs a separate thread, but the issue does need to be addressed.

If you have a RPi 4B run Manjaro ARM64, being based on ARCH it is always latest and greatest packages:

https://manjaro.org/downloads/arm/raspb ... pi-4-xfce/
The information is out there....you just have to let it in.

User avatar
DougieLawson
Posts: 40486
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Setting a timezone

Sat Nov 14, 2020 11:53 am

fruitoftheloom wrote:
Sat Nov 14, 2020 10:52 am

If you have a RPi 4B run Manjaro ARM64, being based on ARCH it is always latest and greatest packages:

https://manjaro.org/downloads/arm/raspb ... pi-4-xfce/
I've never tried Manjaro; is that Arch with a easy to get configured GUI? Plain old Arch isn't good for folks who've not had years of experience with Linux. I tend to like Ubuntu and Mint because they're in the familiar territory of apt, apt-get, synaptic and aptitude for package management.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

bls
Posts: 1041
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: Setting a timezone

Sat Nov 14, 2020 3:14 pm

OP: Just a thought...If you explained what you are trying to accomplish overall, instead of the point "how do I change the timezone in Python and how does this affect the system" questions, you might get additional helpful responses.
Pi tools:
Quickly and easily build customized-just-for-you SD Cards: https://github.com/gitbls/sdm
Easily run your network's DHCP/DNS on a Pi: https://github.com/gitbls/ndm
Easy strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

mmkw43
Posts: 653
Joined: Tue Dec 24, 2013 6:18 pm

Re: Setting a timezone

Sat Nov 14, 2020 10:11 pm

I'm simply trying to allow a user (who can live anywhere in the US) to change the timezone. Guess I didn't communicate that.
Using PI3 and Jessie and python3.4 -- and did this,

pip install pytz (tzdata was already installed).

It installed but I can't even get this done in python --

import pytz
for timezone in pytz.all_timezones:
print(timezone)

says pytz has no attribute all_timezones -- I verified that it DOES.

mmkw43
Posts: 653
Joined: Tue Dec 24, 2013 6:18 pm

Re: Setting a timezone

Sat Nov 14, 2020 10:44 pm

Spent all day on it to find out --

timedatectl set-timezone "America/City"

pretty god damn easy.
I'm not pro like you guys -- just a hobbyist. looking for simple answers.

User avatar
DougieLawson
Posts: 40486
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Setting a timezone

Sun Nov 15, 2020 8:56 am

mmkw43 wrote:
Sat Nov 14, 2020 10:44 pm
Spent all day on it to find out --

timedatectl set-timezone "America/City"

pretty god damn easy.
I'm not pro like you guys -- just a hobbyist. looking for simple answers.
That's what sudo raspi-config does for you. I think we'd assumed you'd set the system timezone right before starting your python struggle. That setting you've changed is for every user and every process.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

bls
Posts: 1041
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: Setting a timezone

Sun Nov 15, 2020 3:20 pm

DougieLawson wrote:
Sun Nov 15, 2020 8:56 am
I think we'd assumed you'd set the system timezone right before starting your python struggle.
This is precisely why I asked the question. The correct way is to have the timezone set as part of bringing up the system at each location.

There are many ways to do this, of course. One approach is to build the SD Cards for each site with the correct timezone in them. If the OP used my sdm tool (see sig), he could still have a single master image, yet generate SD cards for each location with the correct timezone.
Pi tools:
Quickly and easily build customized-just-for-you SD Cards: https://github.com/gitbls/sdm
Easily run your network's DHCP/DNS on a Pi: https://github.com/gitbls/ndm
Easy strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

Return to “Python”