Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Day of week calculation

by reisinge (Hermit)
on Sep 21, 2016 at 05:58 UTC ( [id://1172272]=perlquestion: print w/replies, xml ) Need Help??

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

Hello guys. Can someone explain to me how would one come up with the idea behind the code from line 12 to 18? I mean I kind of understand it but I would have never though of it :-). (It's from Intermediate Perl book, by the way).

1 #!perl 2 use strict; 3 use warnings; 4 use utf8; 5 use File::Find; 6 use Time::Local; 7 my $target_dow = 1; # Sunday is 0, Monday is 1, ... 8 my @starting_directories = ("."); 9 my $seconds_per_day = 24 * 60 * 60; 10 my($sec, $min, $hour, $day, $mon, $yr, $dow) = localtime; 11 my $start = timelocal(0, 0, 0, $day, $mon, $yr); # midn +ight today 12 while ($dow != $target_dow) { 13 # Back up one day 14 $start -= $seconds_per_day; # hope no DST! :-) 15 if (--$dow < 0) { 16 $dow += 7; 17 } 18 } 19 my $stop = $start + $seconds_per_day; 20 my($gather, $yield) = gather_mtime_between($start, $stop); 21 find($gather, @starting_directories); 22 my @files = $yield->( ); 23 for my $file (@files) { 24 my $mtime = (stat $file)[9]; # mtime via slice 25 my $when = localtime $mtime; 26 print "$when: $file\n"; 27 }
I have never taken any courses in computer science or software engineering. -- esr (http://www.catb.org/~esr/resume.html)

Replies are listed 'Best First'.
Re: Day of week calculation
by Random_Walk (Prior) on Sep 21, 2016 at 08:31 UTC

    12 while ($dow != $target_dow) { # do the following until $dow +is what we want 13 # Back up one day 14 $start -= $seconds_per_day; # remove a days worth of secon +ds from our epoch time 15 if (--$dow < 0) { # remove one from our $dow cou +nter 16 $dow += 7; # if we ended up with -1 as da +y of week, wrap it back to 6 (-1 + 7) 17 } # end if 18 } # end while loop, now our $dow + matches our target

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Day of week calculation
by Marshall (Canon) on Sep 21, 2016 at 09:03 UTC
    This program is going to print files that have an mtime during the previous Monday (a 24 hour period).

    On line 11, $start is intialized to midnight of the current day. $start is expressed as a number of seconds from what is called the "epoch".

    Now we have to "back up" from the current day's midnight to the previous Monday. If today was say Tuesday, then dow would be 2. while ($dow != $target_dow) would be while (2 != 1){}. A day's worth of seconds are subtracted from $start, $dow is decremented, loop stops.

    if (--$dow < 0) { $dow += 7;} What happens if we were starting on Sunday, $dow of 0?. We have problems because current dow (0) is less than the Monday (1). We are one week off. We want to count: 6,5,4,3,2,1, winding up subtracting 6 full days of seconds, hence the reason to add 7.

    There is more than one way to do this, but we are essentially "counting" in a circle, imagine a roulette wheel where the numbers around the circle are 0,1,2,3,4,5,6,7. Wheel in the case only goes anti-clockwise. Hope that analogy works for you.

    Once the proper Monday "start date" is figured out, then my $stop = $start + $seconds_per_day; adds one day of seconds to it.

    Here is full code without line numbers (one subroutine was left out):

    #!perl use strict; use warnings; use utf8; use File::Find; use Time::Local; my $target_dow = 1; # Sunday is 0, Monday is 1, ... my @starting_directories = ("."); my $seconds_per_day = 24 * 60 * 60; my($sec, $min, $hour, $day, $mon, $yr, $dow) = localtime; my $start = timelocal(0, 0, 0, $day, $mon, $yr); # midnight tod +ay while ($dow != $target_dow) { # Back up one day $start -= $seconds_per_day; # hope no DST! :-) if (--$dow < 0) { $dow += 7; } } my $stop = $start + $seconds_per_day; my($gather, $yield) = gather_mtime_between($start, $stop); find($gather, @starting_directories); my @files = $yield->( ); for my $file (@files) { my $mtime = (stat $file)[9]; # mtime via slice my $when = localtime $mtime; print "$when: $file\n"; } sub gather_mtime_between{ my ($begin, $end) = @_; my @files; my $gatherer = sub { my $timestamp = (stat $_)[9]; unless (defined $timestamp){ warn "Can't stat $File::Find::name $!, skipping\n"; return; } push @files, $File::Find::name if $timestamp >= $begin and $timestamp <= $end; }; my $fetcher = sub {@files }; ($gatherer, $fetcher); }
Re: Day of week calculation
by Anonymous Monk on Sep 21, 2016 at 06:29 UTC

    Can someone explain to me how would one come up with the idea behind the code from line 12 to 18? I mean I kind of understand it but I would have never though of it :-).

    Its a very basic, its just counting , like for hide and seek, like turning the pages of a book, board games, summing up groceries, or subtracting from cash on hand ($20)...

Re: Day of week calculation
by Anonymous Monk on Sep 21, 2016 at 06:27 UTC

    Care to remove the line numbers for anybody interested in running the code? ;-{

      Please follow the link for the original code without line numbers.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1172272]
Approved by beech
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found