Get time from internet timeserver to use in javascript

6,455 views
Skip to first unread message

PVD

unread,
Mar 13, 2012, 9:36:12 AM3/13/12
to comman...@googlegroups.com
Hello,
 
It would be interesting to get an accurate time to use in our Command Fusion projects. Since it seems impossible to use the system clock of the iPad/iPod for this (http://www.ipadforums.net/ipad-os/9422-system-time.html), it would be nice to get a more accurate clock; I assume from the web.
 
I'm wondering and would be very pleased if it would be possible to get the current time (and date) from an internet time server (just like PC's do to sync their internal clock). This time could then be used to update de time of the Luxom home control system.
The Luxom system has a built in clock, but unfortunately, it's not very accurate (at all). So it would be nice to have a correct time and write it to the system now and then (let's say, when pressing a button, or even automatically when opening the project or a specific page).
 
Can you show me how it would be possible to get the current time and date using javascript?
 
If we would add the script (to sync the Luxom time to the time we get from a timeserver), and we'd place it in the Global Token list, when will it be executed? Only on startup of iViewer on iPod/iPad, or also when getting back from standby (in multitasking mode)?
 
Thanks for your support!

Barry Gordon

unread,
Mar 13, 2012, 9:57:46 AM3/13/12
to comman...@googlegroups.com
I use the iPad clock via the js date object and find it very accurate, well within a second or so as related to other clocks in the house. A URL retrieval from the NIST time clock should be fairly trivial to implement in JavaScript, I have the code in vb.

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "CommandFusion" group.
To view this discussion on the web visit https://groups.google.com/d/msg/commandfusion/-/QHGcKw0yUnIJ.
To post to this group, send email to comman...@googlegroups.com.
To unsubscribe from this group, send email to commandfusio...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/commandfusion?hl=en.

Nicolas Mauricio Cuadrado Avila

unread,
Mar 13, 2012, 10:30:10 AM3/13/12
to comman...@googlegroups.com
Just like Barry said, you can use JS Date object,

Here's an JS file in wich i use that object, it could be useful for you.

Regards
--
Nicolas Mauricio Cuadrado Avila
Area de Desarrollo
IT Espacios

Fecha_Clima.js

PVD

unread,
Mar 13, 2012, 7:27:09 PM3/13/12
to comman...@googlegroups.com
Thanks for your reply!

I know I can use the system clock, but since this clock is not sync'ed (or only during USB connection to a pc), I'd prefer to get the time and date from a timeserver.
When I read some forums, a lot of people are complaining 'bout this issue > so this means to me we can't trust the iPad/iPod internal clock...

Would that be possible from js?

Jarrod Bell

unread,
Mar 14, 2012, 8:06:58 AM3/14/12
to comman...@googlegroups.com
If you can find a time server feed, either a website, RSS feed, etc,
then you can scrape it with JS.

Jarrod

Jordan

unread,
Mar 14, 2012, 8:31:39 AM3/14/12
to comman...@googlegroups.com
You can give a try at this:

http://json-time.appspot.com/

I cannot vouch for accuracy as I've never used it, but it's a start

PVD

unread,
Mar 15, 2012, 7:16:40 AM3/15/12
to comman...@googlegroups.com
Thanks Jordan,

I've tried the json-time url you provided and started to write some code to do an XMLHttpRequest to this URL.
After some trial-and-error, I found a way to get the response into CF and did some improvements afterwards.

Because it might be interesting for other developers, I'll explain how my program works now:
  • I wrote a little php-script to run on a webserver I manage and I know is very stable AND I know the server-clock is quite accurate (because of time-sync to a time-server).
  • This way I’m not depending on the URL you provided, as I’m not sure how stable it works/will work in the future.
  • On a button press, I call a JS function which sends an XLMHttpRequest to the URL on my hosting
  • The URL is extended with a parameter tzo (= Time Zone Offset), which I get from the system through JS
  • The response of this request is nothing more than the server date&time, corrected with the tzo. So from this URL, I get back the current exact time in the timezone the device is configured in.
  • I do some calculations on the time to convert it to the communication-protocol of my home control system
  • I write this converted string to the bus, which sets the internal system time of my system to match the value I got back from the web-request
  • I even extended this a little more by doing a check if the request went fine. If – for some reason – I did not get back a date from the request, I’m using the iPad’s internal clock as a backup.
Tested this (also put my device in different time zones) and it works like a chime!

PVD

unread,
Mar 15, 2012, 7:31:30 AM3/15/12
to comman...@googlegroups.com
Still 2 more questions:

1. XMLHttpRequest readystate

I first tried to write an XMLHttpRequest with an onreadystatechange, but I did not manage to get this to work:

  var xmlHttp = null;

  function httpGet(url) {
    xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = processHttpRequest;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    }

  function processHttpRequest() {
    self.log(xmlHttp.readyState)
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      setLuxomClock(xmlHttp.responseText);
      }
    }

Via the self.log, I only get a "0" returned. A state "4" is never captured and the function setLuxomClock will not be executed...
I then tried to do it a little more direct, and this seems to work, but not sure if this is the most stable way to do it?

  var xmlHttp = null;
 
  function httpGet(url) {
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", url, false);
    xmlHttp.send(null);
    setLuxomClock(xmlHttp.responseText);
    }
   

2. Execute a script when awaking


Is there a way in CF to execute a script when the iPad/iPod returns from a standy (in multitasking mode)?
I know I can add a script in the Global Token list, but I guess this will only be executed when iViewer is closed and reopened, right?

Thanks!

Jarrod Bell

unread,
Mar 15, 2012, 8:09:15 AM3/15/12
to comman...@googlegroups.com
Use CF.request as documented in our JS API docs, not any of the default JavaScript HTTP request functions. This is more forgiving for security limitations browsers impose.

Look at the docs - there is a GUIResumedEvent you can watch and run code from there.

http://www.commandfusion.com/docs/scripting/index.html

Jarrod
--
You received this message because you are subscribed to the Google Groups "CommandFusion" group.
To view this discussion on the web visit https://groups.google.com/d/msg/commandfusion/-/qqvzKEr-ViIJ.

PVD

unread,
Mar 15, 2012, 9:22:06 AM3/15/12
to comman...@googlegroups.com
Thanks Jarrod!

PVD

unread,
Mar 15, 2012, 10:49:03 AM3/15/12
to comman...@googlegroups.com
When using the CF.request() like explained here:
http://www.commandfusion.com/docs/scripting/net.html#cF.request (section Simple request),
would it also be possible to pass variables through the function testWebCallback, like: textWebCallback(intNum,strTxt)?

Florent Pillet

unread,
Mar 15, 2012, 1:58:00 PM3/15/12
to comman...@googlegroups.com
You don't need to if you inline the function in the CF.request() call, you can use variables
that are local to the calling context. It's one of the subtleties of Javascript, very powerful
but can also lead to subtle bugs. Here's an example nonetheless:

var someInt = 5;
var someTxt = "hello";
CF.request("http://www.mysite.com/gimme?somedata", function(status, headers, body) {
CF.log("request returned status: " + status);
CF.log("someInt="+someInt+", someTxt="+someTxt);
});

--
Florent Pillet - Software Engineering Lead
www.commandfusion.com

Jordan

unread,
Mar 15, 2012, 2:16:23 PM3/15/12
to comman...@googlegroups.com
Florent, I assume that this isn't true if I enter a previously defined function as the callback (i.e. a resusable function). Is it possible to pass additional parameters after status, headers, body (using CF.request as example)?


Il giorno martedì 13 marzo 2012 14:36:12 UTC+1, PVD ha scritto:
Il giorno martedì 13 marzo 2012 14:36:12 UTC+1, PVD ha scritto:

Florent Pillet

unread,
Mar 15, 2012, 3:53:53 PM3/15/12
to comman...@googlegroups.com
That is correct, you need to define a lamba function in the local context.

And no, you can't pass extra parameters for a generic callback function to receive.
If you want to do this for clarity and reusability, here is a trick you can play:

function textWebCallback(status, headers, body, extraNum, extraTxt) {
// ... do something here ...
}

function doSomeRequest(url, extraNum, extraTxt) {
CF.request(url, function(status, headers, body) {
textWebCallback(status, headers, body, extraNum, extraTxt);
});
}

Is it clear for you? You just use a mini lambda-function that will capture extraNum and
extraTxt and pass them along with the rest of the request results to you actual processing
function.

Florent

On Mar 15, 2012, at 7:16 PM, Jordan wrote:

> Florent, I assume that this isn't true if I enter a previously defined function as the callback (i.e. a resusable function). Is it possible to pass additional parameters after status, headers, body (using CF.request as example)?

--

Jordan

unread,
Mar 15, 2012, 4:03:22 PM3/15/12
to comman...@googlegroups.com
That's perfectly clear, thanks!

PVD

unread,
Mar 16, 2012, 2:19:42 AM3/16/12
to comman...@googlegroups.com
Even clear to me ;-)

Thanks a lot, Florent!

Reply all
Reply to author
Forward
0 new messages