Logicify / jquery-locationpicker-plugin

JQuery Location Picker plugin
MIT License
389 stars 260 forks source link

Location and time zones #112

Open justincorrigible opened 7 years ago

justincorrigible commented 7 years ago

Is there any way already available through this plugin to provide the local timezone of a chosen place? If not, do you have any pointers as to where I should start producing a plugin/addition for this? I think it'd be a somewhat useful contribution to your plugin.

PanBagnat commented 7 years ago

You can use Googlemaps API for it, here is my code in Python:

def get_now_for_timezone(latitude, longitude):
    if session.seconds_from_utc:
        seconds_from_utc = float(session.seconds_from_utc)
    else:
        today_UTC_timestamp = time.time()
        url = "https://maps.googleapis.com/maps/api/timezone/json?location=" + str(latitude) + "," + str(longitude) + chr(38) + "timestamp=" + str(today_UTC_timestamp) + "&key=MYKEY";
        json_timezone = urllib2.urlopen(url)
        data_timezone = json.load(json_timezone)
        seconds_dstOffset = float(data_timezone['dstOffset'])
        seconds_rawOffset = float(data_timezone['rawOffset'])
        seconds_from_utc = seconds_dstOffset + seconds_rawOffset
        session.seconds_from_utc = seconds_from_utc
    now_from_timezone = datetime.datetime.today() + datetime.timedelta(seconds=seconds_from_utc)
    return now_from_timezone
justincorrigible commented 7 years ago

Hmm that looks good. But what if the selected time zone was part of the return from this plugin, along with the address, city, etc? Even showing whether that place is under DST offset. Personally, I'd prefer keeping this sort of processing on the client since the location picking is already being handled there after load.