Daily iteration 
Author Message
 Daily iteration

Greetings,

I want to perform some action once for every day from today to some
arbitrary point into the future.

my $days = 0;

while ($days < 365)
{
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
gmtime (time () + ($days * 86400));
    ++$days;

Quote:
}

Works, except the time offset results in Today not always being iterated.
(Depending on when the script was called.)

while ($days < 365)
{
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
localtime (time () + ($days * 86400));
    ++$days;

Quote:
}

Works, except Very Bad Things happen when called at certain times... the day
the clock is rolled back appears twice, etc.

Ideas?

sh



Mon, 23 Feb 2004 09:47:24 GMT  
 Daily iteration
[mailed and posted]


Quote:
>Greetings,

>I want to perform some action once for every day from today to some
>arbitrary point into the future.

>my $days = 0;

>while ($days < 365)
>{
>    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
>gmtime (time () + ($days * 86400));
>    ++$days;
>}

>Works, except the time offset results in Today not always being iterated.
>(Depending on when the script was called.)

>while ($days < 365)
>{
>    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
>localtime (time () + ($days * 86400));
>    ++$days;
>}

>Works, except Very Bad Things happen when called at certain times... the day
>the clock is rolled back appears twice, etc.

>Ideas?

This is all perfectly fine, if you're playing around, but if you're
serious about this:

http://search.cpan.org/search?dist=DateManip

and stop messing with the BS Junior High School details of this type
calculation.



Mon, 23 Feb 2004 11:16:33 GMT  
 Daily iteration

Quote:

> I want to perform some action once for every day from today to some
> arbitrary point into the future.

Does it matter at what time the action is performed? Should it be the
same time every day, or just once the first time your program is run?

On a UNIX/Linux box, use cron. If you're on Windows, use the Task
Scheduler (or whatever it's called). Other platforms will almost
certainly have equivalents.

Quote:
> while ($days < 365)
> {
>     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
> gmtime (time () + ($days * 86400));
>     ++$days;
> }

Haven't you missed the sleep() instruction somewhere?

How about this:

    while ($forever) {
        # When will it be tomorrow?
        my $tomorrow = time + (24 * 60 * 60);

        # Do my processing now; this may take a while...
        do { stuff };

        # Wait until tomorrow comes
        sleep ($tomorrow - time) while time < $tomorrow;
    }

Quote:
>     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
> localtime (time () + ($days * 86400));
> Works, except Very Bad Things happen when called at certain times... the day
> the clock is rolled back appears twice, etc.

This is a good reason (and the correct reason, usually) for using gmtime()
instead of localtime().

Chris



Mon, 23 Feb 2004 12:22:39 GMT  
 Daily iteration
Woah, major misconception.

I'm trying to draw a calendar (via CGI) with the first visible day set to
today.

sh

Quote:


> > I want to perform some action once for every day from today to some
> > arbitrary point into the future.

> Does it matter at what time the action is performed? Should it be the
> same time every day, or just once the first time your program is run?

> On a UNIX/Linux box, use cron. If you're on Windows, use the Task
> Scheduler (or whatever it's called). Other platforms will almost
> certainly have equivalents.

> > while ($days < 365)
> > {
> >     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
> > gmtime (time () + ($days * 86400));
> >     ++$days;
> > }

> Haven't you missed the sleep() instruction somewhere?

> How about this:

>     while ($forever) {
> # When will it be tomorrow?
> my $tomorrow = time + (24 * 60 * 60);

> # Do my processing now; this may take a while...
> do { stuff };

> # Wait until tomorrow comes
> sleep ($tomorrow - time) while time < $tomorrow;
>     }

> >     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
> > localtime (time () + ($days * 86400));

> > Works, except Very Bad Things happen when called at certain times... the
day
> > the clock is rolled back appears twice, etc.

> This is a good reason (and the correct reason, usually) for using gmtime()
> instead of localtime().

> Chris



Mon, 23 Feb 2004 11:53:21 GMT  
 Daily iteration

Quote:

>I'm trying to draw a calendar (via CGI) with the first visible day set to
>today.
>> > while ($days < 365)
>> > {
>> >     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
>> > gmtime (time () + ($days * 86400));
>> >     ++$days;
>> > }

The don't use time(). If plain CGI, use $^T instead. That is the time
when the script was started.

If running under mod_perl or similar (long process), this won't work.
Then, assign time() to a variable before entering the loop, and use
that.

--
        Bart.



Mon, 23 Feb 2004 12:47:37 GMT  
 Daily iteration

Sean> Woah, major misconception.
Sean> I'm trying to draw a calendar (via CGI) with the first visible day set to
Sean> today.

See HTML::CalendarMonthSimple in the CPAN.
Don't reinvent the wheel.

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Mon, 23 Feb 2004 14:25:21 GMT  
 Daily iteration
This is hardly a wheel. It should be a simple correction for when we are in
DST. A one-liner.

sh



Quote:

> Sean> Woah, major misconception.
> Sean> I'm trying to draw a calendar (via CGI) with the first visible day
set to
> Sean> today.

> See HTML::CalendarMonthSimple in the CPAN.
> Don't reinvent the wheel.

> print "Just another Perl hacker,"

> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777
0095

> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl

training!


Mon, 23 Feb 2004 20:54:32 GMT  
 Daily iteration

Quote:

> This is hardly a wheel. It should be a simple correction for when we are in
> DST. A one-liner.

Timezones are hard to get right properly. Sadly, if you think they're
easy then you've missed the complexities.

Chris



Fri, 27 Feb 2004 17:36:23 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. ANN: Grabbing daily cartoons

2. Out of Office AutoReply: Deja.Com Daily Digest: comp.lang.perl.mi sc 2/4

3. ANN: Grabbing daily cartoons

4. Run script daily

5. Announcing Daily Update 5.0 -- integrate dynamic data from the internet

6. Help for downloading data from the WEB daily

7. The Perl Institute's Daily News

8. sending daily email

9. daily pics script

10. Daily NAV Updates-Job Opportunity

11. (Q) Daily Execution

 

 
Powered by phpBB® Forum Software