Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

synchronisation heure

8 réponses
Avatar
gilles
je suis =E0 la recherche d'un exemple de synchronisation de l'heure en
VB par rapport a l'heure atomique

merci d'avance

8 réponses

Avatar
Jacques
Bonjour,
voici quelques lignes qui pourraient t'aider ... Pas testé, sorry

Option Explicit

'''''Time Server Port 37 service (RFC 868)
'''''This is a sample project showing how to obtain the GMT
'''''and update the system clock from the port 37 service on
'''''Internet time servers using the winsock control.



Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As
SYSTEMTIME)
Private Declare Function SetSystemTime Lib "kernel32" (lpSystemTime As
SYSTEMTIME) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, _
Source As Any, ByVal Length As Long)
Private Declare Function GetTickCount& Lib "kernel32" ()

Private mlngSecondsDiffTotal As Long

Private msngTimeout As Single
Private mstrHosts(1 To 3) As String
Private mbIsDone As Boolean
Private mintCount As Integer
Private mintNumGood As Integer


Private Sub Command1_Click()
'Reset all counters
mbIsDone = True
mintCount = 0
mlngSecondsDiffTotal = 0
msngTimeout = Timer

Winsock1.RemotePort = 37

'Start the process
Timer1.Enabled = True

End Sub

Private Sub Form_Activate()
Static bNotFirst As Boolean





End Sub

Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 2000

'These hosts are working right now, but that may change.
'See http://www.eecis.udel.edu/~mills/ntp/clock1.htm for list of
servers.
'Use trial and error to find new servers as not all will support RFC 868
'(outdated as SNTP is preferred.)

mstrHosts(1) = "time-A.timefreq.bldrdoc.gov"
mstrHosts(2) = "tick.uh.edu"
mstrHosts(3) = "nist1.dc.certifiedtime.com"

If UCase(Command$) Like "*AUTO*" Then
Command1_Click
End If


End Sub

Private Sub Timer1_Timer()
Dim lngAvgDiff As Long
Dim dt As Date
Dim st As SYSTEMTIME

'Test for a timeout situation.
If Timer - msngTimeout > 20 Or Timer - msngTimeout < -86380 Then
sStatus "Request has timed out"

Debug.Print "Request has timed out"
mbIsDone = True
End If

'If IsDone, then can continue processing
If mbIsDone Then

'Do three different servers
'and take an average.
If mintCount < 3 Then

mintCount = mintCount + 1
Debug.Print mintCount
msngTimeout = Timer
mbIsDone = False
Winsock1.Close
Winsock1.RemoteHost = mstrHosts(mintCount)
sStatus "Host " & mstrHosts(mintCount)
Winsock1.RemotePort = 37
Debug.Print mstrHosts(mintCount)
Winsock1.Connect

Else

lngAvgDiff = CLng(CSng(mlngSecondsDiffTotal) / mintNumGood)
sStatus "Average difference in s: " & lngAvgDiff
Debug.Print DateAdd("s", lngAvgDiff, fGetGMTTime), " Average
difference in s: " & lngAvgDiff
'Add the average difference in seconds to the current system GMT
time.
dt = DateAdd("s", lngAvgDiff, fGetGMTTime)

st.wDay = Day(dt)
st.wMonth = Month(dt)
st.wYear = Year(dt)
st.wHour = Hour(dt)
st.wMinute = Minute(dt)
st.wSecond = Second(dt)
'This function takes GMT time as input and sets your system time
appropriately.
Call SetSystemTime(st)
sStatus "Average difference in s: " & lngAvgDiff & " Time
Synchronized."

Timer1.Enabled = False
If UCase(Command$) Like "*AUTO*" Then
Unload Me
End If


End If

End If
End Sub



Private Sub Winsock1_Connect()

Debug.Print "Connected"


End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)


Dim dblTime As Double
Dim lngTimeFrom1990 As Long
Dim lngTime As Long
Dim dtGMT As Date
Dim dtGMTLocal As Date
Dim st As SYSTEMTIME
Dim i As Integer
Dim strTime As String
Dim b() As Byte

If bytesTotal <> 4 Then
Winsock1.Close
Exit Sub
End If

Winsock1.GetData b(), vbByte, 4
'convert to unsigned long. Use double to prevent overflows.
dblTime = b(0) * 256 ^ 3 + b(1) * 256 ^ 2 + b(2) * 256 ^ 1 + b(3) * 256
^ 0

' Debug.Print dblTime - (DateDiff("s", 2, Now) + 4 * 3600)

On Error Resume Next
'Purpose for this is to avoid overflows with DateAdd
lngTimeFrom1990 = dblTime - 2840140800#
If Err = 6 Then
'No good data received from time server.
sStatus "Error in data returned."
Exit Sub
End If

Debug.Print dblTime

'calculate the GMT based on what was returned
dtGMT = DateAdd("s", lngTimeFrom1990, #1/1/1990#)
dtGMTLocal = fGetGMTTime

'Find the difference between system time and returned time.
Debug.Print DateDiff("s", dtGMTLocal, dtGMT) & " s difference"
sStatus DateDiff("s", dtGMTLocal, dtGMT) & " s difference"
'add to total for averaging.
mlngSecondsDiffTotal = mlngSecondsDiffTotal + DateDiff("s", dtGMTLocal,
dtGMT)
Debug.Print mlngSecondsDiffTotal
mintNumGood = mintNumGood + 1


mbIsDone = True

Winsock1.Close

End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String,
ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal
HelpContext As Long, CancelDisplay As Boolean)
' MsgBox Description
sStatus Description
Winsock1.Close
End Sub

Public Function fGetGMTTime() As Date
Dim st As SYSTEMTIME
Call GetSystemTime(st)
fGetGMTTime = DateSerial(st.wYear, st.wMonth, st.wDay) +
TimeSerial(st.wHour, st.wMinute, st.wSecond)

End Function

Public Sub sStatus(strMsg)
statSync.Panels(1).Text = " " & strMsg
End Sub

"gilles" a écrit dans le message de
news:
je suis à la recherche d'un exemple de synchronisation de l'heure en
VB par rapport a l'heure atomique

merci d'avance
Avatar
gilles
merci de ta rapidite et bonne année . je te tiens au courant si ca
fonctionne
bye et merci
Avatar
gilles
en fait active winsock me demande une license . Comment faire pour
contourner ce probleme

A plus
Avatar
3stone
Salut,

"gilles"
| en fait active winsock me demande une license . Comment faire pour
| contourner ce probleme


Je ne comprend pas toute la démarche...

WinXp le fait tout seul... et pour les autres, j'utiliserais un "micro-soft"
que l'on trouve partout sur le net et je l'appelerai par un simple Shell(...)


--
A+
Pierre (3stone) Access MVP
Perso: http://www.3stone.be/
Conseils MPFA: http://www.mpfa.info/
Avatar
gilles
cest un epu par plaisir et connaitre winsock alors si tu sais comment
je peux avoir cette license po pk ce pb apparait ca serait sympa

A plus

Salut,

"gilles"
| en fait active winsock me demande une license . Comment faire pour
| contourner ce probleme


Je ne comprend pas toute la démarche...

WinXp le fait tout seul... et pour les autres, j'utiliserais un "micro-s oft"
que l'on trouve partout sur le net et je l'appelerai par un simple Shell( ...)


--
A+
Pierre (3stone) Access MVP
Perso: http://www.3stone.be/
Conseils MPFA: http://www.mpfa.info/


Avatar
3stone
"gilles"
cest un epu par plaisir et connaitre winsock



ahhh si c'est pour le plasir...
http://www.frameip.com/vb_mode_tcp_udp/

;-)


--
A+
Pierre (3stone) Access MVP
Perso: http://www.3stone.be/
Conseils MPFA: http://www.mpfa.info/
Avatar
gilles
je peux toujours pas l'utilser pb de license ????
merci davance
Avatar
3stone
"gilles"
| je peux toujours pas l'utilser pb de license ????


Il s'installe normalement avec VB ou Visual Studio....


--
A+
Pierre (3stone) Access MVP
Perso: http://www.3stone.be/
Conseils MPFA: http://www.mpfa.info/