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

I'm using DateTime for days calculations. Is there any method to find next and previous quarter
starting and ending dates based on the current date, using DateTime or any other date modules?

Replies are listed 'Best First'.
Re: previous and next quarter calculation
by dws (Chancellor) on Feb 16, 2005 at 07:05 UTC

    Is there any method to find next and previous quarter starting and ending dates based on the current date, using DateTime or any other date modules?

    Try the no modules approach: Table lookup.

    I'm serious. You can precalculate and store all of the quarter start and stop dates from now until the Unix clock runs out in less memory that most of the date modules will soak you for.

Re: previous and next quarter calculation
by ikegami (Patriarch) on Feb 16, 2005 at 06:53 UTC

    Assuming quarters start on first day on Jan, Apr, Jul and Oct, get the components of the date, and ajust them as follows to get the start of the current quarter:

    # Assumes Jan is month 0, Feb is month 1, etc $month = int($month / 3) * 3; $day = 1;

    Add/substract three months to the start of the current quarter for the start of the next/previous quarter.

Re: previous and next quarter calculation
by perlsen (Chaplain) on Feb 16, 2005 at 11:14 UTC

    DateTime::Fiscal::Year - Calculate the day or week of the Fiscal Year with an arbitrary start date

    This module allows you to calulate the day, week, period or quarter of a date in a fiscal year, given a start date and either a target date or number of periods and target date.
    This is often needed in business, where the fiscal year begins and ends on different days than the calendar year.
    This module is based on the Gregorian calendar.
    Using other DT calendar objects will return results, but the behavior is unpredicatable for calendars that have more than 365 or 366 days.

    Please try this if u wish.

Re: previous and next quarter calculation
by fglock (Vicar) on Feb 16, 2005 at 14:04 UTC

    How about:

    use DateTime::Event::Recurrence; my $fiscal = monthly DateTime::Event::Recurrence( interval => 3 ); print $fiscal->next( DateTime->today ), "\n"; print $fiscal->current( DateTime->today ), "\n"; # 2005-04-01T00:00:00 # 2005-01-01T00:00:00