/// <summary> /// returns UTC offset for particular datetime /// </summary> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <param name="utcDate"></param> /// <returns></returns> public static float GetGPStimeOffset(double latitude, double longitude, DateTime utcDate) { string tzIana = TimeZoneLookup.GetTimeZone(latitude, longitude).Result; TimeZoneInfo tzInfo = TimeZoneConverter.TZConvert.GetTimeZoneInfo(tzIana); return((float)tzInfo.GetUtcOffset(utcDate).TotalHours); }
private DateTimeOffset GetDate() { string tzIana = TimeZoneLookup.GetTimeZone(lat, lon).Result; TimeZoneInfo tzInfo = TZConvert.GetTimeZoneInfo(tzIana); DateTimeOffset convertedTime = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tzInfo); return(convertedTime); }
private static string GetTimeInLocation(Location location) { var timeZone = TimeZoneLookup.GetTimeZone(location.Lattitude, location.Longtitude).Result; var culture = new CultureInfo("uk-UA", true); return(TimeZoneInfo.ConvertTime(DateTimeOffset.Now, TimeZoneInfo.FindSystemTimeZoneById(timeZone)) .ToString("t", culture)); }
public static int GetTimezoneOffsetFromLocationAndDateInSeconds(long timestamp, int latitude, int longitude) { var timezone = TimeZoneLookup.GetTimeZone(FixedPointCoordConversion.ToDouble(latitude), FixedPointCoordConversion.ToDouble(longitude)); Instant instant = new Instant(timestamp * 1000); ZonedDateTime zdt = instant.InZone(DateTimeZoneProviders.Tzdb[timezone.Result]); return(zdt.Offset.Milliseconds / 1000); }
/// <summary> /// get the Local time at a Lat/long at particualar date /// </summary> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <param name="utcDate"></param> /// <returns></returns> public static DateTime GetLocalTimefromGPS(double latitude, double longitude, DateTime utcDate) { string tzIana = TimeZoneLookup.GetTimeZone(latitude, longitude).Result; TimeZoneInfo tzInfo = TimeZoneConverter.TZConvert.GetTimeZoneInfo(tzIana); DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, tzInfo); return(localTime); }
/// <summary> Returns UTC offset (in hours) at specified lat/long </summary> public int GetUTC_Offset(double thisLat, double thisLong) { string ianaTimeZone = TimeZoneLookup.GetTimeZone(thisLat, thisLong).Result; string windowsTimeZone = TimeZoneConverter.TZConvert.IanaToWindows(ianaTimeZone); TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(windowsTimeZone); int offset = timeZoneInfo.BaseUtcOffset.Hours; return(offset); }
private TimeZoneResult DoGetIANATimezoneId(Location location) { if (location != null && location.Latitude.HasValue && location.Longitude.HasValue) { return(TimeZoneLookup.GetTimeZone(location.Latitude.Value, location.Longitude.Value)); } else { return(null); } }
private static List <SunPhase> GetSunPhases(DateTime date, double latitude, double longitude) { string tzName = TimeZoneLookup.GetTimeZone(latitude, longitude).Result; TimeZoneInfo tzInfo = TZConvert.GetTimeZoneInfo(tzName); DateTime localDate = TimeZoneInfo.ConvertTime(date.Add(DateTime.Now.TimeOfDay), tzInfo); // Set time to noon because of https://github.com/mourner/suncalc/issues/107 DateTime utcDate = new DateTimeOffset( localDate.Year, localDate.Month, localDate.Day, 12, 0, 0, tzInfo.GetUtcOffset(localDate)).UtcDateTime; return(SunCalcNet.SunCalc.GetSunPhases(utcDate, latitude, longitude).ToList()); }
public void Can_Lookup_Where_Two_Countries_Meet() { var france = TimeZoneLookup.GetTimeZone(47.589983, 7.587417); Assert.AreEqual("Europe/Paris", france.Result); Assert.AreEqual("Europe/Zurich", france.AlternativeResults[0]); var switzerland = TimeZoneLookup.GetTimeZone(47.58735, 7.588967); Assert.AreEqual("Europe/Zurich", switzerland.Result); Assert.AreEqual("Europe/Paris", switzerland.AlternativeResults[0]); }
public static DateTime ConvertTimeFromCoordinates(this DateTime date, double lat, double lon) { var tzIana = TimeZoneLookup.GetTimeZone(lat, lon).Result; #if Windows // Convert to Windows acceptable TimeZone tzIana = TZConvert.IanaToWindows(tzIana); #endif var tzInfo = TimeZoneInfo.FindSystemTimeZoneById(tzIana); var dt = DateTime.SpecifyKind(date, DateTimeKind.Utc); var convertedTime = TimeZoneInfo.ConvertTimeFromUtc(dt, tzInfo); return(convertedTime); }
public static string GetOpenStatus(Place place) { var tzIana = TimeZoneLookup.GetTimeZone(place.Latitude, place.Longitude).Result; var tzMs = TZConvert.IanaToWindows(tzIana); var tzInfo = TimeZoneInfo.FindSystemTimeZoneById(tzMs); var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzInfo); var isWeekend = now.DayOfWeek == DayOfWeek.Saturday || now.DayOfWeek == DayOfWeek.Sunday; return(!isWeekend & now.TimeOfDay > place.OpenTime && now.TimeOfDay < place.CloseTime || isWeekend & now.TimeOfDay > place.OpenTimeWeekend && now.TimeOfDay < place.CloseTimeWeekend ? String.Empty : "🔒 "); }
public static TimeZoneInfo GetTimeZoneInfoOnGeoLocation(double latitude, double longitude) { TimeZoneInfo timeZoneInfo = null; try { TimeZoneResult timeZoneResult = TimeZoneLookup.GetTimeZone(latitude, longitude); timeZoneInfo = TZConvert.GetTimeZoneInfo(timeZoneResult.Result); } catch { } return(timeZoneInfo); }
private async Task <Data.Shared.Models.Photo> UploadPhoto(IFormFile photo, PhotoType type, string userId, bool storeLocation) { var blobpath = Guid.NewGuid().ToString(); var fileInfo = new FileInfo(photo.FileName); var name = "original" + fileInfo.Extension; var result = await _blobService.UploadPhotoAsync(photo, userId, Path.Combine(blobpath, name)); if (result != null) { var location = GetLocationFromMetadata(result.Metadata); var(height, width) = GetDimensionsFromMetadata(result.Metadata); var timezone = TimeZoneInfo.Local; if (location != null && location.Latitude.HasValue && location.Longitude.HasValue) { var zone = TimeZoneLookup.GetTimeZone(location.Latitude.Value, location.Longitude.Value); timezone = TZConvert.GetTimeZoneInfo(zone.Result); } DateTime?dateTaken = null; if (result.Metadata.TryGetValue(Constants.DateTaken, out var dateTakenEntry)) { if (!string.IsNullOrEmpty(dateTakenEntry)) { if (DateTime.TryParse(dateTakenEntry, out var dateTakenValue)) { dateTaken = TimeZoneInfo.ConvertTimeToUtc(dateTakenValue, timezone); } } } return(new Data.Shared.Models.Photo { Filename = name, BlobPath = blobpath, BlobPathRoot = _blobStorageRoot, Type = type, UserId = userId, ContentType = result.ContentType, Location = storeLocation ? location : null, Height = height, Width = width, DateTaken = dateTaken, DateCreated = DateTime.UtcNow, CreatedBy = userId }); } return(null); }
private async void UpdateValues(XmlDocument doc) { bool metric = Settings.WeatherMetric; _temp = metric ? WeatherFunctions.KelvinToCelsius(double.Parse(doc.GetElementsByTagName("temperature")[0].Attributes["value"].Value)) : WeatherFunctions.KelvinToFarenheit(double.Parse(doc.GetElementsByTagName("temperature")[0].Attributes["value"].Value)); _windSpeed = metric ? WeatherFunctions.MpsToKmH(double.Parse(doc.GetElementsByTagName("speed")[0].Attributes["value"].Value)) : WeatherFunctions.MpsToMph(double.Parse(doc.GetElementsByTagName("speed")[0].Attributes["value"].Value)); _skyCondition = doc.GetElementsByTagName("weather")[0].Attributes["number"].Value; _cloudCover = doc.GetElementsByTagName("clouds")[0].Attributes["value"].Value; _windDirection = doc.GetElementsByTagName("direction")[0].Attributes["code"].Value; _humidity = double.Parse(doc.GetElementsByTagName("humidity")[0].Attributes["value"].Value); _pressure = metric ? double.Parse(doc.GetElementsByTagName("pressure")[0].Attributes["value"].Value) : WeatherFunctions.MillibarToInHg(double.Parse(doc.GetElementsByTagName("pressure")[0].Attributes["value"].Value)); XmlNode sun = doc.GetElementsByTagName("sun")[0]; XmlNode location = doc.GetElementsByTagName("coord")[0]; double longitude = double.Parse(location.Attributes["lon"].Value); double latitude = double.Parse(location.Attributes["lat"].Value); //_sunrise = DateTime.Parse(sun.Attributes["rise"].Value).ToLocalTime().TimeOfDay; //_sunset = DateTime.Parse(sun.Attributes["set"].Value).ToLocalTime().TimeOfDay; TimeZoneInfo tz = await TimeZoneLookup.TimeZone(latitude, longitude); if (tz == null) { tz = TimeZoneInfo.Local; _showTimeZone = true; } else { _showTimeZone = false; } _timeZone = tz; _sunrise = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(sun.Attributes["rise"].Value), tz).TimeOfDay; _sunset = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(sun.Attributes["set"].Value), tz).TimeOfDay; _timestamp = DateTime.Parse(doc.GetElementsByTagName("lastupdate")[0].Attributes["value"].Value).ToLocalTime(); RaiseDataUpdatedEvent(); }
public void FortWayneIndependanceDay() { const string dateTime = "2017-07-04 20:30:40"; var timeZone = TimeZoneLookup.GetTimeZone(40.977506, -85.196059).Result; var d = DateTime.Parse(dateTime); var abbreviation = TZNames.GetAbbreviationsForTimeZone(timeZone, "en-US").Generic; abbreviation.Should().Be("ET"); var ldt = new LocalDateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second); var zdt = ldt.InZoneLeniently(DateTimeZoneProviders.Tzdb[timeZone]); var date = zdt.ToDateTimeOffset(); date.ShouldBeEquivalentTo(new DateTimeOffset(2017, 07, 04, 20, 30, 40, TimeSpan.FromHours(-4))); Console.WriteLine(date.ToString("s")); }
public void FortWayneChristmas() { const string dateTime = "2017-12-25 20:30:40"; var timeZone = TimeZoneLookup.GetTimeZone(40.977506, -85.196059).Result; timeZone.Should().Be("America/Indiana/Indianapolis"); var abbreviation = TZNames.GetAbbreviationsForTimeZone(timeZone, "en-US").Generic; abbreviation.Should().Be("ET"); var pattern = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss"); var ldt = pattern.Parse(dateTime).Value; var zdt = ldt.InZoneLeniently(DateTimeZoneProviders.Tzdb[timeZone]); var date = zdt.ToDateTimeOffset(); date.ShouldBeEquivalentTo(new DateTimeOffset(2017, 12, 25, 20, 30, 40, TimeSpan.FromHours(-5))); Console.WriteLine(date.ToString("s")); }
public void TucsonIndependanceDay() { const string dateTime = "2017-07-04 20:30:40"; var timeZone = TimeZoneLookup.GetTimeZone(32.114510, -110.939259).Result; var d = DateTime.Parse(dateTime); var abbreviation = TZNames.GetAbbreviationsForTimeZone(timeZone, "en-US").Generic; abbreviation.Should().Be("MT"); var ldt = new LocalDateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second); var zdt = ldt.InZoneLeniently(DateTimeZoneProviders.Tzdb[timeZone]); var date = zdt.ToDateTimeOffset(); date.ShouldBeEquivalentTo(new DateTimeOffset(2017, 07, 04, 20, 30, 40, TimeSpan.FromHours(-7))); var s = date.ToString("yyyy-MM-ddTHH\\:mm\\:sszzz"); Console.WriteLine(s); s.Should().Be("2017-07-04T20:30:40-07:00"); }
private static List <SunPhase> GetSunPhases(DateTime date, double latitude, double longitude) { string tzName = TimeZoneLookup.GetTimeZone(latitude, longitude).Result; TimeZoneInfo tzInfo = TZConvert.GetTimeZoneInfo(tzName); TimeSpan deltaOffset = tzInfo.BaseUtcOffset - TimeZoneInfo.Local.BaseUtcOffset; if (deltaOffset < TimeSpan.Zero && DateTime.Now < (DateTime.Today - deltaOffset)) { date = date.AddDays(-1); // California = NY - 3 @ <3am } else if (deltaOffset > TimeSpan.Zero && DateTime.Now > (DateTime.Today.AddDays(1) - deltaOffset)) { date = date.AddDays(1); // Azerbaijan = NY + 9 @ >3pm } // Add 12 hours minus timezone offset because of https://github.com/mourner/suncalc/issues/107 DateTime utcDate = date.AddHours(12) - tzInfo.GetUtcOffset(date); return(SunCalcNet.SunCalc.GetSunPhases(utcDate, latitude, longitude).ToList()); }
public void TucsonChristmas() { const string dateTime = "2017-12-25 20:30:40"; var timeZone = TimeZoneLookup.GetTimeZone(32.114510, -110.939259).Result; timeZone.Should().Be("America/Phoenix"); var abbreviation = TZNames.GetAbbreviationsForTimeZone(timeZone, "en-US").Generic; abbreviation.Should().Be("MT"); var pattern = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss"); var ldt = pattern.Parse(dateTime).Value; var zdt = ldt.InZoneLeniently(DateTimeZoneProviders.Tzdb[timeZone]); var date = zdt.ToDateTimeOffset(); date.ShouldBeEquivalentTo(new DateTimeOffset(2017, 12, 25, 20, 30, 40, TimeSpan.FromHours(-7))); var s = date.ToString("yyyy-MM-ddTHH\\:mm\\:sszzz"); Console.WriteLine(s); s.Should().Be("2017-12-25T20:30:40-07:00"); }
public void UpdateAutoTimeZone() { try { var tzIana = TimeZoneLookup.GetTimeZone(Convert.ToDouble(Lat.Text), Convert.ToDouble(Long.Text)).Result; var tzMs = TZConvert.IanaToWindows(tzIana); var tzInfo = TimeZoneInfo.FindSystemTimeZoneById(tzMs); SequenceTimeZone = tzInfo; //SequenceTimeZoneOffset = TimeZoneInfo.GetUtcOffset; SequenceTimeZoneOffset = GetTZOffset(DateTime.Now, tzInfo); // SequenceTimeZoneOffset = TimeZoneInfo. //TimeZoneInfo.FindSystemTimeZoneById(tzMs); //offsetFromUtc = TimeZoneInfo.ConvertTimeToUtc(DateTime.UtcNow, tzInfo); //Console.WriteLine("{0}", TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzInfo)); LocalTimeNowTextBox.Text = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzInfo).ToString(); var convertedTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzInfo); TimeZoneTextBlock.Text = tzMs.ToString(); SiteLat = Convert.ToDouble(Lat.Text); SiteLong = Convert.ToDouble(Long.Text); } catch { TimeZoneTextBlock.Text = "INVALID"; } }
private async Task ResumeAfterLocationDialogAsync(IDialogContext context, IAwaitable <Place> result) { Place place = await result; if (place != null) { var geoCord = place.GetGeoCoordinates(); if (geoCord.Longitude.HasValue && geoCord.Latitude.HasValue) { var tz = TimeZoneLookup.GetTimeZone(geoCord.Latitude.Value, geoCord.Longitude.Value); try { context.SetUserTimeZoneName(tz.Result); await context.PostAsync($"Time Zone set to {tz.Result}, You can now ask me for meeting room availability, type help for more information."); } catch (Exception) { // set default instead and inform the user. context.SetUserTimeZoneName(DefaultTimeZone); await context.PostAsync($"Unable to resolve timezone for {place.Name}. Time Zone set to Default {DefaultTimeZone}"); } } else { // set default timezone if location return is missing coordinates // set default instead and inform the user. context.SetUserTimeZoneName(DefaultTimeZone); await context.PostAsync($"Unable to resolve timezone for {place.Name}. Time Zone set to Default {DefaultTimeZone}"); } } else { await context.PostAsync("OK, cancelled"); } context.Done <string>(null); }
public async Task <DateTimeZone> GetTimeZone(InlineQuery inlineQuery, CancellationToken cancellationToken = default) { if (inlineQuery.Location is {} userLocation) { if (TimeZoneLookup.GetTimeZone(userLocation.Latitude, userLocation.Longitude).Result is {} timeZoneId) { if (myDateTimeZoneProvider.GetZoneOrNull(timeZoneId) is { } timeZone) { return(timeZone); } } } { var userSettings = await myDB.Set <UserSettings>().Get(inlineQuery.From, cancellationToken); if (userSettings?.TimeZoneId is { } timeZoneId&& myDateTimeZoneProvider.GetZoneOrNull(timeZoneId) is { } timeZone) { return(timeZone); } } return(myDefaultDateTimeZone); }
public void Can_Lookup_Offset_TitanticWreck() { var tz = TimeZoneLookup.GetTimeZone(41.7325, -49.9469).Result; Assert.AreEqual("Etc/GMT+3", tz); }
public void Can_Lookup_Offset_Extreme_East() { var tz = TimeZoneLookup.GetTimeZone(80, 179).Result; Assert.AreEqual("Etc/GMT-12", tz); }
public void Can_Lookup_Offset_SsLulworthHillWreck() { var tz = TimeZoneLookup.GetTimeZone(-10.166667, 1).Result; Assert.AreEqual("UTC", tz); }
public void Can_Lookup_Offset_SsFortLeeWreck() { var tz = TimeZoneLookup.GetTimeZone(-27.583333, 83.183333).Result; Assert.AreEqual("Etc/GMT-6", tz); }
private static string GetTimeZone(GeoPoint geoPoint) { var key = geoPoint.ToString(); return(AddOrGetExisting(key, () => TimeZoneLookup.GetTimeZone(geoPoint.Latitude, geoPoint.Longitude).Result)); }
public void Can_Lookup_TimeZone_Mazatlan() { var tz = TimeZoneLookup.GetTimeZone(23.22, -106.42).Result; Assert.AreEqual("America/Mazatlan", tz); }
public void Can_Lookup_TimeZone_LordHoweIsland() { var tz = TimeZoneLookup.GetTimeZone(-31.55, 159.0833).Result; Assert.AreEqual("Australia/Lord_Howe", tz); }
public void Can_Lookup_TimeZone_Sydney() { var tz = TimeZoneLookup.GetTimeZone(-33.86, 151.2111).Result; Assert.AreEqual("Australia/Sydney", tz); }