date dateUtil::dayRollAdjust(date aDate,DayRollEnum aDayRollConvention, enums::CurrencyEnum market) { long adjustedJDN; switch(aDayRollConvention){ case enums::Following: adjustedJDN = getFolloingJDN(aDate, market); break; case enums::Preceding: adjustedJDN = getPrecedingJDN(aDate, market); break; case enums::Mfollowing: adjustedJDN = getFolloingJDN(aDate, market); if (getYearMonthDay(adjustedJDN)[1]!=aDate.getMonth()) adjustedJDN = getPrecedingJDN(aDate, market); break; case enums::Mfollowingbi: adjustedJDN = getFolloingJDN(aDate, market); if (getYearMonthDay(adjustedJDN)[1]!=aDate.getMonth()|| getYearMonthDay(adjustedJDN)[2]>=15) adjustedJDN = getPrecedingJDN(aDate, market); break; case enums::EOM: break; case enums::DayRollNull: adjustedJDN = aDate.getJudianDayNumber(); break; } date adjustedDate(adjustedJDN); return adjustedDate; }
//! Converts a date to a tm struct. Throws out_of_range exception if date is a special value inline std::tm to_tm(const date& d) { if (d.is_special()) { std::string s = "tm unable to handle "; switch (d.as_special()) { case date_time::not_a_date_time: s += "not-a-date-time value"; break; case date_time::neg_infin: s += "-infinity date value"; break; case date_time::pos_infin: s += "+infinity date value"; break; default: s += "a special date value"; break; } boost::throw_exception(std::out_of_range(s)); } std::tm datetm; std::memset(&datetm, 0, sizeof(datetm)); boost::gregorian::date::ymd_type ymd = d.year_month_day(); datetm.tm_year = ymd.year - 1900; datetm.tm_mon = ymd.month - 1; datetm.tm_mday = ymd.day; datetm.tm_wday = d.day_of_week(); datetm.tm_yday = d.day_of_year() - 1; datetm.tm_isdst = -1; // negative because not enough info to set tm_isdst return datetm; }
int stat_file_manager::merge(date time){ std::vector<stat_file>::iterator it = stat_files.begin(); json_object* jobj = json_object_new_object(); bool update = false; while(it != stat_files.end()){ if(time.is_child(it->get_date())){ json_object* jelement = it->get_jobj(); if(jelement != NULL){ json_object_object_add(jobj, it->get_filename().c_str(), jelement); #ifdef DEBUG std::cout << "remove(): " << it->get_filename() << std::endl; #endif it->remove(); update = true; } } it++; } if(update == false){ return -1; } json_merge(jobj); #ifdef DEBUG std::cout << "write(): " << time.get_stime() << std::endl; #endif write(time.get_stime(), jobj); return 0; }
void date::_copy_members (const date& other) { day (other.day()); month (other.month()); year (other.year()); }
date dateUtil::getEndDateMonthIncrement(date startDate, int numMonth){ if (numMonth==0) return startDate; short startMonth = startDate.getMonth(); short endMonth = (startMonth + numMonth)%12; int yearIncrement = (startMonth + numMonth)/12; yearIncrement = (startMonth + numMonth)<=0?yearIncrement-1:yearIncrement; endMonth = endMonth<=0?endMonth+12:endMonth; short endYear= startDate.getYear()+yearIncrement; date endDate(endYear, endMonth, startDate.getDay()); return endDate; }
void object_serializer::serialize(const char *id, date &x) { if (restore) { int julian_date(0); buffer_->release(&julian_date, sizeof(julian_date)); x.set(julian_date); } else { int jd(x.julian_date()); serialize(id, jd); } }
bool sequential_date(date current_date, date next_date) { if (current_date.day_of_week().as_enum() == Monday) return next_date.day_of_week().as_enum() == Tuesday; if (current_date.day_of_week().as_enum() == Tuesday) return next_date.day_of_week().as_enum() == Wednesday; if (current_date.day_of_week().as_enum() == Wednesday) return next_date.day_of_week().as_enum() == Thursday; if (current_date.day_of_week().as_enum() == Thursday) return next_date.day_of_week().as_enum() == Friday; if (current_date.day_of_week().as_enum() == Friday) return next_date.day_of_week().as_enum() == Monday; return true; }
void FoxPro::setDate(size_t record, size_t field, const date &value) { assert((record < _records.size()) && (field < _fields.size())); Record &r = _records[record]; Field &f = _fields[field]; if (f.type != kTypeDate) throw Exception("Field is not of date type ('%c')", f.type); char *data = (char *) r.fields[field]; snprintf(data, 8, "%04d%02d%02d", (int) value.year(), (int) value.month(), (int) value.day()); updateUpdate(); }
bool date::inferieur_a (const date & d) const { bool resultat = true; if (annee () > d.annee ()) resultat = false; else if (annee () == d.annee ()) { if (mois () > d.mois ()) resultat = false; else if (mois () == d.mois ()) resultat = jour () < d.jour (); } return resultat; }
std::unique_ptr<quoteVector_t> YahooFDS::get_historical_prices(const std::string &symbol, date &start, date &end) { //quoteVector_t *quotes = new quoteVector_t; auto quotes = new quoteVector_t; auto start_d = start.year_month_day(); auto end_d = end.year_month_day(); std::vector<std::string> quoteStrings; std::ostringstream url; url << "http://ichart.yahoo.com/table.csv?s=" << symbol << "&d=" \ << end_d.month - 1 << "&e=" \ << end_d.day << "&f=" \ << end_d.year << "&a=" \ << start_d.month - 1 << "&b=" \ << start_d.day << "&c=" \ << start_d.year << "&ignore=.csv"; // split the response into lines boost:split(quoteStrings, (const std::string &)request(url.str()), boost::is_any_of("\n"),boost::token_compress_on); // Remove header line quoteStrings.erase(quoteStrings.begin()); for (auto it = quoteStrings.rbegin(); it < quoteStrings.rend(); ++it) { // Ensure we have a valid quote here. 38 is presumably the shortest possible quote. 35 is magic as f**k (MAF). if ((*it).length() > 35) { std::vector<std::string> temp; boost::split(temp, *it, boost::is_any_of(", ")); HistoricalQuote quote(symbol, temp.at(0), atof(temp.at(1).c_str()), atof(temp.at(2).c_str()), atof(temp.at(3).c_str()), atof(temp.at(4).c_str()), atol(temp.at(5).c_str()), atof(temp.at(6).c_str())); // Make it rain! quotes->push_back(quote); } } return std::unique_ptr<quoteVector_t>(std::move(quotes)); }
long dateUtil::getFolloingJDN(date refDate, enums::CurrencyEnum market){ //date adjustedDate = adjustInvalidateDate(refDate,true); long JDN = refDate.getJudianDayNumber(); while(!isBizDay(JDN) || isHoliday(JDN,market)){ JDN++; } return JDN; }
long dateUtil::getPrecedingJDN(date refDate, enums::MarketEnum market){ //date adjustedDate = adjustInvalidateDate(refDate,false); long JDN = refDate.getJudianDayNumber(); while(!isBizDay(JDN) || isHoliday(JDN,market)){ JDN--; } return JDN; }
date dateUtil::adjustInvalidateDate(date aDate, bool forwardAdjust){ unsigned short monthlen[]={31,28,31,30,31,30,31,31,30,31,30,31}; if (isleapyear(aDate.getYear()) && aDate.getMonth()==2) monthlen[1]++; if (aDate.getDay()>monthlen[aDate.getMonth()-1]){ if (forwardAdjust){ aDate.setMonth(aDate.getMonth()%12+1); aDate.setDay(1); } else aDate.setDay(monthlen[aDate.getMonth()-1]); } return aDate; }
date dateUtil::getEndDateMonthIncrement(date startDate, int numMonth){ if (numMonth==0) return startDate; short startMonth = startDate.getMonth(); short endMonth = (startMonth + numMonth); bool isEndMonthPow12 = (startMonth + numMonth)%12==0?true:false; endMonth = isEndMonthPow12 ? 12 : (startMonth + numMonth)%12; int yearIncrement = abs((startMonth + numMonth)/12) - (isEndMonthPow12 ? 1 : 0); if (startMonth + numMonth<0) yearIncrement = -abs((startMonth + numMonth)/12) - 1 - (isEndMonthPow12 ? 1 : 0); endMonth = endMonth<=0 ? endMonth+12 : endMonth; short endYear= startDate.getYear()+ yearIncrement; date endDate(endYear, endMonth, startDate.getDay()); // Adjust the return day to the end of month if the start date is also end of month //if (startDate.getDay() == getMonthLastDay(startDate.getYear(), startDate.getMonth())) //endDate.setDay(getMonthLastDay(endDate.getYear(), endDate.getMonth())); return endDate; }
date date::backDate(date a,int in) { int adays,amonth,ayear; adays=a.getDay(); amonth=a.getMonth(); ayear=a.getYear(); adays=adays+in; if(adays>30) {amonth++; adays+=-30;} if(amonth>12) { ayear++; amonth-=30; } date b(adays,amonth,ayear); return b; }
//returns the date on which thanksgiving falls given the date of the first of november in any year date thanksgiving(date d) { int gap = 21; while (d.dayOfWeek() != 4) ++d; while (gap > 0) { ++d; --gap; } return d; }
date dateUtil::getBizDateOffSet(date refDate, long offset, enums::CurrencyEnum market) { long JDN = refDate.getJudianDayNumber(); bool forward = offset>=0?true:false; for (long i=0; i<abs(offset); i++){ forward?JDN++:JDN--; while(!isBizDay(JDN) || isHoliday(JDN,market)){ forward?JDN++:JDN--; } } date offsetDate(JDN); return offsetDate; }
inline std::string to_sql_string(const date& d) { date::ymd_type ymd = d.year_month_day(); std::ostringstream ss; ss << ymd.year << "-" << std::setw(2) << std::setfill('0') << ymd.month.as_number() //solves problem with gcc 3.1 hanging << "-" << std::setw(2) << std::setfill('0') << ymd.day; return ss.str(); }
inline std::basic_string<charT> to_sql_string_type(const date& d) { date::ymd_type ymd = d.year_month_day(); std::basic_ostringstream<charT> ss; ss << ymd.year << "-" << std::setw(2) << std::setfill(ss.widen('0')) << ymd.month.as_number() //solves problem with gcc 3.1 hanging << "-" << std::setw(2) << std::setfill(ss.widen('0')) << ymd.day; return ss.str(); }
//! Converts a date to a tm struct. Throws out_of_range exception if date is a special value inline std::tm to_tm(const date& d) { if(d.is_pos_infinity() || d.is_neg_infinity() || d.is_not_a_date()){ std::string s = "tm unable to handle date value of "; #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO) s += to_simple_string(d); #else std::ostringstream ss; ss << d; s += ss.str(); #endif // USE_DATE_TIME_PRE_1_33_FACET_IO boost::throw_exception(std::out_of_range(s)); } std::tm datetm; boost::gregorian::date::ymd_type ymd = d.year_month_day(); datetm.tm_year = ymd.year-1900; datetm.tm_mon = ymd.month-1; datetm.tm_mday = ymd.day; datetm.tm_wday = d.day_of_week(); datetm.tm_yday = d.day_of_year()-1; datetm.tm_hour = datetm.tm_min = datetm.tm_sec = 0; datetm.tm_isdst = -1; // negative because not enough info to set tm_isdst return datetm; }
date dateUtil::getEndDate(date startDate, int increment, enums::DayRollEnum dayRoll, enums::MarketEnum market, DateUnit dateUnit){ date endDate; switch(dateUnit){ case YEAR: endDate = date(startDate.getYear()+increment,startDate.getMonth(), startDate.getDay()); break; case MONTH: endDate = getEndDateMonthIncrement(startDate, increment); break; case WEEK: endDate = date(startDate.getJudianDayNumber()+increment*7); break; case DAY: endDate = date(startDate.getJudianDayNumber()+increment); break; case BIZDAY: endDate = getBizDateOffSet(startDate,increment, market); break; } endDate = dayRollAdjust(adjustInvalidateDate(endDate,false),dayRoll, market); return endDate; }
date dateUtil::adjustInvalidateDate(date aDate, bool forwardAdjust){ int monthLastDay = getMonthLastDay(aDate.getYear(), aDate.getMonth()); if (aDate.getDay()>monthLastDay){ if (forwardAdjust){ aDate.setMonth(aDate.getMonth()%12+1); aDate.setDay(1); } else{ aDate.setDay(monthLastDay); } } return aDate; }
void TestBuildCashFlowLeg::buildCashFlowLegTest(date startDate, int numberOfMonth, double tolerance, enums::MarketEnum market){ int paymentFreq = 4; int buildDirection =1; Market mkt(market); enums::DayCountEnum dayCountSwapConvention = mkt.getDayCountSwapConvention(); enums::DayRollEnum dayRollSwapConvention = mkt.getDayRollSwapConvention(); enums::DayRollEnum accrualAdjustSwapConvention = mkt.getAccrualAdjustSwapConvention(); date swapAccrualStartDate = dateUtil::getBizDateOffSet(startDate,mkt.getBusinessDaysAfterSpot(enums::SWAP),market); date swapMaturityDate = dateUtil::getEndDate(swapAccrualStartDate,numberOfMonth,dayRollSwapConvention,market,dateUtil::MONTH); BuilderCashFlowLeg builderFromTenor(enums::SWAP,startDate,numberOfMonth,1,1,paymentFreq, market); BuilderCashFlowLeg builderFromMaturity(enums::SWAP, startDate, swapMaturityDate,numberOfMonth, 1,1,paymentFreq,market,buildDirection); cashflowLeg* cashflowLegFromMaturity = builderFromMaturity.getCashFlowLeg(); cashflowLeg* cashflowLegFromTenor = builderFromTenor.getCashFlowLeg(); cout<<"Cash flow building Test, start date ["<<startDate.toString()<<"], number of month ["<<numberOfMonth<<"]"<<endl; bool result = compareResult(cashflowLegFromMaturity,cashflowLegFromTenor); if (result==true) cout<<"Test Passed!"<<endl; }
void ShortPosition::sell_short(const date& dt, const Price& price, unsigned size) throw(PositionException) { if( closed() ) throw PositionException("Position is closed"); if( size == 0 ) throw PositionException("Invalid size"); if( dt.is_not_a_date() ) throw PositionException("Invalid date"); if( _sExecutions.sell_short(_symbol, dt, price, size) == Execution::NullID ) throw PositionException("Invalid execution"); _avgShortPrice = ((_avgShortPrice * _shorts) + (price * size)) / (double)(_shorts + size); _size += size; _shorts += size; }
void ShortPosition::cover(const date& dt, const Price& price, unsigned size) throw(PositionException) { if( closed() ) throw PositionException("Position is closed"); if( size == 0 || size > _size ) throw PositionException("Invalid size"); if( dt.is_not_a_date() ) throw PositionException("Invalid date"); if( _sExecutions.cover(_symbol, dt, price, size) == Execution::NullID ) throw PositionException("Invalid execution"); _avgCoverPrice = ((_avgCoverPrice * _covers) + (price * size)) / (_covers + size); _size -= size; _covers += size; }
ShortPosition::ShortPosition(ID id, const string& symbol, const date& dt, const Price& price, unsigned size) throw(PositionException): Position(id, symbol), _shorts(0), _covers(0), _avgShortPrice(0), _avgCoverPrice(0) { if( size == 0 ) throw PositionException("Invalid size"); if( dt.is_not_a_date() ) throw PositionException("Invalid date"); if( _sExecutions.sell_short(_symbol, dt, price, size) == Execution::NullID ) throw PositionException("Invalid execution"); _avgShortPrice = ((_avgShortPrice * _shorts) + (price * size)) / (double)(_shorts + size); _size += size; _shorts += size; }
// Set up the source data static const fudge_byte rawBytesArray [ 16 ] = { 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 }; static const fudge_i16 rawShortsArray [ 10 ] = { -32767, 32767, 0, 1, -1, 100, -100, 0, 16385 }; static const fudge_i32 rawIntsArray [ 4 ] = { 2147483647, 0, -2147483647, 0 }; static const fudge_i64 rawLongsArray [ 12 ] = { ( fudge_i64 ) -9223372036854775807ll, 0, ( fudge_i64 ) 9223372036854775807ll, -1, 2, -3, 5, -8, 13, -21, 34, -55 }; static const fudge_f32 rawFloatsArray [ 8 ] = { 0.0f, 2147483647.0f, 214748364.7f, 21474836.47f, 2147483.647f, 2.147483647f, 21.47483647f, 214.7483647f }; static const fudge_f64 rawDoublesArray [ 5 ] = { 9223372036854775807.0, 0.0, 0.0000000123456, 1234560000000.0, -9223372036854775807.0 }; std::vector<fudge_byte> rawBytes ( rawBytesArray, rawBytesArray + sizeof ( rawBytesArray ) / sizeof ( fudge_byte ) ); std::vector<fudge_i16> rawShorts ( rawShortsArray, rawShortsArray + sizeof ( rawShortsArray ) / sizeof ( fudge_i16 ) ); std::vector<fudge_i32> rawInts ( rawIntsArray, rawIntsArray + sizeof ( rawIntsArray ) / sizeof ( fudge_i32 ) ); std::vector<fudge_i64> rawLongs ( rawLongsArray, rawLongsArray + sizeof ( rawLongsArray ) / sizeof ( fudge_i64 ) ); std::vector<fudge_f32> rawFloats ( rawFloatsArray, rawFloatsArray + sizeof ( rawFloatsArray ) / sizeof ( fudge_f32 ) ); std::vector<fudge_f64> rawDoubles ( rawDoublesArray, rawDoublesArray + sizeof ( rawDoublesArray ) / sizeof ( fudge_f64 ) ); static const date exampleDate ( 2010, 3, 21 ); static const time exampleTime ( 15 * 3600 + 39 * 60 + 27, 0, FUDGE_DATETIME_PRECISION_SECOND ); static const datetime exampleDateTime ( exampleDate.year ( ), exampleDate.month ( ), exampleDate.day ( ), exampleTime.seconds ( ), exampleTime.nanoseconds ( ), exampleTime.precision ( ) ); // Will need a large byte array for the fixed array testing fudge_byte largeByteArray [ 512 ]; for ( size_t index ( 0 ); index < sizeof ( largeByteArray ); ++index ) largeByteArray [ index ] = index % 256 - 128; // Construct an empty message message message1; TEST_EQUALS_INT( message1.size ( ), 0 ); // Test some failure cases TEST_THROWS_EXCEPTION( message1.getFieldAt ( 0 ), exception ); // Invalid index TEST_THROWS_EXCEPTION( message1.getField ( static_cast<fudge_i16> ( 0 ) ), exception ); // Invalid ordinal
int main() { constexpr date d = { 8, 24, 2013 }; static_assert(d.days_since_unix_time() == 15941, ".."); }
bool dateUtil::isHoliday(date aDate, enums::CurrencyEnum market){ return isHoliday(aDate.getJudianDayNumber(),market); }
bool dateUtil::isBizDay(date date0){ return isBizDay(date0.getJudianDayNumber()); }