我需要知道如何在全球任何地点的特定时刻获得当地时间。
这样的事情:
DateTime localTime = GetLocalTime(utcDateTime, city);
或
DateTime localTime = GetLocalTime(utcDateTime, longitude, latitude);
NET Fx或Windows API或全球网络服务中是否有某些API可以在全球某个位置的特定时刻获取当地时间?
答案 0 :(得分:1)
您可以使用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;
}