D
Drunkalot
How can I set the system time using a DateTime object???
[]s...
[]s...
Anders Norås said:You can't set the local time with the DateTime structure, but you can do itHow can I set the system time using a DateTime object???
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/
Anders Norås said:You can't set the local time with the DateTime structure, but you can do itHow can I set the system time using a DateTime object???
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/
Anders Norås said:You can't set the local time with the DateTime structure, but you can do itHow can I set the system time using a DateTime object???
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/
Japster said: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/default.asp?url=/library/en-us/dndotnet/html/win32map.asp).
Any idea whether this is on the cards for the future?
Thanks in advance
JAndyP
Anders Norås said:You can't set the local time with the DateTime structure, but you can doHow can I set the system time using a DateTime object???
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/
Nicholas Paldino said: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]
- (e-mail address removed)
Japster said: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/default.asp?url=/library/en-us/dndotnet/html/win32map.asp).
Any idea whether this is on the cards for the future?
Thanks in advance
JAndyP
Anders Norås said: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/
Japster said:I am trying to implement a system which permits dates to the year
9999, to meet some rather overambitious requirement!