Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Accounting Calendar using Date::Calc

by ikegami (Patriarch)
on Jun 07, 2005 at 19:56 UTC ( [id://464426]=note: print w/replies, xml ) Need Help??


in reply to Accounting Calendar using Date::Calc

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.

Replies are listed 'Best First'.
Re^2: Accounting Calendar using Date::Calc
by Anonymous Monk on Jun 07, 2005 at 21:08 UTC
    Awesome.. Thanks for all your help... Exactly what I was looking for.. Thanks again...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-18 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found