#include #include #include using namespace std; using namespace boost; using namespace boost::gregorian; namespace boost{ namespace date_time { template date_type parse_date2(const std::string& s) { typedef typename date_type::year_type year_type; typedef typename date_type::month_type month_type; bool cDateMacroFormat=false; //Is this of the __DATE__ type string? int pos = 0; typename date_type::ymd_type ymd(year_type::min(),1,1); boost::tokenizer > tok(s); for(boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg) { unsigned short i =0; switch(pos) { case 0: { std::string s = *beg; if((s.at(0) >= '0') && (s.at(0) <= '9')) { i = boost::lexical_cast(*beg); ymd.year = i; } else //__DATE__ type { cDateMacroFormat=true; typename month_type::month_map_ptr_type ptr = month_type::get_month_map_ptr(); s = convert_to_lower(s); typename month_type::month_map_type::iterator iter = ptr->find(s); if(iter != ptr->end()) // required for STLport { i = iter->second; } else{ i = 13; // intentionally out of range - name not found } ymd.month = i; } break; } case 1: { if( !cDateMacroFormat ) { std::string s = *beg; if((s.at(0) >= '0') && (s.at(0) <= '9')) { i = boost::lexical_cast(s); } else { typename month_type::month_map_ptr_type ptr = month_type::get_month_map_ptr(); s = convert_to_lower(s); typename month_type::month_map_type::iterator iter = ptr->find(s); if(iter != ptr->end()) // required for STLport { i = iter->second; } else{ i = 13; // intentionally out of range - name not found } } ymd.month = i; } else { i = boost::lexical_cast(*beg); ymd.day = i; } break; } case 2: { if( !cDateMacroFormat ) { i = boost::lexical_cast(*beg); ymd.day = i; } else { i = boost::lexical_cast(*beg); ymd.year = i; } break; } }; //switch pos++; } return date_type(ymd); } }} int main() { date t(2002, Jan, 10); std::cout << t << std::endl; t = boost::date_time::parse_date2(std::string("2003 02 10")); std::cout << t << std::endl;; std::cout << __DATE__ << std::endl; t = boost::date_time::parse_date2(std::string(__DATE__)); std::cout << t << std::endl;; } #include #include #include #include