If you own a server (physical or virtual), you can make a page there, which returns the current time and call it from Xojo using its URL.
I was going to suggest something similar, and it doesn’t have to be fancy. Via PHP, it can be simply this:
<?php
print time();
?>
On the other end, simply:
var now as new DateTime( resultFromMyServer )
I’m sure this could be done with a Cloudflare worker. Free and 99.999% uptime
This is a good idea and I did setup a page on my hosting account to display the date when you go to the page. It displays the date time when I go to the page which is great. When I use the URLConnection example in Xojo, It doesn’t work. I’m still playing with the URLConnection as I’ve not used it before.
Feel free to tell us how it doesn’t work (error code, exception, empty or unexpected result, etc.).
When I try the following with any URL that should have time, it does not appear to raise any event mentioned in the documentation.
Blockquote
Dim MyConnection as New URLConnection
MyConnection.Send(“GET”, “http://worldtimeapi.org/api/timezone/America/Los_Angeles”)
Try using https
I actually tried that a few different URLs…so far, returns nothing and does not raise any of the documented events.
Do you get anything with SendSync?
I finally got it working after trying several different examples that google showed for the forum here.
Anyway, I have been able to successfully parse the date info out of text returned from
http://worldtimeapi.org/api/timezone/America/Los_Angeles
Seems to be working using fine.
Thanks for all the feedback and suggestions.
I just saw that. Looks like the site has been up since 2018. After reading their message, looks like I’d be better off creating my own page as Arnaud suggests. I have that working with a WordPress Page, but there is a slight delay of about 3 seconds. That should be acceptable for my purposes.
For some reason, as a simple HTML page with some Javascript to return the server time and date displays the information correctly, but SendSync does not return what’s visible on the page, it instead returns a bunch of code with no visible sign of the date.
I’ve tested in Mac and Linux and found Linux not working and receiving the errors shown in the capture in another entry of this thead.
So, I’ve set a breakpoint before and after self.connect and found:
In Linux, BEFORE .connect PORT = 123, but AFTER .connect PORT = 0
Is this a know UDPSocket issue? If so, another Linux glitch.
Perhaps someone knows how to solve/workaround this?
Javascript runs on the client, not the server. Use PHP to create the page instead.
Just a couple of thoughts:
- A possibility might be that you are getting UTF8 escape codes (?).
- Or perhaps some other type of character encoding problem.
- Could you post an example of what the “bunch of code” looks like?
- Quick-n-dirty PHP page which should run on any WordPress server:
<?php
echo gmdate('Y-m-d H:i:s'); // GMT/UTC time as SQL format "YYYY-MM-DD HH:mm:ss"
?>
Of course, you can format the gmdate() any other way you like, but the above is a simple option.
(Note: If you copy-and-paste the PHP code above, you’ll have to replace the “smart” quotes inserted by the forum editor with ordinary quotes.)
Well, I think I’ve catched it (the Linux problem).
The problem is port 123, that port (all <1024) can’t be used in Linux (Unix) if not running as root FOR LISTENING.
It seems it try to listen and that’s the season it fails. If I set a port >1024 it connects the UDP Socket (of course, doesn’t work with NTP)
That’s weird, so it seems to listen in .port and found no way to prevent this.
I’ve no services running in port 123, and fails with any port < 1024.
This of course assumes that the date/time on your server is also correct and set to UTC. That’s usually but not always the case.
Thanks Edward. I ended up using a simple plug-in to add the time/date to a page. Works fine, takes a few seconds but works fine for what I need.
Ideally your date/time should be packaged into a JSON message, so in case of some intercomm mess like some truncation or some server error instead of the expected content, you will easily detect a bad formed message and discard that value and do something else.
Or add your own prefix and suffix, like “;;;date&time;;;”.
Or follow simple and correct standards and use functionalities already baked in the language. A bad JSON string will rise an exception without you needing all that effort to recreate the wheel to check consistency.