Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Loop through epoch days

by Scrat (Monk)
on Sep 27, 2007 at 10:34 UTC ( #641322=perlquestion: print w/replies, xml ) Need Help??

Scrat has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I have an epoch start-date (1135202400) and an epoch end-date (1188252000).

I'd like to loop through each day from start-date to end-date and perform an action for that day. Using a for loop, I tried to increment the number by 86400:

for (my $i=1135202400; $i <= 1188252000; $i+86400) { print ">$i<\n"; }

Which obviously didn't work and returned the message:

Useless use of addition (+) in void context at...

Then I tried this:

for (my $i=1135202400; $i <= 1188252000; $i++) { print ">$i<\n" unless ($i%86400); }
Which returned:
>1135209600< >1135296000< >1135382400< >1135468800< >1135555200< >1135641600< >1135728000< >1135814400< >1135900800< >1135987200< ...
a value for each day, except for the first value which, if converted to date format, turns out to be the same day as that of the start-date value, just 2 hours later...

I just need to loop through each day, from the start-date to the end-date, and when I convert the value back to date format the time must be displayed as 00:00:00 for each day, and not 02:00:00. Preferably I'd like to loop only once through each day to return a value, and not 86400 times. Thanks for any input / advice.

Replies are listed 'Best First'.
Re: Loop through epoch days
by jeanluca (Deacon) on Sep 27, 2007 at 10:36 UTC
    try this
    for (my $i=1135202400; $i <= 1188252000; $i+=86400) { print ">$i<\n"; }

    LuCa

      Thanks LuCa, That's exactly what I was looking for.

      Cheers
        Scrat, you can't assign using an idiom similar to pre/post increment/decrement. Also the code, the code that goes between the semi-colons is not limited to the traditional (my $i=0; $i<$X; $i++), although I am pretty sure that the middle code segment (;$i<$X;) must evaluate to true or false in some form because it is the stopping condition.

        And while we're throwing around our favorite date modules, I like Date::Pcalc.
Re: Loop through epoch days
by andreas1234567 (Vicar) on Sep 27, 2007 at 11:16 UTC
    I'd use DateTime:
    $ cat 641322.pl use strict; use warnings; use DateTime; my $dt1 = DateTime->from_epoch( epoch => 1135202400 ); my $dt2 = DateTime->from_epoch( epoch => 1188252000 ); $dt1->add( hours => 2 ); # adjust to midnight while ($dt2 > $dt1){ print $dt1->ymd() . q{ } . $dt1->hms(); $dt1->add( days => 1 ); } __END__ $ perl -wl 641322.pl | head 2005-12-22 00:00:00 2005-12-23 00:00:00 2005-12-24 00:00:00 2005-12-25 00:00:00 2005-12-26 00:00:00 2005-12-27 00:00:00 2005-12-28 00:00:00 2005-12-29 00:00:00 2005-12-30 00:00:00 2005-12-31 00:00:00
    --
    Andreas

      This is great: in your while loop you also converted the epoch values back to a character string that is compatible with MSSQL, which is exactly what I needed to do next! Thanks Andreas.

Re: Loop through epoch days
by regexes (Hermit) on Sep 27, 2007 at 11:09 UTC
    Hello,

    Just for general information, you might want to take a look at Date::Manip
    Somewhat overkill for simple tasks, but it might be something you would want to investigate...

    regexes


    -------------------------
    Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan "press on" has solved and always will solve the problems of the human race.
    -- Calvin Coolidge, 30th President of the USA.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://641322]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2021-06-22 17:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (108 votes). Check out past polls.

    Notices?