C# 检查夏令时是否有效?
如何检查丹麦的夏令时制度是否生效,如果生效,则在我的数据中添加1小时,否则不生效? 我有一个xml文件:C# 检查夏令时是否有效?,c#,windows-phone-7,datetime,dst,C#,Windows Phone 7,Datetime,Dst,如何检查丹麦的夏令时制度是否生效,如果生效,则在我的数据中添加1小时,否则不生效? 我有一个xml文件: <day = "1" month = "5" sunrise ="06:30" sunset ="21:30" /> 您可以使用 您认为需要将此xml转换为DateTime,然后使用TimeZoneInfo类 如果您是当地时间: DateTime thisTime = DateTime.Now; bool isDaylight = TimeZoneInfo.Local.IsD
<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);
当我按照上面的方式为纽约编码时,我在调试器中发现时间设置正确(包括DST)
这是一个普通的测试,如果我的数学不正确,很高兴被纠正。在我的例子中,我只需要得到时区的GMT偏移量,不管它在世界上的什么地方
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);
我只是简单地找到了当前时间,如果它是在白天,那么在GMT偏移量上加上+1
这适用于Visual Studio Express 2013。您需要做两件事:
IsAmbiguous
IsDaylightSavingTime
if(TimeZoneInfo.Local.isambiguustime(unclearDate)|
TimeZoneInfo.Local.IsDaylightSavingTime(未清除))
WriteLine(“{0}可能是{1}”中的夏令时,unclearDate,TimeZoneInfo.Local.DisplayName)代码>
这是我的简短解决方案,可以在所有时区使用:
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;
}
重要的
myDateTime.IsDaylightSavingTime确实返回正确的值。。。但是它至少精确到一天中的某个小时,仅仅传递日期是不够的
例如,今年(2019年)3/10/2019 02:00:00作为myDateTime传递将返回false,但3/10/2019 03:00:00将返回true。基于上面提供的其他代码,我编写了一个完整的代码来运行和进行测试。变量cityTz正在接收IANA时区名称示例。IANA时区模式在Mac和Linux中使用(Windows使用不同的时区样式)。2020年,纽约的夏时制(DST)将于11月1日结束。如果测试下面的代码,返回值将为FALSE,因为“theDate”是DST结束后的第二天11月2日。但如果您更改注释行并将日期设置为11月1日(纽约的最后一个DST日期),则返回值将为真
您可以在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);
}
}
为什么对于特定的新日期时间(1995年,10月,15日)
日期,“E.南美洲标准时间”不适用?你不能在这里测试它,在1995年10月15日的文档历史中,它真的是一天之光什么是模糊的?Kiquenet如果是真的,你不能说,只有时间,如果是DST或不是DST,时间存在于DST和标准时间中,比如说DST某处在2018年12月23日00:00结束,那么,在这个地方,2018-12-22 23:30:00将是不明确的,将有这个时间DST,在00:00,时钟延迟1小时,将再次有这个时间,现在是标准时间。什么是GetLocalDateTime
?对不起,我忘记复制这个方法了。以上编辑
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);
}
}