Re: Set the local time

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Drunkalot (r_luceac_at_hotmail.com)
Date: 12/16/04


Date: Thu, 16 Dec 2004 18:05:33 -0200

Hi, thanks for the program...

Where can I find information about these function that are in windows dll???

"Anders Norås [MCAD]" <anders.noras@objectware.no> wrote in message
news:Ot8xwDw4EHA.208@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/
>
>



Relevant Pages

  • Re: Set the local time
    ... >>> How can I set the system time using a DateTime object??? ... >> with the SetSystemTime method in kernel32.dll via PInvoke. ... >> method accepts a SystemTime structure as its parameter. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DateTime.Now returns incorrect time after time zone change
    ... Not sure why you are calling SetSystemTime - you should just be calling GetLocalTime and converting the value to a DateTime e.g. ... // Get local time ... Because SYSTEMTIME is declared as private then so should the GetLocalTime P/Invoke. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Same problem when timezone is changed outside the application
    ... All times in my app are in UTC, and recalculated on the fly when they are shown. ... Not sure why you are calling SetSystemTime - you should just be calling ... GetLocalTime and converting the value to a DateTime e.g. ... Because SYSTEMTIME is declared as private then so should the GetLocalTime ...
    (microsoft.public.dotnet.framework.compactframework)
  • RE: Same problem when timezone is changed outside the application
    ... you can also arrange to be notified when time zone is changed. ... Not sure why you are calling SetSystemTime - you should just be calling ... GetLocalTime and converting the value to a DateTime e.g. ... Because SYSTEMTIME is declared as private then so should the GetLocalTime ...
    (microsoft.public.dotnet.framework.compactframework)
  • Same problem when timezone is changed outside the application
    ... Not sure why you are calling SetSystemTime - you should just be calling ... GetLocalTime and converting the value to a DateTime e.g. ... Because SYSTEMTIME is declared as private then so should the GetLocalTime ... DateTime.Now returns incorrect time after time zone change ...
    (microsoft.public.dotnet.framework.compactframework)