zoukankan      html  css  js  c++  java
  • 时区的修改[修改为北京时区]

      1public class SysTimeZoneCheck
      2    {
      3        /// <summary>
      4        /// [Win32 API call]
      5        /// The GetTimeZoneInformation function retrieves the current time-zone parameters. 
      6        /// These parameters control the translations between Coordinated Universal Time (UTC) 
      7        /// and local time.
      8        /// </summary>
      9        /// <param name="lpTimeZoneInformation">[out] Pointer to a TIME_ZONE_INFORMATION structure to receive the current time-zone parameters.</param>
     10        /// <returns>
     11        /// If the function succeeds, the return value is one of the following values.
     12        /// <list type="table">
     13        /// <listheader>
     14        /// <term>Return code/value</term>
     15        /// <description>Description</description>
     16        /// </listheader>
     17        /// <item>
     18        /// <term>TIME_ZONE_ID_UNKNOWN == 0</term>
     19        /// <description>
     20        /// The system cannot determine the current time zone. This error is also returned if you call the SetTimeZoneInformation function and supply the bias values but no transition dates. 
     21        /// This value is returned if daylight saving time is not used in the current time zone, because there are no transition dates.
     22        /// </description>
     23        /// </item>
     24        /// <item>
     25        /// <term>TIME_ZONE_ID_STANDARD == 1</term>
     26        /// <description>
     27        /// The system is operating in the range covered by the StandardDate member of the TIME_ZONE_INFORMATION structure.
     28        /// </description>
     29        /// </item>
     30        /// <item>
     31        /// <term>TIME_ZONE_ID_DAYLIGHT == 2</term>
     32        /// <description>
     33        /// The system is operating in the range covered by the DaylightDate member of the TIME_ZONE_INFORMATION structure.
     34        /// </description>
     35        /// </item>
     36        /// </list>
     37        /// If the function fails, the return value is TIME_ZONE_ID_INVALID. To get extended error information, call GetLastError.
     38        /// </returns>

     39        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
     40        private static extern int GetTimeZoneInformation(out TimeZoneInformation lpTimeZoneInformation);
     41
     42        /// <summary>
     43        /// [Win32 API call]
     44        /// The SetTimeZoneInformation function sets the current time-zone parameters. 
     45        /// These parameters control translations from Coordinated Universal Time (UTC) 
     46        /// to local time.
     47        /// </summary>
     48        /// <param name="lpTimeZoneInformation">[in] Pointer to a TIME_ZONE_INFORMATION structure that contains the time-zone parameters to set.</param>
     49        /// <returns>
     50        /// If the function succeeds, the return value is nonzero.
     51        /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
     52        /// </returns>

     53        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
     54        private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
     55
     56        /// <summary>
     57        /// The SystemTime structure represents a date and time using individual members 
     58        /// for the month, day, year, weekday, hour, minute, second, and millisecond.
     59        /// </summary>

     60        [StructLayoutAttribute(LayoutKind.Sequential)]
     61        private struct SystemTime
     62        {
     63            public short year;
     64            public short month;
     65            public short dayOfWeek;
     66            public short day;
     67            public short hour;
     68            public short minute;
     69            public short second;
     70            public short milliseconds;
     71        }

     72
     73        /// <summary>
     74        /// The TimeZoneInformation structure specifies information specific to the time zone.
     75        /// </summary>

     76        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
     77        private struct TimeZoneInformation
     78        {
     79            /// <summary>
     80            /// Current bias for local time translation on this computer, in minutes. The bias is the difference, in minutes, between Coordinated Universal Time (UTC) and local time. All translations between UTC and local time are based on the following formula: 
     81            /// <para>UTC = local time + bias</para>
     82            /// <para>This member is required.</para>
     83            /// </summary>

     84            public int bias;
     85            /// <summary>
     86            /// Pointer to a null-terminated string associated with standard time. For example, "EST" could indicate Eastern Standard Time. The string will be returned unchanged by the GetTimeZoneInformation function. This string can be empty.
     87            /// </summary>

     88            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
     89            public string standardName;
     90            /// <summary>
     91            /// A SystemTime structure that contains a date and local time when the transition from daylight saving time to standard time occurs on this operating system. If the time zone does not support daylight saving time or if the caller needs to disable daylight saving time, the wMonth member in the SystemTime structure must be zero. If this date is specified, the DaylightDate value in the TimeZoneInformation structure must also be specified. Otherwise, the system assumes the time zone data is invalid and no changes will be applied.
     92            /// <para>To select the correct day in the month, set the wYear member to zero, the wHour and wMinute members to the transition time, the wDayOfWeek member to the appropriate weekday, and the wDay member to indicate the occurence of the day of the week within the month (first through fifth).</para>
     93            /// <para>Using this notation, specify the 2:00a.m. on the first Sunday in April as follows: wHour = 2, wMonth = 4, wDayOfWeek = 0, wDay = 1. Specify 2:00a.m. on the last Thursday in October as follows: wHour = 2, wMonth = 10, wDayOfWeek = 4, wDay = 5.</para>
     94            /// </summary>

     95            public SystemTime standardDate;
     96            /// <summary>
     97            /// Bias value to be used during local time translations that occur during standard time. This member is ignored if a value for the StandardDate member is not supplied. 
     98            /// <para>This value is added to the value of the Bias member to form the bias used during standard time. In most time zones, the value of this member is zero.</para>
     99            /// </summary>

    100            public int standardBias;
    101            /// <summary>
    102            /// Pointer to a null-terminated string associated with daylight saving time. For example, "PDT" could indicate Pacific Daylight Time. The string will be returned unchanged by the GetTimeZoneInformation function. This string can be empty.
    103            /// </summary>

    104            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    105            public string daylightName;
    106            /// <summary>
    107            /// A SystemTime structure that contains a date and local time when the transition from standard time to daylight saving time occurs on this operating system. If the time zone does not support daylight saving time or if the caller needs to disable daylight saving time, the wMonth member in the SystemTime structure must be zero. If this date is specified, the StandardDate value in the TimeZoneInformation structure must also be specified. Otherwise, the system assumes the time zone data is invalid and no changes will be applied.
    108            /// <para>To select the correct day in the month, set the wYear member to zero, the wHour and wMinute members to the transition time, the wDayOfWeek member to the appropriate weekday, and the wDay member to indicate the occurence of the day of the week within the month (first through fifth).</para>
    109            /// </summary>

    110            public SystemTime daylightDate;
    111            /// <summary>
    112            /// Bias value to be used during local time translations that occur during daylight saving time. This member is ignored if a value for the DaylightDate member is not supplied. 
    113            /// <para>This value is added to the value of the Bias member to form the bias used during daylight saving time. In most time zones, the value of this member is –60.</para>
    114            /// </summary>

    115            public int daylightBias;
    116        }

    117
    118        /// <summary>
    119        /// Sets new time-zone information for the local system.
    120        /// </summary>
    121        /// <param name="tzi">Struct containing the time-zone parameters to set.</param>

    122        private static void SetTimeZone(TimeZoneInformation tzi)
    123        {
    124            // set local system timezone
    125            SetTimeZoneInformation(ref tzi);
    126        }

    127
    128        /// <summary>
    129        /// Gets current timezone information for the local system.
    130        /// </summary>
    131        /// <returns>Struct containing the current time-zone parameters.</returns>

    132        private static TimeZoneInformation GetTimeZone()
    133        {
    134            // create struct instance
    135            TimeZoneInformation tzi;
    136
    137            // retrieve timezone info
    138            int currentTimeZone = GetTimeZoneInformation(out tzi);
    139
    140            return tzi;
    141        }

    142
    143        public static bool TimeZoneCheck()
    144        {
    145            if (GetTimeZone().bias != -480)
    146            
    147                TimeZoneInformation tzi=new TimeZoneInformation();
    148                tzi.bias=-480;
    149                tzi.standardName = "China Standard Time";
    150                tzi.daylightName = "China Standard Time";
    151                SetTimeZone(tzi);
    152                return false;
    153            }

    154            return true;
    155        }

    156    }

    157

     在修改系统时区为北京时区后,应该重启电脑生效。附重启电脑程序代码:如下:
     1/// 关机调用API程序  参数是修改 flg为 EWX_SHUTDOWN(关机)或者 EWX_LOGOFF(注销), EWX_REBOOT(重启)
     2    /// </summary>

     3    public class Shoutdown
     4    {
     5        [StructLayout(LayoutKind.Sequential, Pack = 1)]
     6        internal struct TokPriv1Luid
     7        {
     8            public int Count;
     9            public long Luid;
    10            public int Attr;
    11        }

    12
    13        [DllImport("kernel32.dll", ExactSpelling = true)]
    14        internal static extern IntPtr GetCurrentProcess();
    15
    16        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    17        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
    18
    19        [DllImport("advapi32.dll", SetLastError = true)]
    20        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
    21
    22        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    23        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
    24        ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
    25
    26        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    27        internal static extern bool ExitWindowsEx(int flg, int rea);
    28
    29        public const int SE_PRIVILEGE_ENABLED = 0x00000002;
    30        public const int TOKEN_QUERY = 0x00000008;
    31        public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    32        public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
    33        public const int EWX_LOGOFF = 0x00000000;
    34        public const int EWX_SHUTDOWN = 0x00000001;
    35        public const int EWX_REBOOT = 0x00000002;
    36        public const int EWX_FORCE = 0x00000004;
    37        public const int EWX_POWEROFF = 0x00000008;
    38        public const int EWX_FORCEIFHUNG = 0x00000010;
    39        /// <summary>
    40        /// 应用时调用下面的程序
    41        /// 参数是修改 flg为 EWX_SHUTDOWN(关机)或者 EWX_LOGOFF(注销), EWX_REBOOT(重启)等实现不同功能
    42        /// </summary>
    43        /// <param name="flg"></param>

    44        public  void DoExitWin(int flg)
    45        {
    46            bool ok;
    47            TokPriv1Luid tp;
    48            IntPtr hproc = GetCurrentProcess();
    49            IntPtr htok = IntPtr.Zero;
    50            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
    51            tp.Count = 1;
    52            tp.Luid = 0;
    53            tp.Attr = SE_PRIVILEGE_ENABLED;
    54            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
    55            ok = AdjustTokenPrivileges(htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero);
    56            ok = ExitWindowsEx(flg, 0);
    57        }
           
    58    }

    调用:
    1
    2                    Shoutdown shutDown = new Shoutdown();
    3                    shutDown.DoExitWin(Shoutdown.EWX_SHUTDOWN);
    4
    5                    
    6                    Application.Exit();



  • 相关阅读:
    jQuery Colorpicker Spectrum api 中文 文档 属性 事件 方法
    java使用dbutils工具类实现小程序 管家婆记账软件
    java实现服务端开启多线程处理客户端的上传图片请求
    java 基于tcp客户端服务端发送接收数据
    java基于udp实现键盘录入聊天
    java实现udp发送端和接收端
    java通过读取本地文件获取反射方法参数,执行对象方法
    java通过反射获取私有的构造方法,及反射擦除泛型数据类型约束
    Java反射获取类对象的三种方式
    java使用DBCP连接池创建工具类
  • 原文地址:https://www.cnblogs.com/winnxm/p/1059163.html
Copyright © 2011-2022 走看看