public void TestUpperLongitudeLimit() { GoogleTimeZone googleTimeZone = new GoogleTimeZone("AIzaSyCVGJtR2LgD5arGM_61hToUE2iNlb_VBfc"); DateTime dt = DateTime.Now; GeoLocation location = new GeoLocation() //Fiji Island { Latitude = -17.847737, Longitude = 177.904347 }; GoogleTimeZoneResult result = googleTimeZone.GetTimeZoneByLocation(dt, location); Assert.IsTrue(result != null, "Request Succeded with longitude of 180"); location.Longitude = 179; //Somewhere in the ocean try { result = null; result = googleTimeZone.GetTimeZoneByLocation(dt, location); } catch (Exception e) { Assert.IsTrue(result == null, "Getting no result"); } location.Longitude = 181; //Out of scope try { result = null; result = googleTimeZone.GetTimeZoneByLocation(dt, location); } catch (Exception e) { Assert.IsTrue(result == null, "Bad Exception errors"); } }
public static DateTime GetLocalDateTime(string utcDate, double latitude, double longitude) { DateTime date; DateTime.TryParse(utcDate, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out date); if (m_GoogleTimeZone == null) m_GoogleTimeZone = GetGoogleTimeZone(latitude, longitude); return date.AddSeconds(m_GoogleTimeZone.rawOffset + m_GoogleTimeZone.dstOffset); }
public static GoogleTimeZone GetGoogleInfo(string Location, IConfiguration _config) { long unixTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds(); string GoogleApiUrl = _config.GetSection("GoogleApiUrl").Value; string url = $"{GoogleApiUrl}location={Location}×tamp={unixTime}"; HttpClient http = new HttpClient(); var data = http.GetAsync(url).Result.Content.ReadAsStringAsync().Result; GoogleTimeZone result = JsonConvert.DeserializeObject <GoogleTimeZone>(data); return(result); }
private Tuple <string, int?> IdentifyTimeZone(Location location) { try { var googleTimeZone = new GoogleTimeZone(ConfigurationManager.AppSettings["google.apiKey"]); var googleTimeZoneResult = googleTimeZone.ConvertDateTime(DateTime.Now, new GeoLocation() { Latitude = location.latitude, Longitude = location.longitude }); return(new Tuple <string, int?>(googleTimeZoneResult.TimeZoneName, (int)(googleTimeZoneResult.RawOffset / 3600))); } catch { return(new Tuple <string, int?>("", null)); } }
public void TestGetTimeZoneByAddress() { GoogleTimeZone googleTimeZone = new GoogleTimeZone("AIzaSyCVGJtR2LgD5arGM_61hToUE2iNlb_VBfc"); DateTime dt = DateTime.Now; string address = "Quebec, Canada"; GoogleTimeZoneResult result = googleTimeZone.GetTimeZoneByAddress(dt, address); Assert.IsTrue(result != null); Console.WriteLine("DateTime on the server : " + dt); Console.WriteLine("Server time in particular to : " + address); Console.WriteLine("TimeZone Id : " + result.TimeZoneId); Console.WriteLine("TimeZone Name : " + result.TimeZoneName); Console.WriteLine("Converted DateTime : " + result.DateTime); }
static void Main(string[] args) { GoogleTimeZone googleTimeZone = new GoogleTimeZone("AIzaSyCNijWGcrNwwlGAl2zUUsoo_Qb2C1Sx0vk"); DateTime dt = DateTime.Now; string address = "Quebec, Canada"; GeoLocation location = new GeoLocation() { Latitude = 6.9216318, Longitude = 79.8212827 }; GeoLocation location2 = new GeoLocation() { Latitude = -19.613200, Longitude = 133.641453 }; GoogleTimeZoneResult result = googleTimeZone.GetTimeZoneByAddress(dt, address); //Console.WriteLine("DateTime on the server : " + dt); //Console.WriteLine("Server time in particular to : " + address); //Console.WriteLine("TimeZone Id : " + result.TimeZoneId); //Console.WriteLine("TimeZone Name : " + result.TimeZoneName); //Console.WriteLine("Converted DateTime : " + result.DateTime); //result = googleTimeZone.GetTimeZoneByLocation(dt, location); //Console.WriteLine("DateTime on the server : " + dt); //Console.WriteLine("Server time in particular to : " + location.Latitude + "," + location.Longitude); //Console.WriteLine("TimeZone Id : " + result.TimeZoneId); //Console.WriteLine("TimeZone Name : " + result.TimeZoneName); //Console.WriteLine("Converted DateTime : " + result.DateTime); result = googleTimeZone.GetTimeZoneByLocation(dt, location2); Console.WriteLine("DateTime on the server : " + dt); Console.WriteLine("Server time in particular to : " + location2.Latitude + "," + location2.Longitude); Console.WriteLine("TimeZone Id : " + result.TimeZoneId); Console.WriteLine("TimeZone Name : " + result.TimeZoneName); Console.WriteLine("Converted DateTime : " + result.DateTime); Console.ReadKey(); }
public void TestGetTimeZoneByLocation() { GoogleTimeZone googleTimeZone = new GoogleTimeZone("AIzaSyCVGJtR2LgD5arGM_61hToUE2iNlb_VBfc"); DateTime dt = DateTime.Now; GeoLocation location = new GeoLocation(); location.Latitude = 6.9216318; location.Longitude = 79.8212827; GoogleTimeZoneResult result = googleTimeZone.GetTimeZoneByLocation(dt, location); Assert.IsTrue(result != null); Console.WriteLine("DateTime on the server : " + dt); Console.WriteLine("Server time in particular to : " + location.Latitude + "," + location.Longitude); Console.WriteLine("TimeZone Id : " + result.TimeZoneId); Console.WriteLine("TimeZone Name : " + result.TimeZoneName); Console.WriteLine("Converted DateTime : " + result.DateTime); }
public string Get([FromRoute] string ZipCode) { string CITY_NAME = ""; string TEMPERATURE = ""; string TIMEZONE = ""; string Message = ""; string location = ""; { try { OpenWeatherMap OpenWeatherInfo = Common.CommonMethods.GetOpenWeatherInfo(ZipCode, _config); if (OpenWeatherInfo != null) { CITY_NAME = OpenWeatherInfo.name; TEMPERATURE = OpenWeatherInfo.main.temp; location = $"{OpenWeatherInfo.coord.lat}, {OpenWeatherInfo.coord.lon}"; GoogleTimeZone GoogleInfo = Common.CommonMethods.GetGoogleInfo(location, _config); if (GoogleInfo != null) { TIMEZONE = $"{GoogleInfo.timeZoneId} ({GoogleInfo.timeZoneName})"; } else { TIMEZONE = Resources.MessagesRes.Unknown; } } else { CITY_NAME = Resources.MessagesRes.Unknown; TEMPERATURE = Resources.MessagesRes.NotDefined; TIMEZONE = Resources.MessagesRes.Unknown; } Message = $"At the location {CITY_NAME}, \n the temperature is {TEMPERATURE}, \n and the timezone is {TIMEZONE}"; } //catch (Exception ex) //{ // //Logging // { // CITY_NAME = Resources.MessagesRes.Unknown; // TEMPERATURE = Resources.MessagesRes.NotDefined; // TIMEZONE = Resources.MessagesRes.Unknown; // } // Message = $"At the location {CITY_NAME}, \n the temperature is {TEMPERATURE}, \n and the timezone is {TIMEZONE}"; //} catch { //Logging { CITY_NAME = Resources.MessagesRes.Unknown; TEMPERATURE = Resources.MessagesRes.NotDefined; TIMEZONE = Resources.MessagesRes.Unknown; } Message = $"At the location {CITY_NAME}, \n the temperature is {TEMPERATURE}, \n and the timezone is {TIMEZONE}"; } } return(Message); }
private static DateTime GetLocalDateTime(GoogleTimeZone timezone, DateTime utcDate) { return(utcDate.AddSeconds(timezone.RawOffset + timezone.DstOffset)); }
public ActionResult FlightValidation(string AirlineName, string FlightNumber, string DepartureName, string ArrivalName, string DepartureTime, string ArrivalTime, string Stops, string TripTypeName, string PassengerName, string PassengerLastName, string PassengerEmail) { var flightService = new ConnxysWCFRestService(); var fsm = new FlightStatusMessages(); string jsonData; GoogleTimeZone timezone = new GoogleTimeZone(); fsm.isValid = false; fsm.messages = new Message(); _airlinesRepository = new AirlinesRepository(); _tagsRepository = new TagsRepository(); _airportsRepository = new AirportsRepository(); if (!AirlineName.Equals("") && !FlightNumber.Equals("") && !DepartureName.Equals("") && !ArrivalName.Equals("") && !DepartureTime.Equals("") && !ArrivalTime.Equals("") && !Stops.Equals("") && !TripTypeName.Equals("")) { #region Complete Info var model = new BasicItineraryModel { AirlineName = AirlineName, FlightNumber = Convert.ToInt32(FlightNumber), DepartureName = DepartureName, ArrivalName = ArrivalName, DepartureTime = Convert.ToDateTime(DepartureTime), ArrivalTime = Convert.ToDateTime(ArrivalTime), Stops = Convert.ToInt32(Stops), TripTypeName = TripTypeName, PassengerName = PassengerName, PassengerLastName = PassengerLastName, PassengerEmail = PassengerEmail }; //Get Airport Information: Departure and Arrival airports Airports DepartureAirport = new Airports(); DepartureAirport = _airportsRepository.GetAirportbyName(DepartureName); Airports ArrivalAirport = new Airports(); ArrivalAirport = _airportsRepository.GetAirportbyName(ArrivalName); //Get UTC of Departure Airport Double UTCDepartureAir = timezone.GetLocalDateTime(DepartureAirport.Latitude, DepartureAirport.Longitude, DateTime.UtcNow); //Get UTC of Arrival Airport Double UTCArrivalAir = timezone.GetLocalDateTime(ArrivalAirport.Latitude, ArrivalAirport.Longitude, DateTime.UtcNow); //Get the difference between UTC and local time to Departure and Arrival airport model.DepartDateTimeUTC = model.DepartureTime.Value.AddHours(UTCDepartureAir); model.ArrivalDateTimeUTC = model.ArrivalTime.Value.AddHours(UTCArrivalAir); var tempAirline = _airlinesRepository.GetAirlinebyName(model.AirlineName); var result = flightService.GetInfoFlightStatus(model.FlightNumber, tempAirline.AirlineDesignator, Convert.ToDateTime(model.DepartDateTimeUTC)); var listResult = result.flightStatuses.ToList(); var totalFlightStatus = listResult.Count; //Time validation validate the hour and minutes selected by the user if (model.DepartureTime.Value >= model.ArrivalTime.Value) { fsm.messages.mainMessage = "Arrival Time must be greater than Departure Time"; jsonData = new JavaScriptSerializer().Serialize(fsm); return(Json(jsonData, JsonRequestBehavior.AllowGet)); } var utcNow = DateTime.UtcNow; if (model.DepartureTime.Value < utcNow || model.ArrivalTime.Value < utcNow) { fsm.messages.mainMessage = "Departure Time and Arrival Time must be greater or equal than Current UTC time"; jsonData = new JavaScriptSerializer().Serialize(fsm); return(Json(jsonData, JsonRequestBehavior.AllowGet)); } if (totalFlightStatus > 0) { /* If is a real flight */ foreach (var item in listResult) { fsm.messages = new Message(); var matchesElements = true; var flightStatus = item; var serviceDepartureDate = flightStatus.departureDate.dateUtc.ToUniversalTime(); //DateTimeOffset.Parse(flightStatus.departureDate.dateUtc).UtcDateTime; var serviceArrivalDate = flightStatus.arrivalDate.dateUtc.ToUniversalTime(); //DateTimeOffset.Parse().UtcDateTime; #region Departure and Arrival Airports match with real flight if (model.DepartureName == flightStatus.departureAirportFsCode && model.ArrivalName == flightStatus.arrivalAirportFsCode) { if (serviceDepartureDate.CompareTo(model.DepartDateTimeUTC) != 0) { fsm.messages.depDateMessage = "The Departure Date doesn't match with real flight."; matchesElements = false; } if (serviceArrivalDate.CompareTo(model.ArrivalDateTimeUTC) != 0) { fsm.messages.arrDateMessage = "The Arrival Date doesn't match with real flight."; matchesElements = false; } if (matchesElements) { fsm.isValid = true; } break; } #endregion #region Departure Airport match with real flight if (model.DepartureName == flightStatus.departureAirportFsCode) { if (model.ArrivalName != flightStatus.arrivalAirportFsCode) { fsm.messages.arrivalMessage = "The Arrival doesn't match with real flight."; } if (serviceDepartureDate.CompareTo(model.DepartDateTimeUTC) != 0) { fsm.messages.departureMessage = "The Departure Date doesn't match with real flight."; } if (serviceArrivalDate.CompareTo(model.ArrivalDateTimeUTC) != 0) { fsm.messages.arrDateMessage = "The Arrival Date doesn't match with real flight."; } break; } #endregion #region Arrival Airport match with real flight if (model.ArrivalName == flightStatus.arrivalAirportFsCode) { if (model.DepartureName != flightStatus.departureAirportFsCode) { fsm.messages.departureMessage = "The Departure doesn't match with real flight."; } if (serviceDepartureDate.CompareTo(model.DepartDateTimeUTC) != 0) { fsm.messages.depDateMessage = "The Departure Date doesn't match with real flight."; } if (serviceArrivalDate.CompareTo(model.ArrivalDateTimeUTC) != 0) { fsm.messages.arrDateMessage = "The Arrival Date doesn't match with real flight."; } break; } #endregion #region Departure and Arrival Airports don't match with real flight if (model.DepartureName == flightStatus.departureAirportFsCode || model.ArrivalName == flightStatus.arrivalAirportFsCode) { continue; } fsm.messages.departureMessage = "The Departure doesn't match with real flight."; fsm.messages.arrivalMessage = "The Arrival doesn't match with real flight."; if (serviceDepartureDate.CompareTo(model.DepartDateTimeUTC) != 0) { fsm.messages.depDateMessage = "The Departure Date doesn't match with real flight."; } if (serviceArrivalDate.CompareTo(model.ArrivalDateTimeUTC) != 0) { fsm.messages.arrDateMessage = "The Arrival Date doesn't match with real flight."; } #endregion } } else { /* If is not a real flight */ fsm.isValid = true; } if (fsm.isValid) { // Insert the flight into flight's list InsertFlightIntoList(model); } else { fsm.messages.mainMessage = "Error: Flight number " + model.FlightNumber + " doesn't match the Flight Stats information. Please edit the information below."; } #endregion } else { fsm.messages.mainMessage = "Information required: Please complete all the flight information fields."; } jsonData = new JavaScriptSerializer().Serialize(fsm); return(Json(jsonData, JsonRequestBehavior.AllowGet)); }