Windows SetLocalTime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Podi

    #1

    Windows SetLocalTime

    I am trying to set the system time on my Windows computer, but avoid
    using the DOS command (date, time).

    Does anyone know what parameter to pass to function SetLocalTime?

    CSharp ref


    Thanks a bunch,
    P
    >>import ctypes
    >>import pywintypes
    >>t = pywintypes.Time (time.time())
    >>ctypes.windll .kernel32.SetLo calTime(t)
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    ArgumentError: argument 1: exceptions.Type Error: Don't know how to
    convert parameter 1
    >>ctypes.windll .kernel32.SetLo calTime(ctypes. addressof(t))
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: invalid type

  • Rob Williscroft

    #2
    Re: Windows SetLocalTime

    Podi wrote in news:1166040733 .947268.80050@1 6g2000cwy.googl egroups.com
    in comp.lang.pytho n:
    I am trying to set the system time on my Windows computer, but avoid
    using the DOS command (date, time).
    >
    Does anyone know what parameter to pass to function SetLocalTime?
    <url:http://msdn.microsoft.com/library/de...l=/library/en-
    us/sysinfo/base/setlocaltime.as p>

    Google will usually find the documentation of anything in the
    Windows API however sometimes it also helps to add "msdn" to
    your search as in:

    <url:http://www.google.com/search?client=....mozilla%3Aen-
    US%3Aofficial_s &hl=en&q=setloc altime+msdn&btn G=Google+Search >

    Adding site:msdn.micro soft.com is even better (but alas more typing):

    <url:http://www.google.com/search?hl=en&lr=&client=firefox-
    a&rls=org.mozil la%3Aen-US%3Aofficial_s &q=setlocaltime +site%
    3Amsdn.microsof t.com&btnG=Sear ch>



    Rob.
    --

    Comment

    • Podi

      #3
      Re: Windows SetLocalTime

      Yes, thank you.

      I found the function SetLocalTime or SetSystemTime to set the time from
      MSDN http://msdn2.microsoft.com/en-us/library/ms724942.aspx. I am
      having trouble passing parameter to the functions in Python.

      Rob Williscroft wrote:
      >
      Google will usually find the documentation of anything in the
      Windows API however sometimes it also helps to add "msdn" to
      your search as in:
      >

      Comment

      • Rob Williscroft

        #4
        Re: Windows SetLocalTime

        Podi wrote in news:1166041988 .571906.244570@ j72g2000cwa.goo glegroups.com in
        comp.lang.pytho n:
        Rob Williscroft wrote:
        >>
        >Google will usually find the documentation of anything in the
        >Windows API however sometimes it also helps to add "msdn" to
        >your search as in:
        Yes, thank you.
        >
        I found the function SetLocalTime or SetSystemTime to set the time from
        MSDN http://msdn2.microsoft.com/en-us/library/ms724942.aspx. I am
        having trouble passing parameter to the functions in Python.
        >
        Ok I see, you will probably need these 2 bits of the ctypes
        documentation:




        From there on geting to this is farly straight forward:

        from ctypes import *
        from ctypes.wintypes import WORD

        class SYSTEMTIME(Stru cture):
        _fields_ = [
        ( 'wYear', WORD ),
        ( 'wMonth', WORD ),
        ( 'wDayOfWeek', WORD ),
        ( 'wDay', WORD ),
        ( 'wHour', WORD ),
        ( 'wMinute', WORD ),
        ( 'wSecond', WORD ),
        ( 'wMilliseconds' , WORD ),
        ]

        GetSystemTime = windll.kernel32 .GetSystemTime

        st = SYSTEMTIME()

        GetSystemTime( pointer( st ) )

        print st.wYear, st.wMonth, st.wDayOfWeek

        A bit more digging into the docs will get to this:

        prototype = WINFUNCTYPE( None, POINTER( SYSTEMTIME ) )
        paramflags = (1, "lpSystemTi me" ),
        GetSystemTime = prototype(("Get SystemTime", windll.kernel32 ), paramflags )

        st = SYSTEMTIME()
        GetSystemTime( st )
        print st.wYear, st.wMonth, st.wDayOfWeek

        Which will give a much better error message if you try to
        pass something other than a SYSTEMTIME, as well as wrapping
        the argument in pointer for you.

        Rob.
        --

        Comment

        • Podi

          #5
          Re: Windows SetLocalTime

          Thank you. It works :-)
          P

          Rob Williscroft wrote:
          Ok I see, you will probably need these 2 bits of the ctypes
          documentation:
          >


          >
          From there on geting to this is farly straight forward:

          Comment

          Working...