Shell script to get Timezone

Hi friends i need to writing a code to use the longitue and latitude to get a time zone. Example below

            Logitude   Latitude
OMAN     21.9          56.6
BRGHT    27.57        -89.4
BLUGL    26.25        -91.95

latitude lines run horizontally
Longitude are vertical lines

using below example of wget we get the +0400 which is a TIMEZONE . How can we write a program to create a table and put this information in another column.

wget -O test <URL>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<timezone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.earthtools.org/timezone.xsd">
  <version>1.0</version>
  <location>
    <latitude>21.9</latitude>
    <longitude>56.6</longitude>
  </location>
  <offset>4</offset>
  <suffix>D</suffix>
  <localtime>14 Feb 2013 19:49:48</localtime>
  <isotime>2013-02-14 19:49:48 +0400</isotime>
  <utctime>2013-02-14 15:49:48</utctime>
  <dst>Unknown</dst>
</timezone>

Appreciate your time and effort.

Correct me if I am wrong, so you have an XML with latitude, longitude & timezone and you want to parse this XML file and get these values and put them in table format?

If yes, from the provided XML how do you want your output data look like? It will be better if you can post a sample desired output required from the XML input.

Hi Bipinajith

Thank you for checking on this program .

When you do wget you get the xml format in the output . In that output i would like to take out only 1 value out of the xml file and place it in a table

<?xml version="1.0" encoding="ISO-8859-1" ?>
<timezone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.earthtools.org/timezone.xsd">
<version>1.0</version>
<location>
<latitude>21.9</latitude>
<longitude>56.6</longitude>
</location>
<offset>4</offset>
<suffix>D</suffix>
<localtime>14 Feb 2013 19:49:48</localtime>
<isotime>2013-02-14 19:49:48 +0400[/COLOR]</isotime>
<utctime>2013-02-14 15:49:48</utctime>
<dst>Unknown</dst>
</timezone>

Output should be

OMAN 21.9 56.6 +0400
BRGHT 27.57 -89.4 <value>
BLUGL 26.25 -91.95 <value>

appreciate your time .

I'm not sure from where you are reading the location: OMAN , I don't see that in the XML!

You can write an awk program to extract the desired values, here is a code & output:

awk -F'[<>]' '/latitude/ {
                LA=$3;
        } /longitude/ {
                LO=$3;
        } /isotime/ {
                TS=$3;
                sub(/.* /,x,TS);
                sub(/\[.*/,x,TS);
        } END {
                print LA, LO, TS;
} ' xmlfile
21.9 56.6 +0400

Where does BRGHT come from? It's not in the file.

If all you want to do is extract the timezone, awk '/isotime/ { sub(/<.*/,"",$3) ; print $3 }' but I suspect we could find a better solution to your whole problem here if you'd say it.

First column will not show in xml because i have taken only 2 values longitude and latitude to get the result using wget on a URL which creates an xml file on output which i pasted in the initial post.

---------- Post updated at 01:18 PM ---------- Previous update was at 12:15 PM ----------

This gives me a syntax error

a@b.com$ awk -F'[<>]' '/latitude/ {
LA=$3;
} /longitude/ {
LO=$3;
} /isotime/ {
TS=$3;
sub(/.* /,x,TS);
sub(/\[.*/,x,TS);
} END {
print LA, LO, TS;
} ' output3
awk: syntax error near line 7
awk: illegal statement near line 7
awk: syntax error near line 8
awk: illegal statement near line 8

Considering that they bend the lines around nations, this is the approximate or solar time zone, I suppose? Each one is 15 degrees ( 360 / 24 ).

If you can go from lat/long to nation somehow, then you might get the time zone: Time zone - Wikipedia, the free encyclopedia

I get syntax error

[a@b.com]$ cat output3 | awk '/isotime/ { sub(/<.*/,"",$3) ; print $3 }'
awk: syntax error near line 1
awk: illegal statement near line 1

If on Solaris, use nawk

Can you please explain how it works.

---------- Post updated at 01:49 PM ---------- Previous update was at 01:47 PM ----------

nawk works vgersh99 thank you .

nawk '/isotime/ { sub(/<.*/,"",$3) ; print $3 }'

Here is explanation:

nawk -F'[<>]' '/latitude/ {             # Set < > as field separators. Search pattern: latitude
                LA=$3;                  # Set LA = 3rd parameter (tag latitude value)
        } /longitude/ {                 # Search pattern: longitude
                LO=$3;                  # Set LO = 3rd parameter (tag longitude value)
        } /isotime/ [                   # Search pattern: isotime
                TS=$3;                  # Set TS = 3rd parameter (tag isotime value)
                sub(/.* /,x,TS);        # Remove everything before last blank space
                sub(/\[.*/,x,TS);       # Remove everything after opening square bracket
        } END {                         # END
                print LA, LO, TS;       # Print value of variables LA, LO, TS
} ' xmlfile

thank you for the information DGPickett . Do you have a shell script that can give the output i posted

First, you need a database of all the national spaces where tz is not normal, an hour off GMT for each 15 degree zone, starting with +/- 7.5 degrees longitude = GMT or UTC. Looking at the map, it seems more the exception than the rule, so one needs a database of all the state, province or nation political boundaries to extract those that are time zone boundaries. Since many follow rivers or coastlines, it will not be a small database, and it may be in vectors where the boundaries lie, so you need a program to decide if your lat-long is inside their multifaceted polygon. I am not into maps that deep, but I can imagine someone has some sort of app and db that does lat-long to political. Maybe the database can have a lot of lat-long "rectangular" boxes so it becomes a relative simple query. You could take a vectors of boundary database and generate even more rectangles. How fine is fine enough bubbles up very quickly as a requirement question!

excellent bipinajith . thank you for the detailed explanation . It really helps.