/// <summary> /// 设置本地时区 /// </summary> /// <param name="timeZoneName_en"></param> /// <returns></returns> public static bool SetLocalTimeZone(string timeZoneName_en) { if (PrivilegeAPI.GrantPrivilege(PrivilegeConstants.SE_TIME_ZONE_NAME)) { DynamicTimeZoneInformation dtzi = timeZoneName2DynamicTimeZoneInformation(timeZoneName_en); bool success = false; // 检测当前系统是否为旧系统 if (IsOldOsVersion()) { Console.WriteLine("检测当前系统为: Old OS Version"); TimeZoneInformation tzi = DynamicTimeZoneInformation2TimeZoneInformation(dtzi); success = SetTimeZoneInformation(ref tzi); } else { success = SetDynamicTimeZoneInformation(ref dtzi); } if (success) { TimeZoneInfo.ClearCachedData(); // 清除缓存 } if (!PrivilegeAPI.RevokePrivilege(PrivilegeConstants.SE_TIME_ZONE_NAME)) { Console.WriteLine("撤权失败: 更改时区"); } return(success); } Console.WriteLine("授权失败: 更改时区"); return(false); }
public TimeZoneInformation(DynamicTimeZoneInformation DynamicTimeZoneInformation) { Bias = DynamicTimeZoneInformation.Bias; StandardName = DynamicTimeZoneInformation.StandardName; StandardDate = DynamicTimeZoneInformation.StandardDate; StandardBias = DynamicTimeZoneInformation.StandardBias; DaylightName = DynamicTimeZoneInformation.DaylightName; DaylightDate = DynamicTimeZoneInformation.DaylightDate; DaylightBias = DynamicTimeZoneInformation.DaylightBias; }
// 根据时区名获取对应的DynamicTimeZoneInformation private static DynamicTimeZoneInformation timeZoneName2DynamicTimeZoneInformation(string timeZoneName) { TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName); DynamicTimeZoneInformation dtzi = new DynamicTimeZoneInformation(); dtzi.standardName = timeZoneInfo.StandardName; dtzi.standardDate = new SystemTime(); dtzi.daylightName = timeZoneInfo.DaylightName; dtzi.daylightDate = new SystemTime(); dtzi.timeZoneKeyName = timeZoneInfo.Id; dtzi.dynamicDaylightTimeDisabled = false; dtzi.bias = -Convert.ToInt32(timeZoneInfo.BaseUtcOffset.TotalMinutes); return(dtzi); }
/// <summary> /// 获取本地时区 /// </summary> /// <returns></returns> public static string GetLocalTimeZone() { // 检测当前系统是否为旧系统 if (IsOldOsVersion()) { TimeZoneInformation tzi = new TimeZoneInformation(); GetTimeZoneInformation(ref tzi); return(TimeZoneInfo2CustomString(tzi)); } DynamicTimeZoneInformation dtzi = new DynamicTimeZoneInformation(); GetDynamicTimeZoneInformation(ref dtzi); return(DynamicTimeZoneInfo2CustomString(dtzi)); }
internal static DateTime GetTimeZoneTime(string TimeZone) { try { SystemTime SystemTimeLocal; TimeZoneInformation TimeZoneInformation; SystemTime SystemTimeNow = new SystemTime(DateTime.UtcNow); DynamicTimeZoneInformation DynamicTimeZoneInformation = AllEnumerateSystemTimeZones.First(x => x.TimeZoneKeyName == TimeZone); if (GetTimeZoneInformationForYear(SystemTimeNow.Year, ref DynamicTimeZoneInformation, out TimeZoneInformation)) { if (SystemTimeToTzSpecificLocalTime(ref TimeZoneInformation, ref SystemTimeNow, out SystemTimeLocal)) { return(new DateTime(SystemTimeLocal.Year, SystemTimeLocal.Month, SystemTimeLocal.Day, SystemTimeLocal.Hour, SystemTimeLocal.Minute, 0, 0, DateTimeKind.Utc)); } } } catch { } return(new DateTime()); }
protected override void _Set() { WindowsTokenPrivileges.EnableSetTimeZonePrivileges(); var info = new DynamicTimeZoneInformation(); int index = 0; while (true) { if (EnumDynamicTimeZoneInformation(index, out info) != 0) break; if (info.StandardName == _timeZone.StandardName) break; index++; } if (info.StandardName.Length > 0) SetDynamicTimeZoneInformation(ref info); else throw new Exception("Time Zone not found"); }
private static extern bool SetDynamicTimeZoneInformation(ref DynamicTimeZoneInformation lpDynamicTimeZoneInformation);
// 将DynamicTimeZoneInformation转换为TimeZoneInformation private static TimeZoneInformation DynamicTimeZoneInformation2TimeZoneInformation(DynamicTimeZoneInformation dtzi) { return(new TimeZoneInformation { bias = dtzi.bias, standardName = dtzi.standardName, standardDate = dtzi.standardDate, standardBias = dtzi.standardBias, daylightName = dtzi.daylightName, daylightDate = dtzi.daylightDate, daylightBias = dtzi.daylightBias }); }
// 将DynamicTimeZoneInformation转换为自定义string private static string DynamicTimeZoneInfo2CustomString(DynamicTimeZoneInformation dtzi) { return(dtzi.standardName + "(" + dtzi.bias + ")"); }
private static extern bool GetDynamicTimeZoneInformation(out DynamicTimeZoneInformation lpTimeZoneInformation);
internal static extern int EnumDynamicTimeZoneInformation([In] int dwIndex, out DynamicTimeZoneInformation lpTimeZoneInformation);
static extern bool GetTimeZoneInformationForYear(short wYear, ref DynamicTimeZoneInformation pdtzi, out TimeZoneInformation ptzi);
static extern int EnumDynamicTimeZoneInformation(int dwIndex, out DynamicTimeZoneInformation lpTimeZoneInformation);