1

Hello I would determine a method to input any kind of location data (I can cater it to just about anything) such as a city/state, a zip code, a street address etc. And get back the local time for that location.

Is that functionality build in somewhere or is there a good resource/class I can use already developed?

Thanks.

ikathegreat
  • 2,311
  • 9
  • 49
  • 80

1 Answers1

0

Ended up retrieving the search result from a Google search, since I did not have the lat/long.

User HTML Agility to extract the page contents, filtered the nodes that contained "Time", a simple enough the first item was the needed result.

If you google "time cincinnati oh" you get back "1:41pm Friday (EDT) - Time in Cincinnati, OH" at the top of the page. this code block extracts that. The safety is if the time is unable to be determined, the search page only shows the results, so the first item in the array is like, "Showing the results for "yourSearch"" etc.

public void timeZoneUpdate()
        {
            try
            {
                arrayToParse.Clear();

                string URL = @"https://www.google.com/search?q=time+" + rowCity + "%2C+" + rowState;

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
                myRequest.Method = "GET";
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = sr.ReadToEnd();
                sr.Close();
                myResponse.Close();
                //Console.Write(result);

                HtmlAgilityPack.HtmlDocument htmlSnippet = new HtmlAgilityPack.HtmlDocument();
                htmlSnippet.Load(new StringReader(result));

                bool foundSection = false;

                foreach (HtmlAgilityPack.HtmlNode table in htmlSnippet.DocumentNode.SelectNodes("//table"))
                {
                    foreach (HtmlAgilityPack.HtmlNode row in table.SelectNodes("tr"))
                    {
                        foreach (HtmlAgilityPack.HtmlNode cell in row.SelectNodes("td"))
                        {
                            if (cell.InnerText.Contains("Time"))
                            {
                                foundSection = true;
                            }
                            if (foundSection)
                            {
                                //Console.WriteLine("Cell value : " + cell.InnerText);
                                arrayToParse.Add(cell.InnerText);
                            }
                        }
                    }
                }
            retrievedTimeZone = arrayToParse[0].ToString().Split('-')[0].Trim();

            if(retrievedTimeZone.Contains("Showing"))
            {
                retrievedTimeZone = "Undetermined";
            }
        }
ikathegreat
  • 2,311
  • 9
  • 49
  • 80