PLearn 0.1
Public Member Functions | Protected Attributes
PLearn::SDBVMFieldDateDiff Class Reference

difference between two dates ("source1-source2" expressed as an integer number of days, months, or years) More...

#include <SDBVMat.h>

Inheritance diagram for PLearn::SDBVMFieldDateDiff:
Inheritance graph
[legend]
Collaboration diagram for PLearn::SDBVMFieldDateDiff:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SDBVMFieldDateDiff (SDBVMSource source1, SDBVMSource source2, char the_unit= 'D', FieldValue date1_threshold=FieldValue(), FieldValue date2_threshold=FieldValue())
 unit can be 'D' for days, 'M' for months or 'Y' for years (result is always an integer number of days, months, or years)
 SDBVMFieldDateDiff (SDBVMSource source1, PDate the_refdate, char the_unit= 'D')
 This will compute difference between date in source1 and a fixed refdate.
virtual void convertField (const SDBWithStats &sdb, const Row &row, const Vec &output) const
virtual int fieldWidth () const
 Width that the field occupies in the VMat.

Protected Attributes

PDate refdate
FieldValue date1_threshold_
FieldValue date2_threshold_
char unit
 unit can be 'D' for days, 'M' for months or 'Y' for years

Detailed Description

difference between two dates ("source1-source2" expressed as an integer number of days, months, or years)

Definition at line 555 of file SDBVMat.h.


Constructor & Destructor Documentation

PLearn::SDBVMFieldDateDiff::SDBVMFieldDateDiff ( SDBVMSource  source1,
SDBVMSource  source2,
char  the_unit = 'D',
FieldValue  date1_threshold = FieldValue(),
FieldValue  date2_threshold = FieldValue() 
) [inline]

unit can be 'D' for days, 'M' for months or 'Y' for years (result is always an integer number of days, months, or years)

This will compute difference between dates in source1 and source2 The sources must both be dates. Optionally, two thresholds can be specified, such that if a date is below or equal to its respective threshold, then the date is considered MISSING_VALUE. This is useful to filter out some obvious outliers.

(TO BE IMPLEMENTED: or one can be a date and the other an integer numerical type; in this latter case, the integer is understood as beeing expressed in the given unit...)

Definition at line 577 of file SDBVMat.h.

        : SDBVMFieldSource2(source1,source2,MISSING_VALUE,VMField::Continuous),
          refdate(), date1_threshold_(date1_threshold),
          date2_threshold_(date2_threshold), unit(the_unit) {}
PLearn::SDBVMFieldDateDiff::SDBVMFieldDateDiff ( SDBVMSource  source1,
PDate  the_refdate,
char  the_unit = 'D' 
) [inline]

This will compute difference between date in source1 and a fixed refdate.

Definition at line 586 of file SDBVMat.h.


Member Function Documentation

void PLearn::SDBVMFieldDateDiff::convertField ( const SDBWithStats sdb,
const Row theRow,
const Vec outputField 
) const [virtual]

Given a database row, convert the appropriate parts to a (preallocated) output vector of the correct width (given by fieldWidth). Replace MISSING_VALUEs by missing_values_mapping.

Implements PLearn::SDBVMField.

Definition at line 507 of file SDBVMat.cc.

References date1_threshold_, date2_threshold_, PLearn::SDBVMSource::getValue(), PLearn::FieldValue::isDate(), PLearn::FieldValue::isIntegral(), PLearn::PDate::isMissing(), PLearn::FieldValue::isMissing(), MISSING_VALUE, PLearn::PDate::month, PLERROR, refdate, PLearn::PDate::setMissing(), PLearn::FieldValue::setMissing(), PLearn::SDBVMFieldSource2::source1_, PLearn::SDBVMFieldSource2::source2_, PLearn::FieldValue::toDate(), PLearn::PDate::toJulianDay(), unit, and PLearn::PDate::year.

{
    // WARNING.  Convoluted logic ahead.  Should fix this.

    FieldValue v1 = source1_.getValue(sdb,row);
    if (!v1.isMissing() && !date1_threshold_.isMissing() &&
        v1 <= date1_threshold_)
        v1.setMissing();
  
    PDate d1 = v1.toDate();

    FieldValue v2;
    PDate d2 = refdate;
    output[0] = MISSING_VALUE;  // default value

    if(!d1.isMissing() && refdate.isMissing())
    {
        v2 = source2_.getValue(sdb,row);
        if (!v2.isMissing() && !date2_threshold_.isMissing() &&
            v2 <= date2_threshold_)
            v2.setMissing();
    
        if(v2.isDate())
            d2 = v2.toDate();
        else if(v2.isIntegral()) {
            if (!v2.isMissing()) {
                int value2 = int(v2);
                switch(unit) {
                case 'D':
                    output[0] = d1.toJulianDay() - value2;
                    break;
                case 'M':
                    output[0] = d1.month - value2;
                    break;
                case 'Y':
                    output[0] = d1.year - value2;
                    break;
                default:
                    PLERROR("In SDBVMFieldDateDiff: unrecognized unit %c", unit);
                }
                return;
            }
        }
        else 
            PLERROR("In SDBVMFieldDateDiff convertField: second field is neither "
                    "a date nor an integer type!");
    }

    // Hack for consistency: (We found a date with year==-32764 (sdb select policy_start_date,drv_last_suspension_end,drv_suspension_days FROM icbc3:2530858:1 ) )
    if(!d1.isMissing() && d1.year<1900)
        d1.setMissing();
    if(!d2.isMissing() && d2.year<1900)
        d2.setMissing();
    if(!d1.isMissing() && !d2.isMissing())
    {
        switch(unit)
        {
        case 'D':
            output[0] = d1-d2;
            break;
        case 'M':
            output[0] = (d1.year*12+d1.month) - (d2.year*12+d2.month);
            break;
        case 'Y':
            output[0] = d1.year - d2.year;
            break;
        default:
            PLERROR("In SDBVMFieldDateDiff: unrecognized unit %c", unit);
        }
    }
}

Here is the call graph for this function:

int PLearn::SDBVMFieldDateDiff::fieldWidth ( ) const [virtual]

Width that the field occupies in the VMat.

Implements PLearn::SDBVMField.

Definition at line 580 of file SDBVMat.cc.

{
    return 1;
}

Member Data Documentation

Definition at line 559 of file SDBVMat.h.

Referenced by convertField().

Definition at line 560 of file SDBVMat.h.

Referenced by convertField().

Definition at line 558 of file SDBVMat.h.

Referenced by convertField().

unit can be 'D' for days, 'M' for months or 'Y' for years

Definition at line 561 of file SDBVMat.h.

Referenced by convertField().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines