如何获取地球上任意位置特定时刻的当地时间
How to get the local time at a specific moment at any location on the globe
我需要知道如何获取地球上任意位置特定时刻的当地时间。
像这样:
DateTime localTime = GetLocalTime(utcDateTime, city);
或
DateTime localTime = GetLocalTime(utcDateTime, longitude, latitude);
NET Fx 或 Windows API 或全球 Web 服务中是否有一些 API 可以在全球某个位置的特定时刻获取当地时间?
您可以使用Google API SDK获取位置。 https://developers.google.com/maps/documentation/timezone/intro
public DateTime GetLocalTime(
DateTime utcDate, double latitude, double longitude )
{
var request = new TimeZoneRequest
{
ApiKey = _apiConfiguration.GoogleApiKey,
Location = new Location(latitude, longitude),
TimeStamp = utcDate,
Sensor = false
};
var response = GoogleMaps.TimeZone.Query(request);
if (response.Status != GoogleMapsApi.Entities.TimeZone.Response.Status.OK)
return utcDate;
var offset = TimeSpan.FromSeconds(response.RawOffSet + response.OffSet);
var timeZone = TimeZoneInfo.CreateCustomTimeZone(response.TimeZoneId, offset, response.TimeZoneName, response.TimeZoneName);
var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, timeZone);
return localTime;
}
我需要知道如何获取地球上任意位置特定时刻的当地时间。
像这样:
DateTime localTime = GetLocalTime(utcDateTime, city);
或
DateTime localTime = GetLocalTime(utcDateTime, longitude, latitude);
NET Fx 或 Windows API 或全球 Web 服务中是否有一些 API 可以在全球某个位置的特定时刻获取当地时间?
您可以使用Google API SDK获取位置。 https://developers.google.com/maps/documentation/timezone/intro
public DateTime GetLocalTime(
DateTime utcDate, double latitude, double longitude )
{
var request = new TimeZoneRequest
{
ApiKey = _apiConfiguration.GoogleApiKey,
Location = new Location(latitude, longitude),
TimeStamp = utcDate,
Sensor = false
};
var response = GoogleMaps.TimeZone.Query(request);
if (response.Status != GoogleMapsApi.Entities.TimeZone.Response.Status.OK)
return utcDate;
var offset = TimeSpan.FromSeconds(response.RawOffSet + response.OffSet);
var timeZone = TimeZoneInfo.CreateCustomTimeZone(response.TimeZoneId, offset, response.TimeZoneName, response.TimeZoneName);
var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, timeZone);
return localTime;
}