I want to get Internet time and change my system clock when the app runs… is it possible ?
thanks
I want to get Internet time and change my system clock when the app runs… is it possible ?
thanks
Yes. You can set the system date & time via a shell command with elevated UAC privileges (run as administrator).
If the Application is running on the Windows platform, and you would prefer to use a Declare rather than a shell command, then SetSystemTime in the Kernel32 Library could do the trick. Using this approach, the application would also need privileges.
hmm… intresting… I read that how should I type the declare ?
Declare Function SetSystemTime Lib “Kernel32” (d As string) ? (where d = ‘1997-07-16T19:20:30+01:00’ )
well… I tried like this…
Declare Function SetSystemTime Lib “Kernel32” (d As CString) as Integer
ok = SetSystemTime(“2015-07-22T17:39:33+01:00”)
but it didn’t work…
SetSystemTime requires that you pass a pointer to a structure of type SystemTime. Please refer to this page and this page.
The argument to SetSystemTime is not a string, it’s a pointer to a 16-byte data structure. Use the GetSystemTime function to get a structure representing the current time, modify it, and then pass it to SetSystemTime. e.g.:
//untested forum code
Declare Sub GetSystemTime Lib "Kernel32" (Time As Ptr)
Declare Function SetSystemTime Lib "Kernel32" (Time As Ptr) As Boolean
Dim time As New MemoryBlock(16)
GetSystemTime(time)
time.UInt16Value(8) = 7 // change the hour which is 2 bytes at offset 8
If Not SetSystemTime(time) Then
MsgBox("Time set failed")
End If
Note that times are represented in UTC. To deal with the user’s timezone, use the similar SetLocalTime function.
cool! That works fine… but how can I know the offset for the minute, second, day, etc ?
It’s in the documentation I linked to.
The user may not appreciate that his clock has been set without his will. Even if only during your app run, he may have apps that rely on the precision of his clock that you will send to kingdom come. Not to mention his file date messed with.
Is there a special reason for not using date or Xojo.Core.Date with a local ?
My app needs the clock to be well set. I was just wondering If I could warn and give the option to the user to set it up automatically.
That’s your problem, not your user’s. Do not change their clock.
If you don’t trust your user’s system clock, poll for the internet time, and then note the offset between internet time and system time. You can then use that offset for your time calculations throughout the app.
I did have that idea in mind… I just needed someone to confirm that was the right approach. I’ll go that way… But also I wanted to experiment with the SetSytemTime declare, as I am new to that and was curious to see how it works…
Thanks to all.