Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WPF/C#中显示时区。发现夏令时偏移_C#_Wpf_Datetime_Timezone - Fatal编程技术网

在WPF/C#中显示时区。发现夏令时偏移

在WPF/C#中显示时区。发现夏令时偏移,c#,wpf,datetime,timezone,C#,Wpf,Datetime,Timezone,我很难理解系统注册表如何帮助我将DateTime对象转换为相应时区。我举了一个例子,我一直在尝试反向工程,但我就是不能遵循一个关键步骤,即UTCtime根据夏令时进行偏移 我正在使用.NET3.5(感谢上帝),但它仍然让我感到困惑 谢谢 编辑:附加信息:此问题用于WPF应用程序环境。我在下面留下的代码片段进一步介绍了答案示例,以获得我想要的内容 您可以使用DateTimeOffset获取UTC偏移量,因此不需要深入注册表获取该信息 TimeZone.CurrentTimeZone返回其他时区数据

我很难理解系统注册表如何帮助我将DateTime对象转换为相应时区。我举了一个例子,我一直在尝试反向工程,但我就是不能遵循一个关键步骤,即UTCtime根据夏令时进行偏移

我正在使用.NET3.5(感谢上帝),但它仍然让我感到困惑

谢谢


编辑:附加信息:此问题用于WPF应用程序环境。我在下面留下的代码片段进一步介绍了答案示例,以获得我想要的内容

您可以使用DateTimeOffset获取UTC偏移量,因此不需要深入注册表获取该信息

TimeZone.CurrentTimeZone返回其他时区数据,TimeZoneInfo.Local具有关于时区的元数据(例如是否支持夏令时、其各种状态的名称等)

更新:我认为这明确回答了你的问题:

var tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2008, 10, 22, 13, 6, 0, tzi.BaseUtcOffset);
Console.WriteLine(dto);
Console.ReadLine();

该代码在-8偏移量处创建一个DateTime。默认安装的时区是。

这里是我在WPF应用程序中使用的C语言代码片段。这将为您提供时区id的当前时间(调整为夏令时)

// _timeZoneId is the String value found in the System Registry.
// You can look up the list of TimeZones on your system using this:
// ReadOnlyCollection<TimeZoneInfo> current = TimeZoneInfo.GetSystemTimeZones();
// As long as your _timeZoneId string is in the registry 
// the _now DateTime object will contain
// the current time (adjusted for Daylight Savings Time) for that Time Zone.
string _timeZoneId = "Pacific Standard Time";
DateTime startTime = DateTime.UtcNow;
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(_timeZoneId);
_now = TimeZoneInfo.ConvertTime(startTime, TimeZoneInfo.Utc, tst);
/\u timeZoneId是在系统注册表中找到的字符串值。
//您可以使用以下命令在系统上查找时区列表:
//ReadOnlyCollection current=TimeZoneInfo.GetSystemTimeZones();
//只要您的_timeZoneId字符串在注册表中
//_now DateTime对象将包含
//该时区的当前时间(调整为夏令时)。
字符串_timeZoneId=“太平洋标准时间”;
DateTime startTime=DateTime.UtcNow;
TimeZoneInfo tst=TimeZoneInfo.FindSystemTimeZoneById(_timeZoneId);
_现在=TimeZoneInfo.ConvertTime(startTime,TimeZoneInfo.Utc,tst);
这就是我最后得到的代码snippit。谢谢你的帮助。

/code>/C#NET
//C#.NET
    public static bool IsDaylightSavingTime()
    {
        return IsDaylightSavingTime(DateTime.Now);
    }
    public static bool IsDaylightSavingTime(DateTime timeToCheck)
    {
        bool isDST = false;
        System.Globalization.DaylightTime changes 
            = TimeZone.CurrentTimeZone.GetDaylightChanges(timeToCheck.Year);
        if (timeToCheck >= changes.Start && timeToCheck <= changes.End)
        {
            isDST = true;
        }
        return isDST;
    }


'' VB.NET
Const noDate As Date = #1/1/1950#
Public Shared Function IsDaylightSavingTime( _ 
 Optional ByVal timeToCheck As Date = noDate) As Boolean
    Dim isDST As Boolean = False
    If timeToCheck = noDate Then timeToCheck = Date.Now
    Dim changes As DaylightTime = TimeZone.CurrentTimeZone _
         .GetDaylightChanges(timeToCheck.Year)
    If timeToCheck >= changes.Start And timeToCheck <= changes.End Then
        isDST = True
    End If
    Return isDST
End Function
公共静态bool是daylightsavingtime() { 返回IsDaylightSavingTime(DateTime.Now); } 公共静态布尔值为DaylightSavingTime(DateTime时间检查) { bool-isDST=false; System.Globalization.DaylightTime变化 =时区.CurrentTimeZone.GetDaylightChanges(timeToCheck.Year);
如果(timeToCheck>=changes.Start&&timeToCheck=changes.Start和timeToCheck DateTimeOffset、TimeZone和TimeZoneInfo是3.0中可用的值类型,抱歉,我的答案中不太清楚。如果创建一个新的DateTime对象,你能设置它的时区吗?这是我大脑受到伤害的地方。这是我的理解,可能不正确TimeZone.CurrentTimeZone返回本地计算机的时区信息,但无法将其设置为其他区域。请转到MSDN进行更深入的挖掘。谢谢,您可以,我已使用代码更新了答案,我在其中验证了这一点。“该代码创建了日期时间…”应为“该代码创建了日期时间偏移…”此外,此解决方案不考虑夏令时。TimeZoneInfo.ConvertTime将检查时间是否在DST范围内并自动调整。我想我应该指出,这与WPF无关,因此您可能希望将其从标题/说明/标记中删除。实际上,我的问题是用于WPF应用程序的从我下面的回答中可以看出,虽然是.NET代码,但WPF是最终用途,因此我在下面留下的代码片段是WPF的C#-1。请改进标题,其中50%是多余的。每个so问题都可能以“任何人使用过X?”开头。接受的答案是:“是的,有些人确实使用过它。”