वहाँ Hello_। यह बहुत देर हो सकती है लेकिन मैं यहां इस कोड के लिए इस्तेमाल किया गया कोड पोस्ट करूंगा। यह संभवतः इसे लागू करने के लिए किसी के समय को सुरक्षित कर सकता है। मैंने वास्तव में लिंक की मदद से यह किया है @ jamiegs उत्तर है।
public static DateTime? GetNextTransition(DateTime asOfTime, TimeZoneInfo timeZone)
{
TimeZoneInfo.AdjustmentRule[] adjustments = timeZone.GetAdjustmentRules();
if (adjustments.Length == 0)
{
// if no adjustment then no transition date exists
return null;
}
int year = asOfTime.Year;
TimeZoneInfo.AdjustmentRule adjustment = null;
foreach (TimeZoneInfo.AdjustmentRule adj in adjustments)
{
// Determine if this adjustment rule covers year desired
if (adj.DateStart.Year <= year && adj.DateEnd.Year >= year)
{
adjustment = adj;
break;
}
}
if (adjustment == null)
{
// no adjustment found so no transition date exists in the range
return null;
}
DateTime dtAdjustmentStart = GetAdjustmentDate(adjustment.DaylightTransitionStart, year);
DateTime dtAdjustmentEnd = GetAdjustmentDate(adjustment.DaylightTransitionEnd, year);
if (dtAdjustmentStart >= asOfTime)
{
// if adjusment start date is greater than asOfTime date then this should be the next transition date
return dtAdjustmentStart;
}
else if (dtAdjustmentEnd >= asOfTime)
{
// otherwise adjustment end date should be the next transition date
return dtAdjustmentEnd;
}
else
{
// then it should be the next year's DaylightTransitionStart
year++;
foreach (TimeZoneInfo.AdjustmentRule adj in adjustments)
{
// Determine if this adjustment rule covers year desired
if (adj.DateStart.Year <= year && adj.DateEnd.Year >= year)
{
adjustment = adj;
break;
}
}
dtAdjustmentStart = GetAdjustmentDate(adjustment.DaylightTransitionStart, year);
return dtAdjustmentStart;
}
}
public static DateTime GetAdjustmentDate(TimeZoneInfo.TransitionTime transitionTime, int year)
{
if (transitionTime.IsFixedDateRule)
{
return new DateTime(year, transitionTime.Month, transitionTime.Day);
}
else
{
// For non-fixed date rules, get local calendar
Calendar cal = CultureInfo.CurrentCulture.Calendar;
// Get first day of week for transition
// For example, the 3rd week starts no earlier than the 15th of the month
int startOfWeek = transitionTime.Week * 7 - 6;
// What day of the week does the month start on?
int firstDayOfWeek = (int)cal.GetDayOfWeek(new DateTime(year, transitionTime.Month, 1));
// Determine how much start date has to be adjusted
int transitionDay;
int changeDayOfWeek = (int)transitionTime.DayOfWeek;
if (firstDayOfWeek <= changeDayOfWeek)
transitionDay = startOfWeek + (changeDayOfWeek - firstDayOfWeek);
else
transitionDay = startOfWeek + (7 - firstDayOfWeek + changeDayOfWeek);
// Adjust for months with no fifth week
if (transitionDay > cal.GetDaysInMonth(year, transitionTime.Month))
transitionDay -= 7;
return new DateTime(year, transitionTime.Month, transitionDay, transitionTime.TimeOfDay.Hour, transitionTime.TimeOfDay.Minute, transitionTime.TimeOfDay.Second);
}
}
नमूना उपयोग इस तरह दिखेगा:
// This should give you DateTime object for date 26 March 2017
// because this date is first transition date after 1 January 2017 for Central Europe Standard Time zone
DateTime nextTransitionDate = GetNextTransition(new DateTime(2017, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time"))
आप कोड है कि मैं here के साथ खेला पा सकते हैं।
डीएसटी एक बहुत ही जानवर है ... ध्यान रखें कि एरिज़ोना के बहुमत (कुछ भारतीय आरक्षण सम्मान डीएसटी) डीएसटी का सम्मान नहीं करते हैं। हमारा टाइमज़ोन एमएसटी बनी हुई है और इस प्रकार हम अपने समय को समायोजित नहीं करते हैं ... जबकि डेनवर एमडीटी में संक्रमण करेगा और अपना समय समायोजित करेगा। –
नोडा समय का उपयोग करें और देखें [यह धागा] (http://stackoverflow.com/questions/24373618/getting-daylight-savings-time-start-and-end-in-nodatime) इसका उपयोग करके संक्रमण समय कैसे प्राप्त करें। – Azimuth