Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Finding gaps in date ranges

by bjelli (Pilgrim)
on May 11, 2001 at 01:15 UTC ( #79569=note: print w/ replies, xml ) Need Help??


in reply to Re: Finding gaps in date ranges
in thread Finding gaps in date ranges

boohoo, the above article is mine, but I posted it anonymously by mistake. oh higher powers, if it please you, transfer the XPs of the article to this humble scribe.

I'm a big fan of peanos axioms. So I started with the idea: if it weren't dates we were dealing with, but natural numbers, then I would just sort the numbers, and compute the gaps with one simple loop. I decided to convert you date-format to something that can be sorted numerically (YYYYMM, with month in two digits, like "02"), and I made up a function "nextmonth" for incrementing the date.

sub nextmonth { my($year, $month); $year = substr($_[0], 0, 4); $month = substr($_[0], 4, 2); $month++; while ($month > 12) { $year++; $month-=12; } return $year . sprintf("%02d", $month); } sub find_gaps { my $dates = shift; my(@missing, @keys, $last); # convert your date-format to mine @keys = map { s/-(.)$/0$1/; s/-//; $_ } keys %$dates; # my format can be sorted numerically @keys = sort @keys; # in the loop we need two variables: # $last for the date before the current one, # and $date for the current date $last = shift(@keys); foreach my $date ( @keys ) { while (1) { $last = nextmonth($last); last if $last eq $date; push(@missing, $last); } $last = $date; } # convert my date-format back to yours @missing = map { s/0(.)$/$1/; s/^(....)/$1-/; $_ } @missing; return \@missing; }

share and enjoy

--
Brigitte    'I never met a chocolate I didnt like'    Jellinek
http://www.horus.com/~bjelli/         http://perlwelt.horus.at


Comment on Re: Re: Finding gaps in date ranges
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2015-07-06 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (71 votes), past polls