Problem setting system time...

T

trant

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?
 
M

miher

trant said:
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
 
T

trant

Hello,

Thank you for your help!

Yeah this definitely does fix the situation. It is working now!



miher said:
trant said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top