有没有办法用 UTC 检查它是否是 DST(夏令时)?
Is there a way to check if it's DST (Daylight Saving Time) with UTC?
有没有办法在不使用转换的情况下检查它是否是带 UTC 的 DST(夏令时)?
我不想使用转换,因为它在 10 月 28 日凌晨 2 点不明确。这个:
using System;
namespace Rextester
{
public class Program
{
public static void PrintSeasonKindTime(DateTime utcDate)
{
// Convert dt to Romance Standard Time
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
DateTime localDate = TimeZoneInfo.ConvertTime(utcDate, tzi);
Console.WriteLine("Local date: " + localDate.ToString("yyyy.MM.d HH:mm:ss") + (tzi.IsDaylightSavingTime(localDate) ? " is summer" : " is winter"));
}
public static void Main(string[] args)
{
DateTime currentUTCDateTime = new DateTime(2018,10,27,23,59,0, DateTimeKind.Utc);
double nbMinutes = 1.0;
PrintSeasonKindTime(currentUTCDateTime);
PrintSeasonKindTime(currentUTCDateTime.AddMinutes(nbMinutes));
}
}
}
会显示这个:
Local date: 2018.10.28 01:59:00 is summer
Local date: 2018.10.28 02:00:00 is winter
虽然我希望显示如下:
Local date: 2018.10.28 01:59:00 is summer
Local date: 2018.10.28 02:00:00 is summer
由于时间更改为 2018.10.28 03:00:00 指定时区的当地时间而不是凌晨 2 点(请参阅此处 enter link description here)。
但是,该行为是 "ambiguously" 正确的,因为它是 10 月 28 日凌晨 2 点的两倍;一次在 00:00 UTC(夏令时凌晨 2 点),一次在凌晨 1 点(冬令时凌晨 2 点)。你有什么想法吗?
不可能从 UTC 获取夏令时,因为它是 UTC - 检查 UTC 描述 here
您需要当前时区。有了这些信息,您可以使用 TimeZoneInfo.IsDaylightSavingTime 来确定当前时区是否在夏令时。
如果您在服务器端(例如网络)存储数据,您应该始终尝试获取用户时区并转换为 UTC。
只需使用 TimeZoneInfo.IsDaylightSavingTime
,传入您的 UTC DateTime
值。这将 有效地 将该 UTC 值转换为给定时区并检查结果是否采用夏令时,尽管它是否实际执行该转换或只是检查它是否 是否在夏令时是另一回事。
请注意,这绝不会含糊不清,因为 UTC 值绝不会含糊不清。
TimeZoneInfo.IsDaylightSavingTime
returns true
在您感兴趣的时区经过 2018-10-28T00:00:00Z 时。
有没有办法在不使用转换的情况下检查它是否是带 UTC 的 DST(夏令时)?
我不想使用转换,因为它在 10 月 28 日凌晨 2 点不明确。这个:
using System;
namespace Rextester
{
public class Program
{
public static void PrintSeasonKindTime(DateTime utcDate)
{
// Convert dt to Romance Standard Time
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
DateTime localDate = TimeZoneInfo.ConvertTime(utcDate, tzi);
Console.WriteLine("Local date: " + localDate.ToString("yyyy.MM.d HH:mm:ss") + (tzi.IsDaylightSavingTime(localDate) ? " is summer" : " is winter"));
}
public static void Main(string[] args)
{
DateTime currentUTCDateTime = new DateTime(2018,10,27,23,59,0, DateTimeKind.Utc);
double nbMinutes = 1.0;
PrintSeasonKindTime(currentUTCDateTime);
PrintSeasonKindTime(currentUTCDateTime.AddMinutes(nbMinutes));
}
}
}
会显示这个:
Local date: 2018.10.28 01:59:00 is summer
Local date: 2018.10.28 02:00:00 is winter
虽然我希望显示如下:
Local date: 2018.10.28 01:59:00 is summer
Local date: 2018.10.28 02:00:00 is summer
由于时间更改为 2018.10.28 03:00:00 指定时区的当地时间而不是凌晨 2 点(请参阅此处 enter link description here)。
但是,该行为是 "ambiguously" 正确的,因为它是 10 月 28 日凌晨 2 点的两倍;一次在 00:00 UTC(夏令时凌晨 2 点),一次在凌晨 1 点(冬令时凌晨 2 点)。你有什么想法吗?
不可能从 UTC 获取夏令时,因为它是 UTC - 检查 UTC 描述 here
您需要当前时区。有了这些信息,您可以使用 TimeZoneInfo.IsDaylightSavingTime 来确定当前时区是否在夏令时。
如果您在服务器端(例如网络)存储数据,您应该始终尝试获取用户时区并转换为 UTC。
只需使用 TimeZoneInfo.IsDaylightSavingTime
,传入您的 UTC DateTime
值。这将 有效地 将该 UTC 值转换为给定时区并检查结果是否采用夏令时,尽管它是否实际执行该转换或只是检查它是否 是否在夏令时是另一回事。
请注意,这绝不会含糊不清,因为 UTC 值绝不会含糊不清。
TimeZoneInfo.IsDaylightSavingTime
returns true
在您感兴趣的时区经过 2018-10-28T00:00:00Z 时。