aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Adapting to the timezone of a country

Thu Jul 20, 2017 10:53 am

Hello,

I have a temperature sensor attached to my raspberry pi.
My Pi will be moving from 1 country to another.

How can I generate time and date according to the different countries as my pi moves from 1 part to another part of the world.
Is it possible.

For example,
I pi is in france, it should show the temperature along with the time and date in france according to the french timezone.
If it moves to china , then it should send the timezone of that country.

P.s: i am using a raspberry pi 2

Thanks,
Aarushi

User avatar
topguy
Posts: 7310
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Adapting to the timezone of a country

Thu Jul 20, 2017 11:12 am

If you have Internet connection on both places you could maybe use some geoIp service http://ipinfo.io/developers
You would have to set the TZ environment variable to the correct timezone for that country. If there is a limited number of places your Pi can be then you can hardcode the values in a script.

EDIT:
I found that Ubuntu has a service too that they use to guess Timezone during ubuntu installations.

Code: Select all

wget -O - -q http://geoip.ubuntu.com/lookup

It returns an XML and includes Linux TimeZone value.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><Response><Ip>92.66.66.666</Ip><Status>OK</Status><CountryCode>NO</CountryCode><CountryCode3>NOR</CountryCode3><CountryName>Norway</CountryName><RegionCode>16</RegionCode><RegionName>Sor-Trondelag</RegionName><City>Trondheim</City><ZipPostalCode>7022</ZipPostalCode><Latitude>63.4305</Latitude><Longitude>10.3951</Longitude><AreaCode>0</AreaCode><TimeZone>Europe/Oslo</TimeZone></Response>

aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Re: Adapting to the timezone of a country

Fri Jul 21, 2017 8:40 am

Is it possible that the pi automatically changes timezones according to the country ?

@topguy i am not able to understand your approach. Could you plz elaborate

User avatar
topguy
Posts: 7310
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Adapting to the timezone of a country

Fri Jul 21, 2017 9:13 am

The Pi itself can not be set up ( as far as I know ) to do this automatically, but you can write a script (or program) that does it for you at every boot.

I have just given information about how that script could get an estimated Timezone based on the IP address it gets when it connects to the internet.

aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Re: Adapting to the timezone of a country

Mon Jul 24, 2017 4:50 am

topguy wrote:The Pi itself can not be set up ( as far as I know ) to do this automatically, but you can write a script (or program) that does it for you at every boot.

I have just given information about how that script could get an estimated Timezone based on the IP address it gets when it connects to the internet.
Thanks @topguy for the help :D

For others who may encounter the same problem, heres the solution i followed :
write a bash script , for example my-bash-script.sh, to set the TimeZone on your pi
======================
#!/bin/bash

# Fetch timezone from Ubuntu's geoip server
#Ubuntu uses the ubuntu-geoip package, which queries http://geoip.ubuntu.com/.
TZ=`wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/p'`

# Set the current time
cp /usr/share/zoneinfo/$TZ /etc/localtime
=======================
I followed this help : https://askubuntu.com/questions/154499/ ... e-timezone

Also , i wanted it to set the TimeZone hourly as my Raspberry Pi would be on the move , so i used crontab
open crontab using :

$crontab -e

choose any editing option.
I used option 2: that is editing with nano.

Add this line to the end of the file:
0 * * * * <path-of-my-script>/my-bash-script.sh

Refer to this link for more help on crontab : https://www.computerhope.com/unix/ucrontab.htm

Thanks,
Aarushi :)

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

Re: Adapting to the timezone of a country

Mon Jul 24, 2017 12:40 pm

aarushi_mittal wrote:
topguy wrote:The Pi itself can not be set up ( as far as I know ) to do this automatically, but you can write a script (or program) that does it for you at every boot.

I have just given information about how that script could get an estimated Timezone based on the IP address it gets when it connects to the internet.
Thanks @topguy for the help :D

For others who may encounter the same problem, heres the solution i followed :
write a bash script , for example my-bash-script.sh, to set the TimeZone on your pi
======================
#!/bin/bash

# Fetch timezone from Ubuntu's geoip server
#Ubuntu uses the ubuntu-geoip package, which queries http://geoip.ubuntu.com/.
TZ=`wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/p'`

# Set the current time
cp /usr/share/zoneinfo/$TZ /etc/localtime
=======================
I followed this help : https://askubuntu.com/questions/154499/ ... e-timezone

Also , i wanted it to set the TimeZone hourly as my Raspberry Pi would be on the move , so i used crontab
open crontab using :

$crontab -e

choose any editing option.
I used option 2: that is editing with nano.

Add this line to the end of the file:
0 * * * * <path-of-my-script>/my-bash-script.sh

Refer to this link for more help on crontab : https://www.computerhope.com/unix/ucrontab.htm

Thanks,
Aarushi :)
You're not quite updating everything with that method, you should also update the contents of /etc/timezone to be the name of the time zone. Plus I'd get the tzdata package to do the update of /etc/localtime for you once you set /etc/timezone to the correct value (reconfiguring the tzdata package has the benefit that if /etc/timezone contains an invalid time zone string it will be set back to the last known good value).

Code: Select all

#!/bin/sh

zone=$(wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/ p')

if [ "$zone" != "" ] ;then
    echo $zone >/etc/timezone
    dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Re: Adapting to the timezone of a country

Mon Jul 24, 2017 5:33 pm

Paeryn wrote:
aarushi_mittal wrote:
topguy wrote:The Pi itself can not be set up ( as far as I know ) to do this automatically, but you can write a script (or program) that does it for you at every boot.

I have just given information about how that script could get an estimated Timezone based on the IP address it gets when it connects to the internet.
Thanks @topguy for the help :D

For others who may encounter the same problem, heres the solution i followed :
write a bash script , for example my-bash-script.sh, to set the TimeZone on your pi
======================
#!/bin/bash

# Fetch timezone from Ubuntu's geoip server
#Ubuntu uses the ubuntu-geoip package, which queries http://geoip.ubuntu.com/.
TZ=`wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/p'`

# Set the current time
cp /usr/share/zoneinfo/$TZ /etc/localtime
=======================
I followed this help : https://askubuntu.com/questions/154499/ ... e-timezone

Also , i wanted it to set the TimeZone hourly as my Raspberry Pi would be on the move , so i used crontab
open crontab using :

$crontab -e

choose any editing option.
I used option 2: that is editing with nano.

Add this line to the end of the file:
0 * * * * <path-of-my-script>/my-bash-script.sh

Refer to this link for more help on crontab : https://www.computerhope.com/unix/ucrontab.htm

Thanks,
Aarushi :)
You're not quite updating everything with that method, you should also update the contents of /etc/timezone to be the name of the time zone. Plus I'd get the tzdata package to do the update of /etc/localtime for you once you set /etc/timezone to the correct value (reconfiguring the tzdata package has the benefit that if /etc/timezone contains an invalid time zone string it will be set back to the last known good value).

Code: Select all

#!/bin/sh

zone=$(wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/ p')

if [ "$zone" != "" ] ;then
    echo $zone >/etc/timezone
    dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
=================
Thanks for the info.
Is this related to me not getting th elocal time in my java code ?
Firstly, after booting up the pi, it takes time to set theTZ to the one i am present in.

Secondly, I am executing a java prog that prints some sensor readings alons with the system local time.
Although my LXTerminal shows my countries TZ, the output of code shows the time in CEST.

this is the screenshot : https://drive.google.com/open?id=0B1L2m ... 2Y4bEZZOVU

Help !!
Aarushi

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

Re: Adapting to the timezone of a country

Mon Jul 24, 2017 6:13 pm

aarushi_mittal wrote:Thanks for the info.
Is this related to me not getting th elocal time in my java code ?
Firstly, after booting up the pi, it takes time to set theTZ to the one i am present in.

Secondly, I am executing a java prog that prints some sensor readings alons with the system local time.
Although my LXTerminal shows my countries TZ, the output of code shows the time in CEST.

this is the screenshot : https://drive.google.com/open?id=0B1L2m ... 2Y4bEZZOVU

Help !!
Aarushi
It will take a bit of time to pick up the new time zone after booting, after all it takes time for the internet to be available which is needed to query where you are and if you use the dpkg-reconfigure method it can take a few seconds for that to run.

I've no idea on why your program doesn't pick up on the changed timezone, I've no experience with java or your program. One possibility could be that in the environment that your program is run from you are setting the environment variable TZ to CEST (or any location which uses CEST) which the standard linux libraries will use in preference to whatever is set up in /etc/timezone and /etc/localtime. Another is that the timezone is only picked up at the start of your program and is enforced for the duration of the program.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

aarushi_mittal
Posts: 40
Joined: Wed Jul 05, 2017 10:16 am
Location: Delhi, India

Re: Adapting to the timezone of a country

Mon Jul 24, 2017 6:26 pm

You're not quite updating everything with that method, you should also update the contents of /etc/timezone to be the name of the time zone. Plus I'd get the tzdata package to do the update of /etc/localtime for you once you set /etc/timezone to the correct value (reconfiguring the tzdata package has the benefit that if /etc/timezone contains an invalid time zone string it will be set back to the last known good value).

Code: Select all

#!/bin/sh

zone=$(wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/ p')

if [ "$zone" != "" ] ;then
    echo $zone >/etc/timezone
    dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
=================
Thanks for the info.
Is this related to me not getting th elocal time in my java code ?
Firstly, after booting up the pi, it takes time to set theTZ to the one i am present in.

Secondly, I am executing a java prog that prints some sensor readings alons with the system local time.
Although my LXTerminal shows my countries TZ, the output of code shows the time in CEST.

this is the screenshot : https://drive.google.com/open?id=0B1L2m ... 2Y4bEZZOVU

Help !!
Aarushi[/quote]

================================================
Thank you sooo much :D :D :D
I got hat I wanted
It now shows IST.
I was not making changing in /etc/timezone , probably thats why the changes were not reflecting in the system

User avatar
ebolisa
Posts: 167
Joined: Sat Dec 30, 2017 3:35 pm
Location: Madrid, Spain

Re: Adapting to the timezone of a country

Sat Dec 15, 2018 12:50 pm

Hi,

I'd like to use this code at power up but, for some reasons, it's not updating.

This' what I've done:
created a bash file and made it executable (sudo chmod +x myfile.sh)
ran it with sudo ./myfile.sh
the file echos correctly the local timezone but doesn't update the /etc/localtime file.

What am I doing wrong?

TIA

Code: Select all

#!/bin/sh

# Fetch timezone from Ubuntu's geoip server
#Ubuntu uses the ubuntu-geoip package, which queries http://geoip.ubuntu.com/
zone=$(wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/ p')

#echo $zone <- prints out correctly the local timezone

if [ "$zone" != "" ]; then
    echo $zone >/etc/timezone
    cp /usr/share/zoneinfo/$zone /etc/localtime
    dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
I know that I know nothing

DirkS
Posts: 10787
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Adapting to the timezone of a country

Sat Dec 15, 2018 1:37 pm

The copy (cp) command looks a bit odd to me.
Tried to follow all the symbolic links and it looks like you try to copy a directory to a file

User avatar
ebolisa
Posts: 167
Joined: Sat Dec 30, 2017 3:35 pm
Location: Madrid, Spain

Re: Adapting to the timezone of a country

Sat Dec 15, 2018 1:54 pm

but I don't get any errors

EDIT:
After reviewing the above threads, I ajusted my code but still, doesn't do the job. It appears that the data from raspi.config prevails.

Code: Select all

#!/bin/sh

zone=$(wget -O - -q http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/ p')

if [ "$zone" != "" ] ;then
    echo $zone >/etc/timezone
    dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
I know that I know nothing

Return to “Advanced users”