Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setting the system clock on the fly
Message
From
06/08/2008 20:40:22
 
 
To
06/08/2008 18:32:42
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01337084
Message ID:
01337113
Views:
13
This message has been marked as the solution to the initial question of the thread.
>Hi all ,
>i need to set the system clock programmatically.
>I used a code that i remember was working but now i cannot have no clock change !!!
>I tried on Vista and ...well Vista is Vista and I gave up but i have problems also under win XP
>
>here is the code
>
>
>Function SetSystemDate
>  Lparameters tdDate
>  Declare Integer SetLocalTime In win32api String @ lpTime
>  Declare Integer GetLocalTime In win32api String @ lpTime
>  Local lpCurrent, lcNewTime
>  lpCurrent = Space(40)
>  GetLocalTime(@lpCurrent) && save current
>
>  lcNewTime = Num2Word(Year(m.tdDate))+;
>    Num2Word(Month(m.tdDate))+;
>    Num2Word(Dow(m.tdDate))+;
>    Num2Word(Day(m.tdDate))+;
>    Substr(m.lpCurrent,9)
>
>  SetLocalTime(@lcNewTime)
>  SetLocalTime(@lcNewTime)
>
>EndFunc
>
>Function Num2Word
>  Lparameters tnDecimal
>  Return Chr(m.tnDecimal%256)+Chr(Int(m.tnDecimal/256))
>EndFunc
>
>
>
>
>Why it does not work ? and...is there a different way to do it ?
>

This works for me:
Function SetTime(tnYear, tnMonth, tnDay, tnHours, tnMinutes, tnSeconds, tnMilliseconds)

	*!* SetTime(tDateTime)
	*!* SetTime(nYear, nMonth, nDay, nHours, nMinutes, nSeconds [, nMilliseconds]

	*!* SetLocalTime Function
	*!* _http://msdn.microsoft.com/en-us/library/ms724936(VS.85).aspx
	*!* SYSTEMTIME Structure
	*!* _http://msdn.microsoft.com/en-us/library/ms724950(VS.85).aspx
	*!* WM_TIMECHANGE Message
	*!* _http://msdn.microsoft.com/en-us/library/ms725498.aspx

	*!*	typedef struct _SYSTEMTIME {
	*!*	  WORD wYear;
	*!*	  WORD wMonth;
	*!*	  WORD wDayOfWeek;
	*!*	  WORD wDay;
	*!*	  WORD wHour;
	*!*	  WORD wMinute;
	*!*	  WORD wSecond;
	*!*	  WORD wMilliseconds;

	*!* "The wDayOfWeek member of the SYSTEMTIME structure is ignored."

	Local ltDateTime As Datetime, lcVartypes As String, lcSystemTime As String, lnRetVal As Integer

	If Vartype(m.tnYear) = "T" Then
		m.ltDateTime = m.tnYear
		m.tnYear = Year(m.ltDateTime)
		m.tnMonth = Month(m.ltDateTime)
		m.tnDay = Day(m.ltDateTime)
		m.tnHours = Hour(m.ltDateTime)
		m.tnMinutes = Minute(m.ltDateTime)
		m.tnSeconds = Sec(m.ltDateTime)
		m.tnMilliseconds = 0

	Else

		m.lcVartypes = ;
			Vartype(m.tnYear) + ;
			Vartype(m.tnMonth) + ;
			Vartype(m.tnDay) + ;
			Vartype(m.tnHours) + ;
			Vartype(m.tnMinutes) + ;
			Vartype(m.tnSeconds)

		If m.lcVartypes # "NNNNNN" Then
			Return .F.
		Endif

		If Vartype(m.tnMilliseconds) # "N" Then
			m.tnMilliseconds = 0
		Endif

	Endif

	Declare Integer SetLocalTime In win32api As apiSetLocalTime;
		String lpSystemTime

	m.lcSystemTime = ;
		Bintoc(m.tnYear, "2rs") + ;
		Bintoc(m.tnMonth, "2rs") + ;
		Bintoc(0, "2rs") + ;
		Bintoc(m.tnDay, "2rs") + ;
		Bintoc(m.tnHours, "2rs") + ;
		Bintoc(m.tnMinutes, "2rs") + ;
		Bintoc(m.tnSeconds, "2rs") + ;
		Bintoc(m.tnMilliseconds, "2rs")

	m.lnRetVal = apiSetLocalTime(m.lcSystemTime)

	Return m.lnRetVal # 0
Endfunc
Carlos Alloatti
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform