Page 1 of 1

Get date from internet?

Posted: Tue May 03, 2011 10:42 am
by MachineCode
How can my app get the real date from the internet? Reason being is my app needs to download data based on the current date, and if the user changes the date on their PC, my app will download the wrong stuff. So it needs to know the REAL date to avoid this problem. (One day's difference is okay, to account for timezone differences).

I was going to download the HTML source of http://www.timeanddate.com/worldclock/ and parse it (since it has the current UTC date and time in it), but is there a better way, in that I don't need to rely on this site?

Re: Get date from internet?

Posted: Tue May 03, 2011 12:23 pm
by IdeasVacuum
I think you are going to have to do as you described in order to get the latest date from the Internet, unless your customers do not mind also installing an Atomic Clock Util, which will help ensure their PC Clock is correct:

http://www.worldtimeserver.com/atomic-clock/

Re: Get date from internet?

Posted: Tue May 03, 2011 1:39 pm
by dhouston

Re: Get date from internet?

Posted: Tue May 03, 2011 2:18 pm
by ts-soft

Code: Select all

EnableExplicit

Procedure GetInternetTime(Server.s = "ptbtime2.ptb.de")
  Protected lSocket = OpenNetworkConnection(Server, 37)
  Protected *lBuffer, lNTPTime
  
  If lSocket
    Repeat
      Select NetworkClientEvent(lSocket)
        Case #PB_NetworkEvent_Data
          *lBuffer = AllocateMemory(5)
          If *lBuffer
            If ReceiveNetworkData(lSocket, *lBuffer, 4) = 4                       
              lNTPTime = (PeekA(*lBuffer + 0)) << 24
              lNTPTime + (PeekA(*lBuffer + 1)) << 16
              lNTPTime + (PeekA(*lBuffer + 2)) << 8
              lNTPTime + (PeekA(*lBuffer + 3))
              
              lNTPTime = AddDate(lNTPTime - 2840140800, #PB_Date_Year, 20)
              lNTPTime = AddDate(lNTPTime, #PB_Date_Hour, 2)
              
              FreeMemory(*lBuffer)
            EndIf
            CloseNetworkConnection(lSocket)
            Break
          EndIf
      EndSelect
    ForEver
  EndIf
  
  ProcedureReturn lNTPTime
EndProcedure

InitNetwork()
Debug FormatDate("%hh:%ii:%ss %dd.%mm.%yyyy", GetInternetTime())
Debug FormatDate("%hh:%ii:%ss %dd.%mm.%yyyy", GetInternetTime("time.fu-berlin.de"))
Better add a timeout to the loop :wink:

Re: Get date from internet?

Posted: Tue May 03, 2011 2:29 pm
by MachineCode
Looking good, ts-soft! :) Thanks to all who replied.

Re: Get date from internet?

Posted: Tue May 03, 2011 2:44 pm
by IdeasVacuum
Definitely the cleanest way code-wise, but determining the correct string (e.g. "time.fu-berlin.de") for the correct location is tricky I think. Using GetLocaleInfo_() only delivers what is set on the User's PC, it does not guarantee that, say, a PC with Spanish locale is actually in Spain and in a European Time Zone (I have customers in the USA who do not have the USA locale set).

Edit: ....but OK if you are sure being up to a day out (well, 23 hours?) does not affect matters for your app.

Re: Get date from internet?

Posted: Tue May 03, 2011 2:54 pm
by ts-soft

Re: Get date from internet?

Posted: Thu May 05, 2011 12:38 pm
by blueznl
When I need a date from the Internet I always go to russianbride.ru or adultfriendfinder.com... would your program work just as well?

Re: Get date from internet?

Posted: Thu May 05, 2011 1:38 pm
by MachineCode
I don't need those sites. Women flock to me naturally.

Re: Get date from internet?

Posted: Thu May 05, 2011 3:45 pm
by Foz
I had to do something similar about a year ago - my crappy laptop cmos battery gave out and reset the date time on every shutdown.

I couldn't be bothered to (a) buy a specific battery, and (b) unscrew the laptop, so I wrote my own start up process that would get the date & time and set the system date.

Code: Select all

; wait 5 seconds for the system to load

Delay(5000)

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

Define Event, Time, Size, String$, Inhalt
Define BufferSize = $1000, *Buffer = AllocateMemory(BufferSize)
Define RetryCount = 0

RetryConnection:

ConnectionID = OpenNetworkConnection("wwv.nist.gov", 13, #PB_Network_TCP)
If ConnectionID
  Repeat
    Event = NetworkClientEvent(ConnectionID)
    If Event = #PB_NetworkEvent_Data
      String$ = ""
      Repeat
        Size = ReceiveNetworkData(ConnectionID, *Buffer, BufferSize)
        String$ + PeekS(*Buffer, Size, #PB_Ascii) 
      Until Not Size
      
      ;Debug String$
      
      Quit = 1
    EndIf 
    
  Until Quit = 1 
  
  CloseNetworkConnection(ConnectionID)
  
  Define st.SYSTEMTIME
  st\wYear = Val("20" + Mid(String$, 8,2))
  st\wMonth = Val(Mid(String$, 11,2))
  st\wDay = Val(Mid(String$, 14,2))
  st\wHour = Val(Mid(String$, 17,2))
  st\wMinute = Val(Mid(String$, 20,2))
  st\wSecond = Val(Mid(String$, 23,2))
  
  ;Debug SetSystemTime_(@st)
  
  SetSystemTime_(@st)
Else
  If RetryCount < 10 ; if failed to connect, assume that the network hasn't connected yet and automatically retry
    RetryCount + 1
    Delay(1000)
    Goto RetryConnection
  EndIf
  
  If MessageRequester("Date Time Setter", "Can't find the server!", 5) = 4
    RetryCount = 0
    Goto RetryConnection
  EndIf
EndIf
*** edit: address changed due to site going offline

Re: Get date from internet?

Posted: Wed May 18, 2011 1:24 pm
by MachineCode
ts-soft wrote:lNTPTime = AddDate(lNTPTime - 2840140800, #PB_Date_Year, 20)
What does 2840140800 mean? And is "time.fu-berlin.de" reliable, both now and for the future? Is it a govt server? I need to know before I commit this code to my app. :)

Re: Get date from internet?

Posted: Fri Jun 10, 2011 1:43 pm
by MachineCode
I finally ended up solving this myself by buying a domain name and hosting a small PHP script that returns the server date. :P