Internet long date?

Hi,
I am currently using the code below to automatically set todays date as a variable (which will eventually get written to a text field).

It is paramount that the date is correct, and I have just realised that if the time / date on the user’s computer is wrong, the wrong date will be displayed.
Can someone point me to how I first check for an internet connection, and then if connected, fetch the correct date from the internet - whilst also keeping it in UK long date format?

Dim D As new Date Dim infoDate as String = D.LongDate

Thank you all in advance.

See this thread:

https://forum.xojo.com/4122-how-to-get-the-real-current-date

The easiest solution there is

Thanks Kem,
I think my best solution however, is to make the user select the date himself - that way, if it is wrong - they are to blame and not me :slight_smile:

I guess that’s right. It doesn’t hurt you to attempt to get it from the Internet first as a convenience though.

In the interest of completeness, this code works:

  dim url as String = "www.timeapi.org/utc/now"
  dim http as new HTTPSocket
  http.SetRequestHeader( "user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14" )
  
  dim r as string = http.Get( url, 10 ).Trim
  
  dim rx as new RegEx
  rx.SearchPattern = "(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})([+-]\\d{2}:\\d{2})"
  
  dim d as new Date
  dim match as RegExMatch = rx.Search( r )
  if match <> nil then
    dim offset as string = match.SubExpressionString( 7 ).Replace( ":", "." )
    d.GMTOffset = offset.Val
    
    d.Year = match.SubExpressionString( 1 ).Val
    d.Month = match.SubExpressionString( 2 ).Val
    d.Day = match.SubExpressionString( 3 ).Val
    
    d.Hour = match.SubExpressionString( 4 ).Val
    d.Minute = match.SubExpressionString( 5 ).Val
    d.Second = match.SubExpressionString( 6 ).Val
  end if
  d.GMTOffset = 0.0

  // d now has a date in GMT

wa wa wee wa !
First of all - thank you for spending the time to do that :slight_smile:

I have 2 questions though:

  1. Would that time adjust for british summertime when the clocks go back 1 hour etc?

  2. When in that code do I need to check for an internet connection?

Thank you Kem.

  1. No adjustment needed since that will ultimately return GMT time (offset 0.0) whether it gets it from the Internet or from the client’s machine.

  2. If there is no response, it will time out within 10 seconds (at most) and match will be nil because r will be empty. You can fiddle with the timeout time, but that site has a delay, probably to prevent rapid requests.

Another thing you can do is set up a special page on your own web site (assuming you have one) that returns the date/time of your web server. Presumably, that will always be right, and response will be nearly instant.

Again, for completeness, this PHP code will just print the current date/time in the same format:

<?php print date( "c", time() ) ; ?>

Kem,
I use a U.S server (MediaTemple) for my website, so using my website server is a no go - but that’s fine.

So basically, I would just need to add an Else statement to provide a no internet alternative?

I’m not sure why that’s a no-go. The date will be converted to GMT no matter where you get it from. The response just has to include the local offset which, if you use my code, it will.

You don’t really need an else. If there is no Internet connection, the http.Get should timeout pretty quickly.

What I meant was if I put an else statement in, then I can alternatively set it to the date which is set on the user’s PC.

Thanks for everything Kem :slight_smile:

No, I got what you meant, but the code does that already when it creates the new Date. That is already the user’s date/time, and that’s what will be used if there is no response from the server.

Ahhhhhhhhhh - in that case - that was definitely a complete solution :slight_smile:

Only complete solutions around here. :slight_smile:

I would expect nothing less from you, Kem :slight_smile: