के बाद भी देखा है कि काम WP7/सिल्वरलाइट ढांचे की मदद से archieved नहीं किया जा सकता हो जाएगा, मैं एक छोटे से सहायक ने लिखा है कि काम करता है:
public static class DateTimeHelper
{
/// <summary>
/// Tries to parse the given datetime string that is not annotated with a timezone
/// information but known to be in the CET/CEST zone and returns a DateTime struct
/// in UTC (so it can be converted to the devices local time). If it could not be
/// parsed, result contains the current date/time in UTC.
/// </summary>
public static bool TryParseCetCest(string s, string format, IFormatProvider provider, DateTimeStyles style, out DateTime result)
{
// Parse datetime, knowing it is in CET/CEST timezone. Parse as universal as we fix it afterwards
if (!DateTime.TryParseExact(s, format, provider, style, out result))
{
result = DateTime.UtcNow;
return false;
}
result = DateTime.SpecifyKind(result, DateTimeKind.Utc);
// The boundaries of the daylight saving time period in CET and CEST (_not_ in UTC!)
// Both DateTime structs are of kind 'Utc', to be able to compare them with the parsing result
DateTime DstStart = LastSundayOf(result.Year, 3).AddHours(2);
DateTime DstEnd = LastSundayOf(result.Year, 10).AddHours(3);
// Are we inside the daylight saving time period?
if (DstStart.CompareTo(result) <= 0 && result.CompareTo(DstEnd) < 0)
result = result.AddHours(-2); // CEST = UTC+2h
else
result = result.AddHours(-1); // CET = UTC+1h
return true;
}
/// <summary>
/// Returns the last sunday of the given month and year in UTC
/// </summary>
private static DateTime LastSundayOf(int year, int month)
{
DateTime firstOfNextMonth = new DateTime(year, month + 1, 1, 0, 0, 0, DateTimeKind.Utc);
return firstOfNextMonth.AddDays(firstOfNextMonth.DayOfWeek == DayOfWeek.Sunday ? -7 :
(-1 * (int)firstOfNextMonth.DayOfWeek));
}
}
चाल थी यह, बिना पार्स करने के लिए DateTimeStyles.AssumeUniversal ध्वज (इस TryParseExact
मान तारीख UTC और तारीख लौटने परिवर्तित/स्थानीय करने के लिए समायोजित है बनाता है) यूटीसी के रूप में यह respecifying और फिर मैन्युअल रूप से actu को समायोजित करें अल यूटीसी समकक्ष।
यह डीएसटी नियमों का पालन करता है जो here पाया जा सकता है। मैंने इसे डेलाइट सेविंग टाइम के प्रारंभ/अंत के ठीक पहले/बाद के सभी 4 सीमा मामलों के साथ परीक्षण किया। इससे फिर से परीक्षण का महत्व दिखाया गया: मुझे ऑपरेटर को DstStart.CompareTo(result) < 0
से <=
में सही परिणाम देने के लिए बदलना पड़ा।
मुझे यह महसूस हो रहा था कि मैं यहां पहिया को पुनर्निर्मित कर रहा हूं (जिसे मैं नफरत करता हूं), लेकिन इस सरल नौकरी के लिए समर्पित पुस्तकालय का उपयोग नहीं करना चाहता था। मैंने नोडा टाइम पर एक नज़र डाली जो कि एक महान परियोजना है, लेकिन मुझे लगता है कि इसके लिए यह जरूरी नहीं है।
मुझे आशा है कि मैं इस छोटे से सहायक के साथ किसी को थोडा समय बचा सकता हूं। यह जानबूझकर सभी समय क्षेत्रों के लिए सामान्य नहीं है (यदि आपको इसके बजाय नोडा टाइम की तरह एक lib का उपयोग करने की आवश्यकता है), लेकिन इन मामलों के लिए जिसमें आपके पास एक निश्चित एकल समय क्षेत्र है, जैसे कि मेरे मामले में।
संभावित डुप्लिकेट [सी # एफएक्स 3.5 में एक विशिष्ट समय क्षेत्र में डेटटाइम बनाना] (http://stackoverflow.com/questions/246498/creating-a-datetime-in-a- विशिष्ट- टाइम- ज़ोन-in -सी-एफएक्स -3-5) –
@ माइकल हरेन: नहीं, यह एक डुप्लिकेट नहीं है। मैंने इस सवाल से पहले यह देखा था और इससे मेरी विशिष्ट समस्या में मदद नहीं मिली। –
शायद डुप्लिकेट नहीं है लेकिन मुझे लगा कि यह सहायक होगा –