0

I'm creating a website with Flask and want to show a live clock with the time in different cities. I have longitude and latitude stored in my database for every city. Using geonames plugin I'm able to get timezoneid by coordinates.

Example:

import geonames
geonames_client = geonames.GeonamesClient('demo')
geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928})
print geonames_result['timezoneId']

Output:

'Europe/Paris'

How can I display a live clock on my page?

Example:

Current time in the city: 12:59

UPD: Figured out how to show the live time knowing the timezone with javascript thanks to @Matt:

<script type='text/javascript'>
        function update() {
          $('#clock').html(moment().tz("{{timezone}}").format('H:mm:ss'));
        }
        setInterval(update, 1000);
</script>

Now, I'm having another problem - I cannot get the timezone id by coordinates.Line "geonames.GeonamesClient('username')" does not work for some reason. I read result['Lat'] and result['Lng'] from my database.

import geonames
try:
    location = {}
    location['lat'] = result['Lat']
    location['lng'] = result['Lng']
    geonames_client = geonames.GeonamesClient('username')
    print "test"
    geonames_result = geonames_client.find_timezone(location)
    print geonames_result['timezoneId']
    result['Timezone'] = geonames_result['timezoneId']
except:
    result['Timezone'] = 'America/Los_Angeles'

So, I'm constantly getting the except value.

I am using Flask + virtualenv + uwsgi + Nginx

UPD2: I managed to do it. I am reading from mysql database lat and lng of the city and city's timezone, if timezone is not specified - I make a request to geonames.org and get the timezone id by the long and lat and write it to my database, so I don't need to request next time. Then I send the timezone id to the Jinja2 template. And using moment.js and moment-timezone display the current time in the city. You can see the updated page (clear the cache first): [link]test.fantazia-svit.com.ua/city/9

In order to get timezone from geonames I use this function:

import urllib
import json
try:
    url = "http://api.geonames.org/timezoneJSON?lat=%s&lng=%s&username=%s" % (Lat, Lng, username)
    response = urllib.urlopen(url);
    data = json.loads(response.read())
    Timezone = data['timezoneId']
except:
    Timezone = ''

Where Lat = your latitude, Lng = longitude and username = your username in genomes.org

par-n-go
  • 49
  • 3
  • Can you provide more context ? Do you have an existing page ? Do you use jinja2 ? How do you display a your list of cities ? – Timothée Jeannin Apr 02 '15 at 14:40
  • @TimothéeJeannin Yes, I use Flask with Jinja 2, use mysql to store the data, example page link: http://test.fantazia-svit.com.ua/city/9 I want to display time in each city independently – par-n-go Apr 02 '15 at 15:38
  • 1
    Do you want to do this in client-side JavaScript, or in server-side Python? For JS, take a look at the [moment-timezone](http://momentjs.com/timezone/) plugin for [moment.js](http://momentjs.com). – Matt Johnson-Pint Apr 02 '15 at 19:06
  • @MattJohnson I was thinking of getting the time zone by location server side and put the timezone into javascript, but I cannot understand how can I the clock with the moment.js. Maybe you can help me with the code? – par-n-go Apr 02 '15 at 19:52
  • It's all in the docs... – Matt Johnson-Pint Apr 02 '15 at 20:22
  • Do you want to know how to get the current time in `Europe/Paris` (or other given timezone) on the client i.e., in javascript? – jfs Apr 02 '15 at 21:49
  • @J.F.Sebastian Yes, that was exactly what I wanted. I managed to do it yesterday. I am reading from mysql database lat and lng of the city and city's timezone, if timezone is not specified - I make a request to geonames and get the timezone id by the long and lat and put it in my database, so I don't need to request next time. Then I send the timezone id to the Jinja2 template. And using moment.js and moment-timezone display the current time in the city. You can see the updated page (clear the cache first): http://test.fantazia-svit.com.ua/city/9 – par-n-go Apr 03 '15 at 11:21
  • please, limit yourself to one issue per question so that it might have been useful to future visitors. – jfs Apr 03 '15 at 18:17

1 Answers1

2

Figured out how to show the live time knowing the timezone with javascript thanks to @Matt:

<script src="moment.js"></script>
<script src="moment-timezone-with-data-2010-2020.js"></script>
<script type='text/javascript'>
    function update() {
      $('#clock').html(moment().tz("{{timezone}}").format('H:mm:ss'));
    }
    setInterval(update, 1000);
</script>

For backend. I am reading from mysql database lat and lng of the city and city's timezone, if timezone is not specified - I make a request to geonames.org and get the timezone id by the long and lat and write it to my database, so I don't need to request next time. Then I send the timezone id to the Jinja2 template. And using moment.js and moment-timezone display the current time in the city. You can see the updated page (clear the cache first): http://test.fantazia-svit.com.ua/city/9

In order to get timezone from geonames I use this function:

import urllib
import json
try:
    url = "http://api.geonames.org/timezoneJSON?lat=%s&lng=%s&username=%s" % (Lat, Lng, username)
    response = urllib.urlopen(url);
    data = json.loads(response.read())
    Timezone = data['timezoneId']
except:
    Timezone = ''

Where Lat = your latitude, Lng = longitude and username = your username in gonames.org and Timezone - is your timezone id that you insert in your frontend

par-n-go
  • 49
  • 3
  • if you are caching `coordinates -> tzid` pairs in a database anyway; then you could [get tzid from latitude, longitude using an offline database (e.g., generated from efele.net data) in the first place](http://stackoverflow.com/a/16519004/4279) – jfs Apr 03 '15 at 20:48
  • @J.F.Sebastian I tried it, but it seems pointless to maintain this database if you can do only one request to external server for each city. It would be an option if I would need to check the timezone id for each request, or am I thinking wrong? – par-n-go Apr 06 '15 at 08:06
  • The source data hasn't been updated since November 26, 2013 I don't see why would you need an online service for a static data. – jfs Apr 06 '15 at 19:23