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

I have a string as "Jul 2006". I want to know to which Fiscal year the date belongs to.

The Fiscal year 2007(FY07) is from Jul 2006 to Jun 2007 and FY06 is from Jul 2005 to Jun 2006 and so on.

So, if i give input as "Dec 2006" i must get FY07...
< and if i give Jun 2006 it must give FY06

I used Date::Manip for this purpose.
I subtracted 7 mnths from the date and derived the Fiscal year from the resultant date. It was slower.

I want to this calculation on a larger scale.
Can u suggest some solution??

Replies are listed 'Best First'.
Re: Get the Fiscal Year from Date
by BrowserUk (Patriarch) on Jan 03, 2007 at 11:30 UTC

    A simple lookup table that adjusts the year value according to the month would be much quicker than using generic date modules. This is a slight tweak of my earlier post in another thread (It's generally better to ask follow up questions in the original thread rather than start a new thread). Of course, any answer can only be as good as the information you provide. If there is further undisclosed information (the fiscal year in the UK runs 6th April through 5th April), then further tweaks may be necessary:

    #! perl -slw use strict; my %months = ( FY => 0, Jan => 0, Feb => 0, Mar => 0, Apr => 0, May => 0, Jun => 0, Jul => +1, Aug => +1, Sep => +1, Oct => +1, Nov => +1, Dec => +1, ); print for map{ unpack 'x[n]A*', $_ } sort map { my( $alpha, $num ) = m[^(\S+?)\s*(\d+)$]; $num += 2000 if $num <= 49; $num += 1900 if $num <= 99; pack 'nA*', $num + $months{ $alpha }, $_; } <DATA>; __DATA__ Apr 2006 FY05 FY98 FY04 Dec 2007 Jan 1997 Jan 1998 Dec 1998

    Output

    C:\test>junk Jan 1997 FY98 Jan 1998 Dec 1998 FY04 FY05 Apr 2006 Dec 2007

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      A simple lookup table <...] would be much quicker than using generic date modules

      In the case of Data::Manip, this is quite true. Being a pure-Perl module, it is quite slow. On the other hand cpan:://Date::Calc is largely written in C, and as a result tends to be much faster for general date manipulations.

      But in the present scenario, I would tend to agree, a lookup table will be the way to go.

      • another intruder with the mooring in the heart of the Perl

Re: Get the Fiscal Year from Date
by f00li5h (Chaplain) on Jan 03, 2007 at 11:24 UTC

    I'm pretty sure Date::Manip will be able to help you out here, although it is quite a big module, it does pretty much anything date related you could ever want.

    Normally i'd post another alternative, but Date::Manip pretty much does it all

    Update Having just tried to put together some code to go with this magical hand waving, it turns out to be a little more tricky than i thought... mainly becasue some websites tell me that I can start my fiscial year whenever I want, and not in June, as I thought (which is the Australian Tax year) - although Date::Manip is still a good start.

    f00li5h sits down to a nice meal of humble pie

    Re: sort array by date by shonorio seems to be on the money.

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: Get the Fiscal Year from Date
by polettix (Vicar) on Jan 03, 2007 at 11:26 UTC
    Never used, but DateTime::Fiscal::Year sounds good.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Get the Fiscal Year from Date
by l.frankline (Hermit) on Jan 03, 2007 at 11:42 UTC

    Hi

    If my guess is right, then try this code

    %Months = ( 'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6, 'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12 ); $_='Jan 2007'; if ($_=~/(\w{3}) (\d{4})/) { ($m,$y) = ($Months{$1},$2); $f=$y; $s=$y; } if ($m >= 1 && $m <= 6) { print "FY - Jul " . --$f . " - Jun " . $s . "\n"; } elsif ($m >= 7 && $m <= 12) { print "FY - Jul " . $f . " - Jun " . ++$s . "\n"; }

    Output:

    FY - Jul 2006 - Jun 2007

    regards
    Franklin

    Don't put off till tomorrow, what you can do today.