- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用以下代码显示 2005 年到 2035 年之间的夏令时。
对于 2005 年,这 page显示 DST 在 4 月 3 日和 10 月 30 日之间。但是 GetDaylightChanges 会在 3 月 13 日和 11 月 6 日返回。
.NET GetDaylightChanges 函数可靠吗?
public static void GetCurrentTimeZone()
{
for (int i = 0; i < 30; i++)
{
var dlt = TimeZone.CurrentTimeZone.GetDaylightChanges(2005 + i);
Console.WriteLine(2005 + i);
Console.WriteLine(dlt.Start.ToLongDateString());
Console.WriteLine(dlt.End.ToLongDateString());
Console.WriteLine(" ");
}
}
最佳答案
从 1987 年到 2006 年,规则是:DST 从 4 月的第一个星期日到 10 月的最后一个星期日有效。
从 2007 年至今,规则是:DST 从 3 月的第二个星期日到 11 月的第一个星期日有效。
但是,正如@MaheshKava 所指出的,GetDaylightChanges API 的备注部分是这样说的:
Because the TimeZone class supports only one daylight saving time adjustment rule, the GetDaylightChanges method applies the current adjustment rule to any year, regardless of whether the adjustment rule actually applies to that year.
这意味着当前规则(3 月的第二个星期日到 11 月的第一个星期日)适用于所有年份,无论该规则在该年是否实际生效。因此,总而言之,GetDaylightChanges 将为您提供 2007 年之前任何年份的不准确结果,显然这是设计使然。
API 文档进一步说明您可以使用 TimeZoneInfo.GetAdjustmentRules 获取更准确的信息。我对此很好奇,所以我写了这段代码:
static void Main(string[] args)
{
PrintAllDaylightSavingsAdjustmentDates();
Console.ReadLine();
}
public static void PrintAllDaylightSavingsAdjustmentDates()
{
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
TimeZoneInfo.AdjustmentRule[] adjustmentRules = timeZoneInfo.GetAdjustmentRules();
for (int year = 2000; year < 2030; year++)
{
PrintDaylightSavingsAdjustmentDatesForYear(adjustmentRules, year);
}
}
public static void PrintDaylightSavingsAdjustmentDatesForYear
(
TimeZoneInfo.AdjustmentRule[] adjustmentRules,
int year
)
{
DateTime firstOfYear = new DateTime(year, 1, 1);
foreach (TimeZoneInfo.AdjustmentRule adjustmentRule in adjustmentRules)
{
if ((adjustmentRule.DateStart <= firstOfYear) && (firstOfYear <= adjustmentRule.DateEnd))
{
Console.WriteLine("In {0}, DST started on {1} and ended on {2}.",
year,
GetTransitionDate(adjustmentRule.DaylightTransitionStart, year).ToString("MMMM dd"),
GetTransitionDate(adjustmentRule.DaylightTransitionEnd, year).ToString("MMMM dd"));
}
}
}
public static DateTime GetTransitionDate
(
TimeZoneInfo.TransitionTime transitionTime,
int year
)
{
if (transitionTime.IsFixedDateRule)
{
return new DateTime(year, transitionTime.Month, transitionTime.Day);
}
else
{
if (transitionTime.Week == 5)
{
// Special value meaning the last DayOfWeek (e.g., Sunday) in the month.
DateTime transitionDate = new DateTime(year, transitionTime.Month, 1);
transitionDate = transitionDate.AddMonths(1);
transitionDate = transitionDate.AddDays(-1);
while (transitionDate.DayOfWeek != transitionTime.DayOfWeek)
{
transitionDate = transitionDate.AddDays(-1);
}
return transitionDate;
}
else
{
DateTime transitionDate = new DateTime(year, transitionTime.Month, 1);
transitionDate = transitionDate.AddDays(-1);
for (int howManyWeeks = 0; howManyWeeks < transitionTime.Week; howManyWeeks++)
{
transitionDate = transitionDate.AddDays(1);
while (transitionDate.DayOfWeek != transitionTime.DayOfWeek)
{
transitionDate = transitionDate.AddDays(1);
}
}
return transitionDate;
}
}
}
输出如下:
In 2000, DST started on April 02 and ended on October 29.
In 2001, DST started on April 01 and ended on October 28.
In 2002, DST started on April 07 and ended on October 27.
In 2003, DST started on April 06 and ended on October 26.
In 2004, DST started on April 04 and ended on October 31.
In 2005, DST started on April 03 and ended on October 30.
In 2006, DST started on April 02 and ended on October 29.
In 2007, DST started on March 11 and ended on November 04.
In 2008, DST started on March 09 and ended on November 02.
In 2009, DST started on March 08 and ended on November 01.
In 2010, DST started on March 14 and ended on November 07.
In 2011, DST started on March 13 and ended on November 06.
In 2012, DST started on March 11 and ended on November 04.
In 2013, DST started on March 10 and ended on November 03.
In 2014, DST started on March 09 and ended on November 02.
In 2015, DST started on March 08 and ended on November 01.
In 2016, DST started on March 13 and ended on November 06.
In 2017, DST started on March 12 and ended on November 05.
In 2018, DST started on March 11 and ended on November 04.
In 2019, DST started on March 10 and ended on November 03.
In 2020, DST started on March 08 and ended on November 01.
In 2021, DST started on March 14 and ended on November 07.
In 2022, DST started on March 13 and ended on November 06.
In 2023, DST started on March 12 and ended on November 05.
In 2024, DST started on March 10 and ended on November 03.
In 2025, DST started on March 09 and ended on November 02.
In 2026, DST started on March 08 and ended on November 01.
In 2027, DST started on March 14 and ended on November 07.
In 2028, DST started on March 12 and ended on November 05.
In 2029, DST started on March 11 and ended on November 04.
关于c# - .NET TimeZone.CurrentTimeZone.GetDaylightChanges 返回错误的 2005 夏令时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29981072/
我正在使用 TimeZone.CurrentTimeZone 来获取用户与 UTC 的时间偏移量,如下所示: TimeZone zone = TimeZone.CurrentTimeZone; Tim
如果在 ASP.NET 应用程序中运行,TimeZone.CurrentTimeZone 如何工作?它是否返回运行应用程序的应用程序池的时区?如果使用模拟,它是否返回其模拟用户的时区? 最佳答案 它是
我使用以下代码显示 2005 年到 2035 年之间的夏令时。 对于 2005 年,这 page显示 DST 在 4 月 3 日和 10 月 30 日之间。但是 GetDaylightChanges
我是一名优秀的程序员,十分优秀!