in reply to Finding gaps in date ranges
use strict; use Carp; # Takes a list of months in yyyymm format, returns the gaps in sorted +order. sub find_gaps { my @dates = sort {$a <=> $b} map {yyyy_mm2mon_num($_)} @_; my $last_mon_count = shift @dates; my @gaps; foreach my $mon_count (@dates) { foreach my $missed (($last_mon_count + 1)..($mon_count-1)) { push @gaps, mon_num2yyyy_mm($missed); } $last_mon_count = $mon_count; } return @gaps; } # Converts a count of months back into yyyymm format sub mon_num2yyyy_mm { my $mon_num = shift; use integer; my $yyyy = $mon_num / 12; my $mm = 1 + $mon_num % 12; # Sanity check unless (1900 < $yyyy and $yyyy < 2200) { carp("Month number '$mon_num' gives a year of $yyyy which seems st +range"); } return "$yyyy-$mm"; } # Converts yyyymm format into a count of months from year 0. (Which d +idn't # exist, shoot me. It is a convenient base-point for calculations.) sub yyyy_mm2mon_num { my $date = shift; if ($date =~ /(\d{4}).*?(\d\d?)/) { return 12*$1 + $2 - 1; } else { confess("Date '$date' is not in yyyymm format?"); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid - nice try)Re: Re (tilly) 1: Finding gaps in date ranges
by Ovid (Cardinal) on May 11, 2001 at 00:57 UTC | |
by tilly (Archbishop) on May 11, 2001 at 01:05 UTC |