By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

Set the local time

P: n/a
How can I set the system time using a DateTime object???
[]s...
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> How can I set the system time using a DateTime object???
You can't set the local time with the DateTime structure, but you can do it
with the SetSystemTime method in kernel32.dll via PInvoke. The SetSystemTime
method accepts a SystemTime structure as its parameter. Populate this
structure with the corresponding members of the DateTime structure.

public class MyClass
{
public static void Main()
{
DateTime dt=DateTime.Now;
SystemTime st=new SystemTime();
st.Year=Convert.ToUInt16(dt.Year);
st.Hour=Convert.ToUInt16(dt.Hour-1);
SetSystemTime(ref st);
}

[DllImport("kernel32.dll")]
public static extern int SetSystemTime(ref SystemTime systemTime);
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
public System.UInt16 Year;
public System.UInt16 Month;
public System.UInt16 DayOfWeek;
public System.UInt16 Day;
public System.UInt16 Hour;
public System.UInt16 Minute;
public System.UInt16 Second;
public System.UInt16 Millisecond;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #2

P: n/a
Hi, thanks for the program...

Where can I find information about these function that are in windows dll???
"Anders Norås [MCAD]" <an**********@objectware.no> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
How can I set the system time using a DateTime object??? You can't set the local time with the DateTime structure, but you can do

it with the SetSystemTime method in kernel32.dll via PInvoke. The SetSystemTime method accepts a SystemTime structure as its parameter. Populate this
structure with the corresponding members of the DateTime structure.

public class MyClass
{
public static void Main()
{
DateTime dt=DateTime.Now;
SystemTime st=new SystemTime();
st.Year=Convert.ToUInt16(dt.Year);
st.Hour=Convert.ToUInt16(dt.Hour-1);
SetSystemTime(ref st);
}

[DllImport("kernel32.dll")]
public static extern int SetSystemTime(ref SystemTime systemTime);
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
public System.UInt16 Year;
public System.UInt16 Month;
public System.UInt16 DayOfWeek;
public System.UInt16 Day;
public System.UInt16 Hour;
public System.UInt16 Minute;
public System.UInt16 Second;
public System.UInt16 Millisecond;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 16 '05 #3

P: n/a
Every time i update the hour it gets my hour - 2...
even if I put a + 2 in my datetime dt.hour...

Whats happening???
"Anders Norås [MCAD]" <an**********@objectware.no> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
How can I set the system time using a DateTime object??? You can't set the local time with the DateTime structure, but you can do

it with the SetSystemTime method in kernel32.dll via PInvoke. The SetSystemTime method accepts a SystemTime structure as its parameter. Populate this
structure with the corresponding members of the DateTime structure.

public class MyClass
{
public static void Main()
{
DateTime dt=DateTime.Now;
SystemTime st=new SystemTime();
st.Year=Convert.ToUInt16(dt.Year);
st.Hour=Convert.ToUInt16(dt.Hour-1);
SetSystemTime(ref st);
}

[DllImport("kernel32.dll")]
public static extern int SetSystemTime(ref SystemTime systemTime);
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
public System.UInt16 Year;
public System.UInt16 Month;
public System.UInt16 DayOfWeek;
public System.UInt16 Day;
public System.UInt16 Hour;
public System.UInt16 Minute;
public System.UInt16 Second;
public System.UInt16 Millisecond;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 16 '05 #4

P: n/a
I have a slight twist to the tale....

I am trying to implement a system which permits dates to the year 9999, to
meet some rather overambitious requirement!

To avoid declaring a deficiency, I was hoping that I could use the DateTime
structure that supports dates to end of 9999. But this seems to have been
thwarted by the absence of an equivalent call in .NET framework.

Using the p/invoke solution, I have encountered a problem that if I set a
year above 8907 the year is not set (or perhaps reverts to existing value),
despite SDK documentation suggesting that windows XP can handle values up to
30827
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/sysinfo/base/systemtime_str.htm).
Is this a bug? Is there a way around? Can anyone successfully set year to
8908 or above?

Finally, I am thinking that it would have been nice if the .Net framework
had offered an equivalent to SetSystemTime, just as it did for GetSystemTime
(http://msdn.microsoft.com/library/de.../win32map.asp). Any idea whether this is on the cards for the future?

Thanks in advance

JAndyP
"Anders Norås [MCAD]" wrote:
How can I set the system time using a DateTime object???

You can't set the local time with the DateTime structure, but you can do it
with the SetSystemTime method in kernel32.dll via PInvoke. The SetSystemTime
method accepts a SystemTime structure as its parameter. Populate this
structure with the corresponding members of the DateTime structure.

public class MyClass
{
public static void Main()
{
DateTime dt=DateTime.Now;
SystemTime st=new SystemTime();
st.Year=Convert.ToUInt16(dt.Year);
st.Hour=Convert.ToUInt16(dt.Hour-1);
SetSystemTime(ref st);
}

[DllImport("kernel32.dll")]
public static extern int SetSystemTime(ref SystemTime systemTime);
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
public System.UInt16 Year;
public System.UInt16 Month;
public System.UInt16 DayOfWeek;
public System.UInt16 Day;
public System.UInt16 Hour;
public System.UInt16 Minute;
public System.UInt16 Second;
public System.UInt16 Millisecond;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 16 '05 #5

P: n/a
JAndyP,

I haven't seen the original post, so I am not sure if I have this in the
right context.

However, it might be very well possible that you can not set the year
above 8907 in the OS. I believe this is the case because the date in the OS
is stored in a 32 bit value internally (similar to the automation format).

I played around in VB, and when converting long values to dates and vice
versa, the max values for integers created dates around that date.

Of course, this is pure hypothesis.

One does have to ask who in their right mind expects a software system
to last until the year 9999? And if the requirement is that it handle
everything up to that year, why not just go one more year, and handle that
scenario? It just seems a little strange.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Japster" <Ja*****@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
I have a slight twist to the tale....

I am trying to implement a system which permits dates to the year 9999, to
meet some rather overambitious requirement!

To avoid declaring a deficiency, I was hoping that I could use the
DateTime
structure that supports dates to end of 9999. But this seems to have been
thwarted by the absence of an equivalent call in .NET framework.

Using the p/invoke solution, I have encountered a problem that if I set a
year above 8907 the year is not set (or perhaps reverts to existing
value),
despite SDK documentation suggesting that windows XP can handle values up
to
30827
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/sysinfo/base/systemtime_str.htm).
Is this a bug? Is there a way around? Can anyone successfully set year to
8908 or above?

Finally, I am thinking that it would have been nice if the .Net framework
had offered an equivalent to SetSystemTime, just as it did for
GetSystemTime
(http://msdn.microsoft.com/library/de.../win32map.asp).
Any idea whether this is on the cards for the future?

Thanks in advance

JAndyP
"Anders Norås [MCAD]" wrote:
> How can I set the system time using a DateTime object???

You can't set the local time with the DateTime structure, but you can do
it
with the SetSystemTime method in kernel32.dll via PInvoke. The
SetSystemTime
method accepts a SystemTime structure as its parameter. Populate this
structure with the corresponding members of the DateTime structure.

public class MyClass
{
public static void Main()
{
DateTime dt=DateTime.Now;
SystemTime st=new SystemTime();
st.Year=Convert.ToUInt16(dt.Year);
st.Hour=Convert.ToUInt16(dt.Hour-1);
SetSystemTime(ref st);
}

[DllImport("kernel32.dll")]
public static extern int SetSystemTime(ref SystemTime systemTime);
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
public System.UInt16 Year;
public System.UInt16 Month;
public System.UInt16 DayOfWeek;
public System.UInt16 Day;
public System.UInt16 Hour;
public System.UInt16 Minute;
public System.UInt16 Second;
public System.UInt16 Millisecond;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 16 '05 #6

P: n/a
Thanks Nicholas, I think you are confirming my conclusions.

I entirely agree that only those in their left/wrong or out of their minds
would really insist on meeting a requirement for the year 9999, although I
wouldn't like to be quite so direct with the customer!... they are always
right don't you know ;-)
In reality, I am sure the customer will not expect the software to be used
in 9999. This is why I expect we will raise/declare a deficiency against the
requirements, and I can't see any reason why they would object to conceding
this deficiency.

None-the-less it would have been welcome to find someone who knew a way to
set such a date on the system clock. There is of course an alternative if
they don't concede the deficiency, and that is to represent millenia, or
years above 8907 as a member variable in some time handling wrapper class,
but then it all starts to get messy and costly to implement for no practical
reason.

To me, it still seems to me to be inconsistent that .NET declares that you
can read a DateTime representing current time, and this time structure has
the capacity to represent a year of 9999, and yet there appears to be no
converse method/function to write a value value represented in this data
structure back to the operating system.

Perhaps (if the OS can't support such a year) this is the very reason a Set
DateTime function does not exist.

I would still be interested to know if there is anything in the pipeline
(.Net 2.0?) to write a DateTime structure back to the system clock in .NET.
If not perhaps someone should consider highlighting the shortcoming in the
MSDN documentation; however irrational writing 9999 may seem.... you never
know they might invent timetravel ;-).

There also seems to be a fault in the microsoft documentation to suggest XP
can handle years to 30827 when it appears not to support this in practice (as
identified earlier in this thread).

Any further thoughts?

JAndyP (aka Japster)

"Nicholas Paldino [.NET/C# MVP]" wrote:
JAndyP,

I haven't seen the original post, so I am not sure if I have this in the
right context.

However, it might be very well possible that you can not set the year
above 8907 in the OS. I believe this is the case because the date in the OS
is stored in a 32 bit value internally (similar to the automation format).

I played around in VB, and when converting long values to dates and vice
versa, the max values for integers created dates around that date.

Of course, this is pure hypothesis.

One does have to ask who in their right mind expects a software system
to last until the year 9999? And if the requirement is that it handle
everything up to that year, why not just go one more year, and handle that
scenario? It just seems a little strange.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Japster" <Ja*****@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
I have a slight twist to the tale....

I am trying to implement a system which permits dates to the year 9999, to
meet some rather overambitious requirement!

To avoid declaring a deficiency, I was hoping that I could use the
DateTime
structure that supports dates to end of 9999. But this seems to have been
thwarted by the absence of an equivalent call in .NET framework.

Using the p/invoke solution, I have encountered a problem that if I set a
year above 8907 the year is not set (or perhaps reverts to existing
value),
despite SDK documentation suggesting that windows XP can handle values up
to
30827
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/sysinfo/base/systemtime_str.htm).
Is this a bug? Is there a way around? Can anyone successfully set year to
8908 or above?

Finally, I am thinking that it would have been nice if the .Net framework
had offered an equivalent to SetSystemTime, just as it did for
GetSystemTime
(http://msdn.microsoft.com/library/de.../win32map.asp).
Any idea whether this is on the cards for the future?

Thanks in advance

JAndyP
"Anders Norås [MCAD]" wrote:
> How can I set the system time using a DateTime object???
You can't set the local time with the DateTime structure, but you can do
it
with the SetSystemTime method in kernel32.dll via PInvoke. The
SetSystemTime
method accepts a SystemTime structure as its parameter. Populate this
structure with the corresponding members of the DateTime structure.

public class MyClass
{
public static void Main()
{
DateTime dt=DateTime.Now;
SystemTime st=new SystemTime();
st.Year=Convert.ToUInt16(dt.Year);
st.Hour=Convert.ToUInt16(dt.Hour-1);
SetSystemTime(ref st);
}

[DllImport("kernel32.dll")]
public static extern int SetSystemTime(ref SystemTime systemTime);
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
public System.UInt16 Year;
public System.UInt16 Month;
public System.UInt16 DayOfWeek;
public System.UInt16 Day;
public System.UInt16 Hour;
public System.UInt16 Minute;
public System.UInt16 Second;
public System.UInt16 Millisecond;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/


Nov 16 '05 #7

P: n/a
Japster wrote:
I am trying to implement a system which permits dates to the year
9999, to meet some rather overambitious requirement!


Caught out by the Y2K bug a few years ago ? Well thats nothing compared
to the looming Y10K bug. Act now and not be caught out !! My team of
highly skilled consultants are standing by to assist you today, just
call 1 800 Y10KBugFix

;-)
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.