|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem setting system time...I use the P Invoke method as described on MSDN web site. All the fields are set properly except for one: hour. For example I set it to a value of 15 and that somehow becomes 11 am on the clock. Here is my code: [StructLayout(LayoutKind.Sequential)] public struct SYSTEMTIME { public ushort year; public ushort month; public ushort dayOfWeek; public ushort day; public ushort hour; public ushort minute; public ushort second; public ushort milli; } [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetSystemTime([In] ref SYSTEMTIME st); SYSTEMTIME systime = new SYSTEMTIME(); // set fields .... // i.e. : systime.hour = 15; SetSystemTime(ref systime); System time gets updated but hour is 4 hours off what I set. I dont see any way to specify timezone so it can't be that... what gives? I got the order of the fields correct in the struct, don't I?
Show quote
Hide quote
"trant" <tr***@discussions.microsoft.com> wrote in message Hi,news:BD1C41F1-E007-45C1-B46D-3AEBBDE59D30@microsoft.com... > I am trying to set my system time using C#. > > I use the P Invoke method as described on MSDN web site. > > All the fields are set properly except for one: hour. > > For example I set it to a value of 15 and that somehow becomes 11 am on > the > clock. > > Here is my code: > > [StructLayout(LayoutKind.Sequential)] > public struct SYSTEMTIME > { > public ushort year; > public ushort month; > public ushort dayOfWeek; > public ushort day; > public ushort hour; > public ushort minute; > public ushort second; > public ushort milli; > } > > [DllImport("kernel32.dll", SetLastError = true)] > public static extern bool SetSystemTime([In] ref SYSTEMTIME st); > > SYSTEMTIME systime = new SYSTEMTIME(); > > // set fields .... > // i.e. : > systime.hour = 15; > > SetSystemTime(ref systime); > > System time gets updated but hour is 4 hours off what I set. > > I dont see any way to specify timezone so it can't be that... what gives? > I > got the order of the fields correct in the struct, don't I? I checked the msdn docs for the function and it says : "Sets the current system time and date. The system time is expressed in Coordinated Universal Time (UTC).", so the timezone can be the problem as You assumed. If that is then doing something like this might help: DateTime dateTimeToSet = new DateTime(2009, 07, 03, 15, 20, 45).ToUniversalTime(); SYSTEMTIME systime = new SYSTEMTIME(); systime.hour = dateTimeToSet.Hour; // ... set other fields of the struct too ... Hope You find this useful. -Zsolt Hello,
Thank you for your help! Yeah this definitely does fix the situation. It is working now! Show quoteHide quote "miher" wrote: > > > "trant" <tr***@discussions.microsoft.com> wrote in message > news:BD1C41F1-E007-45C1-B46D-3AEBBDE59D30@microsoft.com... > > I am trying to set my system time using C#. > > > > I use the P Invoke method as described on MSDN web site. > > > > All the fields are set properly except for one: hour. > > > > For example I set it to a value of 15 and that somehow becomes 11 am on > > the > > clock. > > > > Here is my code: > > > > [StructLayout(LayoutKind.Sequential)] > > public struct SYSTEMTIME > > { > > public ushort year; > > public ushort month; > > public ushort dayOfWeek; > > public ushort day; > > public ushort hour; > > public ushort minute; > > public ushort second; > > public ushort milli; > > } > > > > [DllImport("kernel32.dll", SetLastError = true)] > > public static extern bool SetSystemTime([In] ref SYSTEMTIME st); > > > > SYSTEMTIME systime = new SYSTEMTIME(); > > > > // set fields .... > > // i.e. : > > systime.hour = 15; > > > > SetSystemTime(ref systime); > > > > System time gets updated but hour is 4 hours off what I set. > > > > I dont see any way to specify timezone so it can't be that... what gives? > > I > > got the order of the fields correct in the struct, don't I? > > Hi, > > I checked the msdn docs for the function and it says : "Sets the current > system time and date. The system time is expressed in Coordinated Universal > Time (UTC).", so the timezone can be the problem as You assumed. > If that is then doing something like this might help: > > DateTime dateTimeToSet = new DateTime(2009, 07, 03, 15, 20, > 45).ToUniversalTime(); > SYSTEMTIME systime = new SYSTEMTIME(); > systime.hour = dateTimeToSet.Hour; > // ... set other fields of the struct too ... > > Hope You find this useful. > -Zsolt > > "trant" <tr***@discussions.microsoft.com> wrote in message You certainly got the order right.news:BD1C41F1-E007-45C1-B46D-3AEBBDE59D30@microsoft.com... > I got the order of the fields correct in the struct, don't I? You might try copying the struct's definition from the pinvoke.net site and see if that makes any difference: http://www.pinvoke.net/default.aspx/Structures/SYSTEMTIME.html Please post all the values that you are assigning to the various elements of the struct, not just the hour - I'm particularly interested to see the DayOfWeek value. That will allow people to simulate exactly what you're doing... Also, see the Notes section on the above page which describes a similar problem that the author has experienced... |
|||||||||||||||||||||||