2011-07-27 12 views
5

के लिए काम करता है मैंने प्रोग्रामिंग रूप से मेरी मशीन पर टाइमज़ोन सेट करने के लिए निम्न कोड लिखा है। यह ठीक काम करता है अगर मैं सकारात्मक यूटीसी समय का उपयोग करता हूं, उदाहरण के लिए न्यूज़ीलैंड मानक समय। यदि मैं एक नकारात्मक यूटीसी समय का उपयोग करता हूं, जैसे माउंटेन स्टैंडर्ड टाइम, कोड त्रुटियों के बिना चलता है, लेकिन टाइमज़ोन अंतर्राष्ट्रीय दिनांक रेखा पश्चिम (-12: 00) पर सेट है।प्रोग्रामिंग समय-समय पर सेट करना + यूटीसी टाइमज़ोन

क्या मुझे कुछ याद आया?

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
public struct TimeZoneInformation 
{ 
    public int Bias; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
    public string StandardName; 
    public SystemTime StandardDate; 
    public int StandardBias; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
    public string DaylightName; 
    public SystemTime DaylightDate; 
    public int DaylightBias; 

    public static TimeZoneInformation FromTimeZoneInfo(TimeZoneInfo timeZoneInfo) 
    { 
     var timeZoneInformation = new TimeZoneInformation(); 

     timeZoneInformation.StandardName = timeZoneInfo.StandardName; 
     timeZoneInformation.DaylightName = timeZoneInfo.DaylightName; 

     var timeZoneRegistryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\" + timeZoneInfo.Id; 
     var tzi = (byte[])Microsoft.Win32.Registry.GetValue(timeZoneRegistryPath, "TZI", new byte[] {}); 

     if (tzi == null || tzi.Length != 44) 
     { 
      throw new ArgumentException("Invalid REG_TZI_FORMAT"); 
     } 

     timeZoneInformation.Bias = BitConverter.ToInt32(tzi, 0); 
     timeZoneInformation.StandardBias = BitConverter.ToInt32(tzi, 4); 
     timeZoneInformation.DaylightBias = BitConverter.ToInt32(tzi, 8); 
     timeZoneInformation.StandardDate.Year = BitConverter.ToInt16(tzi, 12); 
     timeZoneInformation.StandardDate.Month = BitConverter.ToInt16(tzi, 14); 
     timeZoneInformation.StandardDate.DayOfWeek = BitConverter.ToInt16(tzi, 0x10); 
     timeZoneInformation.StandardDate.Day = BitConverter.ToInt16(tzi, 0x12); 
     timeZoneInformation.StandardDate.Hour = BitConverter.ToInt16(tzi, 20); 
     timeZoneInformation.StandardDate.Minute = BitConverter.ToInt16(tzi, 0x16); 
     timeZoneInformation.StandardDate.Second = BitConverter.ToInt16(tzi, 0x18); 
     timeZoneInformation.StandardDate.Millisecond = BitConverter.ToInt16(tzi, 0x1a); 
     timeZoneInformation.DaylightDate.Year = BitConverter.ToInt16(tzi, 0x1c); 
     timeZoneInformation.DaylightDate.Month = BitConverter.ToInt16(tzi, 30); 
     timeZoneInformation.DaylightDate.DayOfWeek = BitConverter.ToInt16(tzi, 0x20); 
     timeZoneInformation.DaylightDate.Day = BitConverter.ToInt16(tzi, 0x22); 
     timeZoneInformation.DaylightDate.Hour = BitConverter.ToInt16(tzi, 0x24); 
     timeZoneInformation.DaylightDate.Minute = BitConverter.ToInt16(tzi, 0x26); 
     timeZoneInformation.DaylightDate.Second = BitConverter.ToInt16(tzi, 40); 
     timeZoneInformation.DaylightDate.Millisecond = BitConverter.ToInt16(tzi, 0x2a); 

     return timeZoneInformation; 
    } 
} 

[DllImport("kernel32.dll", SetLastError = true)] 
public static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation timeZoneInformation); 

var t = TimeZoneInformation.FromTimeZoneInfo(TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time")); 
SetTimeZoneInformation(ref t); 
+2

ओह डॉन - मैंने गलती से बाईस और स्टैंडर्डबायस फ़ील्ड को मेरे कोड में इंट्स की बजाय लंबे समय तक लिखा था। सीएलआर में 64 बिट्स की लम्बाई मेरी संरचना के लेआउट को गड़बड़ कर रही है। ऐसा लगता है कि मैं अपने प्रश्न का उत्तर देने में कामयाब रहा। – Nathanael

+3

आपको उत्तर को उत्तर के रूप में पोस्ट करना चाहिए और इसे स्वीकार करना चाहिए। –

उत्तर

7

public struct TimeZoneinformation में मैं Bias और StandardBiaslong बजाय int के रूप में परिभाषित:

यहाँ कोड मैं का उपयोग कर रहा है।

Long सीएलआर में हमेशा 64 बिट मान होता है, सी ++ के विपरीत जहां यह आमतौर पर 32 बिट नहीं होता है। इसने मेरी संरचना के आकार को कुल 64 बिट्स से बढ़ा दिया और देशी कोड को उनके द्वारा देखे गए मूल्यों को गलत तरीके से परिभाषित करने का कारण बना दिया। यह पूरी तरह से दुर्घटना से था कि + यूटीसी टाइमज़ोन काम किया।

मैंने उपरोक्त कोड को सही किया है, और यदि कोई दिलचस्पी लेता है तो यह सफलतापूर्वक टाइमज़ोन सेट करता है।

संबंधित मुद्दे