Get date from internet?

Just starting out? Need help? Post your questions and find answers here.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Get date from internet?

Post 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?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Get date from internet?

Post 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/
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Re: Get date from internet?

Post by dhouston »

http://davehouston.org
Mac Mini (Intel) 10.6.8 - iMac G4 (PPC) 10.4.11
Dell Dimension 2400 W98SE,W2K,XP,Vista,W7,Debian,Ubuntu,Kubuntu,Xubuntu,Fedora,Mandriva,Mint
(on swappable HDDs)
Vizio VTAB1008 - Android 3.1
MK808 miniAndroidPC (Android 4.1)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Get date from internet?

Post 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:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Get date from internet?

Post by MachineCode »

Looking good, ts-soft! :) Thanks to all who replied.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Get date from internet?

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Get date from internet?

Post by ts-soft »

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Get date from internet?

Post 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?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Get date from internet?

Post by MachineCode »

I don't need those sites. Women flock to me naturally.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Get date from internet?

Post 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
Last edited by Foz on Sun May 22, 2011 1:28 pm, edited 1 time in total.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Get date from internet?

Post 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. :)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Get date from internet?

Post 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
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply