Giter Club home page Giter Club logo

Comments (9)

cstich avatar cstich commented on July 17, 2024

You are importing the module tzwhere and not the class. And I should fix the documentation.
Try something like

from tzwhere import tzwhere

and that should work

from pytzwhere.

ImanYZ avatar ImanYZ commented on July 17, 2024

@cstich, Thank you so much.

from pytzwhere.

cstich avatar cstich commented on July 17, 2024

You are welcome :)

from pytzwhere.

ImanYZ avatar ImanYZ commented on July 17, 2024

By the way, this package is awesome. It took a while for me to find it, but it is exactly what I wanted.

Actually I have a list of city and state or country names and I was trying to identify their current time.
For this purpose, I combined the following four libraries:

from datetime import datetime
from geopy import geocoders
from tzwhere import tzwhere
from pytz import timezone

and the following is an example of my code:

location = "Sackville, Canada"
g = geocoders.GoogleV3()
place, (lat, lng) = g.geocode(location)
tz = tzwhere.tzwhere()
timeZoneObj = tz.tzNameAt(lat, lng)
now_time = datetime.now(timezone(timeZoneObj))

print now_time => 2015-06-26 15:56:37.425000-03:00

Do you know any more efficient way for this purpose?

Thank you again,
Iman

from pytzwhere.

cstich avatar cstich commented on July 17, 2024

Thank you.

As you are already effectively queering Google, you could just try queering Google for the timezone directly. Have a look at their API and see whether that fits your requirements.

Alternatively there is also a database of cities and their timezone - cities timezone - but I am not sure how complete the list is.

from pytzwhere.

ImanYZ avatar ImanYZ commented on July 17, 2024

I am actually using Google Geocoder API in order to locate the coordinates of addresses, cities, states, or countries. I think this is important, because these addresses are not formatted appropriately. Some of them may include only country name and others might include city and state. After having the coordinates, it's not efficient for me to use Google Time Zone API, because it doubles the number of requests that my program sends to Google APIs using the same key, which might exceed my quota.

from pytzwhere.

cstich avatar cstich commented on July 17, 2024

That makes sense and the code you posted looks very reasonable to me.

The only way and from the top of my head to speed up your code would be to parallize it. That should be pretty straightforward.

Edit: Forget what I just said. Sometimes I am too quick to answer :D

There is an obvious bottleneck in your code. If you have more than one point to look-up, do not re-initialize the tzwhere class. I also doubt that you have to initialize the geocoders class every time. Something like this should be quite faster if you have more than one point. This is also now trivial to turn into a function, you feed to a parallel version of map if you want to go that way.

tz = tzwhere.tzwhere()
g = geocoders.GoogleV3()
locations = ["Sackville, Canada", "London, UK"]

for l in locations:
    place, (lat, lng) = g.geocode(l)
    timeZoneObj = tz.tzNameAt(lat, lng)
    now_time = datetime.now(timezone(timeZoneObj))
    print(now_time)

from pytzwhere.

cstich avatar cstich commented on July 17, 2024

@ImanYZ this comment is just to make sure you see my edited answer from just a second ago. I am not sure how github handles edits and notifications. Better be safe than sorry.

from pytzwhere.

ImanYZ avatar ImanYZ commented on July 17, 2024

@cstich, Thank you so much for your comment.

from pytzwhere.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.