有一个UTC格式的字符串,
然后要根据这个字符串来设置本地时间。涉及到两个问题
1 UTC时间转换为本地时间
2 将转换后的本地时间设置为系统时间。在网上查了查,发现只能用WIN32的API?
.net没有这样的函数来实现吗?
是否有例子参考,谢谢。

解决方案 »

  1.   

    只能用WIN32的APIC#本身没有这种函数
      

  2.   

    使用API的例子有吗?
    特别是UTC转LOCAL的
      

  3.   

    使用API的例子有吗?
    首先引用
    using System.RunTime.Inter什么我忘了,只有这一个
    DllImpor("")
    public static void MessageBox(string a ,string b,int q ,int b);
    类似这样的,很容易的你自已看下
      

  4.   

    做过GPS对时的
    读取时间然后跟系统时间比较
    不同修改系统时间
    代码给你个大概参考
    using System.Runtime.InteropServices; //添加引用 //调用Kernel32.DLL
            [DllImport("Kernel32.dll")]
            public static extern void GetLocalTime(SystemTime st);
            [DllImport("Kernel32.dll")]
            public static extern void SetLocalTime(SystemTime st);        [StructLayout(LayoutKind.Sequential)]
            public class SystemTime
            {
                public ushort wYear;
                public ushort wMonth;
                public ushort wDayOfWeek;
                public ushort wDay;
                public ushort Whour;
                public ushort wMinute;
                public ushort wSecond;
                public ushort wMilliseconds;        }        SystemTime sSystemtime = new SystemTime(); dt = DateTime.ParseExact(sss, "HHmmssddMMyy", null);
                             dtSystem = DateTime.Now;
                             if (DateTime.Compare(dt, dtSystem) != 0) 
                             {
                                 sSystemtime.wDay = (ushort)dt.Day;
                                 sSystemtime.wMonth = (ushort)dt.Month;
                                 sSystemtime.wYear = (ushort)dt.Year;                             sSystemtime.Whour = (ushort)dt.Hour;
                                 sSystemtime.wMinute = (ushort)dt.Minute;
                                 sSystemtime.wSecond = (ushort)dt.Second;                             SetSystemTime(sSystemtime);//设置系统时间为读取到的时间我就截取这么多
    楼主应该能看明白
    UTC转换过来应该不是问题吧
    你查查MSDN就有了
      

  5.   


    //api函数声明 
    [dllimport ("kernel32.dll", charset=charset.ansi)] 
    public extern static bool setsystemtime(ref systemtime time); private void button1_click(object sender, system.eventargs e) 

    //调用代码 
    systemtime t = new systemtime (); 
    t.year = 2000; 
    t.month = 1; 
    t.day = 2; 
    t.hour = 12-8; //这个函数使用的是0时区的时间,对于我们用+8时区的,时间要自己算一下.如要设12点,则为12-8 
    t.minute = 5; 
    bool v = setsystemtime(ref t); 
    console.writeline(v.tostring()); 
      

  6.   

           
    //引用 
    using System.Runtime.InteropServices;
     //api函数声明 
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool SetSystemTime(ref SYSTEMTIME time);
            public struct SYSTEMTIME
            {
                public short Year;
                public short Month;
                public short DayOfWeek;
                public short Day;
                public short Hour;
                public short Minute;
                public short Second;
                public short Milliseconds;
            }
            private  void Setdatetime()
            {
                //调用代码 
                DateTime idag = new DateTime((int)DateTime.Now.AddYears(1).Year,//增加年
                    (int)DateTime.Now.AddMonths(0).Month,//增加月
                    (int)DateTime.Now.AddDays(0).Day,//增加天
                    (int)(DateTime.Now.AddHours(0).Hour),//增加小时
                    (int)(DateTime.Now.AddMinutes(0).Minute),//增加分
                    (int)(DateTime.Now.AddSeconds(0).Second),//增加秒
                    DateTimeKind.Local);
                SYSTEMTIME s = new SYSTEMTIME();
                s.Year = (short)idag.Year;
                s.Month = (short)idag.Month;
                s.DayOfWeek = (short)idag.DayOfWeek;
                s.Day = (short)idag.Day;
                s.Hour = (short)idag.Hour;
                s.Minute = (short)idag.Minute;
                s.Second = (short)idag.Second;
                s.Milliseconds = (short)idag.Millisecond;
                bool v= SetSystemTime(ref s);
                Console.WriteLine(v.ToString());
            } //测试通过
      

  7.   

    t.hour = 12-8;
    如果减8后得到负值,函数会自动调整日期,年月吗?
      

  8.   

    肯定会的!这个是datetime类型嘛!!! 我上面用的是local,“ DateTimeKind.Local”你也可以用DateTimeKind.utc,小时减8