public async Task Weather([Leftover] string query = "toronto") { try { using IDisposable _ = Context.Channel.EnterTypingState(); GeolocationResult location = await Service.GetLocationDataAsync(query).ConfigureAwait(false); if (location == null) { await Context.Channel.SendErrorAsync("Cannot find specified location. Please try again."); return; } GeolocationModel addr = location.Results[0]; string result = await Service.GetWeatherDataAsync(addr.Geometry.Location.Lat, addr.Geometry.Location.Lng).ConfigureAwait(false); TimeZoneResult tz = await Service.GetLocalDateTime(addr.Geometry.Location.Lat, addr.Geometry.Location.Lng).ConfigureAwait(false); DateTimeOffset localDt = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.UtcNow, tz.TimeZoneId); await Context.Channel.EmbedAsync(new EmbedBuilder().WithDynamicColor(Context) .WithAuthor("Weather Report") .WithDescription(addr.FormattedAddress + "\n" + Format.Code(result)) .WithFooter($"{localDt:HH:mm, MMM dd, yyyy}, {tz.TimeZoneName}, UTC{localDt:zz}")) .ConfigureAwait(false); } catch { await Context.Channel.SendErrorAsync("Failed to get weather."); } }
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); }
public async Task <Models.TimeZone> GetTimeZone(double latitude, double longitude) { using (WebClient webClient = new WebClient()) { var format = "json"; var subscription = "f6qsIPDhV8rSFwzz2LHUfD6gNhW5jcKdsd_JTcexmjg"; var query = $"{latitude},{longitude}"; var url = $"https://atlas.microsoft.com/timezone/byCoordinates/{format}?subscription-key={subscription}&api-version=1.0&query={query}"; // Download the Web resource and save it into the current filesystem folder. var json = await webClient.DownloadStringTaskAsync(url); var result = TimeZoneResult.FromJson(json); var timeZone = result.TimeZones.ToList().First(); return(timeZone); } }
public static string GetTimeZoneDisplayName(double latitude, double longitude) { TimeZoneResult timeZoneResult = TimeZoneLookup.GetTimeZone(latitude, longitude); return(TZConvert.GetTimeZoneInfo(timeZoneResult.Result).DisplayName); }