Page 1 of 1

SpreadsheetDate.isInRange()

Posted: Thu Sep 04, 2003 6:31 pm
by wildepete
This method has not been implemented (simply returns false), which is causing a problem with the method SerialDateUtilities.countFeb29s().

This means that I cannot calculate an accurate Actual/Actual day count fraction :?

It seems like this would be fairly easy to implement using the compareTo() method, but nobody bothered to.

Posted: Fri Sep 05, 2003 12:43 am
by david.gilbert
Here's what I've written (not tested yet!):

Code: Select all

    /**
     * Returns <code>true</code> if this {@link SerialDate} is within the specified range 
     * (INCLUSIVE).  The date order of d1 and d2 is not important.
     *
     * @param d1  a boundary date for the range.
     * @param d2  the other boundary date for the range.
     *
     * @return A boolean.
     */
    public boolean isInRange(SerialDate d1, SerialDate d2) {
        return isInRange(d1, d2, SerialDate.INCLUDE_BOTH);
    }

    /**
     * Returns true if this SerialDate is within the specified range (caller
     * specifies whether or not the end-points are included).  The order of d1
     * and d2 is not important.
     *
     * @param d1  one boundary date for the range.
     * @param d2  a second boundary date for the range.
     * @param include  a code that controls whether or not the start and end dates are
     *                 included in the range.
     *
     * @return <code>true</code> if this SerialDate is within the specified range.
     */
    public boolean isInRange(SerialDate d1, SerialDate d2, int include) {
        int s1 = d1.toSerial();
        int s2 = d2.toSerial();
        int start = Math.min(s1, s2);
        int end = Math.max(s1, s2);
        
        int s = toSerial();
        if (include == SerialDate.INCLUDE_BOTH) {
            return (s >= start && s <= end);
        }
        else if (include == SerialDate.INCLUDE_FIRST){
            return (s >= start && s < end);            
        }
        else if (include == SerialDate.INCLUDE_SECOND) {
            return (s > start && s <= end);            
        }
        else {
            return (s > start && s < end);            
        }    
    }