我想出了一种方法,可以获取指定时区信息下一年时间间隔内需要考虑的偏移量。我将这个集合传递给数据库,以过滤保存在UTC中的日期时间字段,用于区间筛选输入(如“1am - 2am”)。我已经在系统中的所有时区进行了测试,效果良好。尽管这不是我的原始问题的答案,因为我仍然使用调整规则来获取偏移量,但我试图使其可用。
class Program
{
static void Main(string[] args)
{
foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
{
var result = GetUTCOffsetsByUTCIntervals(1900, 2012, tz);
Console.WriteLine(tz.DisplayName);
foreach (var tuple in result)
{
Console.WriteLine(tuple.Item1 + " " + tuple.Item2 + " " + tuple.Item3);
}
Console.WriteLine("------------------------------------------------------------");
}
Console.Read();
}
public static List<Tuple<TimeSpan, DateTime, DateTime>> GetUTCOffsetsByUTCIntervals(int stYear, int endYear, TimeZoneInfo tz)
{
var cal = CultureInfo.CurrentCulture.Calendar;
var offsetsByUTCIntervals = new List<Tuple<TimeSpan, DateTime, DateTime>>();
var adjRules = tz.GetAdjustmentRules();
for (var year = stYear; year <= endYear && year < DateTime.MaxValue.Year && year >= DateTime.MinValue.Year; year++)
{
var adjRule =
adjRules.FirstOrDefault(
rule =>
rule.DateStart.Year == year || rule.DateEnd.Year == year ||
(rule.DateStart.Year < year && rule.DateEnd.Year > year));
var yrStTime = new DateTime(year, 1, 1);
var yrEndTime = yrStTime.AddYears(1).AddTicks(-1);
if (adjRule != null)
{
var tStDate = GetTransitionDate(adjRule.DaylightTransitionStart, year);
var tEnddate = GetTransitionDate(adjRule.DaylightTransitionEnd, year);
var stTsp = adjRule.DaylightTransitionStart.TimeOfDay.TimeOfDay;
var endTsp = adjRule.DaylightTransitionEnd.TimeOfDay.TimeOfDay;
if (yrStTime.Date == tStDate && yrStTime.TimeOfDay == stTsp)
yrStTime = yrStTime.Add(adjRule.DaylightDelta);
if (yrEndTime.Date == tEnddate && yrEndTime.TimeOfDay == endTsp)
yrEndTime = yrEndTime.Subtract(adjRule.DaylightDelta);
if (tStDate.Month > tEnddate.Month)
{
offsetsByUTCIntervals.Add(new Tuple<TimeSpan, DateTime, DateTime>(tz.BaseUtcOffset + adjRule.DaylightDelta, ConvertTimeToUtc(yrStTime, tz), ConvertTimeToUtc(tEnddate.AddTicks(endTsp.Ticks - 1), tz)));
offsetsByUTCIntervals.Add(new Tuple<TimeSpan, DateTime, DateTime>(tz.BaseUtcOffset, ConvertTimeToUtc(tEnddate.Add(endTsp.Subtract(adjRule.DaylightDelta)), tz), ConvertTimeToUtc(tStDate.AddTicks(stTsp.Ticks - 1), tz)));
offsetsByUTCIntervals.Add(new Tuple<TimeSpan, DateTime, DateTime>(tz.BaseUtcOffset + adjRule.DaylightDelta, ConvertTimeToUtc(tStDate.Add(stTsp.Add(adjRule.DaylightDelta)), tz), ConvertTimeToUtc(yrEndTime, tz)));
}
else
{
offsetsByUTCIntervals.Add(new Tuple<TimeSpan, DateTime, DateTime>(tz.BaseUtcOffset, ConvertTimeToUtc(yrStTime, tz), ConvertTimeToUtc(tStDate.AddTicks(stTsp.Ticks - 1), tz)));
offsetsByUTCIntervals.Add(new Tuple<TimeSpan, DateTime, DateTime>(tz.BaseUtcOffset + adjRule.DaylightDelta, ConvertTimeToUtc(tStDate.Add(stTsp.Add(adjRule.DaylightDelta)), tz), ConvertTimeToUtc(tEnddate.AddTicks(endTsp.Ticks - 1), tz)));
offsetsByUTCIntervals.Add(new Tuple<TimeSpan, DateTime, DateTime>(tz.BaseUtcOffset, ConvertTimeToUtc(tEnddate.Add(endTsp.Subtract(adjRule.DaylightDelta)), tz), ConvertTimeToUtc(yrEndTime, tz)));
}
}
else
{
offsetsByUTCIntervals.Add(new Tuple<TimeSpan, DateTime, DateTime>(tz.BaseUtcOffset, ConvertTimeToUtc(yrStTime, tz), ConvertTimeToUtc(yrEndTime, tz)));
}
}
return offsetsByUTCIntervals;
}
public static DateTime ConvertTimeToUtc(DateTime date, TimeZoneInfo timeZone)
{
if (date == null || timeZone == null)
{
return date;
}
DateTime convertedDate = TimeZoneInfo.ConvertTimeToUtc(date, timeZone);
return convertedDate;
}
private static DateTime GetTransitionDate(TimeZoneInfo.TransitionTime transition, int year)
{
if (transition.IsFixedDateRule)
return new DateTime(year, transition.Month, transition.Day);
int transitionDay;
var cal = CultureInfo.CurrentCulture.Calendar;
var startOfWeek = transition.Week * 7 - 6;
var firstDayOfWeek = (int)cal.GetDayOfWeek(new DateTime(year, transition.Month, 1));
var changeDayOfWeek = (int)transition.DayOfWeek;
if (firstDayOfWeek <= changeDayOfWeek)
transitionDay = startOfWeek + (changeDayOfWeek - firstDayOfWeek);
else
transitionDay = startOfWeek + (7 - firstDayOfWeek + changeDayOfWeek);
if (transitionDay > cal.GetDaysInMonth(year, transition.Month))
transitionDay -= 7;
return new DateTime(year, transition.Month, transitionDay);
}
}
p1.Start < p2.End && p2.Start < p1.End
。使用<=
进行测试。 - Rawling