根据坐标获取当地时间 [英] Get local time based on coordinates

查看:101
本文介绍了根据坐标获取当地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,该应用程序应根据您给出的坐标(经纬度)给出本地时间。

I am programming an application that should give the local time based on the coordinates (lat&long) that you give it.

我只知道两种方法做到这一点:

I only know of 2 methods to do that:

第一个:获取TimeZone名称,然后找到其本地时间。
第二个:使用Google API并接收时间作为偏移量,而不是UTC而不是本地时间。

1st: Get the TimeZone Name, and then find its local time. 2nd: Use the Google API and receive the time as an offset and UTC not Local.

我决定使用第一种方法,因为看起来更简单,所以我决定使用GeoTimeZone来获取时区...问题是,然后我不知道如何获取该TimeZone上的本地时间...这是我编写的获取TimeZone名称的代码。

I decided to use the 1st method because seemed easier, so I decided to use the GeoTimeZone to get the Time Zone... Problem is that then I don´t know how to get the local time on that TimeZone... Here´s the code I wrote to get the TimeZone name.

string tz = TimeZoneLookup.GetTimeZone(lat, lon).Result;

变量 lat & lon 当然是坐标。

variables lat & lon are of course the coordinates.

谢谢!

编辑:我的问题是如何在该TimeZone上获取LocalTime?

My question is how can I get the LocalTime on that TimeZone?

推荐答案

您可以使用Google api来实现

.Net小提琴示例

You can do it using Google api for identifying current timezone.
.Net Fiddle example:

public class Program
{
    public static DateTime GetLocalDateTime(double latitude, double longitude, DateTime utcDate)
    {
        var client = new RestClient("https://maps.googleapis.com");
        var request = new RestRequest("maps/api/timezone/json", Method.GET);
        request.AddParameter("location", latitude + "," + longitude);
        request.AddParameter("timestamp", utcDate.ToTimestamp());
        request.AddParameter("sensor", "false");
        var response = client.Execute<GoogleTimeZone>(request);

        return utcDate.AddSeconds(response.Data.rawOffset + response.Data.dstOffset);
    }

    public static void Main()
    {
        var myDateTime = GetLocalDateTime(33.8323, -117.8803, DateTime.UtcNow);
        Console.WriteLine(myDateTime.ToString());
    }
}

public class GoogleTimeZone 
{
    public double dstOffset { get; set; }
    public double rawOffset { get; set; }
    public string status { get; set; }
    public string timeZoneId { get; set; }
    public string timeZoneName { get; set; }
}

public static class ExtensionMethods 
{
    public static double ToTimestamp(this DateTime date)
    {
        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        TimeSpan diff = date.ToUniversalTime() - origin;
        return Math.Floor(diff.TotalSeconds);
    }
}

然后您可以轻松使用 GetLocalDateTime(双纬度,双经度,DateTime utcDate)方法,如上例所示:

And then you can easily use your GetLocalDateTime(double latitude, double longitude, DateTime utcDate) method as it was shown in the example above:

public static void Main()
{
    var myDateTime = GetLocalDateTime(33.8323, -117.8803, DateTime.UtcNow);
    Console.WriteLine(myDateTime.ToString());
}

这篇关于根据坐标获取当地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆