Article 3XQ5C CodeSOD: Rectangle Marks The Spot

CodeSOD: Rectangle Marks The Spot

by
Ellis Morning
from The Daily WTF on (#3XQ5C)

512px-World_Map_flat_Mercator.png

If you need your user's country of origin, there are many ways you can go about obtaining it programmatically. Some may opt for a simple drop-down that prompts the user to specify his/her country. If you don't want to burden your user this way, you might look at their session data and return their country of origin, time zone, or some other useful information. If you have fancy enough APIs at your disposal, you could even reverse geocode the user's longitude/latitude position and obtain an address.

Or, you can start with their location and perform the code equivalent of throwing a dart at a map littered with creepy-looking post-it notes:

def country(latitude, longitude): if -130 < longitude < -60: return 'US' elif 110 < longitude < 155 and -40 < latitude < -10: return 'AU' elif -8 < longitude < 2 and 50 < latitude < 60: return 'GB' elif 60 < longitude < 100: return 'IN' return Nonedef timezone(longitude): return int(longitude * 0.0655737704918)

Submitter Zekka writes: "When we decided to internationalize our app, it became necessary to determine what country the user was in. That's not even the correct constant to convert longitude to approximate timezone. I have no clue where they got it. It should be 0.06666 ..."

proget-icon.png [Advertisement] ProGet supports your applications, Docker containers, and third-party packages, allowing you to enforce quality standards across all components. Download and see how! TheDailyWtf?d=yIl2AUoC8zAL5zmDtD0w7k
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments