Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #8505

Re: getday() doesn't agree with system clock

Message-ID <3002626.SPkdTlGXAF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-21 10:20 +0100
Subject Re: getday() doesn't agree with system clock
Newsgroups comp.lang.javascript
References <thqjc7tjcififr5ms9bbrs7v46ivmopfj6@4ax.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Swifty wrote:

> Source page: http://www.swiftys.org.uk/retire
> 
> At the bottom of the page, is:
> 
>   var day = weekday();
>   setInterval('getCurrentTime()', 1000);
> 
> Which uses:
> 
>   function weekday() {
>   var date = new Date();
>   var dd = date.getDay();
>   return dd;
> 
> getCurrentTime does this (amongst other things)
> 
>   function getCurrentTime()
>   {
>   ...
>   var newday = weekday();
>   if (newday !== day) window.location.reload();
>   }
> 
> The idea is to reload the page shortly after midnight. Last night, it
> reloaded at 23:59:55 ...
> I know this, because the page is created by a CGI script which writes
> out the time that the page loaded, as part of the page.

Your logic is flawed.  The client-side system clock (which Date uses) and 
the server-side system clock (which the CGI script uses) do not need to 
match.  If the client-side clock was ahead of the server-side clock only
as much as 5 seconds, that would already account for your observations.
 
> I've checked that the API in the scripting language is accurate;

I have no idea what that could possibly mean.

> it matches the system clock to within 300mS at the very least (judged by

The prefixed time unit "millisecond" is written `ms' in the Système 
international d’unités (SI), _not_ `mS'.  There should be a (if possible, 
thin, non-breaking) space between number and unit: `300 ms'.

<http://en.wikipedia.org/wiki/International_System_of_Units>

> eye, comparing my code against the time in the Windows start bar.

That display has been observed to be off the system clock as well, 
particularly with DST switching.  It would be best to compare against the 
clock in the configuration window that appears when you select the 
corresponding menu item in the time display's context menu, or (better) 
against the output of a COMMAND/CMD shell script repeatedly printing the 
system time.

> So, how come the Javascript "jumped the gun" by at least 5 seconds in
> determining the Day?

Most certainly it did not.
 
> Presumably, Javascript is implemented differently in different browsers;

As I have said often before (but what still does not appear to get through 
to some people): *There* *is* *no* (single) *"Javascript"* programming 
language.  

Your presumption is correct only insofar that different runtime environments 
do indeed use different *ECMAScript* implementations.  One of those 
ECMAScript implementations, found in Mozilla-based browsers like Firefox, is 
_JavaScript_.  Others include Microsoft JScript in MSHTML-based browsers 
like IE, Google V8 in Chromium-based browsers, Apple JavaScriptCore in 
Safari and Mobile Safari-based browsers (like the Android browser and its 
derivatives, e. g. Dolphin Browser), Opera ECMAScript in Opera browsers, and 
KJS in Konqueror:

<http://PointedEars.de/es-matrix>

However, at least the mentioned ECMAScript implementations do not appear to 
vary in their implementation of the ECMAScript Date object or its getDay() 
prototype method in a way that either could be 5 seconds (or more) off the 
clock of the operating system they are used with.  (CMIIW)

> should I take this up with the authors of the browser that I
> was using? (Opera; latest public release)

No, you should first synchronize the client-side and server-side clock, or 
(easier) account for the difference between them by generating client-side 
script code to reflect the server-side value and doing the proper client-
side computation.


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

getday() doesn't agree with system clock Swifty <steve.j.swift@gmail.com> - 2011-11-21 06:19 +0000
  Re: getday() doesn't agree with system clock Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-21 08:49 +0000
    Re: getday() doesn't agree with system clock Swifty <steve.j.swift@gmail.com> - 2011-11-21 13:19 +0000
  Re: getday() doesn't agree with system clock Elegie <elegie@anonymous.invalid> - 2011-11-21 09:53 +0100
    Re: getday() doesn't agree with system clock Scott Sauyet <scott.sauyet@gmail.com> - 2011-11-21 05:25 -0800
      Re: getday() doesn't agree with system clock Elegie <elegie@anonymous.invalid> - 2011-11-21 19:45 +0100
  Re: getday() doesn't agree with system clock Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-21 10:20 +0100
    Re: getday() doesn't agree with system clock Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-21 11:11 +0100
  Re: getday() doesn't agree with system clock SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2011-11-21 10:43 +0100
  Re: getday() doesn't agree with system clock Dr J R Stockton <reply1147@merlyn.demon.co.uk> - 2011-11-22 20:05 +0000
    Re: getday() doesn't agree with system clock Swifty <steve.j.swift@gmail.com> - 2011-11-23 05:24 +0000
      Re: getday() doesn't agree with system clock Zlatko Đurić <zladuric@gmail.com> - 2011-11-23 22:30 +0100
        Re: getday() doesn't agree with system clock Swifty <steve.j.swift@gmail.com> - 2011-11-24 06:15 +0000

csiph-web