C# .NET - How to Get User system Datetime by using longitude and latitude?

Asked By Jyoti Singh on 07-Jul-14 04:38 AM
Robbe Morris replied to Jyoti Singh on 07-Jul-14 11:43 AM
Look up code to get the timezone from longitude/latitude.  Lots of examples out there. From there, it is a pretty easy conversion.
Jyoti Singh replied to Robbe Morris on 08-Jul-14 01:26 AM
Hi ,

I have tried many things but no solution.

const string GOOGLE_API = "https://maps.googleapis.com";
        const string GOOGLE_TIMEZONE_REQUEST =

        // Pensacola, FL
        const double LATITUDE = 25.7877;
        const double LONGITUDE = -80.2241;
 static string get_time_zone(double latitude,
                                      double longitude,
                                      DateTime date)

            RestClient client;
            string location;
            RestRequest request;
            RestResponse response;
            TimeSpan time_since_midnight_1970;
            double time_stamp;
            string time_zone = "In error";

                client = new RestClient(GOOGLE_API);
                request = new RestRequest(GOOGLE_TIMEZONE_REQUEST,
                location = String.Format("{0},{1}",
                time_since_midnight_1970 = date.Subtract(
                            new DateTime(1970, 1, 1, 0, 0, 0));
                time_stamp = time_since_midnight_1970.TotalSeconds;

                request.AddParameter("location", location);
                request.AddParameter("timestamp", time_stamp);
                request.AddParameter("sensor", "false");

                response = (RestResponse)client.Execute(request);


                if (response.StatusDescription.Equals("OK"))
                    string dtHeaderDate =  response.Headers.ElementAt(8).Value.ToString();
                    XmlNode nodeTimeZone;
                    XmlDocument xml_document = new XmlDocument();

                    nodeTimeZone = xml_document.SelectSingleNode(

                    if (nodeTimeZone != null)
                        time_zone = nodeTimeZone.InnerText;
                        double dst_offset = Convert.ToDouble(xml_document.SelectSingleNode("/TimeZoneResponse/dst_offset").InnerText.ToString());
                        double raw_offset = Convert.ToDouble(xml_document.SelectSingleNode("/TimeZoneResponse/raw_offset").InnerText.ToString());
                        double dtUserDateTime = time_stamp + dst_offset + raw_offset;
                        DateTime MyDate = DateTime.FromOADate(dtUserDateTime);

                        TimeZoneInfo timeZoneInfo = System.TimeZoneInfo.FindSystemTimeZoneById(time_zone);
                        DateTime dataTimeByZoneId = System.TimeZoneInfo.ConvertTime(DateTime.Now, System.TimeZoneInfo.Local, timeZoneInfo);
            catch (Exception ex)
                time_zone = "In error";

            return (time_zone);


Above is my code, and i am getting invalid time zone error because the Googleapi time zones are different from Windows.