简体   繁体   English

如何为特定时区创建TimeZone对象? C#

[英]How to create a TimeZone object for a specific timezone? c#

I found the exact thing I went to know, and it is so simple (or should be): 我发现了我确切知道的事情,它很简单(或应该是):

TimeZone.GetDaylightChanges() returns exactly what I need: When the daylight time starts and ends: TimeZone.GetDaylightChanges()完全返回我需要的内容:当白天时间开始和结束时:

To that end, I would want to do: 为此,我想做:

TimeZoneInfo currentTimezone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);
TimeZone zone = new TimeZone (currentTimezone);  // Compile error here.

DaylightTime changes = zone.GetDaylightChanges();

This, of course won't compile because the constructor of the TimeZone doesn't take a TimeZoneInfo (Wtf?), and I can't figure out how to get the DaylightTime Changes any other way. 这当然不会编译,因为TimeZone的构造函数没有采用TimeZoneInfo(Wtf?),我无法弄清楚如何以任何其他方式获取DaylightTime更改。 I need for someone to set 'timeZoneName' to any timezone and be able to get the start/end DateTimes for whatever timezone they set it to. 我需要有人将'timeZoneName'设置为任何时区,并能够为他们设置的任何时区获取开始/结束日期时间。

Of Key importance: The server is running (most likely) UTC, and NOT the timezone that is in 'timeZoneName' 至关重要:服务器正在运行(最有可能)UTC,而不是'timeZoneName'中的时区

Unfortunately, you can't instantiate a new TimeZone object like that. 不幸的是,你不能像这样实例化一个新的TimeZone对象。 However, I believe you can get the info you want from the TimeZoneInfo class by looking through the AdjustmentRules . 但是,我相信您可以通过查看AdjustmentRules从TimeZoneInfo类中获取所需的信息。

Below are a few methods that I wrote to enable what you want. 下面是我编写的一些方法,可以实现您想要的功能。 See the sample usage at the bottom for how it compares to using the TimeZone method for the current system time zone. 请参阅底部的示例用法,了解它与当前系统时区使用TimeZone方法的比较。

UPDATE UPDATE

I've modified the code into a couple of classes to help out with the date math, and borrowed heavily from the sample here: https://msdn.microsoft.com/en-us/library/system.timezoneinfo.transitiontime.isfixeddaterule.aspx 我已经将代码修改为几个类来帮助处理日期数学,并从这里的样本中大量借用: https//msdn.microsoft.com/en-us/library/system.timezoneinfo.transitiontime.isfixeddaterule的.aspx

public static DaylightTime GetDaylightChanges(string timeZoneName, int year)
{
    TimeZoneInfo currentTimezone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);

    var currentRules =
        currentTimezone.GetAdjustmentRules().FirstOrDefault(rule =>
            rule.DateStart <= DateTime.Today &&
            rule.DateEnd >= DateTime.Today);

    if (currentRules != null)
    {
        var daylightStart = 
            GetTransitionDate(currentRules.DaylightTransitionStart, year);

        var daylightEnd = 
            GetTransitionDate(currentRules.DaylightTransitionEnd, year);

        return new DaylightTime(daylightStart, daylightEnd, 
            currentRules.DaylightDelta);
    }

    return null;
}

private static DateTime GetTransitionDate(TimeZoneInfo.TransitionTime transition, 
    int year)
{
    return (transition.IsFixedDateRule)
        ? new DateTime(year, transition.Month, transition.Day,
            transition.TimeOfDay.Hour, transition.TimeOfDay.Minute,
            transition.TimeOfDay.Second)
        : GetNonFixedTransitionDate(transition, year);
}

private static DateTime GetNonFixedTransitionDate(
    TimeZoneInfo.TransitionTime transition, int year)
{
    var calendar = CultureInfo.CurrentCulture.Calendar;
    int startOfWeek = transition.Week * 7 - 6;
    int firstDayOfWeek = (int) calendar.GetDayOfWeek(new DateTime(year, 
        transition.Month, 1));

    int changeDayOfWeek = (int) transition.DayOfWeek;

    int transitionDay = (firstDayOfWeek <= changeDayOfWeek) 
        ? startOfWeek + (changeDayOfWeek - firstDayOfWeek)
        : startOfWeek + (7 - firstDayOfWeek + changeDayOfWeek);

    if (transitionDay > calendar.GetDaysInMonth(year, transition.Month))
        transitionDay -= 7;

    return new DateTime(year, transition.Month, transitionDay, 
        transition.TimeOfDay.Hour, transition.TimeOfDay.Minute, 
        transition.TimeOfDay.Second);
}   

And here's and example of how to use it, compared to using the current TimeZone method: 与使用当前的TimeZone方法相比,这里是如何使用它的示例:

public static void Main()
{
    var daylightTime = GetDaylightChanges("Pacific Standard Time", DateTime.Today.Year);
    var dylightTime2 = TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Today.Year);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM