using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Core.Mes.ClientFrameWork;


namespace Core.Mes.ClientManager
{
    class SetTime
    {
        //imports SetLocalTime function from kernel32.dll 
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern int SetLocalTime(ref SystemTime lpSystemTime);

        public static void SetClientTime()
        {
            string strOut = "";
            object obj1 = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "getDBSrvTime",
                      null, out strOut);
            if (strOut != "") throw new Exception(strOut);

            if (obj1 != null)
            {
                // And then set up a structure with the required properties and call the api from code: 

                DateTime dt = Convert.ToDateTime(obj1);

                SystemTime systNew = new SystemTime();

                // 设置属性 
                systNew.wDay = Convert.ToInt16(dt.Day);
                systNew.wMonth = Convert.ToInt16(dt.Month);
                systNew.wYear = Convert.ToInt16(dt.Year);
                systNew.wHour = Convert.ToInt16(dt.Hour);
                systNew.wMinute = Convert.ToInt16(dt.Minute);
                systNew.wSecond = Convert.ToInt16(dt.Second);

                // 调用API，更新系统时间 
                SetLocalTime(ref systNew);
            }
        }
    }

    //struct for date/time apis 
    public struct SystemTime
    {
        public short wYear;
        public short wMonth;
        public short wDayOfWeek;
        public short wDay;
        public short wHour;
        public short wMinute;
        public short wSecond;
        public short wMilliseconds;
    }
}
