Eugene van der Pijll wrote:
Tim Allwine schreef:

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.


AAAAAAAARGH! Please no! 'week' and 'day_of_year' were two of the hardest
methods to write in DT::C::Christian, and now you want to add more of
that kind!

But now seriously:

(in _rd2ymd:)

        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;

..


          PUSHs(sv_2mortal(newSViv(doy)));
+          PUSHs(sv_2mortal(newSViv(doq)));
        }


Even though _rd2ymd is an internal function, please don't change the
return values. Put 'quarter' at the end. It doesn't need to be set for
all datetime's, so it's OK if it's inside the if(extra){}. Like week and
doy, it's a superfluous variable: y, m, and d are sufficient to define a
date.


+ @{ $self->{local_c} }{ qw( year month day quarter day_of_week day_of_year day_of_quarter) } =


So this becomes


+ @{ $self->{local_c} }{ qw( year month day day_of_week day_of_year quarter day_of_quarter) } =


OK, fixed. It even passes all tests now.

-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      Tue Mar 25 12:44:39 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:
@@ -111,16 +112,21 @@
         PUSHs(sv_2mortal(newSViv(d)));
 
         if (extra) {
+         quarter = 1.0/3.1*m+1;
           dow = ((rd_days + 6) % 7) + 1;
           PUSHs(sv_2mortal(newSViv(dow)));
 
           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(quarter)));
+          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  Tue Mar 25 12:48: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 day_of_week day_of_year quarter 
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
@@ -1135,6 +1149,14 @@
 =item * day_of_year, doy
 
 Returns the day of the year.
+
+=item * day_of_quarter, doq
+
+Returns the day of the quarter.
+
+=item * quarter, qtr
+
+Returns the quarter of the year.
 
 =item * ymd( $optional_separator ), date
 
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