time_conversion_helper.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED)
  9. #define BOOST_TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED
  10. #include <ctime>
  11. #include <cstring>
  12. #include <boost/config.hpp>
  13. #include <boost/spirit/include/classic_core.hpp>
  14. #include <boost/spirit/include/classic_symbols.hpp>
  15. #include <boost/spirit/include/classic_assign_actor.hpp>
  16. #include <boost/spirit/include/classic_push_back_actor.hpp>
  17. #if !defined(spirit_append_actor)
  18. #define spirit_append_actor(actor) boost::spirit::classic::push_back_a(actor)
  19. #define spirit_assign_actor(actor) boost::spirit::classic::assign_a(actor)
  20. #endif // !defined(spirit_append_actor)
  21. // this must occur after all of the includes and before any code appears
  22. #ifdef BOOST_HAS_ABI_HEADERS
  23. #include BOOST_ABI_PREFIX
  24. #endif
  25. ///////////////////////////////////////////////////////////////////////////////
  26. namespace boost {
  27. namespace wave {
  28. namespace util {
  29. namespace time_conversion {
  30. using namespace std; // some systems have std::tm etc. in namespace std
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // define, whether the rule's should generate some debug output
  33. #define TRACE_CPP_TIME_CONVERSION \
  34. (BOOST_SPIRIT_DEBUG_FLAGS_CPP & BOOST_SPIRIT_DEBUG_FLAGS_TIME_CONVERSION) \
  35. /**/
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // Grammar for parsing a date/time string generated by the C++ compiler from
  38. // __DATE__ and __TIME__
  39. class time_conversion_grammar :
  40. public boost::spirit::classic::grammar<time_conversion_grammar>
  41. {
  42. public:
  43. time_conversion_grammar() : fYearIsCorrected(false)
  44. {
  45. using namespace std; // some systems have memset in std
  46. memset (&time_stamp, 0, sizeof(tm));
  47. BOOST_SPIRIT_DEBUG_TRACE_RULE_NAME(*this, "time_conversion_grammar",
  48. TRACE_CPP_TIME_CONVERSION);
  49. }
  50. template <typename ScannerT>
  51. struct definition {
  52. definition(time_conversion_grammar const &self)
  53. {
  54. using boost::spirit::classic::int_p;
  55. using boost::spirit::classic::add;
  56. char const *m[] = {
  57. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  58. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  59. };
  60. for (int i = 0; i < 12; ++i)
  61. add (month, m[i], i);
  62. time_rule // expected format is 'Dec 29 2001 11:23:59'
  63. = month[spirit_assign_actor(self.time_stamp.tm_mon)]
  64. >> int_p[spirit_assign_actor(self.time_stamp.tm_mday)]
  65. >> int_p[spirit_assign_actor(self.time_stamp.tm_year)]
  66. >> int_p[spirit_assign_actor(self.time_stamp.tm_hour)] >> ':'
  67. >> int_p[spirit_assign_actor(self.time_stamp.tm_min)] >> ':'
  68. >> int_p[spirit_assign_actor(self.time_stamp.tm_sec)]
  69. ;
  70. BOOST_SPIRIT_DEBUG_TRACE_RULE(time_rule, TRACE_CPP_TIME_CONVERSION);
  71. }
  72. boost::spirit::classic::rule<ScannerT> time_rule;
  73. boost::spirit::classic::symbols<> month;
  74. boost::spirit::classic::rule<ScannerT> const&
  75. start() const { return time_rule; }
  76. };
  77. void correct_year()
  78. {
  79. if (!fYearIsCorrected) {
  80. time_stamp.tm_year -= 1900;
  81. fYearIsCorrected = true;
  82. }
  83. }
  84. mutable tm time_stamp;
  85. bool fYearIsCorrected;
  86. };
  87. ///////////////////////////////////////////////////////////////////////////////
  88. // calculate the time of the compilation as a std::time_t to ensure correctness
  89. // of the saved dfa table
  90. class time_conversion_helper
  91. {
  92. public:
  93. time_conversion_helper(char const *act_time) : compile_time(0)
  94. {
  95. using namespace boost::spirit::classic;
  96. time_conversion_grammar g;
  97. parse_info<> pi = parse (act_time, g, space_p);
  98. if (pi.hit) {
  99. g.correct_year();
  100. compile_time = mktime(&g.time_stamp);
  101. }
  102. BOOST_ASSERT(0 != compile_time);
  103. }
  104. time_t get_time() const { return compile_time; }
  105. private:
  106. time_t compile_time;
  107. };
  108. ///////////////////////////////////////////////////////////////////////////////
  109. #undef TRACE_CPP_TIME_CONVERSION
  110. } // namespace time_conversion
  111. // import time_conversion into the boost::wave::util namespace
  112. using namespace time_conversion;
  113. ///////////////////////////////////////////////////////////////////////////////
  114. } // namespace util
  115. } // namespace wave
  116. } // namespace boost
  117. // the suffix header occurs after all of the code
  118. #ifdef BOOST_HAS_ABI_HEADERS
  119. #include BOOST_ABI_SUFFIX
  120. #endif
  121. #endif // !defined(BOOST_TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED)