//! 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;
}
  //! 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;
  }
예제 #3
0
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));
}
예제 #4
0
 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();
 }
예제 #5
0
 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();
 }