I like core module Time::Local's timelocal_nocheck / timegm_nocheck for doing time and date arithmetic.

use strict; use warnings; use POSIX qw( strftime ); use Time::Local qw( timegm_nocheck ); # Supplied definitions. my $base_year = 2004; my $base_month = 1; # 0-based my $base_day = 1; # 1-based my @weeks_in_month = ( 4, # Feb 5, # Mar 4, # Apr 4, # May 5, # Jun 4, # Jul 4, # Aug 5, # Sep 4, # Oct 4, # Nov 5, # Dec 4, # Jan ); # Derived constants. my @days_before_month = (0); for (0..11) { $days_before_month[$_+1] = $days_before_month[$_] + $weeks_in_month[$_] * 7; } my $days_in_year = $days_before_month[12]; # The guts. sub first_day_of_accounting_month { my ($year, $month) = @_; my $day = $base_day + ($year - $base_year) * $days_in_year + $days_before_month[$month]; my $time = timegm_nocheck(0,0,0, $day, $base_month, $base_year); } sub first_day_of_accounting_year { push(@_, 0); goto &first_day_of_accounting_month; } # Generate a bunch of data for demo purposes. # The output format can easily be changed. my $date_format = '%b %d, %Y'; for my $year (2004 .. 2015) { my $time; $time = first_day_of_accounting_year($year); print("Accounting year $year\n"); print(strftime($date_format, gmtime($time)) . "\n"); print("\n"); for my $month (0..11) { # Feb..Jan $time = first_day_of_accounting_month($year, $month); print(strftime($date_format, gmtime($time)) . "\n"); } print("\n"); print("\n"); }

The above prints:

Accounting year 2004 Feb 01, 2004 Feb 01, 2004 Feb 29, 2004 Apr 04, 2004 May 02, 2004 May 30, 2004 Jul 04, 2004 Aug 01, 2004 Aug 29, 2004 Oct 03, 2004 Oct 31, 2004 Nov 28, 2004 Jan 02, 2005 Accounting year 2005 Jan 30, 2005 Jan 30, 2005 Feb 27, 2005 ... Accounting year 2006 Jan 29, 2006 Jan 29, 2006 Feb 26, 2006 ... Accounting year 2007 Jan 28, 2007 Jan 28, 2007 Feb 25, 2007 ...

Update: I moved the guts into functions, and I added a few more comments.


In reply to Re: Accounting Calendar using Date::Calc by ikegami
in thread Accounting Calendar using Date::Calc by Anonymous Monk

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.