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

Windows SetLocalTime

P: n/a
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
http://groups.google.com/group/micro...fc37c6c9148961

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

Dec 14 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Podi wrote in news:11*********************@16g2000cwy.googlegrou ps.com
in comp.lang.python:
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.asp>

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=setlocaltime+msdn&btnG=Goo gle+Search>

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

<url:http://www.google.com/search?hl=en&lr=&client=firefox-
a&rls=org.mozilla%3Aen-US%3Aofficial_s&q=setlocaltime+site%
3Amsdn.microsoft.com&btnG=Search>

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Dec 14 '06 #2

P: n/a
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:
Dec 14 '06 #3

P: n/a
Podi wrote in news:11**********************@j72g2000cwa.googlegr oups.com in
comp.lang.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:
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:

http://docs.python.org/lib/ctypes-st...es-unions.html
http://docs.python.org/lib/ctypes-pointers.html

From there on geting to this is farly straight forward:

from ctypes import *
from ctypes.wintypes import WORD

class SYSTEMTIME(Structure):
_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, "lpSystemTime" ),
GetSystemTime = prototype(("GetSystemTime", 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.
--
http://www.victim-prime.dsl.pipex.com/
Dec 14 '06 #4

P: n/a
Thank you. It works :-)
P

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

http://docs.python.org/lib/ctypes-st...es-unions.html
http://docs.python.org/lib/ctypes-pointers.html

From there on geting to this is farly straight forward:
Dec 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.