Date Problem

Discussion in 'Perl Misc' started by Graham Stow, Oct 8, 2008.

  1. Graham Stow

    Graham Stow Guest

    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";
    }
     
    Graham Stow, Oct 8, 2008
    #1
    1. Advertising

  2. Graham Stow <> 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/"
     
    Tad J McClellan, Oct 8, 2008
    #2
    1. Advertising

  3. Graham Stow <> 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
     
    Eric Pozharski, Oct 8, 2008
    #3
  4. Graham Stow

    Graham Stow Guest

    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" <> wrote in message
    news:...
    > Graham Stow <> 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?
     
    Graham Stow, Oct 9, 2008
    #4
  5. "Graham Stow" <> 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
     
    Jürgen Exner, Oct 9, 2008
    #5
  6. Graham Stow <> 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.


    > "Eric Pozharski" <> wrote in message
    > news:...
    >> Graham Stow <> 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?


    [ 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.

    --
    Torvalds' goal for Linux is very simple: World Domination
     
    Eric Pozharski, Oct 9, 2008
    #6
  7. On 2008-10-08 16:26, Tad J McClellan <> wrote:
    > Graham Stow <> 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
     
    Peter J. Holzer, Oct 11, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    619
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,295
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    531
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    734
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    415
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page