A Core method

# a core method determine start/end dates of prior months use strict; use warnings; use Time::Local; my @startend; # [0] is current noncompleted month others are full mo +nths back # in each of those the sub elements are # [0] yyyy mm # [1] is start yyyy mm dd # [2] is end yyyy mm dd my $date; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime( +time) ; # move to noon so DST changes dont kill ya my $now_fix12=timelocal( 0, 0, 12, $mday, $mon, $year); $date=$now_fix12; my $back=24; for my $m (0..$back) {push @startend,['']}; my $todo=0; my $doing; my ($ym,$ymd); $doing=$startend[$todo]; ($date,$ym,$ymd)=getparts($date); startup($doing,$ym,$ymd); while (1) { ($date,$ym,$ymd)=getparts($date); # print $ymd."\n"; unless ($doing->[0] eq $ym) { $todo++; last if ($todo>$back); $doing=$startend[$todo]; startup($doing,$ym,$ymd); } else {$doing->[1]=$ymd; } } # while for my $doing (@startend) { print join(' | ',@$doing)."\n"; } exit; sub startup { my $doing=shift; my $ym=shift; my $ymd=shift; $doing->[0]=$ym; $doing->[1]=$ymd; $doing->[2]=$ymd; } # startup sub getparts { my $date=shift; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime( +$date) ; my $ymd=sprintf('%4d %02d %02d',$year+1900,$mon+1,$mday); my $ym=substr($ymd,0,7); $date=$date-24*60*60; return ($date,$ym,$ymd); }
Result
2017 06 | 2017 06 01 | 2017 06 23 2017 05 | 2017 05 01 | 2017 05 31 2017 04 | 2017 04 01 | 2017 04 30 2017 03 | 2017 03 01 | 2017 03 31 2017 02 | 2017 02 01 | 2017 02 28 2017 01 | 2017 01 01 | 2017 01 31 2016 12 | 2016 12 01 | 2016 12 31 2016 11 | 2016 11 01 | 2016 11 30 2016 10 | 2016 10 01 | 2016 10 31 2016 09 | 2016 09 01 | 2016 09 30 2016 08 | 2016 08 01 | 2016 08 31 2016 07 | 2016 07 01 | 2016 07 31 2016 06 | 2016 06 01 | 2016 06 30 2016 05 | 2016 05 01 | 2016 05 31 2016 04 | 2016 04 01 | 2016 04 30 2016 03 | 2016 03 01 | 2016 03 31 2016 02 | 2016 02 01 | 2016 02 29 2016 01 | 2016 01 01 | 2016 01 31 2015 12 | 2015 12 01 | 2015 12 31 2015 11 | 2015 11 01 | 2015 11 30 2015 10 | 2015 10 01 | 2015 10 31 2015 09 | 2015 09 01 | 2015 09 30 2015 08 | 2015 08 01 | 2015 08 31 2015 07 | 2015 07 01 | 2015 07 31 2015 06 | 2015 06 01 | 2015 06 30
Note that element 0 in @startup is the current non completed month, others are full months back

If you are just dealing with days, $date=$date-24*60*60; is fine if $date starts at noon.


In reply to Re: Generate a report for every month for a year by huck
in thread Generate a report for every month for a year by younggrasshopper13

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.