コード例 #1
0
		/// <summary>
		/// Use the Initialize method with the gmtOffset parameter
		/// to create a list of timezones which have a specific
		/// offset value.  For example, for all timezones at 
		/// GMT-5:00, pass -300 (minutes).  To get all of the time
		/// zones known to the OS, pass ALL_TIMEZONES_LIST or call
		/// the method with no parameters.
		/// </summary>
		/// <param name="gmtOffset">Offset of the target timezones
		/// from GMT (in minutes).</param>
		public void Initialize( int gmtOffset )
		{
			TimeZoneInformation mTimeZoneInformation = new OpenNETCF.Win32.TimeZoneInformation();
			// On Windows CE 3.0 other than Pocket PC, we can scan
			// the registry for a list of timezones.  

			// HKLM\Time Zones is the root.  Under this root are all
			// of the timezone entries that the system knows about.
			// The name of each key for a timezone entry is the 
			// 'standard time' description for the timezone (Eastern
			// Standard Time, for example).  Under this key are various
			// values:
			// Display - The name of the timezone for display purposes.
			// This value includes the offset from GMT, as well as the
			// name, in some form, of the timezone.
			// Dlt - The name of the timezone for the 'daylight time'
			// part of the year (Eastern Daylight Time, for example).
			// TZI - A binary structure, like TIME_ZONE_INFORMATION,
			// describing the characteristics of the timezone.

			// Scan through the registry, looking for the indicated
			// timezone offset from GMT in the display string.  When 
			// we find it, we add it to a list, since there might
			// be several matches (Arizona and normal MST would both
			// match GMT-7, for example).
			if (!System.IO.File.Exists("\\windows\\citydb.dll"))//OpenNETCF.EnvironmentEx.PlatformName.IndexOf("Pocket")==-1)
			{
				RegistryKey	baseKey;

				baseKey = Registry.LocalMachine.OpenSubKey("Time Zones", false);
				if ( baseKey != null )
				{
					// Enumerate all keys under the base timezone key.
					//string	cls;
					string[] subkeynames = baseKey.GetSubKeyNames();
					foreach(string thiskeyname in subkeynames)
					{
						// Open the enumerated key.
						RegistryKey	tzKey = baseKey.OpenSubKey(thiskeyname, false);
						if ( tzKey  != null )
						{
							// Get the display name value for the timezone.
							string			dispName;
							
							dispName = tzKey.GetValue("Display").ToString();

							if(dispName != null)
							{
								// Extract the offset from GMT from the display string.
								// The basic format of the string is:
								// (GMT-%02d:%02d) <Name>
								// The problem is when you are *at* GMT, where the sign 
								// and the numbers aren't present.
								int	hoursOffset = 999, minutesOffset = 999;

								string	hours;
								string	minutes;
								hours = dispName.Substring( 4, 3 );
								minutes = dispName.Substring( 8, 2 );

								// For the case of GMT itself, there is no
								// offset in the display name, so we
								// set the offset to zero, in that case.
								if ( ( hours[ 0 ] == '-' ) ||
									( hours[ 0 ] == '+' ) )
								{
									hoursOffset = System.Int32.Parse( hours );
									minutesOffset = System.Int32.Parse( minutes );
								}
								else
								{
									hoursOffset = 0;
									minutesOffset = 0;
								}

								// Compute the timezone's total offset in minutes
								int offset = hoursOffset * 60;
								
								if ( hoursOffset < 0 )
								{
									offset -= minutesOffset;
								}
								else
								{
									offset += minutesOffset;
								}
								
								// We now have a value which we can compare to the 
								// value that the user gave us.
								if ( ( gmtOffset == offset ) || 
									( gmtOffset == ALL_TIMEZONES_LIST ) )
								{
									// Match.  Add timezone to list.  So far, we have
									// the standard time name and the display name.  
									// We still need to get the DST name and the 
									// TIME_ZONE_INFORMATION.
									string	dstName;
									dstName = tzKey.GetValue("Dlt").ToString();

									// Read the time zone information from the 
									// registry.  Unfortunately, this is something
									// that only the Control Panel code actually 
									// knows about, so, if they change it, we break.
									TZREG					tzr = new TZREG();
									int						tzrSize = tzr.ToByteArray().Length;
									TimeZoneInformation	tzi = new TimeZoneInformation();
									byte[]					btzr;
									btzr = (byte[])tzKey.GetValue("TZI");
									tzr = new TZREG( btzr );
									tzi.Bias = tzr.Bias;
									tzi.StandardBias = tzr.StandardBias;
									tzi.DaylightBias = tzr.DaylightBias;
									tzi.StandardDate = tzr.StandardDate;
									tzi.DaylightDate = tzr.DaylightDate;

									// Don't forget to copy the standard name and
									// daylight name to the structure.
									tzi.DaylightName = dstName;
									tzi.StandardName = thiskeyname;

									// Copy the display name from the registry to
									// the class.
									tzi.DisplayName = dispName;

									this.Add( tzi );
								}
							}//endif dispname null
							// close the timezone key.
							tzKey.Close();
						}//endif tzkey null
					}//end foreach subkeynames
					// Close the key.
					baseKey.Close();
				}
			}
			else
			{
				//Pocket PC
				InitCityDb();
				ClockLoadAllTimeZoneData();
				int nZones = ClockGetNumTimezones();
				//tzData = new TZData[nZones];

				for( int i = 0; i < nZones; i++ )
				{
					IntPtr p = ClockGetTimeZoneDataByOffset(i);
					TZData tmp= new TZData(p);	
					if ( ( gmtOffset == tmp.GMTOffset ) || 
						( gmtOffset == ALL_TIMEZONES_LIST ) )
					{
						TimeZoneInformation	tzi = new TimeZoneInformation();
																								
						
						tzi.Bias = tmp.GMTOffset;
						tzi.DaylightBias =tmp.DSTOffset;
//						tzi.StandardDate = tzr.StandardDate;
//						tzi.DaylightDate = tzr.DaylightDate;


						// Don't forget to copy the standard name and
						// daylight name to the structure.
						tzi.DaylightName = tmp.DSTName;
						tzi.StandardName = tmp.Name;

						// Copy the display name from the registry to
						// the class.
						tzi.DisplayName = tmp.ShortName;																		
						this.Add(tzi);
					}				
				}
				ClockFreeAllTimeZoneData();
				UninitCityDb();
			}
		}
コード例 #2
0
        /// <summary>
        /// Use the Initialize method with the gmtOffset parameter
        /// to create a list of timezones which have a specific
        /// offset value.  For example, for all timezones at
        /// GMT-5:00, pass -300 (minutes).  To get all of the time
        /// zones known to the OS, pass ALL_TIMEZONES_LIST or call
        /// the method with no parameters.
        /// </summary>
        /// <param name="gmtOffset">Offset of the target timezones
        /// from GMT (in minutes).</param>
        public void Initialize(int gmtOffset)
        {
            TimeZoneInformation mTimeZoneInformation = new OpenNETCF.Win32.TimeZoneInformation();

            // On Windows CE 3.0 other than Pocket PC, we can scan
            // the registry for a list of timezones.

            // HKLM\Time Zones is the root.  Under this root are all
            // of the timezone entries that the system knows about.
            // The name of each key for a timezone entry is the
            // 'standard time' description for the timezone (Eastern
            // Standard Time, for example).  Under this key are various
            // values:
            // Display - The name of the timezone for display purposes.
            // This value includes the offset from GMT, as well as the
            // name, in some form, of the timezone.
            // Dlt - The name of the timezone for the 'daylight time'
            // part of the year (Eastern Daylight Time, for example).
            // TZI - A binary structure, like TIME_ZONE_INFORMATION,
            // describing the characteristics of the timezone.

            // Scan through the registry, looking for the indicated
            // timezone offset from GMT in the display string.  When
            // we find it, we add it to a list, since there might
            // be several matches (Arizona and normal MST would both
            // match GMT-7, for example).
            if (!System.IO.File.Exists("\\windows\\citydb.dll"))            //OpenNETCF.EnvironmentEx.PlatformName.IndexOf("Pocket")==-1)
            {
                RegistryKey baseKey;

                baseKey = Registry.LocalMachine.OpenSubKey("Time Zones", false);
                if (baseKey != null)
                {
                    // Enumerate all keys under the base timezone key.
                    //string	cls;
                    string[] subkeynames = baseKey.GetSubKeyNames();
                    foreach (string thiskeyname in subkeynames)
                    {
                        // Open the enumerated key.
                        RegistryKey tzKey = baseKey.OpenSubKey(thiskeyname, false);
                        if (tzKey != null)
                        {
                            // Get the display name value for the timezone.
                            string dispName;

                            dispName = tzKey.GetValue("Display").ToString();

                            if (dispName != null)
                            {
                                // Extract the offset from GMT from the display string.
                                // The basic format of the string is:
                                // (GMT-%02d:%02d) <Name>
                                // The problem is when you are *at* GMT, where the sign
                                // and the numbers aren't present.
                                int hoursOffset = 999, minutesOffset = 999;

                                string hours;
                                string minutes;
                                hours   = dispName.Substring(4, 3);
                                minutes = dispName.Substring(8, 2);

                                // For the case of GMT itself, there is no
                                // offset in the display name, so we
                                // set the offset to zero, in that case.
                                if ((hours[0] == '-') ||
                                    (hours[0] == '+'))
                                {
                                    hoursOffset   = System.Int32.Parse(hours);
                                    minutesOffset = System.Int32.Parse(minutes);
                                }
                                else
                                {
                                    hoursOffset   = 0;
                                    minutesOffset = 0;
                                }

                                // Compute the timezone's total offset in minutes
                                int offset = hoursOffset * 60;

                                if (hoursOffset < 0)
                                {
                                    offset -= minutesOffset;
                                }
                                else
                                {
                                    offset += minutesOffset;
                                }

                                // We now have a value which we can compare to the
                                // value that the user gave us.
                                if ((gmtOffset == offset) ||
                                    (gmtOffset == ALL_TIMEZONES_LIST))
                                {
                                    // Match.  Add timezone to list.  So far, we have
                                    // the standard time name and the display name.
                                    // We still need to get the DST name and the
                                    // TIME_ZONE_INFORMATION.
                                    string dstName;
                                    dstName = tzKey.GetValue("Dlt").ToString();

                                    // Read the time zone information from the
                                    // registry.  Unfortunately, this is something
                                    // that only the Control Panel code actually
                                    // knows about, so, if they change it, we break.
                                    TZREG tzr               = new TZREG();
                                    int   tzrSize           = tzr.ToByteArray().Length;
                                    TimeZoneInformation tzi = new TimeZoneInformation();
                                    byte[] btzr;
                                    btzr             = (byte[])tzKey.GetValue("TZI");
                                    tzr              = new TZREG(btzr);
                                    tzi.Bias         = tzr.Bias;
                                    tzi.StandardBias = tzr.StandardBias;
                                    tzi.DaylightBias = tzr.DaylightBias;
                                    tzi.StandardDate = tzr.StandardDate;
                                    tzi.DaylightDate = tzr.DaylightDate;

                                    // Don't forget to copy the standard name and
                                    // daylight name to the structure.
                                    tzi.DaylightName = dstName;
                                    tzi.StandardName = thiskeyname;

                                    // Copy the display name from the registry to
                                    // the class.
                                    tzi.DisplayName = dispName;

                                    this.Add(tzi);
                                }
                            }                            //endif dispname null
                            // close the timezone key.
                            tzKey.Close();
                        }                //endif tzkey null
                    }                    //end foreach subkeynames
                    // Close the key.
                    baseKey.Close();
                }
            }
            else
            {
                //Pocket PC
                InitCityDb();
                ClockLoadAllTimeZoneData();
                int nZones = ClockGetNumTimezones();
                //tzData = new TZData[nZones];

                for (int i = 0; i < nZones; i++)
                {
                    IntPtr p   = ClockGetTimeZoneDataByOffset(i);
                    TZData tmp = new TZData(p);
                    if ((gmtOffset == tmp.GMTOffset) ||
                        (gmtOffset == ALL_TIMEZONES_LIST))
                    {
                        TimeZoneInformation tzi = new TimeZoneInformation();


                        tzi.Bias         = tmp.GMTOffset;
                        tzi.DaylightBias = tmp.DSTOffset;
//						tzi.StandardDate = tzr.StandardDate;
//						tzi.DaylightDate = tzr.DaylightDate;


                        // Don't forget to copy the standard name and
                        // daylight name to the structure.
                        tzi.DaylightName = tmp.DSTName;
                        tzi.StandardName = tmp.Name;

                        // Copy the display name from the registry to
                        // the class.
                        tzi.DisplayName = tmp.ShortName;
                        this.Add(tzi);
                    }
                }
                ClockFreeAllTimeZoneData();
                UninitCityDb();
            }
        }