Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set Timezone
#1
Hi Dan:

I wrote this script (timezone.py) to set timezone within SIP (ie., without having to go through sudo raspi-config).
The script detects the ip-address of isp (external_ip), to determine its timezone id (e.g. Asia/Shanghai), and then set the timezone via the command line.
Running this script ("python3 /pi/home/SIP/timezone.py") will change the device time on SIP/template/base.html

To test, I switch the timezone between my isp ip-address and a dummy external_ip (external_ip = '64.233.191.255' is a google ip-address in Los Angelse)

#### begin of timezone.py #####
#!/bin/bash
# path: /home/pi/SIP/timezone.py
# references:
# https://www.raspberrypi.org/forums/viewt...p?t=197938
# https://stackoverflow.com/questions/6357...-in-python
"""
change timezone via python script

"""

import json
import urllib.request
import subprocess

GEO_IP_API_URL = 'http://ip-api.com/json/'
external_ip = urllib.request.urlopen('https://ident.me').read().decode('utf8')
#external_ip = '64.233.191.255'
# Creating request object to GeoLocation API
req = urllib.request.Request(GEO_IP_API_URL+external_ip)
# Getting in response JSON
response = urllib.request.urlopen(req).read()
# Loading JSON from text to object
json_response = json.loads(response.decode('utf-8'))

# Print timezone
timezone = json_response['timezone']
print('timezone: {}'.format(timezone))

if __name__ == "__main__":
    out = subprocess.run('sudo ln -fs /usr/share/zoneinfo/' + timezone + ' /etc/localtime', shell=True)
    print(out)
    out = subprocess.run('sudo dpkg-reconfigure -f noninteractive tzdata', shell=True)
    print(out)

#### end of timezone.py #####

In the options page, I include a "Set timezone" button below the 24-hour clock checkbox. This addition shows up in 3 places in options.html page.

1a. In SIP/templates/options.html, I introduce a button id 'cSetTimezone' below the condition 'if name == "tf"'(so this button will appear to the right of 24-hour clock checkbox):

            if datatype == "boolean":
                output += "<input name='o" + name + "' type='checkbox' " + ("checked" if value>0 else "") + ">\n"
                if name == "tf":
                    output += "&nbsp;<button id='cSetTimezone' title=" + json.dumps(_(u'Use only if device time is wrong; Use isp ip-address to correct'), ensure_ascii=False) + ">" + _(u'Set timezone')+"</button>"

1b. In the jQuery section, after jQuery("button#cRestart"), I include a corresponding jQuery("button#cSetTimezone"). The name='stz' is used further down in webpages.py.

        jQuery("button#cRestart").click(function(){
            jQuery("input[name='rstrt']").val(1);
            jQuery("form[name='of']").submit();
        });

        jQuery("button#cSetTimezone").click(function(){
            jQuery("input[name='stz']").val(1);
            jQuery("form[name='of']").submit();
        });

1c. Under the "hidden" section to hide the background checkbox, after the Restart checkbox (name='rstrt'), I hide the "stz" checkbox:
        <input type="hidden" name="rstrt" value="0">
        <input type="hidden" name="stz" value="0">

The button 'stz' requires a restart to update device time.

2. In SIP/webpages.py, I include a u"stz" (set timezone) section ala u"rstrt".

        if u"rstrt" in qdict and qdict[u"rstrt"] == u"1":
            restart(2)
            raise web.seeother(u"/restart")

        if u"stz" in qdict and qdict[u"stz"] == u"1":
            report_timezone_set()
            subprocess.run('sudo python3 /home/pi/SIP/timezone.py', shell=True)
            restart(2)
            raise web.seeother(u"/restart")

Paul
Reply
#2
Interesting.
Thanks for posting your code. I will give it a try.

Dan
<p><br></p>
Reply
#3
I think my above arrangement is not a good idea. Instead of a button which executes immediately when pressed, the set_timezone function should be implemented as a checkbox like the 24-hour clock format, to be executed after the submit button is clicked.

But unlike the 24-hour clock, the set_timezone checkbox should be left unchecked when the page loads every time, to avoid accidental execution in case user forgets to uncheck. This should be easier to implement.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)