Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting timezone offset from Google App Engine request headers?

As per Google App Engine flexible docs, for any incoming request, as a service to the app, App Engine adds the following headers to all requests:

X-AppEngine-Country  as an ISO 3166-1 alpha-2 country code
X-AppEngine-Region    as an ISO-3166-2 standard
X-AppEngine-City
X-AppEngine-CityLatLong
X-Cloud-Trace-Context
X-Forwarded-For: [CLIENT_IP(s)], [global forwarding rule IP]
X-Forwarded-Proto [http | https]

Is there anyway I can get timezone offset using above info from request header using Java?

like image 585
zeeshan ansari Avatar asked Apr 24 '18 15:04

zeeshan ansari


People also ask

How do I get TimeZone information from Google Maps API?

To learn more, see Get Started with Google Maps Platform. The Time Zone API provides time offset data for locations on the surface of the earth. Request the time zone information for a specific latitude/longitude pair and date. The API returns the name of that time zone, the time offset from UTC, and the daylight savings offset.

What headers does App Engine add to a service request?

As a service to the app, App Engine adds the following headers to all requests: Country from which the request originated, as an ISO 3166-1 alpha-2 country code. App Engine determines this code from the client's IP address.

What is the time zone API developer guide?

The Time Zone API Developer Guide is intended for website and mobile developers who want to include time data on maps provided by one of the Google Maps Platform APIs. It provides an introduction to using the API and reference material on the available parameters.

Does App Engine check the content-encoding request header?

Also, the Content-Encoding request header is not checked by the server, so if the client sends a gzipped request body, it will be sent in compressed form to the application. As a service to the app, App Engine adds the following headers to all requests:


1 Answers

Add below to the pom.xml

  <dependency>
    <groupId>net.iakovlev</groupId>
    <artifactId>timeshape</artifactId>
    <version>2018d.1</version>
  </dependency>

And then run below type of code

package taruntest;

import net.iakovlev.timeshape.TimeZoneEngine;

import java.time.ZoneId;
import java.util.Optional;

public class ZoneInfo {
    public static TimeZoneEngine engine = null;
    private static Optional<ZoneId> ZoneID;

    public static void main(String[] args) {
        ZoneID = getZoneIdFromLatLong("12.971599,77.594563");
        System.out.println(ZoneID.toString());
    }

    public static Optional<ZoneId> getZoneIdFromLatLong(String latLong) {
        if (engine == null)
        {
            engine = TimeZoneEngine.initialize();
        }
        String[] latLongArr = latLong.split(",");
        double _lat = Double.parseDouble(latLongArr[0]);
        double _long = Double.parseDouble(latLongArr[1]);

        Optional<ZoneId> maybeZoneId = engine.query(_lat, _long);

        return maybeZoneId;
    }
}

The result is

Optional[Asia/Kolkata]

You can get your current coords using

https://mylocationtest.appspot.com/

like image 168
Tarun Lalwani Avatar answered Oct 05 '22 02:10

Tarun Lalwani