Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Accounting Calendar using Date::Calc

by Anonymous Monk
on Jun 07, 2005 at 19:20 UTC ( #464419=perlquestion: print w/replies, xml ) Need Help??

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

Im trying to create an Accouting Calender using Date::Calc but Im not sure where to start. Our business accounting calendar starts in Feb. Here are some details about our acct cal. In 2004 our acct cal started on Feb 1st - Feb 28(this is the 1st month). In 2005 the acct cal starts on Jan 30 - Feb 26 but is considered Feb accounting month. Feb, April, May, July, Aug, Oct, Nov, Jan are always 4(always 28 days) weeks long. Mar, Jun, Sep and Dec are all 5(always 35 days) weeks long. Anyone know of a module or how to start on the calculation? Thanks for your help, Taylor.

Replies are listed 'Best First'.
Re: Accounting Calendar using Date::Calc
by ikegami (Pope) on Jun 07, 2005 at 19:56 UTC

    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.

      Awesome.. Thanks for all your help... Exactly what I was looking for.. Thanks again...
Re: Accounting Calendar using Date::Calc
by fglock (Vicar) on Jun 07, 2005 at 22:14 UTC

    If you don't mind installing a module, here is how to use DateTime::Event::Recurrence (I guessed the parameters from your example):

    use DateTime::Event::Recurrence; my $cal = DateTime::Event::Recurrence->yearly( weeks => 5, week_start_day => 'su' ); my $dt = DateTime->new( year=> 2004 ); for ( 1 .. 10 ) { $dt = $cal->next( $dt ); print $dt, "\n" }
    2004-02-01T00:00:00
    2005-01-30T00:00:00
    2006-01-29T00:00:00
    2007-01-28T00:00:00
    2008-01-27T00:00:00
    2009-02-01T00:00:00
    2010-01-31T00:00:00
    2011-01-30T00:00:00
    2012-01-29T00:00:00
    2013-01-27T00:00:00

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-03-07 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (122 votes). Check out past polls.

    Notices?