如何检查丹麦是否实行了夏令时,并在此基础上将我的数据增加1小时,否则不进行操作?我有一个XML文件:
<day = "1"
month = "5"
sunrise ="06:30"
sunset ="21:30"
/>
如何检查丹麦是否实行了夏令时,并在此基础上将我的数据增加1小时,否则不进行操作?我有一个XML文件:
<day = "1"
month = "5"
sunrise ="06:30"
sunset ="21:30"
/>
我认为你需要将这个XML转换为DateTime,然后使用TimeZoneInfo类。
如果你在丹麦,那么这就是本地时间:
DateTime thisTime = DateTime.Now;
bool isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(thisTime);
否则,您需要获取丹麦时区:
DateTime thisTime = DateTime.Now;
// get Denmark Standard Time zone - not sure about that
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Denmark Standard Time");
bool isDaylight = tst.IsDaylightSavingTime(thisTime);
当我按上述方式编码 - 对于纽约,我在调试器中发现时间已正确设置(包括夏令时)。
TimeZoneInfo nyTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime nyTime = GetLocalDateTime(DateTime.UtcNow, nyTimeZone);
if (nyTimeZone.IsDaylightSavingTime(nyTime))
nyTime = nyTime.AddHours(1);
public static DateTime GetLocalDateTime(DateTime utcDateTime, TimeZoneInfo timeZone)
{
utcDateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc);
DateTime time = TimeZoneInfo.ConvertTime(utcDateTime, timeZone);
return time;
}
GetLocalDateTime
? - Kiquenet您可以使用TimeZoneInfo.IsDaylightSavingTime
DateTime theDate = new DateTime(2012, 5, 1); // may 1st
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
bool isCurrentlyDaylightSavings = tzi.IsDaylightSavingTime(theDate);
int timezone;
TimeZoneInfo localZone = TimeZoneInfo.Local;
DateTime myTime = DateTime.Now;
bool isDayLight = TimeZoneInfo.Local.IsDaylightSavingTime(myTime);
if (isDayLight)
timezone = Math.Abs(localZone.BaseUtcOffset.Hours) + 1;
else
timezone = Math.Abs(localZone.BaseUtcOffset.Hours);
Debug.WriteLine("timezone is " + timezone);
我只是找到了当前时间,如果它处于夏令时期间,则将+1添加到GMT偏移量。
这适用于Visual Studio Express 2013。
重要
myDateTime.IsDaylightSavingTime确实返回正确的值...但是...它准确到至少一天的小时,仅传递日期是不够的。
例如今年(2019年)3/10/2019 02:00:00作为myDateTime传递将返回false,但3/10/2019 03:00:00将返回true。
这是我的简短解决方案,可在所有时区使用:
DateTime utcTime = DateTime.Parse("30.10.2018 18:21:34")
DateTime localtime = ConvertUTCToLocalTime(utcTime);
public static DateTime ConvertUTCToLocalTime(DateTime UTCTime)
{
var localZone = TimeZone.CurrentTimeZone;
var offset = localZone.GetUtcOffset(UTCTime);
var localTime = UTCTime.AddHours(offset.Hours);
return localTime;
}
基于上面提供的其他代码,我编写了一个完整的代码来运行和进行测试。变量cityTz接收一个IANA时区名称示例。 IANA时区模式在Mac和Linux中使用(Windows使用不同的时区样式)。 在2020年,纽约的夏令时(DST)将于11月01日结束。 如果您测试下面的代码,则返回值将为FALSE,因为“theDate”是11月02日,即DST结束后的第二天。 但是,如果您更改注释行并将theDate设置为11月01日(NY的最后一个DST日期),则返回值将为TRUE。
您可以在Mac或Linux终端中键入以下内容来编译此程序:
csc testDST.cs
运行您的程序:
mono testDST.exe
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
class Program{
static void Main(string[] args)
{
string cityTz;
//cityTz = "America/Sao_Paulo";
cityTz = "America/New_York";
//DateTime theDate = new DateTime(2020, 11, 1); //returns TRUE
DateTime theDate = new DateTime(2020, 11, 2); //returns FALSE
Console.WriteLine("Data: "+theDate);
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(cityTz);
bool isDaylight = tzi.IsDaylightSavingTime(theDate);
Console.WriteLine("isDaylight this date in "+ cityTz +"?: "+ isDaylight);
}
}
你需要做两件事:
IsAmbiguous
IsDaylightSavingTime
if (TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) ||
TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate))
Console.WriteLine("{0} may be daylight saving time in {1}.", unclearDate, TimeZoneInfo.Local.DisplayName);
https://msdn.microsoft.com/en-us/library/bb460642(v=vs.110).aspx
IsAmbiguous
? - Kiquenet
new DateTime(1995, 10, 15)
中,“E.南美洲标准时间”不能很好地工作?您可以在https://dotnetfiddle.net/ilwIZu上测试,也可以在https://pt.m.wikipedia.org/wiki/Lista_de_períodos_em_que_vigorou_o_horário_de_verão_no_Brasil的文档历史记录中查看。1995-10-15确实是夏令时。 - Alexsandro