You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.1 KiB
66 lines
2.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IOTContainer.Common
|
|
{
|
|
public class TimeManage
|
|
{
|
|
#region 设置本地时间
|
|
[DllImport("Kernel32.dll")]
|
|
public static extern bool SetLocalTime(ref SystemTime sysTime);
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct SystemTime
|
|
{
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wYear;
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wMonth;
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wDayOfWeek;
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wDay;
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wHour;
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wMinute;
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wSecond;
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
public short wMilliseconds;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置本地时间
|
|
/// </summary>
|
|
/// <param name="timestr"></param>
|
|
/// <returns></returns>
|
|
public static bool SetLocalTimeByStr(string timestr)
|
|
{
|
|
bool flag = false;
|
|
SystemTime sysTime = new SystemTime();
|
|
DateTime dt = Convert.ToDateTime(timestr);
|
|
sysTime.wYear = Convert.ToInt16(dt.Year);
|
|
sysTime.wMonth = Convert.ToInt16(dt.Month);
|
|
sysTime.wDay = Convert.ToInt16(dt.Day);
|
|
sysTime.wHour = Convert.ToInt16(dt.Hour);
|
|
sysTime.wMinute = Convert.ToInt16(dt.Minute);
|
|
sysTime.wSecond = Convert.ToInt16(dt.Second);
|
|
try
|
|
{
|
|
flag = SetLocalTime(ref sysTime);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("SetLocalTimeByStr函数执行异常" + ex.ToString(), "TimeManageError");
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|