#ifndef tImE_H #define tImE_H 1 #include #include "types.h" // has ul // Oldham, Jeffrey D. // 2000Apr25 // CS3352 // TIME // Stores all dates for the current time frame. // Months are specified as 1, 2, ..., 12. class Time { public: // Constructor // input <- beginning of time frame (year, month) // number of months between incrementing // asset-holding period (in months) // duration of time frame (in months) // (optional) person's age (in years) // Only specify if using age-dependent asset allocations. // If not computing taxes, the asset-holding period's value is ignored. Time(const ul startYear, const ul startMonth, const ul incrementPeriod, const ul assetHoldingPeriod, const ul duration, const ul age = 0) { yr = startYear; assert(1 <= startMonth && startMonth <= 12); mon = startMonth; incrementPerd = incrementPeriod; monthsUntilIncrement = incrementPerd - 1; assetPeriod = assetHoldingPeriod; monthsUntilSellAssets = assetPeriod - 1; timeFrameDuration = duration; monthsUntilTimeEnds = timeFrameDuration - 1; birthYear = (ul) (startYear+(startMonth-1)/12.0) - age; return; } // Return the current year and month. ul year(void) const { // returns number, e.g., 1945 return yr; } ul month(void) const { // returns number, e.g., 1, 2, ..., 12 return mon; } // JDO: Remove this function. // Return the number of years since 1926. // TMP ul yearsSince1926(void) const { // TMP return yr - 1926; // TMP } // Return the number of years since the time frame began. ul yearsSinceTimeFrameBegan(void) const { return (ul) ((timeFrameDuration - monthsUntilTimeEnds)/12.0); } // Move to next month, i.e., move time forward one month. void nextMonth(void) { if (++mon == 13) { mon = 1; ++yr; } if (monthsUntilIncrement == 0) monthsUntilIncrement = incrementPerd; --monthsUntilIncrement; if (monthsUntilSellAssets == 0) monthsUntilSellAssets = assetPeriod; --monthsUntilSellAssets; assert(monthsUntilTimeEnds != 0); --monthsUntilTimeEnds; return; } // Check for incrementing, selling all assets, and end of the time frame. bool incrementP(void) const { // returns true if should increment // investment at the end of this month return monthsUntilIncrement == 0; } bool sellAssetsP(void) const {// returns true if should sell all assets // at the end of this month return monthsUntilSellAssets == 0; } bool endOfTimeFrameP(void) const { // returns true if this is last month // of time frame return monthsUntilTimeEnds == 0; } // For age-dependent asset allocations only: // Call this function only inside the assetAllocation class! double currentAge(void) const{ // age in years (possibly with a // fraction for months) return yr + (mon-1)/12.0 - birthYear; } // Return the number of months in the holding period. ul GetHoldingPeriod() const { return assetPeriod; } // Return the number of months in the last holding period. ul GetLastHoldingPeriod() const { return timeFrameDuration % assetPeriod; } private: ul yr; ul mon; // 1 = January, ..., 12 = December ul incrementPerd; ul monthsUntilIncrement; ul assetPeriod; ul monthsUntilSellAssets; ul timeFrameDuration; ul monthsUntilTimeEnds; ul birthYear; // used to compute age }; #endif