检查夏令时是否有效?

时间:2012-05-19 13:43:54

标签: c# windows-phone-7 datetime dst

如何检查丹麦白天节省的时间是否已经生效,如果是,那么我的数据加1小时,否则不是? 我有一个xml文件:

<day = "1"
month = "5"
sunrise ="06:30"
sunset ="21:30"
/>

8 个答案:

答案 0 :(得分:60)

认为您需要将此xml转换为DateTime,然后使用TimeZoneInfo类。

如果您当地时间丹麦:

DateTime thisTime = DateTime.Now;
bool isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(thisTime);

否则你需要获得丹麦TimeZone:

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);

答案 1 :(得分:7)

当我按上面编码时 - 对于New-York,我在调试器中发现时间设置正确(包括DST)

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;

    }

答案 2 :(得分:6)

您可以使用TimeZoneInfo.IsDaylightSavingTime

DateTime theDate = new DateTime(2012, 5, 1); // may 1st
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
bool isCurrentlyDaylightSavings = tzi.IsDaylightSavingTime(theDate);

答案 3 :(得分:0)

这是一项通用测试,如果我的数学不正确,我很乐意予以纠正。在我的情况下,我只需要获得时区的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);

我只是找到了当前的时间,如果它在Day Light Savings期间增加了+1 GMT偏移量。

这适用于Visual Studio Express 2013。

答案 4 :(得分:0)

你需要做两件事:

  1. 致电IsAmbiguous
  2. 列出项目IsDaylightSavingTime
  3. 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

答案 5 :(得分:0)

这是我可以在所有时区使用的简短解决方案:

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;
}

答案 6 :(得分:0)

重要

myDateTime.IsDaylightSavingTime确实返回正确的值...但是...至少在一天中的某个小时才准确,仅通过日期本身是不够的。

例如今年(2019)3/10/2019 02:00:00通过,因为myDateTime将返回false,但是3/10/2019 03:00:00将返回true。

答案 7 :(得分:0)

基于上面提供的其他代码,我编写了完整的代码来运行和进行测试。变量cityTz正在接收IANA时区名称示例。 IANA时区模式用于Mac和Linux(Windows使用不同的时区样式)。在2020年,纽约的夏时制(DST)将于11月1日结束。如果您测试以下代码,则返回值为FALSE,因为“ theDate”是DST结束后的第二天11月02日。但是,如果您更改注释行,并将theDate设置为11月01日(纽约的夏令时最后一个日期),则返回值为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);
    }
}
相关问题