Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I would like to display the server time(clock) using javascript/perl code.
Following HTML can be used to display the client clock time.
<HTML><HEAD><TITLE>Clock</TITLE> <SCRIPT> <!-- //Define a couple of global variables. var timerID = null var timerRunning = false function stopTimer(){ //stop the clock if(timerRunning) { clearTimeout(timerID) timerRunning = false } } function startTimer(){ // Stop the clock (in case it's running), then make it go. stopTimer() runClock() } function runClock(){ document.clock.face.value = timeNow() timerID = setTimeout("runClock()",1000) timerRunning = true } function timeNow() { //Grabs the current time and formats it into hh:mm:ss am/pm fo +rmat. now = new Date() hours = now.getHours() minutes = now.getMinutes() seconds = now.getSeconds() timeStr = "" + ((hours > 12) ? hours - 12 : hours) timeStr += ((minutes < 10) ? ":0" : ":") + minutes timeStr += ((seconds < 10) ? ":0" : ":") + seconds timeStr += (hours >= 12) ? " PM" : " AM" return timeStr } // End of custom functions, stop hiding code --> </SCRIPT> </HEAD> <BODY text=#000000 vLink=#cc0000 aLink=#3300ff link=#33ff00 bgColor=#f +fffff onload=startTimer() onunload=stopTimer()><FONT size=+1>Clock</FONT> <P> <CENTER> <P> <FORM name=clock><INPUT size=11 name=face></FORM> <P></P></CENTER></BODY></HTML>
Is there any method to display clock time from server.
Thanks

Replies are listed 'Best First'.
Re: Display clock
by Zaxo (Archbishop) on Sep 14, 2005 at 00:34 UTC
      Hi,
      I need to display a running clock.
      localtime module will display the current date and time.
      But time will not change.
      If I am wrong, please correct me
      Thanks
Re: Display clock
by pg (Canon) on Sep 14, 2005 at 04:24 UTC

    All you need to do is to send the server side time with the HTML. At the beginning of the JavaScript, calculate the difference between server time and the local client time. Within you loop, you just add this difference on top of the client time (which you got already with your existing script).

    Well, there is this time spent on the way of trasmission, which you probably have to ignore, and I don't think that's a big deal.

Re: Display clock
by chanio (Priest) on Sep 14, 2005 at 19:00 UTC
    Simply, provide the server's starting date in the javascript (see the example)! You need to write it with the expected format. Then, every page would start from that date, instead of from the user's one. Remember that javascript starts when the user loads the page. But, the server does all, before that.
    function timeNow() { //Shows the server's time and formats it into hh:mm:ss am/pm f +ormat. now = new Date($REPLACE_WITH_THE_SERVER'S_DATE) ...
    <UPDATED> You should write your code into a $scalar variable and first have your $REPLACE_WITH..._DATE loaded with your desired date (with the scalar localtime function). See the following one-liner (you can try it, first)...
    perl -e'print "Wed Sep 14 23:22:59 EDT 2005\n";my @ti=split(/\s+/,scal +ar localtime);my $year=pop(@ti);push(@ti,"EDT",$year);print join(" ", +@ti),"\n"' Wed Sep 14 23:22:59 EDT 2005 Mon Sep 26 21:01:16 EDT 2005
    Try it several times and you'll see that it changes to your actual date!

    You could do it better, for sure! But this simply shows how you could have the same format as the one needed for the js new Date() format...

    Why using such a horrible and long variable instead of writing the time? Because you are going to run the cgi script every time that a new user calls it. So you should display the actual server's time, and not the old one, Ok?

    If you need more acuracy, you should use:scalar localtime(time + $elapsed_seconds), where $elapsed_seconds would have the time that elapsed from the time when the server sends the html page to the time when the user sees it :). (TODO: read about perldoc POSIX and perldoc -f localtime)

    NOTICE: All these code intends to start the user's JS clock at the exact time that the user asks the page. If the clock starts at the correct time, it should update every second (depends on the js code, not on the server's code any more) to keep on being correct until the user leaves the page. What happens if the user saves a copy of the html page at his PC? Well, the clock would start again but with the old time, because the server does not display that page again for the user. But there are tricks to do it again, this way, also. Find out more about the "Web Site Story" somewhere where you could find javascript code, it is a rating technique.

      Hi,
      I followed your instruction and modified the function as follows.
      function timeNow() { //Grabs the current time and formats it into hh:mm:ss am/pm fo +rmat. // now = new Date() now = new Date('Wed Sep 14 23:22:59 EDT 2005') hours = now.getHours() minutes = now.getMinutes() seconds = now.getSeconds() timeStr = "" + ((hours > 12) ? hours - 12 : hours) timeStr += ((minutes < 10) ? ":0" : ":") + minutes timeStr += ((seconds < 10) ? ":0" : ":") + seconds timeStr += (hours >= 12) ? " PM" : " AM" return timeStr }
      This data (assume from Server) is for testing purpose. But the Clock display will not change. Clock display as 11:22:59 PM. How can I display the running clock?
      Please correct me if I am wrong.
      Thanks