Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Date Problem

0 views
Skip to first unread message

Graham Stow

unread,
Oct 8, 2008, 12:04:56 PM10/8/08
to
The following script (or something much like it) runs continuously on a web
server and does something when the date changes (i.e. at the bewitching
hour). Problem is, it does it 2 or 3 times, when I want it to do it only
once. Any ideas anyone?

$start_date = "2008-09-20"; # this 'date' (string) will never change
and could be anything
&get_todays_date;
$date = $todays_date;
while ($date ne $start_date) { # the 2 'dates' will never equate, so
this script runs forever
&get_todays_date;
if ($date ne $todays_date) {
$date= $todays_date;
do something.....
}
}

sub get_todays_date {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year=$year+1900;
$mon=$mon+1;
if (length($mon) eq 1) {
$mon = "0"."$mon";
}
if (length($mday) eq 1) {
$mday = "0"."$mday";
}
$todays_date = "$year"."-"."$mon"."-"."$mday";
}

Tad J McClellan

unread,
Oct 8, 2008, 12:26:19 PM10/8/08
to
Graham Stow <graha...@gmail.com> wrote:
> The following script (or something much like it) runs continuously on a web
> server


That is likely to waste a boatload of cycles.

It would probably be better to run it once a minute or so using
whatever job scheduling your OS provides, such as "cron".


> and does something when the date changes (i.e. at the bewitching
> hour). Problem is, it does it 2 or 3 times, when I want it to do it only
> once. Any ideas anyone?


I do not see how it is happening more than once...


> $start_date = "2008-09-20"; # this 'date' (string) will never change
> and could be anything
> &get_todays_date;


You should not use an ampersand on subroutine calls unless you know
what it does, and what it does is what you want to do (it seldom is).

get_todays_date();


> $date = $todays_date;


Communication via global variables is a horrible idea. You should instead
communicate via subroutine arguments and return values:

$todays_date = get_todays_date();


> while ($date ne $start_date) { # the 2 'dates' will never equate, so
> this script runs forever


Why take that chance? If you want a truly infinite loop (which I don't
think you really need here), then

while ( 1 ) {
or even
while ( 'infinite' ) {

would be better.


> &get_todays_date;

$todays_date = get_todays_date();

> if ($date ne $todays_date) {
> $date= $todays_date;
> do something.....
> }
> }
>
> sub get_todays_date {
> ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
> $year=$year+1900;
> $mon=$mon+1;
> if (length($mon) eq 1) {
> $mon = "0"."$mon";

^^^^^^

perldoc -q vars

What's wrong with always quoting "$vars"?

So make that:

$mon = '0'. $mon;

or
$mon = "0$mon";

(but using printf/sprintf is Much Better than trying to do it yourself.)


> }
> if (length($mday) eq 1) {
> $mday = "0"."$mday";
> }
> $todays_date = "$year"."-"."$mon"."-"."$mday";
> }


sub get_todays_date {
my($day, $mon, $year) = (localtime)[3,4,5];
return sprintf '%04d-%02d-%02d', $year+1900, $mon+1, $day;
}


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Eric Pozharski

unread,
Oct 8, 2008, 6:10:38 PM10/8/08
to
Graham Stow <graha...@gmail.com> wrote:
*SKIP*

> if ($date ne $todays_date) {
> $date= $todays_date;
> do something.....
> }

That "does something" because I<$date> is not equal to I<$todays_date>.

*CUT*

Surely you're interested why condition succedes? To know it you must do
a big deal of cleanup -- insert 2 statements (C<use strict;> and
C<use warnings;>) before any code; then clean your code until perl will
be happy with your attempts; then run. I believe, (dirt guessing here)
sooner or later you'll see warning about something undefined.

p.s. excuse my bad mood, but I think you won't.

--
Torvalds' goal for Linux is very simple: World Domination

Graham Stow

unread,
Oct 9, 2008, 4:13:51 AM10/9/08
to
My logic is that, when the value of <$todays_date> changes, it will because
it will have become tomorrow's date, and will not equal <$date> anymore. So
then assign the value of <$todays_date> to <$date> and do something.

"Eric Pozharski" <why...@pozharski.name> wrote in message
news:unnvr5x...@carpet.zombinet...

Jürgen Exner

unread,
Oct 9, 2008, 5:09:57 AM10/9/08
to
"Graham Stow" <graha...@gmail.com> wrote:
>My logic is that, when the value of <$todays_date> changes, it will because
>it will have become tomorrow's date, and will not equal <$date> anymore. So
>then assign the value of <$todays_date> to <$date> and do something.

So you want to do something once every day,right?

There are tasks Perl is good at and there are tasks other tools are
custom made for. For this application a cron job seems to be much better
choice.

jue

Eric Pozharski

unread,
Oct 9, 2008, 6:16:43 AM10/9/08
to

[ Don't top-post -- people don't like to feel stupid ]

Intentions and code are two different beasts.

Do what you was said to do (twice already) -- use fscking cron.

Peter J. Holzer

unread,
Oct 11, 2008, 4:28:13 AM10/11/08
to
On 2008-10-08 16:26, Tad J McClellan <ta...@seesig.invalid> wrote:
> Graham Stow <graha...@gmail.com> wrote:
>> The following script (or something much like it) runs continuously on
>> a web server
>
> That is likely to waste a boatload of cycles.

Yes, if what the script does once a day is the only thing it does. But
if it provides a useful service continuosly it isn't. (For example it
might be an FCGI script which serves web requests all day long and only
needs to switch log files or dump statistics at the end of each day).


> It would probably be better to run it once a minute or so using
> whatever job scheduling your OS provides, such as "cron".

If it needs to run only at 00:00, why schedule it every minute?

hp

0 new messages