klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Ugg timezone :<

Mon Apr 24, 2017 9:23 pm

Hello,
I would like a python3 program to set the timezone according to the web ip. I am able to get the time zone location and change the time zone in python3 when I leave it returns to UTC.

Code: Select all

import requests
from urllib.request import urlopen
import re
import os

os.system('date')
#Mon 24 Apr 11:18:03 PDT 2017
#Mon 24 Apr 12:18:04 MDT 2017

#url = 'http://checkip.dyndns.org/'
def OutSideIP(url = 'http://checkip.dyndns.org/'):
    request = urlopen(url).read().decode('utf-8')
    MyIP = re.findall("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", request)
    #MyIP = str(MyIP)
    MyIP = str(MyIP[0])
    return MyIP


def IPLocation(ip):
    global js
    url = 'http://freegeoip.net/json/'+ip
    r = requests.get(url)
    js = r.json()


try:
    ip = OutSideIP('http://XXX.168.X.XXX')#my local modem address
    pass
except OSError:
    ip = OutSideIP()
    pass

try:
    IPLocation(ip)
    pass
except:
    pass


TimeZoneNew = js['time_zone']
os.system('export TZ=%s' % TimeZoneNew)
#os.environ['TZ'] = js['time_zone']
#os.environ.update()

js['country_code']

js['country_name']

js['time_zone']

os.system('date')
exit()
there are a couple different try's in there.

they all work while python3 is working. I would like to run this at the beginning and have it look up the location and change the TZ according.
I have tried under "sudo python3" also.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
Paeryn
Posts: 2613
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Ugg timezone :<

Mon Apr 24, 2017 9:45 pm

When a new process is spawned (e.g. running your Python program) the child gets a copy of the parent's environment. Any changes to the environment can only affect the current process (and subsequently any child processes it spawns). A child process can't change its parent's environment.
She who travels light — forgot something.

ghp
Posts: 1393
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Ugg timezone :<

Tue Apr 25, 2017 4:27 am

Hello,
some of the magic is described in http://www.thegeekstuff.com/2010/09/cha ... -in-linux/
Hope this helps
Gerhard

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Ugg timezone :<

Tue Apr 25, 2017 7:50 am

This is a funny thread, of course, because we're faced with a choice:

Should we explain how/why you can't change the value of an environment variable (in the sense in which people usually mean) from a program? (Which leads, inevitably, to an explanation of how you can do it, via the '.' command)
Or do we explain how to set the timezone on a Linux (Pi) system - for which the answer is "Use raspi-config" (either command line or GUI version).
If this post appears in the wrong forums category, my apologies.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: Ugg timezone :<

Thu Apr 27, 2017 6:25 pm

Found a fix
the down side is that the RPi has to be rebooted. :)
forced a copy for the proper time zone.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
scruss
Posts: 2359
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Ugg timezone :<

Thu Apr 27, 2017 9:00 pm

If you want to try something spectacularly ill-advised that works without a reboot, take your js['time_zone'] value and stick it in the equivalent of a system call in Python. I don't know the exact syntax, nor do I care to learn, but something like:

Code: Select all

sudo ln -fs /usr/share/zoneinfo/js['time_zone'] /etc/localtime
sudo dpkg-reconfigure -f noninteractive tzdata
will reset the time zone without a reboot.

Why so spectacularly ill-advised? Well, you're taking a value you got from a random website on the internet and using it with root privilege. This is a terrible idea. Like CueCat/Flooz/Juicero level of bad idea.

These GeoIP tools don't always work well. For reasons I don't understand, my very very Canadian IP address sometimes shows up in the America/Menominee zone, an hour west of the correct America/Toronto zone.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: Ugg timezone :<

Fri Apr 28, 2017 4:07 pm

scruss wrote:If you want to try something spectacularly ill-advised that works without a reboot, take your js['time_zone'] value and stick it in the equivalent of a system call in Python. I don't know the exact syntax, nor do I care to learn, but something like:

Code: Select all

sudo ln -fs /usr/share/zoneinfo/js['time_zone'] /etc/localtime
sudo dpkg-reconfigure -f noninteractive tzdata
will reset the time zone without a reboot.

Why so spectacularly ill-advised? Well, you're taking a value you got from a random website on the internet and using it with root privilege. This is a terrible idea. Like CueCat/Flooz/Juicero level of bad idea.

These GeoIP tools don't always work well. For reasons I don't understand, my very very Canadian IP address sometimes shows up in the America/Menominee zone, an hour west of the correct America/Toronto zone.
Thanks the noninteractive is what I was looking for.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

Return to “Python”