Chris@16: /*============================================================================= Chris@16: Boost.Wave: A Standard compliant C++ preprocessor library Chris@16: Chris@16: http://www.boost.org/ Chris@16: Chris@16: Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost Chris@16: Software License, Version 1.0. (See accompanying file Chris@16: LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: =============================================================================*/ Chris@16: Chris@16: #if !defined(TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED) Chris@16: #define TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(spirit_append_actor) Chris@16: #define spirit_append_actor(actor) boost::spirit::classic::push_back_a(actor) Chris@16: #define spirit_assign_actor(actor) boost::spirit::classic::assign_a(actor) Chris@16: #endif // !defined(spirit_append_actor) Chris@16: Chris@16: // this must occur after all of the includes and before any code appears Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: #include BOOST_ABI_PREFIX Chris@16: #endif Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { Chris@16: namespace wave { Chris@16: namespace util { Chris@16: Chris@16: namespace time_conversion { Chris@16: Chris@16: using namespace std; // some systems have std::tm etc. in namespace std Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // define, whether the rule's should generate some debug output Chris@16: #define TRACE_CPP_TIME_CONVERSION \ Chris@16: (BOOST_SPIRIT_DEBUG_FLAGS_CPP & BOOST_SPIRIT_DEBUG_FLAGS_TIME_CONVERSION) \ Chris@16: /**/ Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // Grammar for parsing a date/time string generated by the C++ compiler from Chris@16: // __DATE__ and __TIME__ Chris@16: class time_conversion_grammar : Chris@16: public boost::spirit::classic::grammar Chris@16: { Chris@16: public: Chris@16: time_conversion_grammar() : fYearIsCorrected(false) Chris@16: { Chris@16: using namespace std; // some systems have memset in std Chris@16: memset (&time_stamp, 0, sizeof(tm)); Chris@16: BOOST_SPIRIT_DEBUG_TRACE_RULE_NAME(*this, "time_conversion_grammar", Chris@16: TRACE_CPP_TIME_CONVERSION); Chris@16: } Chris@16: Chris@16: template Chris@16: struct definition { Chris@16: Chris@16: definition(time_conversion_grammar const &self) Chris@16: { Chris@16: using boost::spirit::classic::int_p; Chris@16: using boost::spirit::classic::add; Chris@16: Chris@16: char const *m[] = { Chris@16: "Jan", "Feb", "Mar", "Apr", "May", "Jun", Chris@16: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" Chris@16: }; Chris@16: Chris@16: for (int i = 0; i < 12; ++i) Chris@16: add (month, m[i], i); Chris@16: Chris@16: time_rule // expected format is 'Dec 29 2001 11:23:59' Chris@16: = month[spirit_assign_actor(self.time_stamp.tm_mon)] Chris@16: >> int_p[spirit_assign_actor(self.time_stamp.tm_mday)] Chris@16: >> int_p[spirit_assign_actor(self.time_stamp.tm_year)] Chris@16: >> int_p[spirit_assign_actor(self.time_stamp.tm_hour)] >> ':' Chris@16: >> int_p[spirit_assign_actor(self.time_stamp.tm_min)] >> ':' Chris@16: >> int_p[spirit_assign_actor(self.time_stamp.tm_sec)] Chris@16: ; Chris@16: Chris@16: BOOST_SPIRIT_DEBUG_TRACE_RULE(time_rule, TRACE_CPP_TIME_CONVERSION); Chris@16: } Chris@16: Chris@16: boost::spirit::classic::rule time_rule; Chris@16: boost::spirit::classic::symbols<> month; Chris@16: Chris@16: boost::spirit::classic::rule const& Chris@16: start() const { return time_rule; } Chris@16: }; Chris@16: Chris@16: void correct_year() Chris@16: { Chris@16: if (!fYearIsCorrected) { Chris@16: time_stamp.tm_year -= 1900; Chris@16: fYearIsCorrected = true; Chris@16: } Chris@16: } Chris@16: Chris@16: mutable tm time_stamp; Chris@16: bool fYearIsCorrected; Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // calculate the time of the compilation as a std::time_t to ensure correctness Chris@16: // of the saved dfa table Chris@16: class time_conversion_helper Chris@16: { Chris@16: public: Chris@16: time_conversion_helper(char const *act_time) : compile_time(0) Chris@16: { Chris@16: using namespace boost::spirit::classic; Chris@16: Chris@16: time_conversion_grammar g; Chris@16: parse_info<> pi = parse (act_time, g, space_p); Chris@16: Chris@16: if (pi.hit) { Chris@16: g.correct_year(); Chris@16: compile_time = mktime(&g.time_stamp); Chris@16: } Chris@16: BOOST_ASSERT(0 != compile_time); Chris@16: } Chris@16: Chris@16: time_t get_time() const { return compile_time; } Chris@16: Chris@16: private: Chris@16: time_t compile_time; Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: #undef TRACE_CPP_TIME_CONVERSION Chris@16: } // namespace time_conversion Chris@16: Chris@16: // import time_conversion into the boost::wave::util namespace Chris@16: using namespace time_conversion; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: } // namespace util Chris@16: } // namespace wave Chris@16: } // namespace boost Chris@16: Chris@16: // the suffix header occurs after all of the code Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: #include BOOST_ABI_SUFFIX Chris@16: #endif Chris@16: Chris@16: #endif // !defined(TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED)