I was wondering if you would consider making a fundamental addition to DateTime. It would be to add the 'quarter' and 'day_of_quarter' to DateTime.

Attached is a patch to DateTime.xs, DateTime.pm and t/03components.t. It is working for me, although t/09greg.t would have to be changed too.

This patch would give you:
  $dt->quarter  # (1 - 4)
  $dt->qtr      # alias
  $dt->day_of_quarter  #(1 - max_days_in_quarter)
  $dt->doq  # alias

-Tim
diff -r -u DateTime-0.08/DateTime.xs DateTime-0.08.test/DateTime.xs
--- DateTime-0.08/DateTime.xs   Sun Mar 16 21:36:12 2003
+++ DateTime-0.08.test/DateTime.xs      Sun Mar 23 12:09:19 2003
@@ -73,8 +73,9 @@
      PREINIT:
         IV y, m;
         IV c;
+        IV quarter;
         IV yadj = 0;
-        IV dow, doy;
+        IV dow, doy, doq;
         IV rd_days;
 
      PPCODE:
@@ -104,11 +105,13 @@
           ++y;
           m -= 12;
         }
+        quarter = 1.0/3.1*m+1;
 
         EXTEND(SP, extra ? 5 : 3);
         PUSHs(sv_2mortal(newSViv(y)));
         PUSHs(sv_2mortal(newSViv(m)));
         PUSHs(sv_2mortal(newSViv(d)));
+        PUSHs(sv_2mortal(newSViv(quarter)));
 
         if (extra) {
           dow = ((rd_days + 6) % 7) + 1;
@@ -116,11 +119,14 @@
 
           if (_real_is_leap_year(y)) {
             doy = PREVIOUS_MONTH_DOLY[m - 1] + d;
+           doq = doy-PREVIOUS_MONTH_DOLY[3*quarter-3];
           } else {
             doy = PREVIOUS_MONTH_DOY[m - 1] + d;
+           doq = doy-PREVIOUS_MONTH_DOY[3*quarter-3];
           }
 
           PUSHs(sv_2mortal(newSViv(doy)));
+          PUSHs(sv_2mortal(newSViv(doq)));
         }
 
 void
diff -r -u DateTime-0.08/lib/DateTime.pm DateTime-0.08.test/lib/DateTime.pm
--- DateTime-0.08/lib/DateTime.pm       Thu Mar 20 22:02:27 2003
+++ DateTime-0.08.test/lib/DateTime.pm  Mon Mar 24 08:21:04 2003
@@ -153,7 +153,7 @@
 {
     my $self = shift;
 
-    @{ $self->{local_c} }{ qw( year month day day_of_week day_of_year ) } =
+    @{ $self->{local_c} }{ qw( year month day quarter day_of_week day_of_year 
day_of_quarter) } =
         $self->_rd2ymd( $self->{local_rd_days}, 1 );
 
     @{ $self->{local_c} }{ qw( hour minute second ) } =
@@ -292,6 +292,9 @@
 *day  = \&day_of_month;
 *mday = \&day_of_month;
 
+sub quarter {$_[0]->{local_c}{quarter} };
+*qtr = \&quarter;
+
 sub day_of_month_0 { $_[0]->{local_c}{day} - 1 }
 *day_0  = \&day_of_month_0;
 *mday_0 = \&day_of_month_0;
@@ -308,6 +311,11 @@
 
 sub day_abbr { $_[0]->{language}->day_abbreviation( $_[0] ) }
 
+sub day_of_quarter { $_[0]->{local_c}{day_of_quarter} }
+*doq = \&day_of_quarter;
+*day_of_qtr = \&day_of_quarter;
+
+
 sub day_of_year { $_[0]->{local_c}{day_of_year} }
 *doy = \&day_of_year;
 
@@ -849,6 +857,12 @@
 
   $doy    = $dt->day_of_year    # 1-366 (leap years)
   # also $dt->doy
+  
+  $doq    = $dt->day_of_quarter # 1-(number of days)
+  # also $dt->doq, $dt->day_of_qtr
+
+  $qtr    = $dt->quarter        # 1-4 
+  # also  $dt->qtr
 
   # all of the start-at-1 methods above have correponding start-at-0
   # methods, such as $dt->day_of_month_0, $dt->month_0 and so on
diff -r -u DateTime-0.08/t/03components.t DateTime-0.08.test/t/03components.t
--- DateTime-0.08/t/03components.t      Tue Mar 11 23:43:25 2003
+++ DateTime-0.08.test/t/03components.t Sun Mar 23 12:24:21 2003
@@ -1,6 +1,6 @@
 use strict;
 
-use Test::More tests => 72;
+use Test::More tests => 74;
 
 use DateTime;
 
@@ -16,6 +16,7 @@
 is( $d->year, 2001, '->year' );
 is( $d->ce_year, 2001, '->ce_year' );
 is( $d->month, 7, '->month' );
+is( $d->quarter,3,'->quarter');
 is( $d->month_0, 6, '->month_0' );
 is( $d->month_name, 'July', '->month' );
 is( $d->month_abbr, 'Jul', '->month' );
@@ -35,6 +36,7 @@
 
 is( $d->day_of_year, 186, '->day_of_year' );
 is( $d->day_of_year_0, 185, '->day_of_year' );
+is( $d->day_of_quarter, 5, '->day_of_quarter' );
 is( $d->day_of_week, 4, '->day_of_week' );
 is( $d->day_of_week_0, 3, '->day_of_week' );
 is( $d->wday, 4, '->wday' );

Reply via email to