Time Server's

Windows specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by scurrier.

ever see programs that go out and get the time from an internet time server
well here's on written in PB 3.2 enjoy

;
; Free Time
; Coded by Sean Currier
;

Buffer = AllocateMemory(0, 1000, 0)
Global buffer
Global port
Global tzone.l
Dim TimeZone.s(15)
timezone(1)="Atlantic Daylight Time"
timezone(2)="Atlantic Standard Time"
timezone(3)="Eastern Daylight Time"
timezone(4)="Eastern Standard Time"
timezone(5)="Central Daylight Time"
timezone(6)="Central Standard Time"
timezone(7)="Mountain Daylight Time"
timezone(8)="Mountain Standard Time"
timezone(9)="Pacific Daylight Time"
timezone(10)="Pacific Standard Time"
timezone(11)="Alaska Daylight Time"
timezone(12)="Alaska Standard Time"
timezone(13)="Hawaii-Aleutian Daylight Time"
timezone(14)="Hawaii-Aleutian Standard Time"
timezone(15)="Samoa Standard Time"

Procedure GetInternetTime()
Repeat
ConnectionID = OpenNetworkConnection("208.184.49.9", Port)
If ConnectionID
ReceiveNetworkData(ConnectionID, Buffer, 1000)
a$=PeekS(Buffer)
hour$=Mid(a$,17,2)
min$=Mid(a$,20,2)
sec$=Mid(a$,23,2)
month$=Mid(a$,11,2)
year$=Mid(a$,8,2)
day$=Mid(a$,14,2)
EndIf
SetGadgetText(2,hour$+":"+min$+":"+sec$)
hour=(Val(hour$)-tzone)
hour$=Str(hour)
If Len(hour$)=1
hour$="0"+hour$
EndIf
SetGadgetText(5,hour$+":"+min$+":"+sec$)
Delay(1000) ; the delay's are needed or the time server will drop the connection
ForEver
EndProcedure

Procedure CurrentTime()
Repeat
GetTime()
hour.l=Hour()
If Len(hour$)=1
hour$="0"+Str(hour)
Else
hour$=Str(hour)
EndIf
min.l=Minute()
min$=Str(min)
If Len(min$)=1
min$="0"+Str(min)
Else
min$=Str(min)
EndIf
sec.l=Second()
sec$=Str(sec)
If Len(sec$)=1
sec$="0"+Str(sec)
Else
sec$=Str(sec)
EndIf
Time$=hour$+":"+min$+":"+sec$
SetGadgetText(4,Time$)
Delay(1)
ForEver
EndProcedure


ThreadID1=CreateThread(@GetInternetTime(), 154)
ThreadID2=CreateThread(@CurrentTime(), 156)
Port = 13


If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf

If OpenWindow(1,250,450,400,250,#PB_Window_SystemMenu,"Free Time Written By Sean Currier")
If CreateGadgetList(WindowID())
;uttonGadget(1, 120, 220, 150, 25, "Up-Date System Time")
TextGadget(12,20,20,130,21,"Internet UTC Time",#PB_Text_Center )
TextGadget(2,20,40,130,21,"Checking.....",#PB_Text_Border ) ; this is the time that gets updated

TextGadget(7,20,65,130,21,"Your Current Time",#PB_Text_Center )
TextGadget(4,20,85,130,21,"Checking.....",#PB_Text_Border )

TextGadget(8,20,115,130,21,"The Correct Time",#PB_Text_Center )
TextGadget(5,20,135,130,21,"Checking.....",#PB_Text_Border )

TextGadget(6,200,20,130,21,"Pick Your Time Zone",#PB_Text_Center )
ComboBoxGadget(3,160,40,220, 180,#PB_ComboBox_UpperCase )
EndIf
EndIf
For x=1 To 15
AddGadgetItem(3, -1, TimeZone(x))
Next x
Result = SetGadgetState(3, -1)
;
; Main Loop
;
Repeat
WNDevent=WindowEvent()
gagetevent = EventGadgetID()
GagetState = GetGadgetState(3)
Select GagetState ; pick the time zone
Case 0
tzone=3 ;Atlantic Daylight Time subtract 3 hours from UTC
Case 1
tzone=4 ;Atlantic Standard Time subtract 4 hours from UTC
Case 2
tzone=4 ;Eastern Daylight Time subtract 4 hours from UTC
Case 3
tzone=5 ;Eastern Standard Time subtract 5 hours from UTC
Case 4
tzone=5 ;Central Daylight Time subtract 5 hours from UTC
Case 5
tzone=6 ;Central Standard Time subtract 6 hours from UTC
Case 6
tzone=6 ;Mountain Daylight Time subtract 6 hours from UTC
Case 7
tzone=7 ;Mountain Standard Time subtract 7 hours from UTC
Case 8
tzone=7 ;Pacific Daylight Time subtract 7 hours from UTC
Case 9
tzone=8 ;Pacific Standard Time subtract 8 hours from UTC
Case 10
tzone=8 ;Alaska Daylight Time subtract 8 hours from UTC
Case 11
tzone=9 ;Alaska Standard Time subtract 9 hours from UTC
Case 12
tzone=9 ;Hawaii-Aleutian Daylight Time subtract 9 hours from UTC
Case 13
tzone=10 ;Hawaii-Aleutian Standard Time subtract 10 hours from UTC
Case 14
tzone=10 ;Samoa Standard Time subtract 11 hours from UTC
Default
tzone=0
EndSelect

;Delay(1)
Until WNDevent=#PB_EventCloseWindow
KillThread(ThreadID1)
KillThread(ThreadID2)
End
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by scurrier.

you can now download the exe from the resource site
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.

which lib are you using, i copy the source and cant compile : )
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by scurrier.

go to the resource site and i think it's in system2
there should be a list of commands for useing local time
and remember you need to be connected to the internet
for this to work
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

Nice code.

Just for people who want to synchronize with such Internet time, if you use SetSystemTime_, you should not have to consider the time zone because Windows converts with your declared time zone from universal time.

I have made such a program for time synchronization.

One issue may be the accuracy of synchronization. Using different available servers allow to connect to the first accessible one with a low latency. This gives an average accuracy of 50 milliseconds when synchronizing.

The server used in the sample code is :

"nist1.ny.glassey.com", "208.184.49.9", "Abovenet, New York City"

You may embed scurrier code sample with the following list of servers :

"time-a.nist.gov", "129.6.15.28", "NIST, Gaithersburg, Maryland"
"time-b.nist.gov", "129.6.15.29", "NIST, Gaithersburg, Maryland"
"time-a.timefreq.bldrdoc.gov", "132.163.4.101", "NIST, Boulder, Colorado"
"time-b.timefreq.bldrdoc.gov", "132.163.4.102", "NIST, Boulder, Colorado"
"time-c.timefreq.bldrdoc.gov", "132.163.4.103", "NIST, Boulder, Colorado"
"utcnist.colorado.edu", "128.138.140.44", "University of Colorado, Boulder"
"time.nist.gov", "192.43.244.18", "NCAR, Boulder, Colorado"
"time-nw.nist.gov", "131.107.1.10", "Microsoft, Redmond, Washington"
"nist1.datum.com", "63.149.208.50", "Datum, San Jose, California"
"nist1.dc.glassey.com", "216.200.93.8", "Abovenet, Virginia"
"nist1.sj.glassey.com", "207.126.103.204", "Abovenet, San Jose, California"
"nist1.aol-ca.truetime.com", "207.200.81.113", "TrueTime, AOL facility, Sunnyvale, California"
"nist1.aol-va.truetime.com", "205.188.185.33", "TrueTime, AOL facility, Virginia"

The used tcp port is always the same according to corresponding RFC. Better use IP address instead of DNS Name in this case.

I suppose other available servers exist but with such a list you should never loose, it works for years.

Rgrds

Francois Weil
14, rue Douer
F64100 Bayonne
Post Reply