Script to Tell Tell Time then Limit by it?

M

Mike A

Hi,
I'm hoping someone can help me with this.

I have a URL for which I'd like to limit access to by time. For
example,say I have a URL that I don't want accessable on Monday mornings
between 10am-noon and Fri. afternoons between 2-4pm. So when someone clicks
on the URL during those times a message pops up saying somthing like "sorry we're
closed now etc"

Is this possible? if yes, I'm guessing I'll need somesort of "onclick" for the
href and a script to check the sys time/date ?

TIA,
Mike
 
M

Michael Winter

on 15/11/2003:
Hi,
I'm hoping someone can help me with this.

I have a URL for which I'd like to limit access to by time. For
example,say I have a URL that I don't want accessable on Monday
mornings between 10am-noon and Fri. afternoons between 2-4pm. So
when someone clicks on the URL during those times a message pops up
saying somthing like "sorry we're closed now etc"

Is this possible? if yes, I'm guessing I'll need somesort of
"onclick" for the href and a script to check the sys time/date ?

Be aware that it is much better to do this server-side. Many users
routinely disable JavaScript, so your restrictions will be ignored.
However, if your host doesn't allow server-side processing, you might
do something like:

// Returns boolean false if the website
// is closed, true otherwise
// All comparisons are done using UTC (GMT).
function isAccessable()
{
var now = new Date(); // Get current time and date

select( now.getUTCDay() )
{
case 1: // Monday
// Time between 10:00:00 and 11:59:59 UTC
if(( 10 <= now.getUTCHours() ) && ( 12 > now.getUTCHours() ))
return false;
break;
case 5: // Friday
// Time between 14:00:00 and 15:59:59 UTC
if(( 14 <= now.getUTCHours() ) && ( 16 > now.getUTCHours() ))
return false;
break;
}
window.alert(
'Sorry, this site is closed between the following times (GMT):\n'
+ '\n10:00 and 12:00 on Mondays'
+ '\n14:00 and 16:00 on Fridays' );
return true;
}

....and use a link like:

<A href="my-time-restricted-page.html"
onclick="return !isClosed();">Enter</A>

One last point: as I've learnt recently, operations involving dates
can be very tricky. Although you shouldn't have any trouble here (the
comparisons are quite simple in the example above), you might want to
take a look at the 'Date and Time' section of Dr J R Stockton's
JavaScript Index (http://www.merlyn.demon.co.uk/js-index.htm).

Hope that helps,

Mike
 
F

Fabian

Mike A hu kiteb:
Hi,
I'm hoping someone can help me with this.

I have a URL for which I'd like to limit access to by time. For
example,say I have a URL that I don't want accessable on Monday
mornings
between 10am-noon and Fri. afternoons between 2-4pm. So when someone
clicks
on the URL during those times a message pops up saying somthing like
"sorry we're closed now etc"

Is this possible? if yes, I'm guessing I'll need somesort of
"onclick" for the href and a script to check the sys time/date ?

How do you plan to account for different time zones? How do you plan to
account for computers whose clocks are set incorectly?
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
I have a URL for which I'd like to limit access to by time. For
example,say I have a URL that I don't want accessable on Monday mornings
between 10am-noon and Fri. afternoons between 2-4pm. So when someone clicks
on the URL during those times a message pops up saying somthing like "sorry
we're
closed now etc"

Is this possible? if yes, I'm guessing I'll need somesort of "onclick" for the
href and a script to check the sys time/date ?


Firstly, what do you mean by "Monday"? On the WWW, a day lasts for
about 48 hours, starting at 0000h in or to the Eastward of NZ and
finishing at 2400h somewhere South of the Aleutians.

Probably you mean Monday GMT; or Monday your local time; or Monday in
server's local time; or Monday in some other location; or Monday in your
user's time - and, with all but the first, there is the Summer/Winter
question.

What you ask is, of course, impossible. If I know that URL, I can put
it on a page of my own, and I can click on it at any time I like; even
when I am not connected to the Net. You can, in principle, arrange for
that URL to give a modified 404 response at those times (GMT, server
local, or your local; the user's time is AFAIK unknowable at that
stage). That will need some sort of server-side code.


You can put script in the page itself (which will only work if the user
executes script) to do a pop-up instead of showing the page (unless the
user has a pop-up killer), based on time read from the user's clock and
interpreted as local, or GMT, or elsewhere. The user can defeat this by
setting his clock differently.

You can put the URL on a page of your own, with code such that the click
only works in allowable hours; that's easily defeated

Using user's local time, to determine whether the page should be shown:

T = new Date() // Now
D = T.getDay() // Sun=0..Sat=6
H = T.getHours() // 0..23
Not = D==1 ? H>10 && H<12 : D==5 ? H>14 && H<16 : false
or
HoW = D*24+H
Not = (HoW>34 && HoW<36) || (HoW>134 && HoW<136)

then maybe

if (Not) { DoPoP() ; location.href = ... }

For GMT, use the getUTC functions instead. For remote time, adjust the
UTC to that, see via below.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
"Mike A" wrote on 15/11/2003:
Be aware that it is much better to do this server-side. Many users
routinely disable JavaScript, so your restrictions will be ignored.

It is possible to have a lead-in page which uses javascript to call the
real page provided that the time is right. If the lead-in page appends
the time_t of call to the URL, and the real page checks early in loading
that the present time is no earlier than that and not much later
aborting otherwise, modest security is obtained. The time can be
encoded in Base 36 and shuffled, to make the situation obscure. Non-JS
users then do not see the real page, and cheating is non-trivial.


select( now.getUTCDay() )

I'm not familiar with that use of "select"; I'd use "switch".
// Time between 10:00:00 and 11:59:59 UTC

Not really. Time >=10h & <12h.
if(( 10 <= now.getUTCHours() ) && ( 12 > now.getUTCHours() ))
window.alert(
'Sorry, this site is closed between the following times (GMT):\n'
+ '\n10:00 and 12:00 on Mondays'
+ '\n14:00 and 16:00 on Fridays' );
return true;
}

AFAICS, that alert is given when the site is accessible.

On the whole, only tested code is worth posting.
 
M

Michael Winter

Dr John Stockton wrote on 16 Nov 2003:
JRS: In article
<[email protected]>, seen in



It is possible to have a lead-in page which uses javascript to
call the real page provided that the time is right. If the
lead-in page appends the time_t of call to the URL, and the real
page checks early in loading that the present time is no earlier
than that and not much later aborting otherwise, modest security
is obtained. The time can be encoded in Base 36 and shuffled,
to make the situation obscure. Non-JS users then do not see the
real page, and cheating is non-trivial.




I'm not familiar with that use of "select"; I'd use "switch".

Simple confusion between languages.
Not really. Time >=10h & <12h.

That is, for all intents and purposes, the same thing. If the hour
is, or is after 10, and at any time during the 11th hour, it is as I
described (though more correctly 11:59:59.999) and evaluated below.
AFAICS, that alert is given when the site is accessible.

You would be correct. If you check the HTML snippet I included, the
function name is 'isClosed()'. I thought that I'd reverse the logic
to make it easier to use, but I obviously forgot to negate the
comparisons. Each if statement should be corrected to:

if( !( <original expression )) { statements }

Thank you for bringing attention to my lapse in concentration: I've
informed the original poster by e-mail (he contacted me after I made
my post).

Mike
 
M

Michael Winter

I really did make a pigs ear of my original code: all returns need to
have their values inverted too.

I truly am sorry about this. I will make sure that I review and test
any code in my responses thoroughly. I will refrain from posting
further unless I am as certain as I can be about the validity of the
advice or suggestions I give.

Sincerly,
Michael Winter
 
M

Mike A

Ok, As a newbie, I'll have to ask as I'm now totally confused.
I'm trying to edit Michael Winter's original code with the edits
he suggested (in a previous post) with no luck, would someone
be kind and assist?

TIA,
Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
Ok, As a newbie, I'll have to ask as I'm now totally confused.
I'm trying to edit Michael Winter's original code with the edits
he suggested (in a previous post) with no luck, would someone
be kind and assist?

That may be sub-optimal.

To the article to which the above is a response, add :

or
HoW = D*100 + H
Not = (HoW>110 && HoW<112) || (HoW>514 && HoW<516)

or
HoW = D + H/100
Not = (HoW>1.10 && HoW<1.12) || (HoW>5.14 && HoW<5.16)

Note that the numbers are of the form <Day-of-JS-Week><Hours>; cf.
<URL:http://www.merlyn.demon.co.uk/js-date1.htm#DC> and
<URL:http://www.merlyn.demon.co.uk/js-date1.htm#TCp>.
 
M

Michael Winter

Mike A wrote on 17 Nov 2003:
Ok, As a newbie, I'll have to ask as I'm now totally confused.
I'm trying to edit Michael Winter's original code with the edits
he suggested (in a previous post) with no luck, would someone
be kind and assist?

TIA,
Mike


Unless someone can find any more errors (I did boundary test it) or I
overlooked something, this should be fine (under Opera 7.22 and IE 6,
at least). Any comments relating to date operations are also welcome.


The script:

function isAccessable()
{
var now = new Date(); // Get current time and date

switch( now.getUTCDay() )
{
case 1: // Monday
// Time between 10:00:00 and 11:59:59.999 UTC
if( !(( 10 <= now.getUTCHours() ) && ( now.getUTCHours() < 12 )))
return true;
break;
case 5: // Friday
// Time between 14:00:00 and 15:59:59.999 UTC
if( !(( 14 <= now.getUTCHours() ) && ( now.getUTCHours() < 16 )))
return true;
break;
}
window.alert(
'Sorry, this site is closed between the following times (GMT):\n'
+ '\n10:00 and 12:00 on Mondays'
+ '\n14:00 and 16:00 on Fridays' );
return false;
}


The HTML:

<A href="my-time-dependant-page.html"
onclick="return isAccessable();">Enter</A>

I really am sorry about the mix up. Be warned: when entering a month,
remember that they're zero based (0 - January, 1 - February, etc).

Also remember to convert all times to UTC (GMT) so the comparisons
are applicable all over the world.

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
Unless someone can find any more errors (I did boundary test it) or I
overlooked something, this should be fine (under Opera 7.22 and IE 6,
at least). Any comments relating to date operations are also welcome.

I think you overlooked the possibility that it might be Tuesday, for
example; also Wed Thu Sat Sun.

Every switch statement should have a default: at the end, unless it is
absolutely certain that all possible cases are covered, and will remain
covered, above. If it is intended that all cases be covered above, use
default: alert('Blunder') // or suchlike.

You probably need :
default: return true


<A href="my-time-dependant-page.html"
onclick="return isAccessable();">Enter</A>

isAccessable should be isAccessible
dependant should be dependent
 
M

Mike A

Thank you all for assisting me with this script. I'm in the process of
testing
it (Michael's last updated Usenet post) and Have a few followup
questions/uncertainties:

1. The href in question should only be avail on mondays 10-noon and
fridays 2-4pm, any other times the window.alert should appear. I think
this is working correctly, based on the code, is this correct?

2. I'm testing the various days/time periods by manually changing the
clock on my PC (windoze xp)I'm having problems when I change the time
on any given day. i.e. say I change the day to fri, and the time to
1pm, I should get the window.alert but I don't Is this normal?

Thank you for your patience.
Mike
 
G

Grant Wagner

Mike said:
Ok, As a newbie, I'll have to ask as I'm now totally confused.
I'm trying to edit Michael Winter's original code with the edits
he suggested (in a previous post) with no luck, would someone
be kind and assist?

TIA,
Mike

The code is pointless, since you're telling me when the page is
unavailable, I can simply adjust the clock on my PC to avoid your
"security".

Also, even if the target audience is in North America, NA consists
of at least 3 time zones, making "10am to noon on Monday"
meaningless, except to users in your local time zone, and even then,
meaningful only if the clock on their local PC is set accurately.

The only way to disable a page for a specific period of time (to
perform maintenance, updating or backups) is to disable the page on
the server, using some sort of server-side processing. If you have
no access to server-side processing (Perl, PHP, ASP, etc), then the
simplest solution is to simply swap a "This page not available" page
in over the page you want to disable while you are performing
whatever updates are necessary.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
M

Michael Winter

Dr John Stockton wrote on 18 Nov 2003:
JRS: In article
<[email protected]>, seen in


I think you overlooked the possibility that it might be Tuesday,
for example; also Wed Thu Sat Sun.

They were omitted on purpose. This was only an example - in fact, an
implementation of the example given by the original poster. I
expected additional cases to be added as necessary.

I should have also commented the if statements better:

// If time is not between 10:00:00 and 11:59:59.999 UTC, the site
// is accessible (returns true).
if( !(( 10 <= now.getUTCHours() ) && ( now.getUTCHours() < 12 )))
return true;

It's a minor change, but it explains the logic a little better.
Every switch statement should have a default: at the end, unless
it is absolutely certain that all possible cases are covered,
and will remain covered, above. If it is intended that all
cases be covered above, use
default: alert('Blunder') // or suchlike.

You probably need :
default: return true;

You are absolutely correct: a default case is required for the script
to work as desired. The default case should return boolean true (as
quoted above).

I didn't discover this because I only tested the days that had
conditions (serves me right for not preparing proper test cases).
isAccessable should be
isAccessible
dependant should be dependent

I didn't think I had spelt either correctly, but I was more concerned
about replying then ensuring correct spelling.

Mike
 
M

Michael Winter

Mike A wrote on 18 Nov 2003:
Thank you all for assisting me with this script. I'm in the
process of testing
it (Michael's last updated Usenet post) and Have a few followup
questions/uncertainties:

1. The href in question should only be avail on mondays 10-noon
and fridays 2-4pm, any other times the window.alert should
appear. I think this is working correctly, based on the code, is
this correct?

As Dr Stockton pointed out in his reply to the new code, I missed the
default case. The switch should read:

switch ( now.getUTCDay() )
{
case 1: // Monday
// If time is not between 10:00:00 and 11:59:59.999 UTC
// (inclusive), the site is accessible (returns true)

if ( !(( 10 <= now.getUTCHours()) && ( now.getUTCHours() < 12 )))
return true;
break;
case 5: // Friday
// If time is not between 14:00:00 and 15:59:59.999 UTC
// (inclusive), the site is accessible (returns true)

if ( !(( 14 <= now.getUTCHours()) && ( now.getUTCHours() < 16 )))
return true;
break;
default: // Other days have no restrictions
return true;
}

Now the script should only return false on Mondays, between 10:00 and
12:00, and Fridays between 14:00 and 16:00 (closed times). It will
return true at all other times.
2. I'm testing the various days/time periods by manually
changing the clock on my PC (windoze xp)I'm having problems when
I change the time on any given day. i.e. say I change the day to
fri, and the time to 1pm, I should get the window.alert but I
don't Is this normal?

The times the script checks are in the GMT time zone. You could
adjust your clock to cope with the difference in time zones, but
there is an easier way. It also means you don't have to play with
your system's clock.

Comment out the line beginning: var now = .... Add a new line after
it like this:

var now = new Date(
Date.UTC(year, month (zero-based), day, hour, min, sec, millisec));

If you entered: Date.UTC( 2003, 10, 21, 13, 0, 0, 0 ) [Friday, 21
November 2003 @ 1pm], you will get the alert. Just remember to change
back to "var now = new Date();" when you've finished testing.

Remember that using the UTC methods is the only way to ensure that
the date and times will be evaluted consistently across the world.
When you set the disallowed dates and times, change them from your
local time zone to GMT.

Mike
 
M

Michael Winter

Grant Wagner wrote on 18 Nov 2003:
The only way to disable a page for a specific period of time (to
perform maintenance, updating or backups) is to disable the page on
the server, using some sort of server-side processing.

The preferred use of server-side processing has already been
pointed out, but it appears that the original poster doesn't have
that ability.
If you have no access to server-side processing (Perl, PHP, ASP,
etc), then the simplest solution is to simply swap a "This page not
available" page in over the page you want to disable while you are
performing whatever updates are necessary.

I don't believe this is to allow updates. The OP asked for something
to disable pages on a regular, weekly schedule.

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen

Responses should go after quotes; corrected. Read the FAQ.

I'm in the process of
testing
it (Michael's last updated Usenet post) and Have a few followup
questions/uncertainties:

When responding to Michael, please follow-up to the relevant post of
Michael's - not to one of someone else's.

1. The href in question should only be avail on mondays 10-noon and
fridays 2-4pm, any other times the window.alert should appear. I think
this is working correctly, based on the code, is this correct?

Only, I think, if you are referring to code that I have not seen.

2. I'm testing the various days/time periods by manually changing the
clock on my PC (windoze xp)I'm having problems when I change the time
on any given day. i.e. say I change the day to fri, and the time to
1pm, I should get the window.alert but I don't Is this normal?

That is an ABOMINABLE technique; all sorts of other date/time-dependent
actions in your computer system may get fired or missed.

A function like isAccessible() should take the actual date through its
parameter(s), then a specific date can be given for test such as by
isAccessible(new Date("2003/11/21 13:00 GMT")); note that I use a form
in which each month is represented by its customary number, to avoid
confusion. In this case one might also get isInaccessible() to
alert(now) in order to be sure of the day of week being tested.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
The code is pointless, since you're telling me when the page is
unavailable, I can simply adjust the clock on my PC to avoid your
"security".

You can; but the practice is dangerous if any other date/time dependent
processes may be running.

Code as requested would not prevent access, but it would in practice
limit it to the persistent.

Also, even if the target audience is in North America, NA consists
of at least 3 time zones, making "10am to noon on Monday"
meaningless, except to users in your local time zone, and even then,
meaningful only if the clock on their local PC is set accurately.

Most North Americans don't believe in time zones other than their own
.... . The 48 States use 4 full zones, Canada adds 1.5 Eastwards, Alaska
adds 1 Westwards, and various islands add another, still within the 50
States. Then there's Greenland ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top