// simulates legacy C code #include typedef union { int i; struct { unsigned char d; unsigned char m; unsigned short y; } s; } MDATUM; static MDATUM MDatumSetDatum(int d, int m, int y) { MDATUM md={0}; md.s.d=d; md.s.m=m; md.s.y=y; return md; } const char* MDatumSprintf(char *buff, MDATUM md) { static char sbuff[20]; if (!buff) buff=sbuff; sprintf(buff, "%02d.%02d.%04d", md.s.d, md.s.m, md.s.y); return buff; } // new code, please comment #include #include #include #include using namespace std; using boost::gregorian::date; using namespace boost::gregorian; using namespace boost::posix_time; using namespace boost::local_time; #define ID_INP_NORMDAT "%d.%m.%Y" static MDATUM date_to_mdatum(const date &d) { return MDatumSetDatum(d.day(), d.month(), d.year()); } static MDATUM parse_date(const char * const sDate, const char * const sFormat) { string s(sDate); std::stringstream ss(s); date_input_facet* facet(new date_input_facet(sFormat)); ss.imbue(locale(ss.getloc(), facet)); date d(2000,1,1); ss >> d; return date_to_mdatum(d); } int main(int, char **) { try { MDATUM md = parse_date("02.05.1974", ID_INP_NORMDAT); cerr << "read: " << MDatumSprintf(NULL, md) << endl; } catch (const exception &e) { cerr << "Exception: " << e.what() << endl; } return 0; }